Skip to content

Commit

Permalink
set a limit on how far in a ZoomRegion can be zoomed (#61)
Browse files Browse the repository at this point in the history
* set a limit on how far in a ZoomRegion can be zoomed

Prevents an InexactError and also prevents getting stuck at a high zoom level.

* minor tweaks

* force scroll event controller to emit discrete scroll events

The `MouseScroll` object only handles scroll direction, not a step size.
In the future, we should consider adding that.

This makes scrolling with a trackpad a bit less touchy (at least on my
laptop).

* bump version

* use alternative approach
  • Loading branch information
jwahlstrand committed Nov 19, 2023
1 parent 72ebdb5 commit 82390cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GtkObservables"
uuid = "8710efd8-4ad6-11eb-33ea-2d5ceb25a41c"
version = "2.0.3"
version = "2.0.4"

[deps]
Cairo = "159f3aea-2a34-519c-b102-8c37f9878175"
Expand Down
6 changes: 5 additions & 1 deletion src/graphics_interaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ struct MouseHandler{U<:CairoUnit}
g = GtkGestureClick(canvas,0)
gm = GtkEventControllerMotion(canvas)
gs = GtkEventControllerScroll(Gtk4.EventControllerScrollFlags_HORIZONTAL |
Gtk4.EventControllerScrollFlags_VERTICAL , canvas)
Gtk4.EventControllerScrollFlags_VERTICAL |
Gtk4.EventControllerScrollFlags_DISCRETE , canvas)

push!(ids, signal_connect(mousedown_cb, g, "pressed", Nothing, (Int32, Float64, Float64), false, handler))
push!(ids, signal_connect(mouseup_cb, g, "released", Nothing, (Int32, Float64, Float64), false, handler))
Expand Down Expand Up @@ -515,6 +516,9 @@ function zoom(zr::ZoomRegion, s, pos::XY)
w, h = IntervalSets.width(xview), IntervalSets.width(yview)
fx, fy = (centerx-minimum(xview))/w, (centery-minimum(yview))/h
wbb, hbb = s*w, s*h
# set a limit on how far in we can zoom (ImageView issue #297)
wbb = (wbb<2.0) ? 2.0 : wbb
hbb = (hbb<2.0) ? 2.0 : hbb
xview = interior(ClosedInterval(centerx-fx*wbb,centerx+(1-fx)*wbb), xviewlimits)
yview = interior(ClosedInterval(centery-fy*hbb,centery+(1-fy)*hbb), yviewlimits)
ZoomRegion(zr.fullview, XY(xview, yview))
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,19 @@ Base.axes(::Foo) = (Base.OneTo(7), Base.OneTo(9))
@test zrz.currentview.y == 16..55
zrr = GtkObservables.reset(zrz)
@test zrr == zr

# ImageView #297
# When we have zoomed so far in that the region is less than three
# pixels wide, do not zoom in further. This prevents a truncation error
# when the new interval width is 0 and also prevents getting "stuck" at
# that zoom level (since zooming back out doesn't work when the current
# interval width is zero).
zrn = ZoomRegion((1:5,1:5))
c = XY(UserUnit(3),UserUnit(3))
zrz = zoom(zrn, 0.5)
@test zrz.currentview.x == 2..4
zrz = zoom(zrz, 0.5)
@test zrz.currentview.x == 2..4

zrbb = ZoomRegion(zr.fullview, BoundingBox(5, 15, 35, 75))
@test zrbb.fullview === zr.fullview
Expand Down

0 comments on commit 82390cb

Please sign in to comment.