Introduce a new WindowContext::remove_window API

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-04-21 16:39:53 +02:00
parent f12746c4b7
commit 8c7f821d14
8 changed files with 25 additions and 29 deletions

View file

@ -94,14 +94,12 @@ impl ProjectSharedNotification {
} }
fn join(&mut self, _: &JoinProject, cx: &mut ViewContext<Self>) { fn join(&mut self, _: &JoinProject, cx: &mut ViewContext<Self>) {
let window_id = cx.window_id(); cx.remove_window();
cx.remove_window(window_id);
cx.propagate_action(); cx.propagate_action();
} }
fn dismiss(&mut self, _: &DismissProject, cx: &mut ViewContext<Self>) { fn dismiss(&mut self, _: &DismissProject, cx: &mut ViewContext<Self>) {
let window_id = cx.window_id(); cx.remove_window();
cx.remove_window(window_id);
} }
fn render_owner(&self, cx: &mut ViewContext<Self>) -> Element<Self> { fn render_owner(&self, cx: &mut ViewContext<Self>) -> Element<Self> {

View file

@ -273,10 +273,7 @@ impl CopilotCodeVerification {
style.auth.content_width, style.auth.content_width,
&style.auth.cta_button, &style.auth.cta_button,
cx, cx,
|_, _, cx| { |_, _, cx| cx.remove_window(),
let window_id = cx.window_id();
cx.remove_window(window_id)
},
) )
.boxed(), .boxed(),
]) ])
@ -335,8 +332,7 @@ impl CopilotCodeVerification {
&style.auth.cta_button, &style.auth.cta_button,
cx, cx,
|_, _, cx| { |_, _, cx| {
let window_id = cx.window_id(); cx.remove_window();
cx.remove_window(window_id);
cx.platform().open_url(COPILOT_SIGN_UP_URL) cx.platform().open_url(COPILOT_SIGN_UP_URL)
}, },
) )

View file

@ -868,7 +868,9 @@ impl AppContext {
let mut window = app_context.windows.remove(&window_id)?; let mut window = app_context.windows.remove(&window_id)?;
let mut window_context = WindowContext::mutable(app_context, &mut window, window_id); let mut window_context = WindowContext::mutable(app_context, &mut window, window_id);
let result = callback(&mut window_context); let result = callback(&mut window_context);
app_context.windows.insert(window_id, window); if !window_context.removed {
app_context.windows.insert(window_id, window);
}
Some(result) Some(result)
}) })
} }

View file

@ -116,6 +116,7 @@ pub struct WindowContext<'a: 'b, 'b> {
pub(crate) window: Reference<'b, Window>, pub(crate) window: Reference<'b, Window>,
pub(crate) window_id: usize, pub(crate) window_id: usize,
pub(crate) refreshing: bool, pub(crate) refreshing: bool,
pub(crate) removed: bool,
} }
impl Deref for WindowContext<'_, '_> { impl Deref for WindowContext<'_, '_> {
@ -216,6 +217,7 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> {
window: Reference::Mutable(window), window: Reference::Mutable(window),
window_id, window_id,
refreshing: false, refreshing: false,
removed: false,
} }
} }
@ -225,9 +227,14 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> {
window: Reference::Immutable(window), window: Reference::Immutable(window),
window_id, window_id,
refreshing: false, refreshing: false,
removed: false,
} }
} }
pub fn remove_window(&mut self) {
self.removed = true;
}
pub fn window_id(&self) -> usize { pub fn window_id(&self) -> usize {
self.window_id self.window_id
} }

View file

@ -265,8 +265,7 @@ where
icon(style).boxed() icon(style).boxed()
}) })
.on_click(platform::MouseButton::Left, move |_, _, cx| { .on_click(platform::MouseButton::Left, move |_, _, cx| {
let window_id = cx.window_id(); cx.remove_window();
cx.remove_window(window_id);
}) })
.with_cursor_style(platform::CursorStyle::PointingHand) .with_cursor_style(platform::CursorStyle::PointingHand)
.aligned() .aligned()

View file

@ -1741,8 +1741,6 @@ impl View for Pane {
} }
fn render(&mut self, cx: &mut ViewContext<Self>) -> Element<Self> { fn render(&mut self, cx: &mut ViewContext<Self>) -> Element<Self> {
let this = cx.handle().downgrade();
enum MouseNavigationHandler {} enum MouseNavigationHandler {}
Stack::new() Stack::new()
@ -1836,20 +1834,17 @@ impl View for Pane {
.boxed() .boxed()
} }
}) })
.on_down(MouseButton::Navigate(NavigationDirection::Back), { .on_down(
let this = this.clone(); MouseButton::Navigate(NavigationDirection::Back),
move |_, _, cx| { move |_, _, cx| {
cx.dispatch_action(GoBack { let pane = cx.weak_handle();
pane: Some(this.clone()), cx.dispatch_action(GoBack { pane: Some(pane) });
}); },
} )
})
.on_down(MouseButton::Navigate(NavigationDirection::Forward), { .on_down(MouseButton::Navigate(NavigationDirection::Forward), {
let this = this.clone();
move |_, _, cx| { move |_, _, cx| {
cx.dispatch_action(GoForward { let pane = cx.weak_handle();
pane: Some(this.clone()), cx.dispatch_action(GoForward { pane: Some(pane) })
})
} }
}) })
.boxed(), .boxed(),

View file

@ -74,8 +74,8 @@ where
.boxed() .boxed()
}) })
.on_up(MouseButton::Left, { .on_up(MouseButton::Left, {
let pane = cx.handle().downgrade();
move |event, _, cx| { move |event, _, cx| {
let pane = cx.weak_handle();
handle_dropped_item(event, &pane, drop_index, allow_same_pane, split_margin, cx); handle_dropped_item(event, &pane, drop_index, allow_same_pane, split_margin, cx);
cx.notify(); cx.notify();
} }

View file

@ -700,8 +700,7 @@ impl Workspace {
} }
project::Event::Closed => { project::Event::Closed => {
let window_id = cx.window_id(); cx.remove_window();
cx.remove_window(window_id);
} }
_ => {} _ => {}