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::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)