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

Support multiple event loop instances on Windows #3141

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct EventLoopWindowTarget<T: 'static> {
/// easier. But note that constructing multiple event loops is not supported.
#[derive(Default)]
pub struct EventLoopBuilder<T: 'static> {
pub(crate) multiple_instances: bool,
pub(crate) platform_specific: platform_impl::PlatformSpecificEventLoopAttributes,
_p: PhantomData<T>,
}
Expand All @@ -78,6 +79,7 @@ impl<T> EventLoopBuilder<T> {
#[inline]
pub fn with_user_event() -> Self {
Self {
multiple_instances: false,
platform_specific: Default::default(),
_p: PhantomData,
}
Expand Down Expand Up @@ -117,7 +119,7 @@ impl<T> EventLoopBuilder<T> {
)]
#[inline]
pub fn build(&mut self) -> Result<EventLoop<T>, EventLoopError> {
if EVENT_LOOP_CREATED.swap(true, Ordering::Relaxed) {
if !self.multiple_instances && EVENT_LOOP_CREATED.swap(true, Ordering::Relaxed) {
return Err(EventLoopError::RecreationAttempt);
}

Expand Down
12 changes: 12 additions & 0 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub type HMONITOR = isize;

/// Additional methods on `EventLoop` that are specific to Windows.
pub trait EventLoopBuilderExtWindows {
/// Whether to allow multiple event loops to be created.
///
/// By default, only a single event loop is allowed to be created, to make platform
/// compatibility easier.
fn with_multiple_instances(&mut self, multiple_instances: bool) -> &mut Self;

/// Whether to allow the event loop to be created off of the main thread.
///
/// By default, the window is only allowed to be created on the main
Expand Down Expand Up @@ -93,6 +99,12 @@ impl<T> EventLoopBuilderExtWindows for EventLoopBuilder<T> {
self
}

#[inline]
fn with_multiple_instances(&mut self, multiple_instances: bool) -> &mut Self {
self.multiple_instances = multiple_instances;
self
}

#[inline]
fn with_dpi_aware(&mut self, dpi_aware: bool) -> &mut Self {
self.platform_specific.dpi_aware = dpi_aware;
Expand Down