Ensure that on_release callbacks are called even if view outlives its window

This commit is contained in:
Max Brunsfeld 2023-12-20 16:01:52 -08:00
parent e1a4e8ea16
commit 5e7c74c7b6
5 changed files with 27 additions and 19 deletions

View file

@ -1,8 +1,8 @@
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor}; use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
use gpui::{ use gpui::{
actions, div, prelude::*, AppContext, DismissEvent, Div, EventEmitter, FocusHandle, actions, div, prelude::*, AnyWindowHandle, AppContext, DismissEvent, Div, EventEmitter,
FocusableView, Render, SharedString, Styled, Subscription, View, ViewContext, VisualContext, FocusHandle, FocusableView, Render, SharedString, Styled, Subscription, View, ViewContext,
WindowContext, VisualContext,
}; };
use text::{Bias, Point}; use text::{Bias, Point};
use theme::ActiveTheme; use theme::ActiveTheme;
@ -74,15 +74,19 @@ impl GoToLine {
} }
} }
fn release(&mut self, cx: &mut WindowContext) { fn release(&mut self, window: AnyWindowHandle, cx: &mut AppContext) {
let scroll_position = self.prev_scroll_position.take(); window
self.active_editor.update(cx, |editor, cx| { .update(cx, |_, cx| {
editor.highlight_rows(None); let scroll_position = self.prev_scroll_position.take();
if let Some(scroll_position) = scroll_position { self.active_editor.update(cx, |editor, cx| {
editor.set_scroll_position(scroll_position, cx); editor.highlight_rows(None);
} if let Some(scroll_position) = scroll_position {
cx.notify(); editor.set_scroll_position(scroll_position, cx);
}) }
cx.notify();
})
})
.ok();
} }
fn on_line_editor_event( fn on_line_editor_event(

View file

@ -2422,16 +2422,20 @@ impl<'a, V: 'static> ViewContext<'a, V> {
subscription subscription
} }
/// Register a callback to be invoked when the view is released.
///
/// The callback receives a handle to the view's window. This handle may be
/// invalid, if the window was closed before the view was released.
pub fn on_release( pub fn on_release(
&mut self, &mut self,
on_release: impl FnOnce(&mut V, &mut WindowContext) + 'static, on_release: impl FnOnce(&mut V, AnyWindowHandle, &mut AppContext) + 'static,
) -> Subscription { ) -> Subscription {
let window_handle = self.window.handle; let window_handle = self.window.handle;
let (subscription, activate) = self.app.release_listeners.insert( let (subscription, activate) = self.app.release_listeners.insert(
self.view.model.entity_id, self.view.model.entity_id,
Box::new(move |this, cx| { Box::new(move |this, cx| {
let this = this.downcast_mut().expect("invalid entity type"); let this = this.downcast_mut().expect("invalid entity type");
let _ = window_handle.update(cx, |_, cx| on_release(this, cx)); on_release(this, window_handle, cx)
}), }),
); );
activate(); activate();

View file

@ -13,7 +13,7 @@ pub fn init(cx: &mut AppContext) {
.detach(); .detach();
let id = cx.view().entity_id(); let id = cx.view().entity_id();
cx.on_release(move |_, cx| released(id, cx)).detach(); cx.on_release(move |_, _, cx| released(id, cx)).detach();
}) })
.detach(); .detach();
} }
@ -51,8 +51,8 @@ fn blurred(editor: View<Editor>, cx: &mut WindowContext) {
}); });
} }
fn released(entity_id: EntityId, cx: &mut WindowContext) { fn released(entity_id: EntityId, cx: &mut AppContext) {
Vim::update(cx, |vim, _| { cx.update_global(|vim: &mut Vim, _| {
if vim if vim
.active_editor .active_editor
.as_ref() .as_ref()

View file

@ -642,7 +642,7 @@ impl Workspace {
this.serialize_workspace(cx); this.serialize_workspace(cx);
cx.notify(); cx.notify();
}), }),
cx.on_release(|this, cx| { cx.on_release(|this, _, cx| {
this.app_state.workspace_store.update(cx, |store, _| { this.app_state.workspace_store.update(cx, |store, _| {
store.workspaces.remove(&this.window_self); store.workspaces.remove(&this.window_self);
}) })

View file

@ -253,7 +253,7 @@ pub async fn handle_cli_connection(
let (done_tx, done_rx) = oneshot::channel(); let (done_tx, done_rx) = oneshot::channel();
let _subscription = let _subscription =
workspace.update(&mut cx, |workspace, cx| { workspace.update(&mut cx, |workspace, cx| {
cx.on_release(move |_, _| { cx.on_release(move |_, _, _| {
let _ = done_tx.send(()); let _ = done_tx.send(());
}) })
}); });