Skip to content

Add pause_eventloop for do-block syntax #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Gtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const enable_eventloop_lock = Base.ReentrantLock()

Set whether Gtk's event loop is running.
"""
function enable_eventloop(b::Bool = true)
function enable_eventloop(b::Bool = true; wait_stopped::Bool = false)
lock(enable_eventloop_lock) do # handle widgets that are being shown/destroyed from different threads
isassigned(quit_task) && wait(quit_task[]) # prevents starting while the async is still stopping
if b
Expand All @@ -169,11 +169,29 @@ function enable_eventloop(b::Bool = true)
gtk_quit()
gtk_main_running[] = false
end
wait_stopped && wait(quit_task[])
end
end
end
end

"""
Gtk.pause_eventloop(f; force = false)

Pauses the eventloop around a function. Restores the state of the eventloop after
pausing. Respects whether Gtk.jl is configured to allow auto-stopping of the
eventloop, unless `force = true`.
"""
function pause_eventloop(f; force = false)
was_running = is_eventloop_running()
(force || auto_idle[]) && enable_eventloop(false, wait_stopped = true)
try
f()
finally
(force || auto_idle[]) && enable_eventloop(was_running)
end
end

"""
Gtk.is_eventloop_running()::Bool

Expand Down
26 changes: 26 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,30 @@ destroy(win)

@test isa(Gtk.GdkEventKey(), Gtk.GdkEventKey)

@testset "Eventloop control" begin
before = Gtk.auto_idle[]

Gtk.enable_eventloop(true)
@test Gtk.is_eventloop_running()

Gtk.auto_idle[] = true
Gtk.pause_eventloop() do
@test !Gtk.is_eventloop_running()
end
@test Gtk.is_eventloop_running()

Gtk.auto_idle[] = false
Gtk.pause_eventloop() do
@test Gtk.is_eventloop_running()
end
@test Gtk.is_eventloop_running()

Gtk.pause_eventloop(force = true) do
@test !Gtk.is_eventloop_running()
end
@test Gtk.is_eventloop_running()

Gtk.auto_idle[] = before
end

end