From 88006d08a29ca56d6309a4e59ccc1758bc64e9e8 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sat, 24 Jan 2026 20:40:26 -0600 Subject: [PATCH] feat: update handler logic to use async io methods and produce completion messages and implemend handlers to deal with completions --- src/handler.rs | 89 +++++++++++++++----------------------------------- src/main.rs | 9 +++-- 2 files changed, 30 insertions(+), 68 deletions(-) diff --git a/src/handler.rs b/src/handler.rs index dfa3229..110006d 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -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 { state.apply_edit(action); @@ -22,47 +21,6 @@ pub fn link_clicked(url: String) -> Task { Task::none() } -fn save_file(path: Option<&PathBuf>, text: String) -> Option { - 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 { - 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, 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 { match action { FileAction::New => { @@ -76,36 +34,41 @@ pub fn file_action(state: &mut State, action: FileAction) -> Task { 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 { + match result { + Ok((path, content)) => { + state.open_file(path, content); + } + Err(_error) => {} + }; + + Task::none() +} + +pub fn saved_file(state: &mut State, result: Result) -> Task { + match result { + Ok(path) => { + state.set_active_file_path(path); + } + Err(_error) => {} + }; + + Task::none() +} + pub fn view_action(state: &mut State, action: ViewAction) -> Task { match action { ViewAction::Increase => state.increase_font(), diff --git a/src/main.rs b/src/main.rs index 59833b1..ed0867e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::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 { fn theme(_state: &State) -> Theme { Theme::Dark } -