Unnamed repository; edit this file 'description' to name the repository.
add DocumentFocusLost event
Pascal Kuthe 2024-12-18
parent 5537e68 · commit 609c29b
-rw-r--r--helix-term/src/events.rs5
-rw-r--r--helix-view/src/editor.rs30
-rw-r--r--helix-view/src/events.rs2
3 files changed, 30 insertions, 7 deletions
diff --git a/helix-term/src/events.rs b/helix-term/src/events.rs
index 415213c2..15d81152 100644
--- a/helix-term/src/events.rs
+++ b/helix-term/src/events.rs
@@ -1,6 +1,8 @@
use helix_event::{events, register_event};
use helix_view::document::Mode;
-use helix_view::events::{DiagnosticsDidChange, DocumentDidChange, SelectionDidChange};
+use helix_view::events::{
+ DiagnosticsDidChange, DocumentDidChange, DocumentFocusLost, SelectionDidChange,
+};
use crate::commands;
use crate::keymap::MappableCommand;
@@ -16,6 +18,7 @@ pub fn register() {
register_event::<PostInsertChar>();
register_event::<PostCommand>();
register_event::<DocumentDidChange>();
+ register_event::<DocumentFocusLost>();
register_event::<SelectionDidChange>();
register_event::<DiagnosticsDidChange>();
}
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs
index 3b4e4a5c..0b07f6df 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -4,6 +4,7 @@ use crate::{
document::{
DocumentOpenError, DocumentSavedEventFuture, DocumentSavedEventResult, Mode, SavePoint,
},
+ events::DocumentFocusLost,
graphics::{CursorKind, Rect},
handlers::Handlers,
info::Info,
@@ -14,6 +15,7 @@ use crate::{
Document, DocumentId, View, ViewId,
};
use dap::StackFrame;
+use helix_event::dispatch;
use helix_vcs::DiffProviderRegistry;
use futures_util::stream::select_all::SelectAll;
@@ -1590,7 +1592,7 @@ impl Editor {
self.enter_normal_mode();
}
- match action {
+ let focust_lost = match action {
Action::Replace => {
let (view, doc) = current_ref!(self);
// If the current view is an empty scratch buffer and is not displayed in any other views, delete it.
@@ -1640,6 +1642,10 @@ impl Editor {
self.replace_document_in_view(view_id, id);
+ dispatch(DocumentFocusLost {
+ editor: self,
+ doc: id,
+ });
return;
}
Action::Load => {
@@ -1650,6 +1656,7 @@ impl Editor {
return;
}
Action::HorizontalSplit | Action::VerticalSplit => {
+ let focus_lost = self.tree.try_get(self.tree.focus).map(|view| view.doc);
// copy the current view, unless there is no view yet
let view = self
.tree
@@ -1669,10 +1676,17 @@ impl Editor {
let doc = doc_mut!(self, &id);
doc.ensure_view_init(view_id);
doc.mark_as_focused();
+ focus_lost
}
- }
+ };
self._refresh();
+ if let Some(focus_lost) = focust_lost {
+ dispatch(DocumentFocusLost {
+ editor: self,
+ doc: focus_lost,
+ });
+ }
}
/// Generate an id for a new document and register it.
@@ -1903,11 +1917,15 @@ impl Editor {
let doc = doc_mut!(self, &view.doc);
view.sync_changes(doc);
}
+ let view = view!(self, view_id);
+ let doc = doc_mut!(self, &view.doc);
+ doc.mark_as_focused();
+ let focus_lost = self.tree.get(prev_id).doc;
+ dispatch(DocumentFocusLost {
+ editor: self,
+ doc: focus_lost,
+ });
}
-
- let view = view!(self, view_id);
- let doc = doc_mut!(self, &view.doc);
- doc.mark_as_focused();
}
pub fn focus_next(&mut self) {
diff --git a/helix-view/src/events.rs b/helix-view/src/events.rs
index 136d60c5..eb97268c 100644
--- a/helix-view/src/events.rs
+++ b/helix-view/src/events.rs
@@ -13,4 +13,6 @@ events! {
}
SelectionDidChange<'a> { doc: &'a mut Document, view: ViewId }
DiagnosticsDidChange<'a> { editor: &'a mut Editor, doc: DocumentId }
+ // called **after** a document loses focus (but not when its closed)
+ DocumentFocusLost<'a> { editor: &'a mut Editor, doc: DocumentId }
}