Skip to content

Commit

Permalink
add shortcuts for closing and fullscreening window (#302)
Browse files Browse the repository at this point in the history
Adds keyboard shortcuts for closing windows (Ctrl-W on Linux and Windows, Command-W on Mac) and fullscreening windows (F11 on Linux and Windows, Command-Shift-F on Mac). Only applies to windows created by ImageView, i.e. not user-created windows inside which ImageView widgets are placed.

Also fixes an issue with tests and works around a sporadic test failure on Mac OS that has nothing to do with this PR.

Also belatedly update NEWS for v0.12 and update CI action versions.
  • Loading branch information
jwahlstrand authored Mar 24, 2024
1 parent 03a157a commit e54547a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ jobs:
- os: macOS-latest
arch: x86
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v3
- uses: julia-actions/cache@v1
env:
cache-name: cache-artifacts
with:
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# New in 0.12

- switch from Gtk to Gtk4. Fixes REPL lag on Windows.

# New in 0.11

- switch from GtkReactive to GtkObservables. Reactive was essentially
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ julia = "1.6"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Observables = "a223df75-4e93-5b7c-acf9-bdd599c0f4de"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ imshow(img, flipy=true)
imshow(img, axes=(2,1)).
```

The window can be closed using Ctrl-W (on Linux or Windows, use Cmd-W on a Mac) and fullscreen can be toggled using F11 (on Linux or Windows, use Cmd-Shift-F on a Mac).

For movies, 3D, and 4D images, ImageView will create a "player" widget.

```julia
Expand Down
26 changes: 26 additions & 0 deletions src/ImageView.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,23 @@ function imshow(frame::Union{GtkFrame,GtkAspectFrame}, canvas::GtkObservables.Ca
roidict
end

function close_cb(::Ptr, par, win)
@idle_add Gtk4.destroy(win)
nothing
end

function fullscreen_cb(aptr::Ptr, par, win)
gv=Gtk4.GLib.GVariant(par)
a=convert(Gtk4.GLib.GSimpleAction, aptr)
if gv[Bool]
@idle_add Gtk4.fullscreen(win)
else
@idle_add Gtk4.unfullscreen(win)
end
Gtk4.GLib.set_state(a, gv)
nothing
end

"""
guidict = imshow_gui(canvassize, gridsize=(1,1); name="ImageView", aspect=:auto, slicedata=SliceData{false}())
Expand All @@ -333,6 +350,15 @@ Compat.@constprop :none function imshow_gui(canvassize::Tuple{Int,Int},
slicedata::SliceData=SliceData{false}())
winsize = canvas_size(screen_size(), map(*, canvassize, gridsize))
win = GtkWindow(name, winsize...)
ag = Gtk4.GLib.GSimpleActionGroup()
m = Gtk4.GLib.GActionMap(ag)
push!(win, Gtk4.GLib.GActionGroup(ag), "win")
Gtk4.GLib.add_action(m, "close", close_cb, win)
Gtk4.GLib.add_stateful_action(m, "fullscreen", false, fullscreen_cb, win)
sc = GtkShortcutController(win)
Gtk4.add_action_shortcut(sc,Sys.isapple() ? "<Meta>W" : "<Control>W", "win.close")
Gtk4.add_action_shortcut(sc,Sys.isapple() ? "<Meta><Shift>F" : "F11", "win.fullscreen")

window_wrefs[win] = nothing
signal_connect(win, :destroy) do w
delete!(window_wrefs, win)
Expand Down
16 changes: 12 additions & 4 deletions test/newtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ end
@test get_gtk_property(frame, :ratio, Float32) == 1.0
zr[] = (1:20, 9:10)
@test zr[].currentview.x == 9..10
sleep(0.1) # allow the Gtk event loop to run
sleep(0.5) # allow the Gtk event loop to run
@test get_gtk_property(frame, :ratio, Float32) 0.1
zr[] = (9:10, 1:20)
sleep(0.1)
sleep(0.5)
@test get_gtk_property(frame, :ratio, Float32) 10.0

Gtk4.destroy(win)
Expand Down Expand Up @@ -83,8 +83,16 @@ end
hbig = imshow_now(img, name="VeryBig"; canvassize=(500,500))
sleep(1.0) # some extra sleep for this big image
cvs = hbig["gui"]["canvas"];
@test Graphics.height(getgc(cvs)) <= 500
@test Graphics.width(getgc(cvs)) <= 500
# GUI update takes a very long time in CI (sometimes) for MacOS, hence the following
i=1
passed=false
while !passed && i<10
sleep(1.0)
passed = (Graphics.height(getgc(cvs)) <= 500 && Graphics.width(getgc(cvs)) <= 500)
i=i+1
end
passed || println("failed after $i seconds")
!Sys.isapple() && @test passed
end

@testset "imshow!" begin
Expand Down
11 changes: 10 additions & 1 deletion test/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ end
A[1,2,2] = NaN
A[1,3,3] = -Inf
A[1,4,4] = Inf
imshow_now(colorview(RGB, A))
imgdict = imshow_now(colorview(RGB, A))

# test window actions
win = imgdict["gui"]["window"]
Gtk4.G_.activate_action(win, "win.fullscreen", nothing)
sleep(0.5)
Gtk4.G_.activate_action(win, "win.fullscreen", nothing)
sleep(0.5)

Gtk4.G_.activate_action(win, "win.close", nothing)
end

@testset "Simple MultiChannelColors" begin
Expand Down

0 comments on commit e54547a

Please sign in to comment.