-
-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Slider
and IntervalSlider
for dragging
logic
#3231
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,6 @@ function initialize_block!(isl::IntervalSlider) | |
end | ||
end | ||
|
||
dragging = Observable(false) | ||
|
||
# what the slider actually displays currently (also during dragging when | ||
# the slider position is in an "invalid" position given the slider's range) | ||
displayed_sliderfractions = Observable((0.0, 0.0)) | ||
|
@@ -50,7 +48,7 @@ function initialize_block!(isl::IntervalSlider) | |
on(sliderfractions) do fracs | ||
# only update displayed fraction through sliderfraction if not dragging | ||
# dragging overrides the value so there is clear mouse interaction | ||
if !dragging[] | ||
if !isl.dragging[] | ||
displayed_sliderfractions[] = fracs | ||
end | ||
end | ||
|
@@ -87,7 +85,7 @@ function initialize_block!(isl::IntervalSlider) | |
[ci, ci] | ||
end | ||
|
||
endbuttons = scatter!(blockscene, endpoints, color = endbuttoncolors, | ||
endbuttons = scatter!(blockscene, endpoints, color = endbuttoncolors, marker = Circle, | ||
markersize = isl.linewidth, strokewidth = 0, inspectable = false) | ||
|
||
linesegs = linesegments!(blockscene, linepoints, color = linecolors, | ||
|
@@ -107,7 +105,7 @@ function initialize_block!(isl::IntervalSlider) | |
end | ||
buttonsizes = @lift($(isl.linewidth) .* $button_magnifications) | ||
buttons = scatter!(blockscene, middlepoints, color = isl.color_active, strokewidth = 0, | ||
markersize = buttonsizes, inspectable = false) | ||
marker = Circle, markersize = buttonsizes, inspectable = false) | ||
|
||
mouseevents = addmouseevents!(blockscene, isl.layoutobservables.computedbbox) | ||
|
||
|
@@ -118,15 +116,17 @@ function initialize_block!(isl::IntervalSlider) | |
start_disp_fractions = Ref((0.0, 0.0)) | ||
startindices = Ref((1, 1)) | ||
|
||
onmouseleftdrag(mouseevents) do event | ||
|
||
dragging[] = true | ||
function get_fraction(event) | ||
fraction = if isl.horizontal[] | ||
(event.px[1] - endpoints[][1][1]) / (endpoints[][2][1] - endpoints[][1][1]) | ||
else | ||
(event.px[2] - endpoints[][1][2]) / (endpoints[][2][2] - endpoints[][1][2]) | ||
end | ||
fraction = clamp(fraction, 0, 1) | ||
end | ||
|
||
function handle_event(event) | ||
fraction = get_fraction(event) | ||
|
||
if state[] in (:min, :max) | ||
if isl.snap[] | ||
|
@@ -178,47 +178,42 @@ function initialize_block!(isl::IntervalSlider) | |
isl.selected_indices[] = newindices | ||
end | ||
end | ||
end | ||
|
||
onmouseleftdrag(mouseevents) do event | ||
handle_event(event) | ||
return Consume(true) | ||
end | ||
|
||
onmouseleftdragstop(mouseevents) do event | ||
dragging[] = false | ||
isl.dragging[] = false | ||
# adjust slider to closest legal value | ||
sliderfractions[] = sliderfractions[] | ||
notify(isl.selected_indices) | ||
return Consume(true) | ||
end | ||
|
||
onmouseleftdown(mouseevents) do event | ||
isl.dragging[] = true | ||
|
||
pos = event.px | ||
|
||
dim = isl.horizontal[] ? 1 : 2 | ||
frac = clamp( | ||
(pos[dim] - endpoints[][1][dim]) / (endpoints[][2][dim] - endpoints[][1][dim]), | ||
0, 1 | ||
) | ||
|
||
startfraction[] = frac | ||
fraction = get_fraction(event) | ||
startfraction[] = fraction | ||
startindices[] = isl.selected_indices[] | ||
start_disp_fractions[] = displayed_sliderfractions[] | ||
|
||
if state[] in (:both, :none) | ||
return Consume(true) | ||
end | ||
handle_event(event) | ||
|
||
newindex = closest_fractionindex(isl.range[], frac) | ||
if abs(newindex - isl.selected_indices[][1]) < abs(newindex - isl.selected_indices[][2]) | ||
isl.selected_indices[] = (newindex, isl.selected_indices[][2]) | ||
else | ||
isl.selected_indices[] = (isl.selected_indices[][1], newindex) | ||
end | ||
# linecolors[] = [color_active[], color_inactive[]] | ||
return Consume(true) | ||
end | ||
|
||
onmouseleftclick(mouseevents) do event | ||
isl.dragging[] = false | ||
notify(isl.selected_indices) | ||
return Consume(true) | ||
end | ||
Comment on lines
+209
to
213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clicks also trigger a drag start so you end up with a duplicate update here too:
There is an exception to this her though: If you do not move the slider through a click you do not get an interval update through the drag start. So then you only get the click update, which however does not change the values. I'm not sure if that's an update worth pushing out. |
||
|
||
onmouseleftdoubleclick(mouseevents) do event | ||
isl.dragging[] = false | ||
Comment on lines
215
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This causes an update like this:
So if you react to |
||
isl.selected_indices[] = isl.selected_indices[] = if isl.startvalues[] === Makie.automatic | ||
(1, lastindex(isl.range[])) | ||
else | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,16 +40,14 @@ function initialize_block!(sl::Slider) | |
(i - 1) / (length(r) - 1) | ||
end | ||
|
||
dragging = Observable(false) | ||
|
||
# what the slider actually displays currently (also during dragging when | ||
# the slider position is in an "invalid" position given the slider's range) | ||
displayed_sliderfraction = Observable(0.0) | ||
|
||
on(topscene, sliderfraction) do frac | ||
# only update displayed fraction through sliderfraction if not dragging | ||
# dragging overrides the value so there is clear mouse interaction | ||
if !dragging[] | ||
if !sl.dragging[] | ||
displayed_sliderfraction[] = frac | ||
end | ||
end | ||
|
@@ -91,15 +89,17 @@ function initialize_block!(sl::Slider) | |
|
||
mouseevents = addmouseevents!(topscene, sl.layoutobservables.computedbbox) | ||
|
||
onmouseleftdrag(mouseevents) do event | ||
dragging[] = true | ||
dif = event.px - event.prev_px | ||
fraction = clamp(if sl.horizontal[] | ||
function get_fraction(event) | ||
raw = if sl.horizontal[] | ||
(event.px[1] - endpoints[][1][1]) / (endpoints[][2][1] - endpoints[][1][1]) | ||
else | ||
(event.px[2] - endpoints[][1][2]) / (endpoints[][2][2] - endpoints[][1][2]) | ||
end, 0, 1) | ||
end | ||
return clamp(raw, 0, 1) | ||
end | ||
|
||
onmouseleftdrag(mouseevents) do event | ||
fraction = get_fraction(event) | ||
newindex = closest_fractionindex(sliderrange[], fraction) | ||
if sl.snap[] | ||
fraction = (newindex - 1) / (length(sliderrange[]) - 1) | ||
|
@@ -114,23 +114,45 @@ function initialize_block!(sl::Slider) | |
end | ||
|
||
onmouseleftdragstop(mouseevents) do event | ||
dragging[] = false | ||
sl.dragging[] = false | ||
# adjust slider to closest legal value | ||
sliderfraction[] = sliderfraction[] | ||
fraction = get_fraction(event) | ||
newindex = closest_fractionindex(sliderrange[], fraction) | ||
selected_index[] = newindex | ||
|
||
linecolors[] = [sl.color_active_dimmed[], sl.color_inactive[]] | ||
return Consume(true) | ||
end | ||
|
||
onmouseleftclick(mouseevents) do event | ||
sl.dragging[] = false | ||
# adjust slider to closest legal value | ||
sliderfraction[] = sliderfraction[] | ||
fraction = get_fraction(event) | ||
newindex = closest_fractionindex(sliderrange[], fraction) | ||
selected_index[] = newindex | ||
|
||
linecolors[] = [sl.color_active_dimmed[], sl.color_inactive[]] | ||
return Consume(true) | ||
end | ||
Comment on lines
116
to
138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has the same update duplication as IntervalSlider |
||
|
||
onmouseleftdown(mouseevents) do event | ||
pos = event.px | ||
dim = sl.horizontal[] ? 1 : 2 | ||
frac = (pos[dim] - endpoints[][1][dim]) / (endpoints[][2][dim] - endpoints[][1][dim]) | ||
selected_index[] = closest_fractionindex(sliderrange[], frac) | ||
# linecolors[] = [color_active[], color_inactive[]] | ||
sl.dragging[] = true | ||
fraction = get_fraction(event) | ||
newindex = closest_fractionindex(sliderrange[], fraction) | ||
if sl.snap[] | ||
fraction = (newindex - 1) / (length(sliderrange[]) - 1) | ||
end | ||
displayed_sliderfraction[] = fraction | ||
if selected_index[] != newindex | ||
selected_index[] = newindex | ||
end | ||
return Consume(true) | ||
end | ||
|
||
onmouseleftdoubleclick(mouseevents) do event | ||
sl.dragging[] = false | ||
selected_index[] = closest_index(sliderrange[], sl.startvalue[]) | ||
return Consume(true) | ||
end | ||
Comment on lines
154
to
158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same update order problem as IntervalSlider |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just duplicates updates
so I think we should just skip the notify here.