Use Program API in editor example

This commit is contained in:
Héctor Ramón Jiménez 2024-03-17 17:46:03 +01:00
parent 3fde2d041a
commit 80b544e548
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -1,14 +1,10 @@
use iced::executor;
use iced::highlighter::{self, Highlighter}; use iced::highlighter::{self, Highlighter};
use iced::keyboard; use iced::keyboard;
use iced::widget::{ use iced::widget::{
button, column, container, horizontal_space, pick_list, row, text, button, column, container, horizontal_space, pick_list, row, text,
text_editor, tooltip, text_editor, tooltip,
}; };
use iced::{ use iced::{Alignment, Command, Element, Font, Length, Subscription, Theme};
Alignment, Application, Command, Element, Font, Length, Settings,
Subscription, Theme,
};
use std::ffi; use std::ffi;
use std::io; use std::io;
@ -16,11 +12,13 @@ use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
Editor::run(Settings { iced::program("Editor - Iced", Editor::update, Editor::view)
fonts: vec![include_bytes!("../fonts/icons.ttf").as_slice().into()], .load(Editor::load)
default_font: Font::MONOSPACE, .subscription(Editor::subscription)
..Settings::default() .theme(Editor::theme)
}) .font(include_bytes!("../fonts/icons.ttf").as_slice())
.default_font(Font::MONOSPACE)
.run()
} }
struct Editor { struct Editor {
@ -42,27 +40,22 @@ enum Message {
FileSaved(Result<PathBuf, Error>), FileSaved(Result<PathBuf, Error>),
} }
impl Application for Editor { impl Editor {
type Message = Message; fn new() -> Self {
type Theme = Theme; Self {
type Executor = executor::Default; file: None,
type Flags = (); content: text_editor::Content::new(),
theme: highlighter::Theme::SolarizedDark,
fn new(_flags: Self::Flags) -> (Self, Command<Message>) { is_loading: true,
( is_dirty: false,
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),
)
} }
fn title(&self) -> String { fn load() -> Command<Message> {
String::from("Editor - Iced") Command::perform(
load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))),
Message::FileOpened,
)
} }
fn update(&mut self, message: Message) -> Command<Message> { 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)] #[derive(Debug, Clone)]
pub enum Error { pub enum Error {
DialogClosed, DialogClosed,
IoError(io::ErrorKind), 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> { async fn open_file() -> Result<(PathBuf, Arc<String>), Error> {
let picked_file = rfd::AsyncFileDialog::new() let picked_file = rfd::AsyncFileDialog::new()
.set_title("Open a text file...") .set_title("Open a text file...")
@ -238,10 +233,14 @@ async fn open_file() -> Result<(PathBuf, Arc<String>), Error> {
.await .await
.ok_or(Error::DialogClosed)?; .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) let contents = tokio::fs::read_to_string(&path)
.await .await
.map(Arc::new) .map(Arc::new)