From cdab8f90fb525c509e0a15bb1d0b3d7213e176f3 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 8 Apr 2021 12:58:08 -0700 Subject: [PATCH 1/3] add window visibility --- src/application.rs | 11 +++++++++++ winit/src/application.rs | 8 ++++++++ winit/src/application/state.rs | 12 ++++++++++++ winit/src/settings.rs | 4 +++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/application.rs b/src/application.rs index 7b7de6d4..317f9801 100644 --- a/src/application.rs +++ b/src/application.rs @@ -191,6 +191,13 @@ pub trait Application: Sized { false } + /// Returns whether the [`Application`] should be visible or not + /// + /// By default, it returns `true`. + fn visible(&self) -> bool { + true + } + /// Runs the [`Application`]. /// /// On native platforms, this method will take control of the current thread @@ -295,6 +302,10 @@ where fn should_exit(&self) -> bool { self.0.should_exit() } + + fn visible(&self) -> bool { + self.0.visible() + } } #[cfg(target_arch = "wasm32")] diff --git a/winit/src/application.rs b/winit/src/application.rs index 106d5218..e73f3df1 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -98,6 +98,13 @@ pub trait Application: Program { fn should_exit(&self) -> bool { false } + + /// Returns whether the [`Application`] should be visible or not + /// + /// By default, it returns `true`. + fn visible(&self) -> bool { + true + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -145,6 +152,7 @@ where .into_builder( &application.title(), application.mode(), + application.visible(), event_loop.primary_monitor(), ) .build(&event_loop) diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 46297370..d157211a 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -12,6 +12,7 @@ pub struct State { mode: Mode, background_color: Color, scale_factor: f64, + visible: bool, viewport: Viewport, viewport_version: usize, cursor_position: winit::dpi::PhysicalPosition, @@ -26,6 +27,7 @@ impl State { let mode = application.mode(); let background_color = application.background_color(); let scale_factor = application.scale_factor(); + let visible = application.visible(); let viewport = { let physical_size = window.inner_size(); @@ -41,6 +43,7 @@ impl State { mode, background_color, scale_factor, + visible, viewport, viewport_version: 0, // TODO: Encode cursor availability in the type-system @@ -201,5 +204,14 @@ impl State { self.scale_factor = new_scale_factor; } + + // Update window visibility + let new_visible = application.visible(); + + if self.visible != new_visible { + window.set_visible(new_visible); + + self.visible = new_visible; + } } } diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 9ce5cfc5..56be1e07 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -66,6 +66,7 @@ impl Window { self, title: &str, mode: Mode, + visible: bool, primary_monitor: Option, ) -> WindowBuilder { let mut window_builder = WindowBuilder::new(); @@ -80,7 +81,8 @@ impl Window { .with_transparent(self.transparent) .with_window_icon(self.icon) .with_always_on_top(self.always_on_top) - .with_fullscreen(conversion::fullscreen(primary_monitor, mode)); + .with_fullscreen(conversion::fullscreen(primary_monitor, mode)) + .with_visible(visible); if let Some((width, height)) = self.min_size { window_builder = window_builder From 6f6f1d82e822983941f3b3ba1c6d93df3e98b8c3 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 8 Apr 2021 13:09:58 -0700 Subject: [PATCH 2/3] add to glutin --- glutin/src/application.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 79fcf745..5d72ee41 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -55,6 +55,7 @@ where let builder = settings.window.into_builder( &application.title(), application.mode(), + application.visible(), event_loop.primary_monitor(), ); From 84c0c9bc7ab858793183560739c8fd6087e22f6e Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 9 Apr 2021 09:00:29 -0700 Subject: [PATCH 3/3] use Mode::Hidden instead --- glutin/src/application.rs | 1 - src/application.rs | 12 +----------- src/window/mode.rs | 3 +++ winit/src/application.rs | 8 -------- winit/src/application/state.rs | 14 ++------------ winit/src/conversion.rs | 10 +++++++++- winit/src/mode.rs | 3 +++ winit/src/settings.rs | 3 +-- 8 files changed, 19 insertions(+), 35 deletions(-) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 5d72ee41..79fcf745 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -55,7 +55,6 @@ where let builder = settings.window.into_builder( &application.title(), application.mode(), - application.visible(), event_loop.primary_monitor(), ); diff --git a/src/application.rs b/src/application.rs index 317f9801..ee532e0b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -191,13 +191,6 @@ pub trait Application: Sized { false } - /// Returns whether the [`Application`] should be visible or not - /// - /// By default, it returns `true`. - fn visible(&self) -> bool { - true - } - /// Runs the [`Application`]. /// /// On native platforms, this method will take control of the current thread @@ -284,6 +277,7 @@ where match self.0.mode() { window::Mode::Windowed => iced_winit::Mode::Windowed, window::Mode::Fullscreen => iced_winit::Mode::Fullscreen, + window::Mode::Hidden => iced_winit::Mode::Hidden, } } @@ -302,10 +296,6 @@ where fn should_exit(&self) -> bool { self.0.should_exit() } - - fn visible(&self) -> bool { - self.0.visible() - } } #[cfg(target_arch = "wasm32")] diff --git a/src/window/mode.rs b/src/window/mode.rs index 37464711..fdce8e23 100644 --- a/src/window/mode.rs +++ b/src/window/mode.rs @@ -6,4 +6,7 @@ pub enum Mode { /// The application takes the whole screen of its current monitor. Fullscreen, + + /// The application is hidden + Hidden, } diff --git a/winit/src/application.rs b/winit/src/application.rs index e73f3df1..106d5218 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -98,13 +98,6 @@ pub trait Application: Program { fn should_exit(&self) -> bool { false } - - /// Returns whether the [`Application`] should be visible or not - /// - /// By default, it returns `true`. - fn visible(&self) -> bool { - true - } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -152,7 +145,6 @@ where .into_builder( &application.title(), application.mode(), - application.visible(), event_loop.primary_monitor(), ) .build(&event_loop) diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index d157211a..b54d3aed 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -12,7 +12,6 @@ pub struct State { mode: Mode, background_color: Color, scale_factor: f64, - visible: bool, viewport: Viewport, viewport_version: usize, cursor_position: winit::dpi::PhysicalPosition, @@ -27,7 +26,6 @@ impl State { let mode = application.mode(); let background_color = application.background_color(); let scale_factor = application.scale_factor(); - let visible = application.visible(); let viewport = { let physical_size = window.inner_size(); @@ -43,7 +41,6 @@ impl State { mode, background_color, scale_factor, - visible, viewport, viewport_version: 0, // TODO: Encode cursor availability in the type-system @@ -185,6 +182,8 @@ impl State { new_mode, )); + window.set_visible(conversion::visible(new_mode)); + self.mode = new_mode; } @@ -204,14 +203,5 @@ impl State { self.scale_factor = new_scale_factor; } - - // Update window visibility - let new_visible = application.visible(); - - if self.visible != new_visible { - window.set_visible(new_visible); - - self.visible = new_visible; - } } } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0fa27413..b850a805 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -141,13 +141,21 @@ pub fn fullscreen( mode: Mode, ) -> Option { match mode { - Mode::Windowed => None, + Mode::Windowed | Mode::Hidden => None, Mode::Fullscreen => { Some(winit::window::Fullscreen::Borderless(monitor)) } } } +/// Converts a [`Mode`] to a visibility flag. +pub fn visible(mode: Mode) -> bool { + match mode { + Mode::Windowed | Mode::Fullscreen => true, + Mode::Hidden => false, + } +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit diff --git a/winit/src/mode.rs b/winit/src/mode.rs index 37464711..fdce8e23 100644 --- a/winit/src/mode.rs +++ b/winit/src/mode.rs @@ -6,4 +6,7 @@ pub enum Mode { /// The application takes the whole screen of its current monitor. Fullscreen, + + /// The application is hidden + Hidden, } diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 56be1e07..663fa07a 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -66,7 +66,6 @@ impl Window { self, title: &str, mode: Mode, - visible: bool, primary_monitor: Option, ) -> WindowBuilder { let mut window_builder = WindowBuilder::new(); @@ -82,7 +81,7 @@ impl Window { .with_window_icon(self.icon) .with_always_on_top(self.always_on_top) .with_fullscreen(conversion::fullscreen(primary_monitor, mode)) - .with_visible(visible); + .with_visible(conversion::visible(mode)); if let Some((width, height)) = self.min_size { window_builder = window_builder