Skip to content

Commit b66b321

Browse files
committed
add arrow key support for time slider
1 parent e85af2c commit b66b321

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function timeslider_card(app)
201201
app.t[] = _t
202202
end
203203
end
204-
t_slider = ContinuousSlider(twindow, app.t)
204+
t_slider = ContinuousSlider(twindow, app.t; arrowkeys=true)
205205
Card(
206206
Grid(
207207
DOM.span("Time"), t_slider, RoundedLabel(t_slider.value_r),

NetworkDynamicsInspector/src/widgets.jl

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ struct ContinuousSlider{T} <: Bonito.AbstractSlider{T}
6363
track_style::Styles
6464
thumb_style::Styles
6565
track_active_style::Styles
66+
arrowkeys::Bool
6667
end
6768

68-
function ContinuousSlider(range, value::Observable{T}) where {T}
69+
function ContinuousSlider(range, value::Observable{T}; kwargs...) where {T}
6970
value_l = Observable{T}(zero(T)/zero(T))
70-
ContinuousSlider(range, value_l, value)
71+
ContinuousSlider(range, value_l, value; kwargs...)
7172
end
7273

73-
function ContinuousSlider(range, value_l::Observable{T}, value_r::Observable{T}) where {T}
74+
function ContinuousSlider(range, value_l::Observable{T}, value_r::Observable{T}; arrowkeys=false) where {T}
7475
style, track_style, track_active_style, thumb_style = get_slider_style()
7576

7677
#=
@@ -98,6 +99,7 @@ function ContinuousSlider(range, value_l::Observable{T}, value_r::Observable{T})
9899
track_style,
99100
thumb_style,
100101
track_active_style,
102+
arrowkeys
101103
)
102104
return slider
103105
end
@@ -253,28 +255,35 @@ function Bonito.jsrender(session::Session, slider::ContinuousSlider)
253255
}, { signal: controller.signal });
254256
set_thumb_val($(slider.value_l).value, 'l');
255257
set_thumb_val($(slider.value_r).value, 'r');
258+
259+
function move_thumb_incremental(direction, shiftPressed) {
260+
const startval = $(slider.range).value[0];
261+
const endval = $(slider.range).value[1];
262+
let step = (endval - startval) / 100;
263+
if (shiftPressed) {step *= 10;}
264+
265+
let newval;
266+
if (direction === 'right') {
267+
newval = Math.min(thumbval_r + step, endval);
268+
} else if (direction === 'left') {
269+
newval = Math.max(thumbval_r - step, startval);
270+
}
271+
$(slider.value_r).notify(newval);
272+
}
273+
274+
if ($(slider.arrowkeys)) {
275+
document.addEventListener('keydown', function (e) {
276+
if (e.key === 'ArrowRight') {
277+
move_thumb_incremental('right', e.shiftKey);
278+
e.preventDefault();
279+
} else if (e.key === 'ArrowLeft') {
280+
move_thumb_incremental('left', e.shiftKey);
281+
e.preventDefault();
282+
}
283+
}, { signal: controller.signal });
284+
}
256285
}
257286
"""
258-
# function move_thumb_incremental(direction, shiftPressed) {
259-
# const startval = $(slider.values[][begin]);
260-
# const endval = $(slider.values[][end]);
261-
# let step = (endval - startval) / 100;
262-
# if (shiftPressed) {step *= 10;}
263-
# if (direction === 'right') {
264-
# set_thumb_val(Math.min(thumbval + step, endval));
265-
# } else if (direction === 'left') {
266-
# set_thumb_val(Math.max(thumbval - step, startval));
267-
# }
268-
# }
269-
# document.addEventListener('keydown', function (e) {
270-
# if (e.key === 'ArrowRight') {
271-
# move_thumb_incremental('right', e.shiftKey);
272-
# e.preventDefault();
273-
# } else if (e.key === 'ArrowLeft') {
274-
# move_thumb_incremental('left', e.shiftKey);
275-
# e.preventDefault();
276-
# }
277-
# }, { signal: controller.signal });
278287
onload(session, container, jscode)
279288

280289
return jsrender(session, container)

NetworkDynamicsInspector/test/slider_test.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ gui = (;
66
range = Observable{NTuple{2, Float64}}((-1.0, 1.0)),
77
val1 = Observable{Float64}(-1.0),
88
val2 = Observable{Float64}(1.0),
9+
val = Observable{Float64}(0.0)
910
)
1011

1112
let
1213
app = App(;) do session
1314
NetworkDynamicsInspector.clear_obs!(gui)
14-
slider = ContinuousSlider(gui.range, gui.val1, gui.val2)
15+
sl1 = ContinuousSlider(gui.range, gui.val1, gui.val2)
16+
sl2 = ContinuousSlider(gui.range, gui.val; arrowkeys=true)
1517

16-
return wrap_assets(Grid(RoundedLabel(gui.val1), slider, RoundedLabel(gui.val2);
18+
return wrap_assets(Grid(
19+
RoundedLabel(gui.val1), sl1, RoundedLabel(gui.val2),
20+
RoundedLabel(gui.val), sl2;
1721
columns="1fr 3fr 1fr", width="500px"))
1822
end;
1923
serve_app(app)

test/Inspector_test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ sol = let
5252
s0 = NWState(nw)
5353
prob = ODEProblem(nw, uflat(s0), (0,6), copy(pflat(s0)), callback=nwcb)
5454
sol = solve(prob, Tsit5());
55-
end
55+
end;
5656

57-
ENV["JULIA_DEBUG"] = NetworkDynamicsInspector
57+
# ENV["JULIA_DEBUG"] = NetworkDynamicsInspector
5858
# ENV["JULIA_DEBUG"] = ""
5959

6060
app = (;

0 commit comments

Comments
 (0)