diff options
author | 2024-03-16 05:33:47 +0100 | |
---|---|---|
committer | 2024-03-16 05:33:47 +0100 | |
commit | c22269bff3085012d326a0df77bf27ad5bcb41b7 (patch) | |
tree | 1083f21d012ab2bac88fb51537d4dc431bc7f170 /examples/multitouch | |
parent | 0524e9b4571d264018656418f02a1f9e27e268d7 (diff) | |
download | iced-c22269bff3085012d326a0df77bf27ad5bcb41b7.tar.gz iced-c22269bff3085012d326a0df77bf27ad5bcb41b7.tar.bz2 iced-c22269bff3085012d326a0df77bf27ad5bcb41b7.zip |
Introduce `Program` API
Diffstat (limited to 'examples/multitouch')
-rw-r--r-- | examples/multitouch/src/main.rs | 74 |
1 files changed, 16 insertions, 58 deletions
diff --git a/examples/multitouch/src/main.rs b/examples/multitouch/src/main.rs index 956ad471..f4506f51 100644 --- a/examples/multitouch/src/main.rs +++ b/examples/multitouch/src/main.rs @@ -2,101 +2,59 @@ //! a circle around each fingertip. This only works on touch-enabled //! computers like Microsoft Surface. use iced::mouse; +use iced::touch; use iced::widget::canvas::event; use iced::widget::canvas::stroke::{self, Stroke}; use iced::widget::canvas::{self, Canvas, Geometry}; -use iced::{ - executor, touch, window, Application, Color, Command, Element, Length, - Point, Rectangle, Renderer, Settings, Subscription, Theme, -}; +use iced::{Color, Element, Length, Point, Rectangle, Renderer, Theme}; use std::collections::HashMap; pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); - Multitouch::run(Settings { - antialiasing: true, - window: window::Settings { - position: window::Position::Centered, - ..window::Settings::default() - }, - ..Settings::default() - }) + iced::sandbox(Multitouch::update, Multitouch::view) + .title("Multitouch - Iced") + .antialiased() + .centered() + .run() } +#[derive(Default)] struct Multitouch { - state: State, -} - -#[derive(Debug)] -struct State { cache: canvas::Cache, fingers: HashMap<touch::Finger, Point>, } -impl State { - fn new() -> Self { - Self { - cache: canvas::Cache::new(), - fingers: HashMap::new(), - } - } -} - #[derive(Debug)] enum Message { FingerPressed { id: touch::Finger, position: Point }, FingerLifted { id: touch::Finger }, } -impl Application for Multitouch { - type Executor = executor::Default; - type Message = Message; - type Theme = Theme; - type Flags = (); - - fn new(_flags: ()) -> (Self, Command<Message>) { - ( - Multitouch { - state: State::new(), - }, - Command::none(), - ) - } - - fn title(&self) -> String { - String::from("Multitouch - Iced") - } - - fn update(&mut self, message: Message) -> Command<Message> { +impl Multitouch { + fn update(&mut self, message: Message) { match message { Message::FingerPressed { id, position } => { - self.state.fingers.insert(id, position); - self.state.cache.clear(); + self.fingers.insert(id, position); + self.cache.clear(); } Message::FingerLifted { id } => { - self.state.fingers.remove(&id); - self.state.cache.clear(); + self.fingers.remove(&id); + self.cache.clear(); } } - - Command::none() - } - - fn subscription(&self) -> Subscription<Message> { - Subscription::none() } fn view(&self) -> Element<Message> { - Canvas::new(&self.state) + Canvas::new(self) .width(Length::Fill) .height(Length::Fill) .into() } } -impl canvas::Program<Message> for State { +impl canvas::Program<Message> for Multitouch { type State = (); fn update( |