Focus/blur views when application windows become active/inactive

This commit is contained in:
Max Brunsfeld 2022-04-22 14:18:50 -07:00
parent f881c2aa92
commit 7f64076f8d
4 changed files with 99 additions and 2 deletions

View file

@ -1591,6 +1591,7 @@ impl MutableAppContext {
Window {
root_view: root_view.clone().into(),
focused_view_id: Some(root_view.id()),
is_active: true,
invalidation: None,
},
);
@ -1638,10 +1639,17 @@ impl MutableAppContext {
}));
}
{
let mut app = self.upgrade();
window.on_active_status_change(Box::new(move |is_active| {
app.update(|cx| cx.window_changed_active_status(window_id, is_active))
}));
}
{
let mut app = self.upgrade();
window.on_resize(Box::new(move || {
app.update(|cx| cx.resize_window(window_id))
app.update(|cx| cx.window_was_resized(window_id))
}));
}
@ -1856,6 +1864,10 @@ impl MutableAppContext {
.get_or_insert(WindowInvalidation::default());
}
}
Effect::ActivateWindow {
window_id,
is_active,
} => self.handle_activation_effect(window_id, is_active),
Effect::RefreshWindows => {
refreshing = true;
}
@ -1914,11 +1926,18 @@ impl MutableAppContext {
}
}
fn resize_window(&mut self, window_id: usize) {
fn window_was_resized(&mut self, window_id: usize) {
self.pending_effects
.push_back(Effect::ResizeWindow { window_id });
}
fn window_changed_active_status(&mut self, window_id: usize, is_active: bool) {
self.pending_effects.push_back(Effect::ActivateWindow {
window_id,
is_active,
});
}
pub fn refresh_windows(&mut self) {
self.pending_effects.push_back(Effect::RefreshWindows);
}
@ -2204,6 +2223,34 @@ impl MutableAppContext {
}
}
fn handle_activation_effect(&mut self, window_id: usize, active: bool) {
if self
.cx
.windows
.get(&window_id)
.map(|w| w.is_active)
.map_or(false, |cur_active| cur_active == active)
{
return;
}
self.update(|this| {
let window = this.cx.windows.get_mut(&window_id)?;
window.is_active = active;
let view_id = window.focused_view_id?;
if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
if active {
view.on_focus(this, window_id, view_id);
} else {
view.on_blur(this, window_id, view_id);
}
this.cx.views.insert((window_id, view_id), view);
}
Some(())
});
}
fn handle_focus_effect(&mut self, window_id: usize, focused_id: Option<usize>) {
self.pending_focus_index.take();
@ -2567,6 +2614,7 @@ impl ReadView for AppContext {
struct Window {
root_view: AnyViewHandle,
focused_view_id: Option<usize>,
is_active: bool,
invalidation: Option<WindowInvalidation>,
}
@ -2633,6 +2681,10 @@ pub enum Effect {
ResizeWindow {
window_id: usize,
},
ActivateWindow {
window_id: usize,
is_active: bool,
},
RefreshWindows,
}
@ -2714,6 +2766,14 @@ impl Debug for Effect {
.debug_struct("Effect::RefreshWindow")
.field("window_id", window_id)
.finish(),
Effect::ActivateWindow {
window_id,
is_active,
} => f
.debug_struct("Effect::ActivateWindow")
.field("window_id", window_id)
.field("is_active", is_active)
.finish(),
Effect::RefreshWindows => f.debug_struct("Effect::FullViewRefresh").finish(),
}
}