Eliminate GPUI View, ViewContext, and WindowContext types (#22632)
There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
This commit is contained in:
parent
21b4a0d50e
commit
6fca1d2b0b
648 changed files with 36248 additions and 28208 deletions
|
@ -1,4 +1,4 @@
|
|||
use gpui::{AnyView, DismissEvent, FocusHandle, ManagedView, Subscription, View};
|
||||
use gpui::{AnyView, DismissEvent, Entity, FocusHandle, Focusable as _, ManagedView, Subscription};
|
||||
use ui::prelude::*;
|
||||
|
||||
pub enum DismissDecision {
|
||||
|
@ -7,7 +7,11 @@ pub enum DismissDecision {
|
|||
}
|
||||
|
||||
pub trait ModalView: ManagedView {
|
||||
fn on_before_dismiss(&mut self, _: &mut ViewContext<Self>) -> DismissDecision {
|
||||
fn on_before_dismiss(
|
||||
&mut self,
|
||||
_window: &mut Window,
|
||||
_: &mut Context<Self>,
|
||||
) -> DismissDecision {
|
||||
DismissDecision::Dismiss(true)
|
||||
}
|
||||
|
||||
|
@ -17,21 +21,21 @@ pub trait ModalView: ManagedView {
|
|||
}
|
||||
|
||||
trait ModalViewHandle {
|
||||
fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> DismissDecision;
|
||||
fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision;
|
||||
fn view(&self) -> AnyView;
|
||||
fn fade_out_background(&self, cx: &WindowContext) -> bool;
|
||||
fn fade_out_background(&self, cx: &mut App) -> bool;
|
||||
}
|
||||
|
||||
impl<V: ModalView> ModalViewHandle for View<V> {
|
||||
fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> DismissDecision {
|
||||
self.update(cx, |this, cx| this.on_before_dismiss(cx))
|
||||
impl<V: ModalView> ModalViewHandle for Entity<V> {
|
||||
fn on_before_dismiss(&mut self, window: &mut Window, cx: &mut App) -> DismissDecision {
|
||||
self.update(cx, |this, cx| this.on_before_dismiss(window, cx))
|
||||
}
|
||||
|
||||
fn view(&self) -> AnyView {
|
||||
self.clone().into()
|
||||
}
|
||||
|
||||
fn fade_out_background(&self, cx: &WindowContext) -> bool {
|
||||
fn fade_out_background(&self, cx: &mut App) -> bool {
|
||||
self.read(cx).fade_out_background()
|
||||
}
|
||||
}
|
||||
|
@ -62,23 +66,23 @@ impl ModalLayer {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
|
||||
pub fn toggle_modal<V, B>(&mut self, window: &mut Window, cx: &mut Context<Self>, build_view: B)
|
||||
where
|
||||
V: ModalView,
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
B: FnOnce(&mut Window, &mut Context<V>) -> V,
|
||||
{
|
||||
if let Some(active_modal) = &self.active_modal {
|
||||
let is_close = active_modal.modal.view().downcast::<V>().is_ok();
|
||||
let did_close = self.hide_modal(cx);
|
||||
let did_close = self.hide_modal(window, cx);
|
||||
if is_close || !did_close {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let new_modal = cx.new_view(build_view);
|
||||
self.show_modal(new_modal, cx);
|
||||
let new_modal = cx.new(|cx| build_view(window, cx));
|
||||
self.show_modal(new_modal, window, cx);
|
||||
}
|
||||
|
||||
fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
|
||||
fn show_modal<V>(&mut self, new_modal: Entity<V>, window: &mut Window, cx: &mut Context<Self>)
|
||||
where
|
||||
V: ModalView,
|
||||
{
|
||||
|
@ -86,31 +90,35 @@ impl ModalLayer {
|
|||
self.active_modal = Some(ActiveModal {
|
||||
modal: Box::new(new_modal.clone()),
|
||||
_subscriptions: [
|
||||
cx.subscribe(&new_modal, |this, _, _: &DismissEvent, cx| {
|
||||
this.hide_modal(cx);
|
||||
}),
|
||||
cx.on_focus_out(&focus_handle, |this, _event, cx| {
|
||||
cx.subscribe_in(
|
||||
&new_modal,
|
||||
window,
|
||||
|this, _, _: &DismissEvent, window, cx| {
|
||||
this.hide_modal(window, cx);
|
||||
},
|
||||
),
|
||||
cx.on_focus_out(&focus_handle, window, |this, _event, window, cx| {
|
||||
if this.dismiss_on_focus_lost {
|
||||
this.hide_modal(cx);
|
||||
this.hide_modal(window, cx);
|
||||
}
|
||||
}),
|
||||
],
|
||||
previous_focus_handle: cx.focused(),
|
||||
previous_focus_handle: window.focused(cx),
|
||||
focus_handle,
|
||||
});
|
||||
cx.defer(move |_, cx| {
|
||||
cx.focus_view(&new_modal);
|
||||
cx.defer_in(window, move |_, window, cx| {
|
||||
window.focus(&new_modal.focus_handle(cx));
|
||||
});
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
|
||||
fn hide_modal(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
|
||||
let Some(active_modal) = self.active_modal.as_mut() else {
|
||||
self.dismiss_on_focus_lost = false;
|
||||
return false;
|
||||
};
|
||||
|
||||
match active_modal.modal.on_before_dismiss(cx) {
|
||||
match active_modal.modal.on_before_dismiss(window, cx) {
|
||||
DismissDecision::Dismiss(dismiss) => {
|
||||
self.dismiss_on_focus_lost = !dismiss;
|
||||
if !dismiss {
|
||||
|
@ -125,8 +133,8 @@ impl ModalLayer {
|
|||
|
||||
if let Some(active_modal) = self.active_modal.take() {
|
||||
if let Some(previous_focus) = active_modal.previous_focus_handle {
|
||||
if active_modal.focus_handle.contains_focused(cx) {
|
||||
previous_focus.focus(cx);
|
||||
if active_modal.focus_handle.contains_focused(window, cx) {
|
||||
previous_focus.focus(window);
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
|
@ -134,7 +142,7 @@ impl ModalLayer {
|
|||
true
|
||||
}
|
||||
|
||||
pub fn active_modal<V>(&self) -> Option<View<V>>
|
||||
pub fn active_modal<V>(&self) -> Option<Entity<V>>
|
||||
where
|
||||
V: 'static,
|
||||
{
|
||||
|
@ -148,7 +156,7 @@ impl ModalLayer {
|
|||
}
|
||||
|
||||
impl Render for ModalLayer {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let Some(active_modal) = &self.active_modal else {
|
||||
return div();
|
||||
};
|
||||
|
@ -163,8 +171,8 @@ impl Render for ModalLayer {
|
|||
background.fade_out(0.2);
|
||||
el.bg(background)
|
||||
.occlude()
|
||||
.on_mouse_down_out(cx.listener(|this, _, cx| {
|
||||
this.hide_modal(cx);
|
||||
.on_mouse_down_out(cx.listener(|this, _, window, cx| {
|
||||
this.hide_modal(window, cx);
|
||||
}))
|
||||
})
|
||||
.child(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue