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 crate::{Message, state::State};
|
||||||
use iced::Task;
|
use iced::Task;
|
||||||
use iced::widget::text_editor;
|
use iced::widget::text_editor;
|
||||||
use rfd::FileDialog;
|
|
||||||
|
|
||||||
pub fn edit(state: &mut State, action: text_editor::Action) -> Task<Message> {
|
pub fn edit(state: &mut State, action: text_editor::Action) -> Task<Message> {
|
||||||
state.apply_edit(action);
|
state.apply_edit(action);
|
||||||
@@ -22,47 +21,6 @@ pub fn link_clicked(url: String) -> Task<Message> {
|
|||||||
Task::none()
|
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> {
|
pub fn file_action(state: &mut State, action: FileAction) -> Task<Message> {
|
||||||
match action {
|
match action {
|
||||||
FileAction::New => {
|
FileAction::New => {
|
||||||
@@ -76,36 +34,41 @@ pub fn file_action(state: &mut State, action: FileAction) -> Task<Message> {
|
|||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FileAction::Open => {
|
FileAction::Open => Task::perform(io::open_file(), Message::FileOpened),
|
||||||
let (path, content) = open_file();
|
|
||||||
|
|
||||||
if let Some(p) = path {
|
|
||||||
state.open_file(p, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
Task::none()
|
|
||||||
}
|
|
||||||
FileAction::Save => {
|
FileAction::Save => {
|
||||||
let (current_path, content) = state.active_file_data();
|
let (current_path, content) = state.active_file_data();
|
||||||
|
let path = current_path.cloned();
|
||||||
if let Some(path) = save_file(current_path, content) {
|
Task::perform(io::save_file(path, content), Message::FileSaved)
|
||||||
state.set_active_file_path(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
Task::none()
|
|
||||||
}
|
}
|
||||||
FileAction::SaveAs => {
|
FileAction::SaveAs => {
|
||||||
let (_, content) = state.active_file_data();
|
let (_, content) = state.active_file_data();
|
||||||
|
Task::perform(io::save_file(None, content), Message::FileSaved)
|
||||||
if let Some(path) = save_file_as(content) {
|
|
||||||
state.set_active_file_path(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
Task::none()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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> {
|
pub fn view_action(state: &mut State, action: ViewAction) -> Task<Message> {
|
||||||
match action {
|
match action {
|
||||||
ViewAction::Increase => state.increase_font(),
|
ViewAction::Increase => state.increase_font(),
|
||||||
|
|||||||
+4
-5
@@ -1,11 +1,11 @@
|
|||||||
mod components;
|
mod components;
|
||||||
mod constants;
|
mod constants;
|
||||||
mod file;
|
mod file;
|
||||||
|
mod handler;
|
||||||
mod io;
|
mod io;
|
||||||
mod key_bindings;
|
mod key_bindings;
|
||||||
mod state;
|
|
||||||
mod handler;
|
|
||||||
mod message;
|
mod message;
|
||||||
|
mod state;
|
||||||
|
|
||||||
use iced::Theme;
|
use iced::Theme;
|
||||||
use iced::widget::column;
|
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::LinkClicked(url) => handler::link_clicked(url),
|
||||||
Message::FileActionSelected(action) => handler::file_action(state, action),
|
Message::FileActionSelected(action) => handler::file_action(state, action),
|
||||||
Message::ViewActionSelected(action) => handler::view_action(state, action),
|
Message::ViewActionSelected(action) => handler::view_action(state, action),
|
||||||
Message::FileOpened(_) => todo!(),
|
Message::FileOpened(result) => handler::opened_file(state, result),
|
||||||
Message::FileSaved(path_buf) => todo!(),
|
Message::FileSaved(result) => handler::saved_file(state, result),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,4 +83,3 @@ fn subscription(_state: &State) -> Subscription<Message> {
|
|||||||
fn theme(_state: &State) -> Theme {
|
fn theme(_state: &State) -> Theme {
|
||||||
Theme::Dark
|
Theme::Dark
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user