Skip to content

Commit

Permalink
fix MakieOrg#1546 for real (inserting newly created scenes) (MakieOrg…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch authored Jan 6, 2023
1 parent a110787 commit 54d2898
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 48 deletions.
1 change: 0 additions & 1 deletion GLMakie/src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ function Makie.apply_screen_config!(screen::Screen, config::ScreenConfig, scene:
end

function apply_config!(screen::Screen, config::ScreenConfig; visible::Bool=true, start_renderloop::Bool=true)
ShaderAbstractions.switch_context!(screen.glscreen)
glw = screen.glscreen
ShaderAbstractions.switch_context!(glw)
GLFW.SetWindowAttrib(glw, GLFW_FOCUS_ON_SHOW, config.focus_on_show)
Expand Down
6 changes: 3 additions & 3 deletions WGLMakie/src/Serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function add_scene(scene_id, three_scene) {
scene_cache[scene_id] = three_scene;
}

function find_scene(scene_id) {
export function find_scene(scene_id) {
return scene_cache[scene_id];
}

Expand All @@ -26,7 +26,7 @@ export function delete_scene(scene_id) {
delete scene_cache[scene_id];
}

function find_plots(plot_uuids) {
export function find_plots(plot_uuids) {
const plots = [];
plot_uuids.forEach((id) => {
const plot = plot_cache[id];
Expand Down Expand Up @@ -515,4 +515,4 @@ export function deserialize_scene(data, screen) {
return scene;
}

export { TEXTURE_ATLAS };
export { TEXTURE_ATLAS, scene_cache };
92 changes: 79 additions & 13 deletions WGLMakie/src/display.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct ThreeDisplay <: Makie.MakieScreen
struct ThreeDisplay
session::JSServe.Session
end

Expand All @@ -23,6 +23,9 @@ function JSServe.jsrender(session::Session, scene::Scene)
put!(c, three)
screen = Screen(c, true, scene)
Makie.push_screen!(scene, screen)
on(on_init) do i
mark_as_displayed!(screen, scene)
end
return canvas
end

Expand Down Expand Up @@ -59,6 +62,21 @@ mutable struct Screen <: Makie.MakieScreen
three::Channel{ThreeDisplay}
display::Any
scene::Union{Nothing, Scene}
displayed_scenes::Set{String}
function Screen(
three::Channel{ThreeDisplay},
display::Any,
scene::Union{Nothing, Scene})
return new(three, display, scene, Set{String}())
end
end

function mark_as_displayed!(screen::Screen, scene::Scene)
push!(screen.displayed_scenes, js_uuid(scene))
for child_scene in scene.children
mark_as_displayed!(screen, child_scene)
end
return
end

for M in WEB_MIMES
Expand All @@ -69,6 +87,8 @@ for M in WEB_MIMES
Makie.push_screen!(scene, screen)
on(init_obs) do _
put!(screen.three, three)
mark_as_displayed!(screen, scene)
return
end
return canvas
end
Expand All @@ -89,18 +109,24 @@ function Base.size(screen::Screen)
return size(screen.scene)
end

function get_three(screen::Screen; timeout = 100)
function get_three(screen::Screen; timeout = 100, error::Union{Nothing, String}=nothing)::Union{Nothing, ThreeDisplay}
tstart = time()
result = nothing
while true
yield()
if time() - tstart > timeout
return nothing # we waited LONG ENOUGH!!
break # we waited LONG ENOUGH!!
end
if isready(screen.three)
return fetch(screen.three)
result = fetch(screen.three)
break
end
end
return nothing
# Throw error if error message specified
if isnothing(result) && !isnothing(error)
Base.error(error)
end
return result
end

function Makie.apply_screen_config!(screen::ThreeDisplay, config::ScreenConfig, args...)
Expand Down Expand Up @@ -131,6 +157,8 @@ function Base.display(screen::Screen, scene::Scene; kw...)
three, canvas, done_init = three_display(session, scene)
on(done_init) do _
put!(screen.three, three)
mark_as_displayed!(screen, scene)
return
end
return canvas
end
Expand Down Expand Up @@ -165,15 +193,53 @@ function Makie.colorbuffer(screen::Screen)
if screen.display !== true
Base.display(screen, screen.scene)
end
three = get_three(screen)
if isnothing(three)
error("Not able to show scene in a browser")
end
three = get_three(screen; error="Not able to show scene in a browser")
return session2image(three.session, screen.scene)
end

function Base.insert!(td::Screen, scene::Scene, plot::Combined)
disp = get_three(td)
disp === nothing && error("Plot needs to be displayed to insert additional plots")
insert!(disp, scene, plot)
function Base.insert!(screen::Screen, scene::Scene, plot::Combined)
disp = get_three(screen; error="Plot needs to be displayed to insert additional plots")
if js_uuid(scene) in screen.displayed_scenes
plot_data = serialize_plots(scene, [plot])
JSServe.evaljs_value(disp.session, js"""
$(WGL).then(WGL=> {
WGL.insert_plot($(js_uuid(scene)), $plot_data);
})""")
else
# Newly created scene gets inserted!
# This must be a child plot of some parent, otherwise a plot wouldn't be inserted via `insert!(screen, ...)`
parent = scene.parent
@assert parent !== scene
if isnothing(parent)
# This shouldn't happen, since insert! only gets called for scenes, that already got displayed on a screen
error("Scene has no parent, but hasn't been displayed yet")
end
# We serialize the whole scene (containing `plot` as well),
# since, we should only get here if scene is newly created and this is the first plot we insert!
@assert scene.plots[1] == plot
scene_ser = serialize_scene(scene)
parent_uuid = js_uuid(parent)
err = "Cant find scene js_uuid(scene) == $(parent_uuid)"
evaljs_value(disp.session, js"""
$(WGL).then(WGL=> {
const parent = WGL.find_scene($(parent_uuid));
if (!parent) {
throw new Error($(err))
}
const new_scene = WGL.deserialize_scene($scene_ser, parent.screen);
parent.scene_children.push(new_scene);
})
""")
mark_as_displayed!(screen, scene)
end
return
end

function Base.delete!(td::Screen, scene::Scene, plot::Combined)
uuids = js_uuid.(Makie.flatten_plots(plot))
JSServe.evaljs(td.session, js"""
$(WGL).then(WGL=> {
WGL.delete_plots($(js_uuid(scene)), $uuids);
})""")
return
end
30 changes: 0 additions & 30 deletions WGLMakie/src/three_plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@
# We use objectid to find objects on the js side
js_uuid(object) = string(objectid(object))

function Base.insert!(td::ThreeDisplay, scene::Scene, plot::Combined)
JSServe.wait_for_ready(td.session)
plot_data = serialize_plots(scene, [plot])
JSServe.evaljs_value(td.session, js"""
$(WGL).then(WGL=> {
WGL.insert_plot($(js_uuid(scene)), $plot_data);
})""")
return
end

function Base.delete!(td::ThreeDisplay, scene::Scene, plot::Combined)
uuids = js_uuid.(Makie.flatten_plots(plot))
WGL.delete_plots(td.session, js_uuid(scene), uuids)
return
end

function all_plots_scenes(scene::Scene; scene_uuids=String[], plot_uuids=String[])
push!(scene_uuids, js_uuid(scene))
for plot in scene.plots
Expand All @@ -30,20 +14,6 @@ function all_plots_scenes(scene::Scene; scene_uuids=String[], plot_uuids=String[
return scene_uuids, plot_uuids
end

"""
find_plots(td::ThreeDisplay, plot::AbstractPlot)
Gets the ThreeJS object representing the plot object.
"""
function find_plots(td::ThreeDisplay, plot::AbstractPlot)
return find_plots(JSServe.session(td), plot)
end

function find_plots(session::Session, plot::AbstractPlot)
uuids = js_uuid.(Makie.flatten_plots(plot))
return WGL.find_plots(session, uuids)
end

function JSServe.print_js_code(io::IO, plot::AbstractPlot, context::IdDict)
uuids = js_uuid.(Makie.flatten_plots(plot))
# This is a bit more complicated then it has to be, since evaljs / on_document_load
Expand Down
20 changes: 19 additions & 1 deletion WGLMakie/src/wglmakie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import {
delete_scenes,
insert_plot,
delete_plots,
find_plots,
deserialize_scene,
delete_scene,
TEXTURE_ATLAS,
on_next_insert,
scene_cache,
find_scene,
} from "./Serialization.js";

import { event2scene_pixel } from "./Camera.js";
Expand Down Expand Up @@ -423,6 +426,22 @@ export function pick_native_matrix(scene, x, y, w, h) {
return matrix;
}

window.WGL = {
deserialize_scene,
threejs_module,
start_renderloop,
delete_plots,
insert_plot,
find_plots,
delete_scene,
find_scene,
scene_cache,
delete_scenes,
create_scene,
event2scene_pixel,
on_next_insert,
};

export {
deserialize_scene,
threejs_module,
Expand All @@ -433,7 +452,6 @@ export {
delete_scene,
find_scene,
scene_cache,
plot_cache,
delete_scenes,
create_scene,
event2scene_pixel,
Expand Down

0 comments on commit 54d2898

Please sign in to comment.