From c4c5216e3b69d732b0518d510f95675a4ba7010b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Mar 2020 18:00:15 +0200 Subject: Allow passing external state to `Application::new` --- winit/src/application.rs | 21 ++++++++++++++----- winit/src/settings.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++ winit/src/settings/mod.rs | 53 ----------------------------------------------- 3 files changed, 66 insertions(+), 58 deletions(-) create mode 100644 winit/src/settings.rs delete mode 100644 winit/src/settings/mod.rs (limited to 'winit') diff --git a/winit/src/application.rs b/winit/src/application.rs index b9d5fed8..d5f957bf 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -28,7 +28,13 @@ pub trait Application: Sized { /// [`Application`]: trait.Application.html type Message: std::fmt::Debug + Send; - /// Initializes the [`Application`]. + /// The data needed to initialize your [`Application`]. + /// + /// [`Application`]: trait.Application.html + type Flags; + + /// Initializes the [`Application`] with the flags provided to + /// [`run`] as part of the [`Settings`]: /// /// Here is where you should return the initial state of your app. /// @@ -38,7 +44,9 @@ pub trait Application: Sized { /// request, etc. /// /// [`Application`]: trait.Application.html - fn new() -> (Self, Command); + /// [`run`]: #method.run.html + /// [`Settings`]: struct.Settings.html + fn new(flags: Self::Flags) -> (Self, Command); /// Returns the current title of the [`Application`]. /// @@ -90,7 +98,7 @@ pub trait Application: Sized { Mode::Windowed } - /// Runs the [`Application`]. + /// Runs the [`Application`] with the provided [`Settings`]. /// /// This method will take control of the current thread and __will NOT /// return__. @@ -98,8 +106,9 @@ pub trait Application: Sized { /// It should probably be that last thing you call in your `main` function. /// /// [`Application`]: trait.Application.html + /// [`Settings`]: struct.Settings.html fn run( - settings: Settings, + settings: Settings, backend_settings: ::Settings, ) where Self: 'static, @@ -123,7 +132,9 @@ pub trait Application: Sized { Runtime::new(executor, Proxy::new(event_loop.create_proxy())) }; - let (mut application, init_command) = runtime.enter(Self::new); + let flags = settings.flags; + let (mut application, init_command) = + runtime.enter(|| Self::new(flags)); runtime.spawn(init_command); let subscription = application.subscription(); diff --git a/winit/src/settings.rs b/winit/src/settings.rs new file mode 100644 index 00000000..d58c51f0 --- /dev/null +++ b/winit/src/settings.rs @@ -0,0 +1,50 @@ +//! Configure your application. +#[cfg(target_os = "windows")] +#[path = "settings/windows.rs"] +mod platform; +#[cfg(not(target_os = "windows"))] +#[path = "settings/not_windows.rs"] +mod platform; + +pub use platform::PlatformSpecific; + +/// The settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct Settings { + /// The [`Window`] settings + /// + /// [`Window`]: struct.Window.html + pub window: Window, + + /// The data needed to initialize an [`Application`]. + /// + /// [`Application`]: trait.Application.html + pub flags: Flags, +} + +/// The window settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Window { + /// The size of the window. + pub size: (u32, u32), + + /// Whether the window should be resizable or not. + pub resizable: bool, + + /// Whether the window should have a border, a title bar, etc. + pub decorations: bool, + + /// Platform specific settings. + pub platform_specific: platform::PlatformSpecific, +} + +impl Default for Window { + fn default() -> Window { + Window { + size: (1024, 768), + resizable: true, + decorations: true, + platform_specific: Default::default(), + } + } +} diff --git a/winit/src/settings/mod.rs b/winit/src/settings/mod.rs deleted file mode 100644 index b2290b46..00000000 --- a/winit/src/settings/mod.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! Configure your application. -#[cfg(target_os = "windows")] -#[path = "windows.rs"] -mod platform; -#[cfg(not(target_os = "windows"))] -#[path = "not_windows.rs"] -mod platform; - -pub use platform::PlatformSpecific; - -/// The settings of an application. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Settings { - /// The [`Window`] settings - /// - /// [`Window`]: struct.Window.html - pub window: Window, -} - -impl Default for Settings { - fn default() -> Settings { - Settings { - window: Window::default(), - } - } -} - -/// The window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Window { - /// The size of the window. - pub size: (u32, u32), - - /// Whether the window should be resizable or not. - pub resizable: bool, - - /// Whether the window should have a border, a title bar, etc. - pub decorations: bool, - - /// Platform specific settings. - pub platform_specific: platform::PlatformSpecific, -} - -impl Default for Window { - fn default() -> Window { - Window { - size: (1024, 768), - resizable: true, - decorations: true, - platform_specific: Default::default(), - } - } -} -- cgit