Skip to content

Commit

Permalink
Fix GtkMakieWidget Crashes (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joroks authored Jan 19, 2025
1 parent 41b3aac commit 9908177
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ push!(p[2],scatter(rand(10)))
show(win)
```

The `push!` function adds a Makie `Figure` to the widget. Before modifying Makie plots in a callback, you **must** call `Gtk4.make_current` on the corresponding widget to ensure that the right OpenGL context will be modified. See the "widgets.jl" example for a demonstration of how to use the widget.
The `push!` function adds a Makie `Figure` to the widget. Currently, using a `GtkMakieWidget` will likely break the rendering of any `GLFW` Windows (as for example used by standalone GLMakie) created by the same julia instance.

### Bonus functionality

Expand Down
1 change: 0 additions & 1 deletion examples/interactive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ g=grid(screen)
g[1,2]=GtkButton("Generate new random plot")

function gen_cb(b)
Gtk4.make_current(glarea(screen))
empty!(ax)
lines!(ax,rand(10))
end
Expand Down
2 changes: 0 additions & 2 deletions examples/widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ push!(vbox, label, dropdown, new_scatter_button, clear_figure)

signal_connect(new_scatter_button, "clicked") do b
plotnum = Gtk4.G_.get_selected(dropdown) + 1
Gtk4.make_current(p[plotnum]) # it's critical to include this call -- otherwise GLMakie will not use the right GL context!
scatter!(axes[plotnum], rand(100))
end

signal_connect(clear_figure, "clicked") do b
plotnum = Gtk4.G_.get_selected(dropdown) + 1
Gtk4.make_current(p[plotnum]) # not clear this is needed
empty!(axes[plotnum])
end

Expand Down
2 changes: 0 additions & 2 deletions examples/widgets_extended.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ push!(vbox, GtkLabel("Select figure:"), dropdown, new_scatter_button, GtkLabel("

signal_connect(new_scatter_button, "clicked") do b
plotnum = Gtk4.G_.get_selected(dropdown) + 1
Gtk4.make_current(g[plotnum,1]) # it's critical to include this call -- otherwise GLMakie will not use the right GL context!
scatter!(axes[plotnum], rand(100); label = Gtk4.text(entry))
@idle_add Gtk4.model(g[plotnum,2], Gtk4Makie.plots_model(axes[plotnum])) # update the legend
end

signal_connect(clear_figure, "clicked") do b
plotnum = Gtk4.G_.get_selected(dropdown) + 1
Gtk4.make_current(g[plotnum,1]) # not clear this is needed
empty!(axes[plotnum])
@idle_add Gtk4.model(g[plotnum,2], Gtk4Makie.plots_model(axes[plotnum])) # update the legend
end
Expand Down
1 change: 0 additions & 1 deletion src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ end
# overload this to get access to the figure
function Base.display(screen::GLMakie.Screen{T}, figesque::Union{Makie.Figure,Makie.FigureAxisPlot}; update=true, display_attributes...) where T <: GtkWidget
widget = glarea(screen)
Gtk4.isrealized(widget) && Gtk4.make_current(widget) # required when pushing a figure to a widget well after it's realized
fig = isa(figesque,Figure) ? figesque : figesque.figure
if widget.figure != fig
widget.inspector = nothing
Expand Down
7 changes: 7 additions & 0 deletions src/widget.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ function ShaderAbstractions.native_switch_context!(a::GtkGLMakie)
Gtk4.make_current(a)
end

function ShaderAbstractions.is_current_context(a::GtkGLMakie)
Gtk4.G_.get_realized(a) || return false
a == ShaderAbstractions.ACTIVE_OPENGL_CONTEXT[] || return false
curr = Gtk4.G_.get_current()
return curr !== nothing && curr == Gtk4.G_.get_context(a)
end

## Gtk4Makie overloads

glarea(screen::GLMakie.Screen{T}) where T <: GtkGLArea = screen.glscreen
Expand Down

0 comments on commit 9908177

Please sign in to comment.