thief-book-manager/src/main.rs

79 lines
2.0 KiB
Rust

use std::{io, path::Path, sync::Arc};
use iced::{
executor,
widget::{column, text, text_editor},
Application, Command, Element, Settings, Theme,
};
#[derive(Debug, Clone)]
enum Message {
Edit(text_editor::Action),
FileOpened(Result<Arc<String>, io::ErrorKind>),
}
struct BookManagerApp {
book_content: text_editor::Content,
}
impl Application for BookManagerApp {
type Executor = executor::Default;
type Flags = ();
type Message = Message;
type Theme = Theme;
fn new(_flags: Self::Flags) -> (Self, Command<Self::Message>) {
(
Self {
book_content: text_editor::Content::new(),
},
Command::perform(
load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))),
Message::FileOpened,
),
)
}
fn title(&self) -> String {
String::from("Thief Book Manager")
}
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::Edit(action) => self.book_content.perform(action),
Message::FileOpened(result) => {
if let Ok(content) = result {
self.book_content = text_editor::Content::with_text(&content);
}
}
}
Command::none()
}
fn view(&self) -> Element<'_, Self::Message> {
let hello_world = text("Hello, world!");
let active_editor = text_editor(&self.book_content).on_action(Message::Edit);
column![hello_world, active_editor]
.spacing(10)
.padding(10)
.into()
}
fn theme(&self) -> Theme {
Theme::Dark
}
}
fn main() -> iced::Result {
BookManagerApp::run(Settings::default())
}
async fn load_file(path: impl AsRef<Path>) -> Result<Arc<String>, io::ErrorKind> {
tokio::fs::read_to_string(path)
.await
.map(Arc::new)
.map_err(|error| error.kind())
}