Transfer focus to the workspace when window focus is lost
This commit is contained in:
parent
137104e00e
commit
43b8d65fee
3 changed files with 28 additions and 2 deletions
|
@ -646,8 +646,6 @@ impl AppContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Repeatedly called during `flush_effects` to handle a focused handle being dropped.
|
/// Repeatedly called during `flush_effects` to handle a focused handle being dropped.
|
||||||
/// For now, we simply blur the window if this happens, but we may want to support invoking
|
|
||||||
/// a window blur handler to restore focus to some logical element.
|
|
||||||
fn release_dropped_focus_handles(&mut self) {
|
fn release_dropped_focus_handles(&mut self) {
|
||||||
for window_handle in self.windows() {
|
for window_handle in self.windows() {
|
||||||
window_handle
|
window_handle
|
||||||
|
|
|
@ -2419,6 +2419,29 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
subscription
|
subscription
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register a listener to be called when the window loses focus.
|
||||||
|
/// Unlike [on_focus_changed], returns a subscription and persists until the subscription
|
||||||
|
/// is dropped.
|
||||||
|
pub fn on_window_focus_lost(
|
||||||
|
&mut self,
|
||||||
|
mut listener: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
|
||||||
|
) -> Subscription {
|
||||||
|
let view = self.view.downgrade();
|
||||||
|
let (subscription, activate) = self.window.focus_listeners.insert(
|
||||||
|
(),
|
||||||
|
Box::new(move |event, cx| {
|
||||||
|
view.update(cx, |view, cx| {
|
||||||
|
if event.blurred.is_none() && event.focused.is_none() {
|
||||||
|
listener(view, cx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.is_ok()
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
self.app.defer(move |_| activate());
|
||||||
|
subscription
|
||||||
|
}
|
||||||
|
|
||||||
/// Register a listener to be called when the given focus handle or one of its descendants loses focus.
|
/// Register a listener to be called when the given focus handle or one of its descendants loses focus.
|
||||||
/// Unlike [on_focus_changed], returns a subscription and persists until the subscription
|
/// Unlike [on_focus_changed], returns a subscription and persists until the subscription
|
||||||
/// is dropped.
|
/// is dropped.
|
||||||
|
|
|
@ -532,6 +532,11 @@ impl Workspace {
|
||||||
cx.notify()
|
cx.notify()
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
cx.on_window_focus_lost(|this, cx| {
|
||||||
|
let focus_handle = this.focus_handle(cx);
|
||||||
|
cx.focus(&focus_handle);
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
let weak_handle = cx.view().downgrade();
|
let weak_handle = cx.view().downgrade();
|
||||||
let pane_history_timestamp = Arc::new(AtomicUsize::new(0));
|
let pane_history_timestamp = Arc::new(AtomicUsize::new(0));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue