on master help sign in
fix: Reset document mode when losing focus
e4c9d40Blaž Hrastnik
helix-term/src/ui/editor.rs+6−8
@@ -976,16 +976,15 @@ impl EditorView {
976 doc.set_selection(view_id, Selection::point(pos));
976 doc.set_selection(view_id, Selection::point(pos));
977 }
977 }
978
978
979− editor.tree.focus = view_id;
979+ editor.focus(view_id);
980
980
981 return EventResult::Consumed(None);
981 return EventResult::Consumed(None);
982 }
982 }
983
983
984 if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
984 if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
985− editor.tree.focus = view_id;
985+ editor.focus(view_id);
986
986
987− let view = editor.tree.get(view_id);
987+ let (view, doc) = current!(cxt.editor);
988− let doc = editor.documents.get_mut(&view.doc).unwrap();
989
988
990 let path = match doc.path() {
989 let path = match doc.path() {
991 Some(path) => path.clone(),
990 Some(path) => path.clone(),
@@ -1064,10 +1063,9 @@ impl EditorView {
1064
1063
1065 MouseEventKind::Up(MouseButton::Right) => {
1064 MouseEventKind::Up(MouseButton::Right) => {
1066 if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
1065 if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
1067− cxt.editor.tree.focus = view_id;
1066+ cxt.editor.focus(view_id);
1068
1067
1069− let view = cxt.editor.tree.get(view_id);
1068+ let (view, doc) = current!(cxt.editor);
1070− let doc = cxt.editor.documents.get_mut(&view.doc).unwrap();
1071 let line = coords.row + view.offset.row;
1069 let line = coords.row + view.offset.row;
1072 if let Ok(pos) = doc.text().try_line_to_char(line) {
1070 if let Ok(pos) = doc.text().try_line_to_char(line) {
1073 doc.set_selection(view_id, Selection::point(pos));
1071 doc.set_selection(view_id, Selection::point(pos));
@@ -1100,7 +1098,7 @@ impl EditorView {
1100 if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
1098 if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
1101 let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
1099 let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
1102 doc.set_selection(view_id, Selection::point(pos));
1100 doc.set_selection(view_id, Selection::point(pos));
1103− editor.tree.focus = view_id;
1101+ cxt.editor.focus(view_id);
1104 commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
1102 commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
1105
1103
1106 return EventResult::Consumed(None);
1104 return EventResult::Consumed(None);
helix-view/src/editor.rs+22−1
@@ -1091,12 +1091,33 @@ impl Editor {
1091 };
1091 };
1092 }
1092 }
1093
1093
1094+ pub fn focus(&mut self, view_id: ViewId) {
1095+ let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
1096+
1097+ // if leaving the view: mode should reset
1098+ if prev_id != view_id {
1099+ let doc_id = self.tree.get(prev_id).doc;
1100+ self.documents.get_mut(&doc_id).unwrap().mode = Mode::Normal;
1101+ }
1102+ }
1103+
1094 pub fn focus_next(&mut self) {
1104 pub fn focus_next(&mut self) {
1105+ let prev_id = self.tree.focus;
1095 self.tree.focus_next();
1106 self.tree.focus_next();
1107+ let id = self.tree.focus;
1108+
1109+ // if leaving the view: mode should reset
1110+ if prev_id != id {
1111+ let doc_id = self.tree.get(prev_id).doc;
1112+ self.documents.get_mut(&doc_id).unwrap().mode = Mode::Normal;
1113+ }
1096 }
1114 }
1097
1115
1098 pub fn focus_direction(&mut self, direction: tree::Direction) {
1116 pub fn focus_direction(&mut self, direction: tree::Direction) {
1099− self.tree.focus_direction(direction);
1117+ let current_view = self.tree.focus;
1118+ if let Some(id) = self.tree.find_split_in_direction(current_view, direction) {
1119+ self.focus(id)
1120+ }
1100 }
1121 }
1101
1122
1102 pub fn swap_split_in_direction(&mut self, direction: tree::Direction) {
1123 pub fn swap_split_in_direction(&mut self, direction: tree::Direction) {
helix-view/src/tree.rs−6
@@ -504,12 +504,6 @@ impl Tree {
504 Some(child_id)
504 Some(child_id)
505 }
505 }
506
506
507− pub fn focus_direction(&mut self, direction: Direction) {
508− if let Some(id) = self.find_split_in_direction(self.focus, direction) {
509− self.focus = id;
510− }
511− }
512−
513 pub fn focus_next(&mut self) {
507 pub fn focus_next(&mut self) {
514 // This function is very dumb, but that's because we don't store any parent links.
508 // This function is very dumb, but that's because we don't store any parent links.
515 // (we'd be able to go parent.next_sibling() recursively until we find something)
509 // (we'd be able to go parent.next_sibling() recursively until we find something)