From ea92439dfe13a3a625d60476f8665e34d9422eda Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Mon, 21 Mar 2022 14:15:02 +0000 Subject: [PATCH] close folder --- defaults/keymaps-macos.toml | 8 ++++ defaults/keymaps-nonmacos.toml | 8 ++++ lapce-data/src/command.rs | 8 ++++ lapce-data/src/data.rs | 81 ++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) diff --git a/defaults/keymaps-macos.toml b/defaults/keymaps-macos.toml index 8ab8b1d7..d3d751c5 100644 --- a/defaults/keymaps-macos.toml +++ b/defaults/keymaps-macos.toml @@ -116,12 +116,20 @@ mode = "i" key = "meta+s" command = "save" +[[keymaps]] +key = "meta+o" +command = "open_file" + # ----------------------------------- Editor Management ------------------------------- [[keymaps]] key = "meta+w" command = "split_close" +[[keymaps]] +key = "meta+k f" +command = "close_folder" + [[keymaps]] key = "meta+\\" command = "split_vertical" diff --git a/defaults/keymaps-nonmacos.toml b/defaults/keymaps-nonmacos.toml index 375c3004..f81cda4b 100644 --- a/defaults/keymaps-nonmacos.toml +++ b/defaults/keymaps-nonmacos.toml @@ -116,6 +116,10 @@ mode = "i" key = "ctrl+s" command = "save" +[[keymaps]] +key = "ctrl+o" +command = "open_file" + # ----------------------------------- Editor Management ------------------------------- [[keymaps]] @@ -123,6 +127,10 @@ key = "ctrl+w" command = "split_close" mode = "i" +[[keymaps]] +key = "ctrl+k f" +command = "close_folder" + [[keymaps]] key = "ctrl+F4" command = "split_close" diff --git a/lapce-data/src/command.rs b/lapce-data/src/command.rs index 269db224..24c7a742 100644 --- a/lapce-data/src/command.rs +++ b/lapce-data/src/command.rs @@ -103,6 +103,14 @@ pub enum LapceWorkbenchCommand { #[strum(message = "Open Folder")] OpenFolder, + #[strum(serialize = "close_folder")] + #[strum(message = "Close Folder")] + CloseFolder, + + #[strum(serialize = "open_file")] + #[strum(message = "Open File")] + OpenFile, + #[strum(serialize = "change_theme")] #[strum(message = "Change Theme")] ChangeTheme, diff --git a/lapce-data/src/data.rs b/lapce-data/src/data.rs index 5c55f228..2f49a6bd 100644 --- a/lapce-data/src/data.rs +++ b/lapce-data/src/data.rs @@ -866,6 +866,17 @@ pub fn run_workbench_command( env: &Env, ) { match command { + LapceWorkbenchCommand::CloseFolder => { + if self.workspace.path.is_some() { + let mut workspace = (*self.workspace).clone(); + workspace.path = None; + let _ = ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::SetWorkspace(workspace), + Target::Auto, + )); + } + } LapceWorkbenchCommand::OpenFolder => { if !self.workspace.kind.is_remote() { let event_sink = ctx.get_external_handle(); @@ -939,6 +950,76 @@ pub fn run_workbench_command( } } } + LapceWorkbenchCommand::OpenFile => { + if !self.workspace.kind.is_remote() { + let workspace = self.workspace.clone(); + let event_sink = ctx.get_external_handle(); + let tab_id = self.id; + thread::spawn(move || { + let dir = workspace.path.clone().unwrap_or_else(|| { + PathBuf::from( + directories::UserDirs::new() + .and_then(|u| { + u.home_dir().to_str().map(|s| s.to_string()) + }) + .unwrap_or_else(|| ".".to_string()), + ) + }); + if let Some(path) = tinyfiledialogs::open_file_dialog( + "Open file", + dir.to_str().unwrap(), + None, + ) { + let path = PathBuf::from(path); + let _ = event_sink.submit_command( + LAPCE_UI_COMMAND, + LapceUICommand::OpenFile(path), + Target::Widget(tab_id), + ); + } + }); + } else { + let picker = Arc::make_mut(&mut self.picker); + picker.active = true; + if let Some(node) = picker.get_file_node(&picker.pwd) { + if !node.read { + let tab_id = self.id; + let path = node.path_buf.clone(); + let event_sink = ctx.get_external_handle(); + self.proxy.read_dir( + &node.path_buf, + Box::new(move |result| { + if let Ok(res) = result { + let resp: Result< + Vec, + serde_json::Error, + > = serde_json::from_value(res); + if let Ok(items) = resp { + let _ = event_sink.submit_command( + LAPCE_UI_COMMAND, + LapceUICommand::UpdatePickerItems( + path, + items + .iter() + .map(|item| { + ( + item.path_buf + .clone(), + item.clone(), + ) + }) + .collect(), + ), + Target::Widget(tab_id), + ); + } + } + }), + ); + } + } + } + } LapceWorkbenchCommand::EnableModal => { let config = Arc::make_mut(&mut self.config); config.lapce.modal = true;