From d70021fa68b556e20638f29e2e303f6d156afdb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 25 Nov 2019 14:17:13 +0100 Subject: [PATCH] Allow `Application` configuration with `Settings` --- examples/todos.rs | 4 ++-- examples/tour.rs | 4 ++-- src/application.rs | 10 +++++----- src/lib.rs | 3 +++ src/sandbox.rs | 10 +++++----- src/settings.rs | 43 ++++++++++++++++++++++++++++++++++++++++ winit/src/application.rs | 12 ++++++----- winit/src/lib.rs | 2 ++ winit/src/settings.rs | 29 +++++++++++++++++++++++++++ 9 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 src/settings.rs create mode 100644 winit/src/settings.rs diff --git a/examples/todos.rs b/examples/todos.rs index 77013dccde..af8a37a82c 100644 --- a/examples/todos.rs +++ b/examples/todos.rs @@ -1,12 +1,12 @@ use iced::{ button, scrollable, text_input, Align, Application, Background, Button, Checkbox, Color, Column, Command, Container, Element, Font, - HorizontalAlignment, Length, Row, Scrollable, Text, TextInput, + HorizontalAlignment, Length, Row, Scrollable, Settings, Text, TextInput, }; use serde::{Deserialize, Serialize}; pub fn main() { - Todos::run() + Todos::run(Settings::default()) } #[derive(Debug)] diff --git a/examples/tour.rs b/examples/tour.rs index f5d3f28d8a..0121c3bd63 100644 --- a/examples/tour.rs +++ b/examples/tour.rs @@ -1,13 +1,13 @@ use iced::{ button, scrollable, slider, text_input, Background, Button, Checkbox, Color, Column, Container, Element, HorizontalAlignment, Image, Length, - Radio, Row, Sandbox, Scrollable, Slider, Text, TextInput, + Radio, Row, Sandbox, Scrollable, Settings, Slider, Text, TextInput, }; pub fn main() { env_logger::init(); - Tour::run() + Tour::run(Settings::default()) } pub struct Tour { diff --git a/src/application.rs b/src/application.rs index f6d3fb9049..a4d20e68a7 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,4 +1,4 @@ -use crate::{Command, Element}; +use crate::{Command, Element, Settings}; /// An interactive cross-platform application. /// @@ -19,10 +19,10 @@ use crate::{Command, Element}; /// before](index.html#overview). We just need to fill in the gaps: /// /// ```no_run -/// use iced::{button, Application, Button, Column, Command, Element, Text}; +/// use iced::{button, Application, Button, Column, Command, Element, Settings, Text}; /// /// pub fn main() { -/// Counter::run() +/// Counter::run(Settings::default()) /// } /// /// #[derive(Default)] @@ -132,12 +132,12 @@ pub trait Application: Sized { /// It should probably be that last thing you call in your `main` function. /// /// [`Application`]: trait.Application.html - fn run() + fn run(settings: Settings) where Self: 'static, { #[cfg(not(target_arch = "wasm32"))] - as iced_winit::Application>::run(); + as iced_winit::Application>::run(settings.into()); #[cfg(target_arch = "wasm32")] as iced_web::Application>::run(); diff --git a/src/lib.rs b/src/lib.rs index 1d79269b5a..cb491821af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,6 +185,9 @@ mod application; mod platform; mod sandbox; +pub mod settings; + pub use application::Application; pub use platform::*; pub use sandbox::Sandbox; +pub use settings::Settings; diff --git a/src/sandbox.rs b/src/sandbox.rs index 60e3be1499..acf7f5e051 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -1,4 +1,4 @@ -use crate::{Application, Command, Element}; +use crate::{Application, Command, Element, Settings}; /// A sandboxed [`Application`]. /// @@ -19,10 +19,10 @@ use crate::{Application, Command, Element}; /// to remove the use of [`Command`]: /// /// ```no_run -/// use iced::{button, Button, Column, Element, Sandbox, Text}; +/// use iced::{button, Button, Column, Element, Sandbox, Settings, Text}; /// /// pub fn main() { -/// Counter::run() +/// Counter::run(Settings::default()) /// } /// /// #[derive(Default)] @@ -121,11 +121,11 @@ pub trait Sandbox { /// It should probably be that last thing you call in your `main` function. /// /// [`Sandbox`]: trait.Sandbox.html - fn run() + fn run(settings: Settings) where Self: 'static + Sized, { - ::run() + ::run(settings) } } diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 0000000000..2556c51ba9 --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,43 @@ +//! Configure your application. + +/// The settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Settings { + /// The [`Window`] settings. + /// + /// They will be ignored on the Web. + /// + /// [`Window`]: struct.Window.html + pub window: Window, +} + +/// 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, +} + +impl Default for Window { + fn default() -> Window { + Window { + size: (1024, 768), + resizable: true, + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl From for iced_winit::Settings { + fn from(settings: Settings) -> iced_winit::Settings { + iced_winit::Settings { + window: iced_winit::settings::Window { + size: settings.window.size, + resizable: settings.window.resizable, + }, + } + } +} diff --git a/winit/src/application.rs b/winit/src/application.rs index ec1444f6e4..1042b4125c 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -3,7 +3,7 @@ use crate::{ input::{keyboard, mouse}, renderer::{Target, Windowed}, Cache, Command, Container, Debug, Element, Event, Length, MouseCursor, - UserInterface, + Settings, UserInterface, }; /// An interactive, native cross-platform application. @@ -72,7 +72,7 @@ pub trait Application: Sized { /// It should probably be that last thing you call in your `main` function. /// /// [`Application`]: trait.Application.html - fn run() + fn run(settings: Settings) where Self: 'static, { @@ -96,13 +96,15 @@ pub trait Application: Sized { let mut title = application.title(); - // TODO: Ask for window settings and configure this properly + let (width, height) = settings.window.size; + let window = WindowBuilder::new() .with_title(&title) .with_inner_size(winit::dpi::LogicalSize { - width: 1280.0, - height: 1024.0, + width: f64::from(width), + height: f64::from(height), }) + .with_resizable(settings.window.resizable) .build(&event_loop) .expect("Open window"); diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 0a2bf7dd92..00d200f94a 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -26,10 +26,12 @@ pub use iced_native::*; pub use winit; pub mod conversion; +pub mod settings; mod application; pub use application::Application; +pub use settings::Settings; // We disable debug capabilities on release builds unless the `debug` feature // is explicitly enabled. diff --git a/winit/src/settings.rs b/winit/src/settings.rs new file mode 100644 index 0000000000..d257ecd87d --- /dev/null +++ b/winit/src/settings.rs @@ -0,0 +1,29 @@ +//! Configure your application. + +/// The settings of an application. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Settings { + /// The [`Window`] settings + /// + /// [`Window`]: struct.Window.html + pub window: Window, +} + +/// 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, +} + +impl Default for Window { + fn default() -> Window { + Window { + size: (1024, 768), + resizable: true, + } + } +}