Use Program API in todos example
This commit is contained in:
parent
80b544e548
commit
784fa80c0d
3 changed files with 46 additions and 42 deletions
|
|
@ -53,20 +53,20 @@ impl Size {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<[f32; 2]> for Size {
|
impl<T> From<[T; 2]> for Size<T> {
|
||||||
fn from([width, height]: [f32; 2]) -> Self {
|
fn from([width, height]: [T; 2]) -> Self {
|
||||||
Size { width, height }
|
Size { width, height }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<[u16; 2]> for Size {
|
impl<T> From<(T, T)> for Size<T> {
|
||||||
fn from([width, height]: [u16; 2]) -> Self {
|
fn from((width, height): (T, T)) -> Self {
|
||||||
Size::new(width.into(), height.into())
|
Self { width, height }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vector<f32>> for Size {
|
impl<T> From<Vector<T>> for Size<T> {
|
||||||
fn from(vector: Vector<f32>) -> Self {
|
fn from(vector: Vector<T>) -> Self {
|
||||||
Size {
|
Size {
|
||||||
width: vector.x,
|
width: vector.x,
|
||||||
height: vector.y,
|
height: vector.y,
|
||||||
|
|
@ -74,20 +74,23 @@ impl From<Vector<f32>> for Size {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Size> for [f32; 2] {
|
impl<T> From<Size<T>> for [T; 2] {
|
||||||
fn from(size: Size) -> [f32; 2] {
|
fn from(size: Size<T>) -> Self {
|
||||||
[size.width, size.height]
|
[size.width, size.height]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Size> for Vector<f32> {
|
impl<T> From<Size<T>> for Vector<T> {
|
||||||
fn from(size: Size) -> Self {
|
fn from(size: Size<T>) -> Self {
|
||||||
Vector::new(size.width, size.height)
|
Vector::new(size.width, size.height)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Sub for Size {
|
impl<T> std::ops::Sub for Size<T>
|
||||||
type Output = Size;
|
where
|
||||||
|
T: std::ops::Sub<Output = T>,
|
||||||
|
{
|
||||||
|
type Output = Size<T>;
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
Size {
|
Size {
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,11 @@
|
||||||
use iced::alignment::{self, Alignment};
|
use iced::alignment::{self, Alignment};
|
||||||
use iced::font::{self, Font};
|
|
||||||
use iced::keyboard;
|
use iced::keyboard;
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
self, button, checkbox, column, container, keyed_column, row, scrollable,
|
self, button, checkbox, column, container, keyed_column, row, scrollable,
|
||||||
text, text_input, Text,
|
text, text_input, Text,
|
||||||
};
|
};
|
||||||
use iced::window;
|
use iced::window;
|
||||||
use iced::{
|
use iced::{Command, Element, Font, Length, Subscription};
|
||||||
Application, Command, Element, Length, Settings, Size, Subscription, Theme,
|
|
||||||
};
|
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -20,17 +17,17 @@ pub fn main() -> iced::Result {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
Todos::run(Settings {
|
iced::program(Todos::title, Todos::update, Todos::view)
|
||||||
window: window::Settings {
|
.load(Todos::load)
|
||||||
size: Size::new(500.0, 800.0),
|
.subscription(Todos::subscription)
|
||||||
..window::Settings::default()
|
.font(include_bytes!("../fonts/icons.ttf").as_slice())
|
||||||
},
|
.window_size((500.0, 800.0))
|
||||||
..Settings::default()
|
.run()
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Default, Debug)]
|
||||||
enum Todos {
|
enum Todos {
|
||||||
|
#[default]
|
||||||
Loading,
|
Loading,
|
||||||
Loaded(State),
|
Loaded(State),
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +44,6 @@ struct State {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
Loaded(Result<SavedState, LoadError>),
|
Loaded(Result<SavedState, LoadError>),
|
||||||
FontLoaded(Result<(), font::Error>),
|
|
||||||
Saved(Result<(), SaveError>),
|
Saved(Result<(), SaveError>),
|
||||||
InputChanged(String),
|
InputChanged(String),
|
||||||
CreateTask,
|
CreateTask,
|
||||||
|
|
@ -57,21 +53,12 @@ enum Message {
|
||||||
ToggleFullscreen(window::Mode),
|
ToggleFullscreen(window::Mode),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for Todos {
|
impl Todos {
|
||||||
type Message = Message;
|
fn load() -> Command<Message> {
|
||||||
type Theme = Theme;
|
Command::batch(vec![Command::perform(
|
||||||
type Executor = iced::executor::Default;
|
SavedState::load(),
|
||||||
type Flags = ();
|
Message::Loaded,
|
||||||
|
)])
|
||||||
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),
|
|
||||||
]),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@
|
||||||
use crate::application::{self, Application};
|
use crate::application::{self, Application};
|
||||||
use crate::executor::{self, Executor};
|
use crate::executor::{self, Executor};
|
||||||
use crate::window;
|
use crate::window;
|
||||||
use crate::{Command, Element, Font, Result, Settings, Subscription};
|
use crate::{Command, Element, Font, Result, Settings, Size, Subscription};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
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`].
|
/// Sets the [`window::Settings::transparent`] of the [`Program`].
|
||||||
pub fn transparent(self, transparent: bool) -> Self {
|
pub fn transparent(self, transparent: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue