From 80b544e5486d058c439b7d499b18fef2a75317b2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 17:46:03 +0100 Subject: Use `Program` API in `editor` example --- examples/editor/src/main.rs | 69 ++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'examples/editor') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 4c97acb1..ed16018a 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,14 +1,10 @@ -use iced::executor; use iced::highlighter::{self, Highlighter}; use iced::keyboard; use iced::widget::{ button, column, container, horizontal_space, pick_list, row, text, text_editor, tooltip, }; -use iced::{ - Alignment, Application, Command, Element, Font, Length, Settings, - Subscription, Theme, -}; +use iced::{Alignment, Command, Element, Font, Length, Subscription, Theme}; use std::ffi; use std::io; @@ -16,11 +12,13 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; pub fn main() -> iced::Result { - Editor::run(Settings { - fonts: vec![include_bytes!("../fonts/icons.ttf").as_slice().into()], - default_font: Font::MONOSPACE, - ..Settings::default() - }) + iced::program("Editor - Iced", Editor::update, Editor::view) + .load(Editor::load) + .subscription(Editor::subscription) + .theme(Editor::theme) + .font(include_bytes!("../fonts/icons.ttf").as_slice()) + .default_font(Font::MONOSPACE) + .run() } struct Editor { @@ -42,27 +40,22 @@ enum Message { FileSaved(Result), } -impl Application for Editor { - type Message = Message; - type Theme = Theme; - type Executor = executor::Default; - type Flags = (); - - fn new(_flags: Self::Flags) -> (Self, Command) { - ( - Self { - file: None, - content: text_editor::Content::new(), - theme: highlighter::Theme::SolarizedDark, - is_loading: true, - is_dirty: false, - }, - Command::perform(load_file(default_file()), Message::FileOpened), - ) +impl Editor { + fn new() -> Self { + Self { + file: None, + content: text_editor::Content::new(), + theme: highlighter::Theme::SolarizedDark, + is_loading: true, + is_dirty: false, + } } - fn title(&self) -> String { - String::from("Editor - Iced") + fn load() -> Command { + Command::perform( + load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))), + Message::FileOpened, + ) } fn update(&mut self, message: Message) -> Command { @@ -221,16 +214,18 @@ impl Application for Editor { } } +impl Default for Editor { + fn default() -> Self { + Self::new() + } +} + #[derive(Debug, Clone)] pub enum Error { DialogClosed, IoError(io::ErrorKind), } -fn default_file() -> PathBuf { - PathBuf::from(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))) -} - async fn open_file() -> Result<(PathBuf, Arc), Error> { let picked_file = rfd::AsyncFileDialog::new() .set_title("Open a text file...") @@ -238,10 +233,14 @@ async fn open_file() -> Result<(PathBuf, Arc), Error> { .await .ok_or(Error::DialogClosed)?; - load_file(picked_file.path().to_owned()).await + load_file(picked_file).await } -async fn load_file(path: PathBuf) -> Result<(PathBuf, Arc), Error> { +async fn load_file( + path: impl Into, +) -> Result<(PathBuf, Arc), Error> { + let path = path.into(); + let contents = tokio::fs::read_to_string(&path) .await .map(Arc::new) -- cgit