Skip to content

Commit 6c1cc06

Browse files
committed
make electron application/windows singleton
1 parent 827b3bc commit 6c1cc06

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

NetworkDynamicsInspector/assets/app.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ body {
5757
}
5858

5959
.graphplot-card {
60-
height:400px; /* initial widht and height */
61-
width: 400px;
62-
min-width:250px;
60+
height:350px; /* initial widht and height */
61+
width: 350px;
62+
min-width:200px;
6363
resize: both;
6464
overflow: hidden;
6565
}
Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,75 @@
11
module ElectronExt
22

33
using NetworkDynamicsInspector: NetworkDynamicsInspector as NDI
4-
using Electron: Electron
5-
using Bonito: Bonito
4+
using Electron: Electron, windows
5+
using Bonito: Bonito, HTTPServer
66

7-
function NDI.display_electron_app()
7+
const ELECTRON_APP = Ref{Any}(nothing)
8+
9+
function NDI.display_electron_app(restart)
10+
restart && close_windows()
811
webapp = NDI.get_webapp()
9-
# disp = Bonito.use_electron_display()
10-
disp = Bonito.use_electron_display(devtools = true)
12+
disp = get_electron_display()
1113
display(disp, webapp)
1214
nothing
1315
end
1416

17+
function get_electron_display()
18+
window = get_electron_window()
19+
disp = HTTPServer.ElectronDisplay(window, HTTPServer.BrowserDisplay(; open_browser=false))
20+
end
21+
22+
function get_electron_window()
23+
app = get_electron_app()
24+
25+
any(w -> !w.exists, windows(app)) && @warn "App contains reference to nonexistent window(s)"
26+
27+
window = if isempty(windows(app))
28+
opts = Dict(:width => 1200, :height => 800)
29+
Electron.Window(app, opts)
30+
else
31+
length(windows(app)) != 1 && @warn "App contains multiple windows"
32+
first(windows(app))
33+
end
34+
return window
35+
end
36+
haswindow() = hasapp() && !isempty(windows(ELECTRON_APP[]))
37+
38+
function get_electron_app()
39+
if !hasapp()
40+
ELECTRON_APP[] = Electron.Application(;
41+
additional_electron_args=[
42+
"--disable-logging",
43+
"--no-sandbox",
44+
"--user-data-dir=$(mktempdir())",
45+
"--disable-features=AccessibilityObjectModel",
46+
],
47+
)
48+
end
49+
ELECTRON_APP[]
50+
end
51+
hasapp() = !isnothing(ELECTRON_APP[]) && ELECTRON_APP[].exists
52+
53+
function close_windows()
54+
if haswindow()
55+
@info "Close existing Windows"
56+
close.(windows(ELECTRON_APP[]))
57+
end
58+
end
59+
60+
function close_application()
61+
if hasapp()
62+
@info "Close Electron Application"
63+
close(ELECTRON_APP[])
64+
end
65+
end
66+
67+
function NDI.toggle_devtools()
68+
if haswindow()
69+
Electron.toggle_devtools(get_electron_window())
70+
else
71+
error("No window to toggle devtools!")
72+
end
73+
end
74+
1575
end

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ function start_server!(restart=true)
156156
@info "Visit $url:$port to launch App"
157157
end
158158

159-
function start_electron()
159+
function _display_electron_app(restart)
160160
if isempty(methods(display_electron_app))
161161
@error "Electron.jl not available. Please install Electron.jl and `using Electron` before calling this function."
162162
else
163-
display_electron_app()
163+
display_electron_app(restart)
164164
end
165165
end
166166
function display_electron_app end
@@ -253,7 +253,7 @@ function inspect(sol; restart=false, reset=false, electron=false)
253253
end
254254

255255
if electron
256-
start_electron()
256+
_display_electron_app(restart)
257257
else
258258
if !server_running()
259259
start_server!()

NetworkDynamicsInspector/src/utils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,6 @@ function download_assets()
142142
download(url, joinpath(ASSETS, name))
143143
end
144144
end
145+
146+
# function extended by ElectronExt
147+
function toggle_devtools end

0 commit comments

Comments
 (0)