From 847dc54f05cc68ada1aa81803ceac7cb0fecc8be Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Tue, 24 May 2022 13:40:28 +0100 Subject: [PATCH] settings event fix --- lapce-data/src/data.rs | 25 ++-------- lapce-ui/src/editor/tab.rs | 97 ++++++++++++++++++++++++------------- lapce-ui/src/editor/view.rs | 15 ++++-- lapce-ui/src/settings.rs | 67 ++++++++++++++----------- 4 files changed, 119 insertions(+), 85 deletions(-) diff --git a/lapce-data/src/data.rs b/lapce-data/src/data.rs index 791aab6b..6f38b36b 100644 --- a/lapce-data/src/data.rs +++ b/lapce-data/src/data.rs @@ -1984,7 +1984,7 @@ fn new_editor_tab( let editor_tab_id = WidgetId::next(); let editor_tab = LapceEditorTabData { widget_id: editor_tab_id, - split: *self.split_id, + split: split_id, active: 0, children: vec![], layout_rect: Rc::new(RefCell::new(Rect::ZERO)), @@ -1997,7 +1997,7 @@ fn new_editor_tab( SplitContent::EditorTab(editor_tab.widget_id), true, ), - Target::Widget(*self.split_id), + Target::Widget(split_id), )); self.active_tab = Arc::new(Some(editor_tab.widget_id)); split @@ -2009,23 +2009,18 @@ fn new_editor_tab( fn editor_tab_new_settings( &mut self, - ctx: &mut EventCtx, + _ctx: &mut EventCtx, editor_tab_id: 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()); - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::EditorTabAdd(0, child), - Target::Widget(editor_tab.widget_id), - )); } fn editor_tab_new_editor( &mut self, - ctx: &mut EventCtx, + _ctx: &mut EventCtx, editor_tab_id: WidgetId, config: &Config, ) -> WidgetId { @@ -2043,18 +2038,6 @@ fn editor_tab_new_editor( editor.editor_id, editor.find_view_id, )); - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::EditorTabAdd( - 0, - EditorTabChild::Editor( - editor.view_id, - editor.editor_id, - editor.find_view_id, - ), - ), - Target::Widget(editor_tab.widget_id), - )); self.insert_editor(editor.clone(), config); editor.view_id } diff --git a/lapce-ui/src/editor/tab.rs b/lapce-ui/src/editor/tab.rs index 9a758cfd..d1767a54 100644 --- a/lapce-ui/src/editor/tab.rs +++ b/lapce-ui/src/editor/tab.rs @@ -5,8 +5,11 @@ InternalEvent, LayoutCtx, LifeCycle, LifeCycleCtx, MouseEvent, PaintCtx, Point, Rect, RenderContext, Size, Target, UpdateCtx, Widget, WidgetId, WidgetPod, }; +use lapce_core::command::FocusCommand; use lapce_data::{ - command::{LapceUICommand, LAPCE_UI_COMMAND}, + command::{ + CommandKind, LapceCommand, LapceUICommand, LAPCE_COMMAND, LAPCE_UI_COMMAND, + }, config::LapceTheme, data::{ DragContent, EditorTabChild, LapceEditorTabData, LapceTabData, SplitContent, @@ -95,11 +98,8 @@ pub fn remove_child( )); editor_tab.children.remove(i) } else if editor_tab.active == i { - let new_index = if i >= editor_tab.children.len() - 1 { + if i >= editor_tab.children.len() - 1 { editor_tab.active = i - 1; - i - 1 - } else { - i }; if focus { ctx.submit_command(Command::new( @@ -107,11 +107,6 @@ pub fn remove_child( LapceUICommand::EnsureEditorTabActiveVisble, Target::Widget(editor_tab.widget_id), )); - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::Focus, - Target::Widget(editor_tab.children[new_index].widget_id()), - )); } editor_tab.children.remove(i) } else { @@ -120,6 +115,13 @@ pub fn remove_child( } editor_tab.children.remove(i) }; + if focus && !editor_tab.children.is_empty() { + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::Focus, + Target::Widget(editor_tab.children[editor_tab.active].widget_id()), + )); + } if delete { match removed_child { EditorTabChild::Editor(view_id, _, _) => { @@ -187,16 +189,18 @@ fn mouse_up( .get(&self.widget_id) .unwrap(); let split_id = editor_tab.split; + + let new_editor_tab_id = WidgetId::next(); + let mut child = child.clone(); + child.set_editor_tab(data, new_editor_tab_id); let mut new_editor_tab = LapceEditorTabData { - widget_id: WidgetId::next(), + widget_id: new_editor_tab_id, split: split_id, active: 0, children: vec![child.clone()], layout_rect: Rc::new(RefCell::new(Rect::ZERO)), content_is_hot: Rc::new(RefCell::new(false)), }; - let mut child = child.clone(); - child.set_editor_tab(data, new_editor_tab.widget_id); let new_split_id = data.main_split.split( ctx, @@ -224,11 +228,6 @@ fn mouse_up( new_editor_tab.widget_id, Arc::new(new_editor_tab), ); - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::Focus, - Target::Widget(child.widget_id()), - )); ctx.submit_command(Command::new( LAPCE_UI_COMMAND, LapceUICommand::EditorTabRemove( @@ -238,6 +237,11 @@ fn mouse_up( ), Target::Widget(*from_id), )); + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::Focus, + Target::Widget(child.widget_id()), + )); } None => { if from_id == &self.widget_id { @@ -262,11 +266,6 @@ fn mouse_up( ), Target::Widget(editor_tab.widget_id), )); - ctx.submit_command(Command::new( - LAPCE_UI_COMMAND, - LapceUICommand::Focus, - Target::Widget(child.widget_id()), - )); ctx.submit_command(Command::new( LAPCE_UI_COMMAND, LapceUICommand::EditorTabRemove( @@ -276,6 +275,11 @@ fn mouse_up( ), Target::Widget(*from_id), )); + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::Focus, + Target::Widget(child.widget_id()), + )); } } } @@ -305,7 +309,32 @@ fn event( Event::MouseUp(mouse_event) => { self.mouse_up(ctx, data, mouse_event); } + Event::Command(cmd) if cmd.is(LAPCE_COMMAND) => { + ctx.set_handled(); + let cmd = cmd.get_unchecked(LAPCE_COMMAND); + match cmd.kind { + CommandKind::Focus(FocusCommand::SplitVertical) => { + let editor_tab = data + .main_split + .editor_tabs + .get_mut(&self.widget_id) + .unwrap(); + ctx.submit_command(Command::new( + LAPCE_COMMAND, + LapceCommand { + kind: CommandKind::Focus( + FocusCommand::SplitVertical, + ), + data: None, + }, + Target::Widget(editor_tab.active_child().widget_id()), + )); + } + _ => {} + } + } Event::Command(cmd) if cmd.is(LAPCE_UI_COMMAND) => { + ctx.set_handled(); let command = cmd.get_unchecked(LAPCE_UI_COMMAND); match command { LapceUICommand::EditorTabAdd(index, content) => { @@ -393,18 +422,18 @@ fn event( } _ => (), } - self.header.event(ctx, event, data, env); - let tab = data.main_split.editor_tabs.get(&self.widget_id).unwrap(); - match event { - Event::Internal(InternalEvent::TargetedCommand(_)) => { - for child in self.children.iter_mut() { - child.event(ctx, event, data, env); - } - } - _ => { - self.children[tab.active].event(ctx, event, data, env); - } + if ctx.is_handled() { + return; } + self.header.event(ctx, event, data, env); + if event.should_propagate_to_hidden() { + for child in self.children.iter_mut() { + child.event(ctx, event, data, env); + } + } else { + let tab = data.main_split.editor_tabs.get(&self.widget_id).unwrap(); + self.children[tab.active].event(ctx, event, data, env); + }; } fn lifecycle( diff --git a/lapce-ui/src/editor/view.rs b/lapce-ui/src/editor/view.rs index bf51aae0..8bf18f45 100644 --- a/lapce-ui/src/editor/view.rs +++ b/lapce-ui/src/editor/view.rs @@ -8,8 +8,8 @@ use lapce_core::command::{EditCommand, FocusCommand}; use lapce_data::{ command::{ - CommandKind, EnsureVisiblePosition, LapceCommand, LapceUICommand, - LAPCE_COMMAND, LAPCE_UI_COMMAND, + CommandExecuted, CommandKind, EnsureVisiblePosition, LapceCommand, + LapceUICommand, LAPCE_COMMAND, LAPCE_UI_COMMAND, }, config::{EditorConfig, LapceTheme}, data::{ @@ -566,7 +566,16 @@ fn event( } Event::Command(cmd) if cmd.is(LAPCE_COMMAND) => { let command = cmd.get_unchecked(LAPCE_COMMAND); - editor_data.run_command(ctx, command, None, Modifiers::empty(), env); + if editor_data.run_command( + ctx, + command, + None, + Modifiers::empty(), + env, + ) == CommandExecuted::Yes + { + ctx.set_handled(); + } self.ensure_cursor_visible( ctx, &editor_data, diff --git a/lapce-ui/src/settings.rs b/lapce-ui/src/settings.rs index c7a8a007..c9e44e35 100644 --- a/lapce-ui/src/settings.rs +++ b/lapce-ui/src/settings.rs @@ -88,8 +88,6 @@ fn mouse_down( mouse_event: &MouseEvent, _data: &mut LapceTabData, ) { - ctx.set_handled(); - ctx.request_focus(); if self.switcher_rect.contains(mouse_event.pos) { let index = ((mouse_event.pos.y - self.switcher_rect.y0) / self.switcher_line_height) @@ -98,6 +96,8 @@ fn mouse_down( self.active = index; ctx.request_layout(); } + ctx.set_handled(); + ctx.request_focus(); } } } @@ -114,36 +114,28 @@ fn event( data: &mut LapceTabData, env: &Env, ) { - self.children[self.active].event(ctx, event, data, env); - if ctx.is_handled() { - return; - } match event { Event::KeyDown(key_event) => { - let mut keypress = data.keypress.clone(); - let mut focus = LapceSettingsFocusData { - widget_id: self.widget_id, - editor_tab_id: self.editor_tab_id, - main_split: data.main_split.clone(), - }; - let mut_keypress = Arc::make_mut(&mut keypress); - let performed_action = - mut_keypress.key_down(ctx, key_event, &mut focus, env); - data.keypress = keypress; - data.main_split = focus.main_split; - if performed_action { - ctx.set_handled(); + if ctx.is_focused() { + let mut keypress = data.keypress.clone(); + let mut focus = LapceSettingsFocusData { + widget_id: self.widget_id, + editor_tab_id: self.editor_tab_id, + main_split: data.main_split.clone(), + }; + let mut_keypress = Arc::make_mut(&mut keypress); + let performed_action = + mut_keypress.key_down(ctx, key_event, &mut focus, env); + data.keypress = keypress; + data.main_split = focus.main_split; + if performed_action { + ctx.set_handled(); + } } } - Event::MouseMove(_mouse_event) => { - ctx.set_handled(); - } Event::MouseDown(mouse_event) => { self.mouse_down(ctx, mouse_event, data); } - Event::MouseUp(_mouse_event) => { - ctx.set_handled(); - } Event::Command(cmd) if cmd.is(LAPCE_COMMAND) => { let cmd = cmd.get_unchecked(LAPCE_COMMAND); let mut focus = LapceSettingsFocusData { @@ -151,13 +143,22 @@ fn event( editor_tab_id: self.editor_tab_id, main_split: data.main_split.clone(), }; - focus.run_command(ctx, cmd, None, Modifiers::empty(), env); + if focus.run_command(ctx, cmd, None, Modifiers::empty(), env) + == CommandExecuted::Yes + { + ctx.set_handled(); + } data.main_split = focus.main_split; - println!("run cmd {cmd:?}"); } Event::Command(cmd) if cmd.is(LAPCE_UI_COMMAND) => { 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)); + } LapceUICommand::ShowSettings => { ctx.request_focus(); self.active = 0; @@ -180,6 +181,18 @@ fn event( } _ => {} } + + if ctx.is_handled() { + return; + } + + if event.should_propagate_to_hidden() { + for child in self.children.iter_mut() { + child.event(ctx, event, data, env); + } + } else { + self.children[self.active].event(ctx, event, data, env); + } } fn lifecycle(