diff options
author | 2024-03-17 17:46:03 +0100 | |
---|---|---|
committer | 2024-03-17 17:46:03 +0100 | |
commit | 80b544e5486d058c439b7d499b18fef2a75317b2 (patch) | |
tree | 8a8516c97a5f04648e417bf22341236176e5dfbe /examples/editor | |
parent | 3fde2d041ac38504ee7a8c72abdb5d8691984a7e (diff) | |
download | iced-80b544e5486d058c439b7d499b18fef2a75317b2.tar.gz iced-80b544e5486d058c439b7d499b18fef2a75317b2.tar.bz2 iced-80b544e5486d058c439b7d499b18fef2a75317b2.zip |
Use `Program` API in `editor` example
Diffstat (limited to 'examples/editor')
-rw-r--r-- | examples/editor/src/main.rs | 69 |
1 files changed, 34 insertions, 35 deletions
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<PathBuf, Error>), } -impl Application for Editor { - type Message = Message; - type Theme = Theme; - type Executor = executor::Default; - type Flags = (); - - fn new(_flags: Self::Flags) -> (Self, Command<Message>) { - ( - 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<Message> { + Command::perform( + load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))), + Message::FileOpened, + ) } fn update(&mut self, message: Message) -> Command<Message> { @@ -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<String>), Error> { let picked_file = rfd::AsyncFileDialog::new() .set_title("Open a text file...") @@ -238,10 +233,14 @@ async fn open_file() -> Result<(PathBuf, Arc<String>), 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<String>), Error> { +async fn load_file( + path: impl Into<PathBuf>, +) -> Result<(PathBuf, Arc<String>), Error> { + let path = path.into(); + let contents = tokio::fs::read_to_string(&path) .await .map(Arc::new) |