diff options
author | 2024-03-17 17:57:24 +0100 | |
---|---|---|
committer | 2024-03-17 17:57:24 +0100 | |
commit | 784fa80c0d92a7d1fda8a7ff77185d50423e228a (patch) | |
tree | f965e9ffa7a57311cb8fa00113490cdb249b3a25 | |
parent | 80b544e5486d058c439b7d499b18fef2a75317b2 (diff) | |
download | iced-784fa80c0d92a7d1fda8a7ff77185d50423e228a.tar.gz iced-784fa80c0d92a7d1fda8a7ff77185d50423e228a.tar.bz2 iced-784fa80c0d92a7d1fda8a7ff77185d50423e228a.zip |
Use `Program` API in `todos` example
-rw-r--r-- | core/src/size.rs | 29 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 43 | ||||
-rw-r--r-- | src/application/program.rs | 16 |
3 files changed, 46 insertions, 42 deletions
diff --git a/core/src/size.rs b/core/src/size.rs index 90e50d13..267fc90e 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -53,20 +53,20 @@ impl Size { } } -impl From<[f32; 2]> for Size { - fn from([width, height]: [f32; 2]) -> Self { +impl<T> From<[T; 2]> for Size<T> { + fn from([width, height]: [T; 2]) -> Self { Size { width, height } } } -impl From<[u16; 2]> for Size { - fn from([width, height]: [u16; 2]) -> Self { - Size::new(width.into(), height.into()) +impl<T> From<(T, T)> for Size<T> { + fn from((width, height): (T, T)) -> Self { + Self { width, height } } } -impl From<Vector<f32>> for Size { - fn from(vector: Vector<f32>) -> Self { +impl<T> From<Vector<T>> for Size<T> { + fn from(vector: Vector<T>) -> Self { Size { width: vector.x, height: vector.y, @@ -74,20 +74,23 @@ impl From<Vector<f32>> for Size { } } -impl From<Size> for [f32; 2] { - fn from(size: Size) -> [f32; 2] { +impl<T> From<Size<T>> for [T; 2] { + fn from(size: Size<T>) -> Self { [size.width, size.height] } } -impl From<Size> for Vector<f32> { - fn from(size: Size) -> Self { +impl<T> From<Size<T>> for Vector<T> { + fn from(size: Size<T>) -> Self { Vector::new(size.width, size.height) } } -impl std::ops::Sub for Size { - type Output = Size; +impl<T> std::ops::Sub for Size<T> +where + T: std::ops::Sub<Output = T>, +{ + type Output = Size<T>; fn sub(self, rhs: Self) -> Self::Output { Size { diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index aaa86ef8..cfd9dcb7 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,14 +1,11 @@ use iced::alignment::{self, Alignment}; -use iced::font::{self, Font}; use iced::keyboard; use iced::widget::{ self, button, checkbox, column, container, keyed_column, row, scrollable, text, text_input, Text, }; use iced::window; -use iced::{ - Application, Command, Element, Length, Settings, Size, Subscription, Theme, -}; +use iced::{Command, Element, Font, Length, Subscription}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; @@ -20,17 +17,17 @@ pub fn main() -> iced::Result { #[cfg(not(target_arch = "wasm32"))] tracing_subscriber::fmt::init(); - Todos::run(Settings { - window: window::Settings { - size: Size::new(500.0, 800.0), - ..window::Settings::default() - }, - ..Settings::default() - }) + iced::program(Todos::title, Todos::update, Todos::view) + .load(Todos::load) + .subscription(Todos::subscription) + .font(include_bytes!("../fonts/icons.ttf").as_slice()) + .window_size((500.0, 800.0)) + .run() } -#[derive(Debug)] +#[derive(Default, Debug)] enum Todos { + #[default] Loading, Loaded(State), } @@ -47,7 +44,6 @@ struct State { #[derive(Debug, Clone)] enum Message { Loaded(Result<SavedState, LoadError>), - FontLoaded(Result<(), font::Error>), Saved(Result<(), SaveError>), InputChanged(String), CreateTask, @@ -57,21 +53,12 @@ enum Message { ToggleFullscreen(window::Mode), } -impl Application for Todos { - type Message = Message; - type Theme = Theme; - type Executor = iced::executor::Default; - type Flags = (); - - fn new(_flags: ()) -> (Todos, Command<Message>) { - ( - Todos::Loading, - Command::batch(vec![ - font::load(include_bytes!("../fonts/icons.ttf").as_slice()) - .map(Message::FontLoaded), - Command::perform(SavedState::load(), Message::Loaded), - ]), - ) +impl Todos { + fn load() -> Command<Message> { + Command::batch(vec![Command::perform( + SavedState::load(), + Message::Loaded, + )]) } fn title(&self) -> String { diff --git a/src/application/program.rs b/src/application/program.rs index d70f39cf..2e77678e 100644 --- a/src/application/program.rs +++ b/src/application/program.rs @@ -32,7 +32,7 @@ use crate::application::{self, Application}; use crate::executor::{self, Executor}; use crate::window; -use crate::{Command, Element, Font, Result, Settings, Subscription}; +use crate::{Command, Element, Font, Result, Settings, Size, Subscription}; use std::borrow::Cow; @@ -277,6 +277,20 @@ impl<P: Definition> Program<P> { } } + /// Sets the [`window::Settings::size`] of the [`Program`]. + pub fn window_size(self, size: impl Into<Size>) -> Self { + Self { + settings: Settings { + window: window::Settings { + size: size.into(), + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + /// Sets the [`window::Settings::transparent`] of the [`Program`]. pub fn transparent(self, transparent: bool) -> Self { Self { |