Skip to content

Commit

Permalink
Remove window::Mode and introduce Settings::visible
Browse files Browse the repository at this point in the history
Additionally, only show the window once one frame has been rendered to avoid blank flashes on Windows.
  • Loading branch information
hecrj committed Aug 18, 2022
1 parent 07cbed1 commit 277b848
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 121 deletions.
25 changes: 19 additions & 6 deletions glutin/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ where
runtime.enter(|| A::new(flags))
};

let should_be_visible = settings.window.visible;

let context = {
let builder = settings.window.into_builder(
&application.title(),
application.mode(),
event_loop.primary_monitor(),
settings.id,
);
let builder = settings
.window
.into_builder(
&application.title(),
event_loop.primary_monitor(),
settings.id,
)
.with_visible(false);

log::info!("Window builder: {:#?}", builder);

Expand Down Expand Up @@ -135,6 +139,7 @@ where
receiver,
context,
init_command,
should_be_visible,
settings.exit_on_close_request,
));

Expand Down Expand Up @@ -187,6 +192,7 @@ async fn run_instance<A, E, C>(
mut receiver: mpsc::UnboundedReceiver<glutin::event::Event<'_, A::Message>>,
mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>,
init_command: Command<A::Message>,
should_be_visible: bool,
exit_on_close_request: bool,
) where
A: Application + 'static,
Expand All @@ -200,6 +206,7 @@ async fn run_instance<A, E, C>(
let mut clipboard = Clipboard::connect(context.window());
let mut cache = user_interface::Cache::default();
let mut state = application::State::new(&application, context.window());
let mut visible = false;
let mut viewport_version = state.viewport_version();

application::run_command(
Expand Down Expand Up @@ -399,6 +406,12 @@ async fn run_instance<A, E, C>(

debug.render_finished();

if !visible && should_be_visible {
context.window().set_visible(true);

visible = true;
}

// TODO: Handle animations!
// Maybe we can use `ControlFlow::WaitUntil` for this.
}
Expand Down
21 changes: 0 additions & 21 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Build interactive cross-platform applications.
use crate::window;
use crate::{Command, Element, Executor, Settings, Subscription};

pub use iced_native::application::{Appearance, StyleSheet};
Expand Down Expand Up @@ -169,18 +168,6 @@ pub trait Application: Sized {
Subscription::none()
}

/// Returns the current [`Application`] mode.
///
/// The runtime will automatically transition your application if a new mode
/// is returned.
///
/// Currently, the mode only has an effect in native platforms.
///
/// By default, an application will run in windowed mode.
fn mode(&self) -> window::Mode {
window::Mode::Windowed
}

/// Returns the scale factor of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
Expand Down Expand Up @@ -277,14 +264,6 @@ where
self.0.style()
}

fn mode(&self) -> iced_winit::Mode {
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,
}
}

fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}
Expand Down
2 changes: 0 additions & 2 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Configure the window of your application in native platforms.
mod mode;
mod position;
mod settings;

pub mod icon;

pub use icon::Icon;
pub use mode::Mode;
pub use position::Position;
pub use settings::Settings;

Expand Down
12 changes: 0 additions & 12 deletions src/window/mode.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/window/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Settings {
/// The maximum size of the window.
pub max_size: Option<(u32, u32)>,

/// Whether the window should be visible or not.
pub visible: bool,

/// Whether the window should be resizable or not.
pub resizable: bool,

Expand All @@ -38,6 +41,7 @@ impl Default for Settings {
position: Position::default(),
min_size: None,
max_size: None,
visible: true,
resizable: true,
decorations: true,
transparent: false,
Expand All @@ -54,6 +58,7 @@ impl From<Settings> for iced_winit::settings::Window {
position: iced_winit::Position::from(settings.position),
min_size: settings.min_size,
max_size: settings.max_size,
visible: settings.visible,
resizable: settings.resizable,
decorations: settings.decorations,
transparent: settings.transparent,
Expand Down
37 changes: 20 additions & 17 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::mouse;
use crate::renderer;
use crate::widget::operation;
use crate::{
Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Size,
Command, Debug, Error, Executor, Proxy, Runtime, Settings, Size,
Subscription,
};

Expand Down Expand Up @@ -81,16 +81,6 @@ where
Subscription::none()
}

/// Returns the current [`Application`] mode.
///
/// The runtime will automatically transition your application if a new mode
/// is returned.
///
/// By default, an application will run in windowed mode.
fn mode(&self) -> Mode {
Mode::Windowed
}

/// Returns the scale factor of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
Expand Down Expand Up @@ -147,12 +137,15 @@ where
runtime.enter(|| A::new(flags))
};

let builder = settings.window.into_builder(
&application.title(),
application.mode(),
event_loop.primary_monitor(),
settings.id,
);
let should_be_visible = settings.window.visible;
let builder = settings
.window
.into_builder(
&application.title(),
event_loop.primary_monitor(),
settings.id,
)
.with_visible(false);

log::info!("Window builder: {:#?}", builder);

Expand Down Expand Up @@ -189,6 +182,7 @@ where
receiver,
init_command,
window,
should_be_visible,
settings.exit_on_close_request,
));

Expand Down Expand Up @@ -239,6 +233,7 @@ async fn run_instance<A, E, C>(
mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
init_command: Command<A::Message>,
window: winit::window::Window,
should_be_visible: bool,
exit_on_close_request: bool,
) where
A: Application + 'static,
Expand All @@ -252,6 +247,7 @@ async fn run_instance<A, E, C>(
let mut clipboard = Clipboard::connect(&window);
let mut cache = user_interface::Cache::default();
let mut surface = compositor.create_surface(&window);
let mut visible = false;

let mut state = State::new(&application, &window);
let mut viewport_version = state.viewport_version();
Expand Down Expand Up @@ -383,6 +379,7 @@ async fn run_instance<A, E, C>(
event::MacOS::ReceivedUrl(url),
)) => {
use iced_native::event;

events.push(iced_native::Event::PlatformSpecific(
event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(
url,
Expand Down Expand Up @@ -450,6 +447,12 @@ async fn run_instance<A, E, C>(
Ok(()) => {
debug.render_finished();

if !visible && should_be_visible {
window.set_visible(true);

visible = true;
}

// TODO: Handle animations!
// Maybe we can use `ControlFlow::WaitUntil` for this.
}
Expand Down
19 changes: 1 addition & 18 deletions winit/src/application/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::application::{self, StyleSheet as _};
use crate::conversion;
use crate::{Application, Color, Debug, Mode, Point, Size, Viewport};
use crate::{Application, Color, Debug, Point, Size, Viewport};

use std::marker::PhantomData;
use winit::event::{Touch, WindowEvent};
Expand All @@ -13,7 +13,6 @@ where
<A::Renderer as crate::Renderer>::Theme: application::StyleSheet,
{
title: String,
mode: Mode,
scale_factor: f64,
viewport: Viewport,
viewport_version: usize,
Expand All @@ -31,7 +30,6 @@ where
/// Creates a new [`State`] for the provided [`Application`] and window.
pub fn new(application: &A, window: &Window) -> Self {
let title = application.title();
let mode = application.mode();
let scale_factor = application.scale_factor();
let theme = application.theme();
let appearance = theme.appearance(application.style());
Expand All @@ -47,7 +45,6 @@ where

Self {
title,
mode,
scale_factor,
viewport,
viewport_version: 0,
Expand Down Expand Up @@ -193,20 +190,6 @@ where
self.title = new_title;
}

// Update window mode
let new_mode = application.mode();

if self.mode != new_mode {
window.set_fullscreen(conversion::fullscreen(
window.current_monitor(),
new_mode,
));

window.set_visible(conversion::visible(new_mode));

self.mode = new_mode;
}

// Update scale factor
let new_scale_factor = application.scale_factor();

Expand Down
25 changes: 1 addition & 24 deletions winit/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::keyboard;
use crate::mouse;
use crate::touch;
use crate::window;
use crate::{Event, Mode, Point, Position};
use crate::{Event, Point, Position};

/// Converts a winit window event into an iced event.
pub fn window_event(
Expand Down Expand Up @@ -182,29 +182,6 @@ pub fn position(
}
}

/// Converts a [`Mode`] to a [`winit`] fullscreen mode.
///
/// [`winit`]: https://github.com/rust-windowing/winit
pub fn fullscreen(
monitor: Option<winit::monitor::MonitorHandle>,
mode: Mode,
) -> Option<winit::window::Fullscreen> {
match mode {
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
Expand Down
2 changes: 0 additions & 2 deletions winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ pub mod window;
pub mod system;

mod error;
mod mode;
mod position;
mod proxy;

pub use application::Application;
pub use clipboard::Clipboard;
pub use error::Error;
pub use mode::Mode;
pub use position::Position;
pub use proxy::Proxy;
pub use settings::Settings;
Expand Down
12 changes: 0 additions & 12 deletions winit/src/mode.rs

This file was deleted.

0 comments on commit 277b848

Please sign in to comment.