-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Implemented GLib-based dispatcher #17281
Conversation
This should allow running Avalonia on the same thread as another UI toolkit that supports running on top of GRunLoop (e. g. GTK)
You can test this PR using the following package version. |
ec9e2e4
to
3e05019
Compare
You can test this PR using the following package version. |
using (var utf = new Utf8Buffer($"avalonia.app.a{Guid.NewGuid():N}")) | ||
app = gtk_application_new(utf, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A question. assuming, these APIs will be used from the GTK.
Will GTK Application -> Avalonia App -> Dialog specific GTK Application
work well? Don't we need to check for existing gtk app? It's probably out of scope of this PR either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure if it's even required to be called. I suspect that gtk_init_check should be sufficient.
* Implemented GLib-based dispatcher This should allow running Avalonia on the same thread as another UI toolkit that supports running on top of GRunLoop (e. g. GTK) * Force-drain the X11 event queue, since g_runloop_quit won't exit the loop otherwise #Conflicts: # src/Avalonia.X11/X11Platform.cs
This should allow running Avalonia on the same thread as another UI toolkit that supports running on top of GRunLoop (e. g. GTK).
GLib priorities and Avalonia priorities are a bit different. Avalonia follows the WPF model when there
are "background" and "foreground" priority groups. Foreground jobs are executed before any user input processing,
background jobs are executed strictly after user input processing.
GLib has numeric priorities that are used in the following way:
G_PRIORITY_DEFAULT
- polling X11 events (GTK) and default value for g_timeout_addG_PRIORITY_HIGH_IDLE
without a clear definition, used as an anchor value of sortsG_PRIORITY_DEFAULT_IDLE
- "idle" priority sourcesSo, unlike Avalonia, GTK puts way higher priority on input processing, then does resize/layout/render
So, to map our model to GLib we do the following:
we also group input events at that stage (this matches our epoll-based dispatcher)
foreground jobs
Unfortunately we can't detect if there are pending non-idle GLib jobs using g_main_context_pending, since
So we just report that we don't support pending input query and let the dispatcher to
call RequestBackgroundProcessing every time, which results in g_idle_add call for every background job.
Background jobs are expected to be relatively expensive to execute since on Windows
MsgWaitForMultipleObjectsEx results isn't really free too.
For signaling (aka waking up dispatcher for processing high priority jobs we are using
g_idle_add_full with (-1) priority. While the naming suggests that it would enqueue an idle job,
it actually adds an always-triggered source that would be called before other sources with lower priority.
For timers we are using a simple g_timeout_add_full and discard the previous one when dispatcher requests
an update
Since GLib dispatches event sources in batches, we force-check for "signaled" flag to run high-prio jobs
whenever we get control back from GLib. We can still occasionally get GTK code to run before high-prio
Avalonia-jobs, but that should be fine since the point is to keep Avalonia-based jobs ordered properly
and to not have our low-priority jobs to prevent GLib-based code from running its own "foreground" jobs
Another implementation note here is that GLib (just as any other C library) is NOT aware of C# exceptions,
so we are NOT allowed to have exceptions to escape native->managed call boundary. So we have exception handlers
that try to propagate those to the nearest run loop frame that was initiated by Avalonia.
If there is no such frame, we have no choice but to log/swallow those