feat: update handler logic to use async io methods and produce completion messages and implemend handlers to deal with completions
This commit is contained in:
+26
-63
@@ -5,7 +5,6 @@ use crate::message::{FileAction, ViewAction};
|
||||
use crate::{Message, state::State};
|
||||
use iced::Task;
|
||||
use iced::widget::text_editor;
|
||||
use rfd::FileDialog;
|
||||
|
||||
pub fn edit(state: &mut State, action: text_editor::Action) -> Task<Message> {
|
||||
state.apply_edit(action);
|
||||
@@ -22,47 +21,6 @@ pub fn link_clicked(url: String) -> Task<Message> {
|
||||
Task::none()
|
||||
}
|
||||
|
||||
fn save_file(path: Option<&PathBuf>, text: String) -> Option<PathBuf> {
|
||||
let mut save_path = path.cloned();
|
||||
|
||||
if path.is_none() {
|
||||
save_path = FileDialog::new().set_directory("/").save_file();
|
||||
}
|
||||
|
||||
match save_path {
|
||||
Some(p) => {
|
||||
let sp = p.clone();
|
||||
io::save_file_to_disk(sp, text);
|
||||
Some(p)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn save_file_as(text: String) -> Option<PathBuf> {
|
||||
let files = FileDialog::new().set_directory("/").save_file();
|
||||
|
||||
match files {
|
||||
Some(path) => {
|
||||
io::save_file_to_disk(path.clone(), text);
|
||||
Some(path)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn open_file() -> (Option<PathBuf>, String) {
|
||||
let file = FileDialog::new().set_directory("/").pick_file();
|
||||
|
||||
match file {
|
||||
Some(path) => {
|
||||
let content = io::load_file_from_disk(path.clone());
|
||||
(Some(path), content)
|
||||
}
|
||||
None => (None, String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_action(state: &mut State, action: FileAction) -> Task<Message> {
|
||||
match action {
|
||||
FileAction::New => {
|
||||
@@ -76,36 +34,41 @@ pub fn file_action(state: &mut State, action: FileAction) -> Task<Message> {
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
FileAction::Open => {
|
||||
let (path, content) = open_file();
|
||||
|
||||
if let Some(p) = path {
|
||||
state.open_file(p, content);
|
||||
}
|
||||
|
||||
Task::none()
|
||||
}
|
||||
FileAction::Open => Task::perform(io::open_file(), Message::FileOpened),
|
||||
FileAction::Save => {
|
||||
let (current_path, content) = state.active_file_data();
|
||||
|
||||
if let Some(path) = save_file(current_path, content) {
|
||||
state.set_active_file_path(path);
|
||||
}
|
||||
|
||||
Task::none()
|
||||
let path = current_path.cloned();
|
||||
Task::perform(io::save_file(path, content), Message::FileSaved)
|
||||
}
|
||||
FileAction::SaveAs => {
|
||||
let (_, content) = state.active_file_data();
|
||||
|
||||
if let Some(path) = save_file_as(content) {
|
||||
state.set_active_file_path(path);
|
||||
}
|
||||
|
||||
Task::none()
|
||||
Task::perform(io::save_file(None, content), Message::FileSaved)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn opened_file(state: &mut State, result: Result<(PathBuf, String), String>) -> Task<Message> {
|
||||
match result {
|
||||
Ok((path, content)) => {
|
||||
state.open_file(path, content);
|
||||
}
|
||||
Err(_error) => {}
|
||||
};
|
||||
|
||||
Task::none()
|
||||
}
|
||||
|
||||
pub fn saved_file(state: &mut State, result: Result<PathBuf, String>) -> Task<Message> {
|
||||
match result {
|
||||
Ok(path) => {
|
||||
state.set_active_file_path(path);
|
||||
}
|
||||
Err(_error) => {}
|
||||
};
|
||||
|
||||
Task::none()
|
||||
}
|
||||
|
||||
pub fn view_action(state: &mut State, action: ViewAction) -> Task<Message> {
|
||||
match action {
|
||||
ViewAction::Increase => state.increase_font(),
|
||||
|
||||
+4
-5
@@ -1,11 +1,11 @@
|
||||
mod components;
|
||||
mod constants;
|
||||
mod file;
|
||||
mod handler;
|
||||
mod io;
|
||||
mod key_bindings;
|
||||
mod state;
|
||||
mod handler;
|
||||
mod message;
|
||||
mod state;
|
||||
|
||||
use iced::Theme;
|
||||
use iced::widget::column;
|
||||
@@ -41,8 +41,8 @@ fn update(state: &mut State, message: Message) -> Task<Message> {
|
||||
Message::LinkClicked(url) => handler::link_clicked(url),
|
||||
Message::FileActionSelected(action) => handler::file_action(state, action),
|
||||
Message::ViewActionSelected(action) => handler::view_action(state, action),
|
||||
Message::FileOpened(_) => todo!(),
|
||||
Message::FileSaved(path_buf) => todo!(),
|
||||
Message::FileOpened(result) => handler::opened_file(state, result),
|
||||
Message::FileSaved(result) => handler::saved_file(state, result),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,4 +83,3 @@ fn subscription(_state: &State) -> Subscription<Message> {
|
||||
fn theme(_state: &State) -> Theme {
|
||||
Theme::Dark
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user