Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
add pause_eventloop for do-block syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Jan 17, 2022
1 parent c23b075 commit d944baa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Gtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ function enable_eventloop(b::Bool = true)
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()
if (force || auto_idle[])
enable_eventloop(false)
timedwait(!is_eventloop_running, 2.0) # needed because stopping has an async delay
end
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

0 comments on commit d944baa

Please sign in to comment.