add window visibility

This commit is contained in:
Cory Forsstrom 2021-04-08 12:58:08 -07:00
parent 3ea2c4595a
commit cdab8f90fb
4 changed files with 34 additions and 1 deletions

View File

@ -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")]

View File

@ -98,6 +98,13 @@ pub trait Application: Program<Clipboard = Clipboard> {
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)

View File

@ -12,6 +12,7 @@ pub struct State<A: Application> {
mode: Mode,
background_color: Color,
scale_factor: f64,
visible: bool,
viewport: Viewport,
viewport_version: usize,
cursor_position: winit::dpi::PhysicalPosition<f64>,
@ -26,6 +27,7 @@ impl<A: Application> State<A> {
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<A: Application> State<A> {
mode,
background_color,
scale_factor,
visible,
viewport,
viewport_version: 0,
// TODO: Encode cursor availability in the type-system
@ -201,5 +204,14 @@ impl<A: Application> State<A> {
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;
}
}
}

View File

@ -66,6 +66,7 @@ impl Window {
self,
title: &str,
mode: Mode,
visible: bool,
primary_monitor: Option<MonitorHandle>,
) -> 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