Skip to content

Commit 13a6603

Browse files
committed
further work on resizing
1 parent 75da835 commit 13a6603

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

NetworkDynamicsInspector/assets/app.css

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ html, body {
99
.select2-container .select2-selection--single .select2-selection__rendered,
1010
.select2-container .select2-results__option{
1111
font-family: monospace;
12-
//font-size: 20px;
12+
/* font-size: 20px; */
1313
}
1414
.select2-results__group {
1515
font-family: sans-serif;
16-
//font-size: 12px;
17-
//color: blue;
16+
/* font-size: 12px; */
17+
/* color: blue; */
1818
}
1919

2020
.maingrid {
@@ -28,16 +28,19 @@ html, body {
2828
display: flex;
2929
flex-direction: column;
3030
gap: 5px;
31+
/* to keep shadows visible */
32+
/* https://stackoverflow.com/questions/70802682/parents-overflowhidden-doesnt-show-childs-box-shadow */
3133
overflow: hidden; /* otherwise it won't shrink */
34+
margin:-10px;
35+
padding:10px
3236
}
3337

3438
.graphplot-card {
35-
heigth:400px; /* initial widht and height */
39+
height:400px; /* initial widht and height */
3640
width: 400px;
3741
min-width:250px;
3842
resize: both;
3943
overflow: hidden;
40-
/* grid-column: 1; */
4144
}
4245

4346
.gpstate-control-card {
@@ -55,6 +58,7 @@ html, body {
5558
.timeseries-card-container{
5659
display: flex;
5760
flex-direction: column;
61+
overflow: hidden;
5862
height: 100%; /* otherwise it won't shrink */
5963
}
6064
.timeseries-card-container .comp-state-sel-grid {

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ function graphplot_card(app; kwargs...)
146146
node_attr=(;colorrange=app.graphplot.ncolorrange, colormap=app.graphplot.ncolorscheme),
147147
edge_attr=(;colorrange=app.graphplot.ncolorrange, colormap=app.graphplot.ncolorscheme))
148148

149-
# hidespines!(ax)
150-
# hidedecorations!(ax)
149+
hidespines!(ax)
150+
hidedecorations!(ax)
151151
fig, ax
152152
end
153+
xratio = Ref{Float64}(1.0)
154+
yratio = Ref{Float64}(1.0)
153155
on(ax.scene.viewport) do lims
154156
@debug "GP: viewport => adapt xy scaling"
155-
adapt_xy_scaling!(ax)
157+
adapt_xy_scaling!(xratio, yratio, ax)
156158
nothing
157159
end
158160
Card(fig; class="graphplot-card", kwargs...)
@@ -168,21 +170,41 @@ function _gracefully_extract_states!(vec, sol, t, idxs, rel)
168170
vec[mask] .= only(sol([t]; idxs=idxs[mask]).u)
169171
end
170172
end
171-
function adapt_xy_scaling!(ax)
173+
function adapt_xy_scaling!(xratio, yratio, ax)
174+
# in theory, this should work without autolimits, but it doesnt
172175
autolimits!(ax)
176+
173177
vp = ax.scene.viewport[]
174178
fl = ax.finallimits[]
175-
vp_aspect = reduce(/, vp.widths)
176-
fl_aspect = reduce(/, fl.widths)
179+
vp_aspect = reduce(/, Float64.(vp.widths))
180+
fl_aspect = reduce(/, Float64.(fl.widths))
177181
ratio = vp_aspect/fl_aspect
178182

179-
if ratio > 1 # need to increase x limits
180-
low = ax.finallimits[].origin[1]
181-
hi = low + ax.finallimits[].widths[1]
183+
potential_x_factor = xratio[]*ratio
184+
potential_y_factor = yratio[]/ratio
185+
186+
if potential_x_factor >= 1 && potential_y_factor >= 1
187+
# both valid, pick smaller
188+
resize = potential_x_factor < potential_y_factor ? :x : :y
189+
elseif potential_x_factor > 1
190+
resize = :x
191+
elseif potential_y_factor > 1
192+
resize = :y
193+
else
194+
resize = potential_x_factor > potential_y_factor ? :x : :y
195+
@warn "No valid scaling factor found, chose $resize"
196+
end
197+
@info "Resizing $resize"
198+
199+
if resize == :x
200+
xratio[] = potential_x_factor
201+
low = Float64(ax.finallimits[].origin[1])
202+
hi = Float64(low + ax.finallimits[].widths[1])
182203
xlims!(ax, ratio*low, ratio*hi)
183-
else # need to increast y limits
184-
low = ax.finallimits[].origin[2]
185-
hi = low + ax.finallimits[].widths[2]
204+
else
205+
yratio[] = potential_y_factor
206+
low = Float64(ax.finallimits[].origin[2])
207+
hi = Float64(low + ax.finallimits[].widths[2])
186208
ylims!(ax, low/ratio, hi/ratio)
187209
end
188210
end

NetworkDynamicsInspector/src/utils.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,43 @@ end
7777

7878
getcycled(v::AbstractVector, i) = v[mod1(i, length(v))]
7979
gendomid(s::String) = replace(string(gensym("selectbox")), "#"=>"")
80+
81+
mutable struct Throttle{F}
82+
f::F
83+
delay::Float64
84+
@atomic prev::Float64
85+
@atomic future_task::Union{Timer,Nothing}
86+
end
87+
function Throttle(f, delay)
88+
Throttle(f, Float64(delay), 0.0, nothing)
89+
end
90+
91+
function (tr::Throttle)(args...)
92+
now = time()
93+
if !isnothing(tr.future_task)
94+
close(tr.future_task)
95+
@atomic tr.future_task = nothing
96+
end
97+
if now - tr.prev > tr.delay
98+
@atomic tr.prev = now
99+
return tr.f(args...)
100+
else
101+
waitfor = tr.delay - (now - tr.prev) + 1e-3
102+
@atomic tr.future_task = Timer(waitfor) do _
103+
tr(args...)
104+
end
105+
end
106+
end
107+
108+
function on_throttled(f, obs; update=false, delay)
109+
tr = Throttle(f, delay)
110+
on(obs; update) do args...
111+
tr(args...)
112+
end
113+
end
114+
function onany_throttled(f, obs...; update=false, delay)
115+
tr = Throttle(f, delay)
116+
onany(obs; update) do args...
117+
tr(args...)
118+
end
119+
end

test/Inspector_test.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ app = (;
8282
);
8383

8484
let
85+
isfile(WGLMakie.WGL.bundle_file) && rm(WGLMakie.WGL.bundle_file)
8586
_app = App() do session
8687
@info "start new session"
8788
WGLMakie.activate!(resize_to=:parent)
@@ -109,12 +110,12 @@ let
109110
styleTag.innerHTML = `.resize-with-gp { width: ${graphplotWidth} !important; }`;
110111
111112
// Manually trigger the resize event on the window
112-
const resizeEvent = new Event('resize');
113-
window.dispatchEvent(resizeEvent);
113+
// const resizeEvent = new Event('resize');
114+
// window.dispatchEvent(resizeEvent);
114115
};
115116
116117
// Use ResizeObserver for live resizing feedback
117-
const updateResizeWithGpWidth_throttled = Bonito.throttle_function(updateResizeWithGpWidth, 1);
118+
const updateResizeWithGpWidth_throttled = Bonito.throttle_function(updateResizeWithGpWidth, 10);
118119
const resizeObserver = new ResizeObserver(updateResizeWithGpWidth_throttled);
119120
resizeObserver.observe(graphplotCard);
120121

0 commit comments

Comments
 (0)