Skip to content

Commit fd80619

Browse files
committed
impr elecron ext
- respect resolution argument - allow to take screenshots
1 parent a9d2fdc commit fd80619

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

NetworkDynamicsInspector/ext/ElectronExt.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function NDI.close_display(::NDI.ElectronDisp; strict)
2626
end
2727

2828
function get_electron_display()
29-
window = get_electron_window()
29+
window = NDI.get_electron_window()
3030
# BUG: Electron display cannot be reused
3131
# if isnothing(ELECTRON_DISP[]) || window != ELECTRON_DISP[].window
3232
# disp = HTTPServer.ElectronDisplay(window, HTTPServer.BrowserDisplay(; open_browser=false))
@@ -37,23 +37,32 @@ function get_electron_display()
3737
return HTTPServer.ElectronDisplay(window, HTTPServer.BrowserDisplay(; open_browser=false))
3838
end
3939

40-
function get_electron_window()
41-
app = get_electron_app()
40+
function NDI.get_electron_window()
41+
app = NDI.get_electron_app()
4242

4343
any(w -> !w.exists, windows(app)) && @warn "App contains reference to nonexistent window(s)"
4444

4545
window = if isempty(windows(app))
46-
opts = Dict(:width => 1200, :height => 800)
46+
x, y = NDI.CURRENT_DISPLAY[] isa NDI.ElectronDisp ? NDI.CURRENT_DISPLAY[].resolution : (1200, 800)
47+
opts = Dict(:width => x, :height => y, :webPreferences => Dict(:enableRemoteModule => true))
48+
@info "Create new Electron Window with $opts"
4749
Electron.Window(app, opts)
4850
else
4951
length(windows(app)) != 1 && @warn "App contains multiple windows"
5052
first(windows(app))
5153
end
54+
# check window size
55+
if NDI.CURRENT_DISPLAY[] isa NDI.ElectronDisp
56+
x, y = NDI.CURRENT_DISPLAY[].resolution
57+
run(NDI.get_electron_app(),
58+
"BrowserWindow.fromId($(window.id)).setSize($x, $y)")
59+
end
60+
5261
return window
5362
end
5463
haswindow() = hasapp() && !isempty(windows(ELECTRON_APP[]))
5564

56-
function get_electron_app()
65+
function NDI.get_electron_app()
5766
if !hasapp()
5867
ELECTRON_APP[] = Electron.Application(;
5968
additional_electron_args=[
@@ -77,10 +86,12 @@ end
7786

7887
function NDI.toggle_devtools()
7988
if haswindow()
80-
Electron.toggle_devtools(get_electron_window())
89+
Electron.toggle_devtools(NDI.get_electron_window())
8190
else
8291
error("No window to toggle devtools!")
8392
end
8493
end
94+
NDI.has_electron_window() = haswindow()
95+
8596

8697
end

NetworkDynamicsInspector/src/serving.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,24 @@ function close_session(session)
124124
close(session)
125125
end
126126
end
127+
128+
# functions defined in extension
129+
function toggle_devtools end
130+
function has_electron_window end
131+
function get_electron_app end
132+
function get_electron_window end
133+
134+
function save_electron_screenshot(path=joinpath(@__DIR__, "screenshot.png"))
135+
has_electron_window()|| error("No Electron window exists!")
136+
winid = NDI.get_electron_window().id
137+
js = """
138+
let win = BrowserWindow.fromId($winid)
139+
win.webContents.capturePage().then(image => {
140+
const screenshotPath = '$path';
141+
require('fs').writeFileSync(screenshotPath, image.toPNG());
142+
console.log('Screenshot saved to ', screenshotPath);
143+
});
144+
"""
145+
d = run(NDI.get_electron_app(), js)
146+
path
147+
end

NetworkDynamicsInspector/src/utils.jl

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

0 commit comments

Comments
 (0)