From 6cab68ab366313f6c447332cf923110cc41619d4 Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Tue, 24 May 2022 14:00:24 +0100 Subject: [PATCH] settings and key bindings --- lapce-data/src/data.rs | 84 +++++++++++++---------- lapce-ui/src/editor/tab_header_content.rs | 3 +- lapce-ui/src/settings.rs | 13 ++-- 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/lapce-data/src/data.rs b/lapce-data/src/data.rs index 3376de89..590ca75c 100644 --- a/lapce-data/src/data.rs +++ b/lapce-data/src/data.rs @@ -1029,14 +1029,7 @@ pub fn run_workbench_command( } } LapceWorkbenchCommand::OpenSettings => { - self.main_split.open_settings(ctx); - // let settings = Arc::make_mut(&mut self.settings); - // settings.shown = true; - // ctx.submit_command(Command::new( - // LAPCE_UI_COMMAND, - // LapceUICommand::ShowSettings, - // Target::Widget(self.settings.panel_widget_id), - // )); + self.main_split.open_settings(ctx, false); } LapceWorkbenchCommand::OpenSettingsFile => { if let Some(path) = Config::settings_file() { @@ -1054,11 +1047,7 @@ pub fn run_workbench_command( } } LapceWorkbenchCommand::OpenKeyboardShortcuts => { - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::ShowKeybindings, - Target::Widget(self.settings.panel_widget_id), - )); + self.main_split.open_settings(ctx, true); } LapceWorkbenchCommand::OpenKeyboardShortcutsFile => { if let Some(path) = KeyPressData::file() { @@ -2011,11 +2000,12 @@ fn editor_tab_new_settings( &mut self, _ctx: &mut EventCtx, editor_tab_id: WidgetId, - ) { + ) -> WidgetId { let editor_tab = self.editor_tabs.get_mut(&editor_tab_id).unwrap(); let editor_tab = Arc::make_mut(editor_tab); let child = EditorTabChild::Settings(WidgetId::next(), editor_tab_id); editor_tab.children.push(child.clone()); + child.widget_id() } fn editor_tab_new_editor( @@ -2164,45 +2154,63 @@ pub fn jump_to_position( } } - pub fn open_settings(&mut self, ctx: &mut EventCtx) { - match *self.active_tab { + pub fn open_settings(&mut self, ctx: &mut EventCtx, show_key_bindings: bool) { + let widget_id = match *self.active_tab { Some(active) => { let editor_tab = Arc::make_mut(self.editor_tabs.get_mut(&active).unwrap()); + let mut existing: Option = None; for (i, child) in editor_tab.children.iter().enumerate() { if let EditorTabChild::Settings(_, _) = child { - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::Focus, - Target::Widget(active), - )); editor_tab.active = i; - return; + existing = Some(child.widget_id()); + break; } } - let child = - EditorTabChild::Settings(WidgetId::next(), editor_tab.widget_id); - editor_tab - .children - .insert(editor_tab.active + 1, child.clone()); - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::EditorTabAdd(editor_tab.active + 1, child), - Target::Widget(editor_tab.widget_id), - )); - editor_tab.active += 1; + if let Some(widget_id) = existing { + widget_id + } else { + let child = EditorTabChild::Settings( + WidgetId::next(), + editor_tab.widget_id, + ); + editor_tab + .children + .insert(editor_tab.active + 1, child.clone()); + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::EditorTabAdd( + editor_tab.active + 1, + child.clone(), + ), + Target::Widget(editor_tab.widget_id), + )); + editor_tab.active += 1; + child.widget_id() + } } None => { let editor_tab_id = self.new_editor_tab(ctx, *self.split_id); - self.editor_tab_new_settings(ctx, editor_tab_id); + self.editor_tab_new_settings(ctx, editor_tab_id) } - } - if let Some(active) = *self.active_tab { + }; + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::Focus, + Target::Widget(widget_id), + )); + if show_key_bindings { ctx.submit_command(Command::new( LAPCE_UI_COMMAND, - LapceUICommand::Focus, - Target::Widget(active), + LapceUICommand::ShowKeybindings, + Target::Widget(widget_id), + )); + } else { + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::ShowSettings, + Target::Widget(widget_id), )); } } diff --git a/lapce-ui/src/editor/tab_header_content.rs b/lapce-ui/src/editor/tab_header_content.rs index 04d1f573..53fc756d 100644 --- a/lapce-ui/src/editor/tab_header_content.rs +++ b/lapce-ui/src/editor/tab_header_content.rs @@ -16,6 +16,7 @@ data::{DragContent, EditorTabChild, LapceTabData}, document::BufferContent, editor::TabRect, + proxy::VERSION, }; use crate::{ @@ -368,7 +369,7 @@ fn layout( } } EditorTabChild::Settings(_, _) => { - text = "Settings".to_string(); + text = format!("Settings v{}", VERSION); } } let text_layout = ctx diff --git a/lapce-ui/src/settings.rs b/lapce-ui/src/settings.rs index c9e44e35..ebdd5708 100644 --- a/lapce-ui/src/settings.rs +++ b/lapce-ui/src/settings.rs @@ -86,7 +86,7 @@ fn mouse_down( &mut self, ctx: &mut EventCtx, mouse_event: &MouseEvent, - _data: &mut LapceTabData, + data: &mut LapceTabData, ) { if self.switcher_rect.contains(mouse_event.pos) { let index = ((mouse_event.pos.y - self.switcher_rect.y0) @@ -97,9 +97,14 @@ fn mouse_down( ctx.request_layout(); } ctx.set_handled(); - ctx.request_focus(); + self.request_focus(ctx, data); } } + + fn request_focus(&self, ctx: &mut EventCtx, data: &mut LapceTabData) { + data.main_split.active_tab = Arc::new(Some(self.editor_tab_id)); + ctx.request_focus(); + } } impl Widget for LapceSettingsPanel { @@ -154,10 +159,8 @@ fn event( let command = cmd.get_unchecked(LAPCE_UI_COMMAND); match command { LapceUICommand::Focus => { - ctx.request_focus(); ctx.set_handled(); - data.main_split.active_tab = - Arc::new(Some(self.editor_tab_id)); + self.request_focus(ctx, data); } LapceUICommand::ShowSettings => { ctx.request_focus();