Skip to content
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

feat(windows): add WindowExtWindows::dispatch #792

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions .changes/windows-dispatch-fn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Added `dispatch` to `WindowExtWindows` to run a closure on the main thread.
12 changes: 12 additions & 0 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ pub trait WindowExtWindows {
///
/// Enabling this mainly flips the orientation of menus and title bar buttons
fn set_rtl(&self, rtl: bool);

fn dispatch<F>(&self, function: F)
where
F: FnMut() + 'static;
}

impl WindowExtWindows for Window {
Expand Down Expand Up @@ -238,6 +242,14 @@ impl WindowExtWindows for Window {
fn set_rtl(&self, rtl: bool) {
self.window.set_rtl(rtl)
}

#[inline]
fn dispatch<F>(&self, function: F)
where
F: FnMut() + 'static,
{
self.window.thread_executor.execute_in_thread(function)
}
}

/// Additional methods on `WindowBuilder` that are specific to Windows.
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ impl EventLoopThreadExecutor {
///
/// Note that we use a FnMut instead of a FnOnce because we're too lazy to create an equivalent
/// to the unstable FnBox.
pub(super) fn execute_in_thread<F>(&self, mut function: F)
pub(crate) fn execute_in_thread<F>(&self, mut function: F)
where
F: FnMut() + Send + 'static,
F: FnMut() + 'static,
{
unsafe {
if self.in_event_loop_thread() {
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct Window {
window_state: Arc<Mutex<WindowState>>,

// The events loop proxy.
thread_executor: event_loop::EventLoopThreadExecutor,
pub(crate) thread_executor: event_loop::EventLoopThreadExecutor,
}

impl Window {
Expand Down
Loading