1
0
mirror of https://github.com/chylex/Bark-Browser.git synced 2025-06-01 02:34:09 +02:00

Refactor action to edit file or directory & fix refreshing when editing root folder

This commit is contained in:
chylex 2023-07-30 09:55:01 +02:00
parent 97b2dac354
commit 88c9d0cf59
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 13 additions and 15 deletions
src/component/filesystem/action

View File

@ -7,29 +7,27 @@ use std::process::Command;
use slab_tree::NodeRef;
use crate::component::dialog::message::MessageDialogLayer;
use crate::component::filesystem::action::file::{FileNode, get_selected_file};
use crate::component::filesystem::event::FsLayerEvent;
use crate::component::filesystem::FsLayer;
use crate::component::filesystem::tree::FsTreeViewNode;
use crate::file::FileEntry;
use crate::state::action::{Action, ActionResult};
use crate::state::Environment;
use crate::util::slab_tree::NodeRefExtensions;
pub struct EditSelected;
pub struct EditSelectedFileOrDirectory;
impl Action<FsLayer> for EditSelected {
impl Action<FsLayer> for EditSelectedFileOrDirectory {
fn perform(&self, layer: &mut FsLayer, _environment: &Environment) -> ActionResult {
if let Some(node) = layer.selected_node() {
if let Some(entry_path) = layer.tree.get_entry(&node).and_then(FileEntry::path) {
return edit_impl(layer, &node, entry_path);
}
if let Some(FileNode { node, path, .. }) = get_selected_file(layer) {
open_default_editor(layer, &node, path)
} else {
ActionResult::Nothing
}
ActionResult::Nothing
}
}
fn edit_impl(layer: &FsLayer, node: &NodeRef<FsTreeViewNode>, path: &Path) -> ActionResult {
fn open_default_editor(layer: &FsLayer, node: &NodeRef<FsTreeViewNode>, path: &Path) -> ActionResult {
let editor = get_editor();
let status = Command::new(&editor)
.arg(path)
@ -39,9 +37,9 @@ fn edit_impl(layer: &FsLayer, node: &NodeRef<FsTreeViewNode>, path: &Path) -> Ac
return ActionResult::PushLayer(Box::new(MessageDialogLayer::generic_error(layer.dialog_y(), format!("Default editor '{}' not found.", editor.to_string_lossy()))));
}
if let Some(parent_node_id) = node.parent_id() {
FsLayerEvent::RefreshViewNodeChildren(parent_node_id).enqueue(&layer.pending_events);
}
// Refresh the parent directory, or the root node if this is the view root.
let node_id_to_refresh = node.parent_id().unwrap_or_else(|| node.node_id());
FsLayerEvent::RefreshViewNodeChildren(node_id_to_refresh).enqueue(&layer.pending_events);
ActionResult::Redraw
}

View File

@ -2,7 +2,7 @@ use lazy_static::lazy_static;
use crate::component::filesystem::action::application::{Quit, RedrawScreen};
use crate::component::filesystem::action::count::PushCountDigit;
use crate::component::filesystem::action::file::{CreateDirectory, CreateFile, DeleteSelected, EditSelected, RenameSelectedFileOrDirectory};
use crate::component::filesystem::action::file::{CreateDirectory, CreateFile, DeleteSelected, EditSelectedFileOrDirectory, RenameSelectedFileOrDirectory};
use crate::component::filesystem::action::movement::{CollapseSelectedOr, ExpandSelectedOr, MoveBetweenFirstAndLastSibling, MoveDown, MovementWithCountFactory, MovementWithFallbackFactory, MoveOrTraverseUpParent, MoveToFirst, MoveToLast, MoveToLineOr, MoveToNextSibling, MoveToParent, MoveToPreviousSibling, MoveUp, ScreenHeightRatio};
use crate::component::filesystem::action::tree::{ExpandCollapse, RefreshChildrenOfSelected};
use crate::component::filesystem::FsLayer;
@ -35,7 +35,7 @@ fn create_action_map() -> ActionKeyMap {
map(&mut me, "8", PushCountDigit(8));
map(&mut me, "9", PushCountDigit(9));
map(&mut me, "e", EditSelected);
map(&mut me, "e", EditSelectedFileOrDirectory);
map(&mut me, "d", DeleteSelected);
map(&mut me, "gg", MoveToLineOr(MoveToFirst));
map(&mut me, "G", MoveToLineOr(MoveToLast));