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
|
@ -3,20 +3,22 @@ use std::ops::Deref;
|
|||
use futures::channel::oneshot;
|
||||
|
||||
use crate::{
|
||||
div, opaque_grey, white, AnyView, EventEmitter, FocusHandle, FocusableView, InteractiveElement,
|
||||
IntoElement, ParentElement, PromptLevel, Render, StatefulInteractiveElement, Styled, View,
|
||||
ViewContext, VisualContext, WindowContext,
|
||||
div, opaque_grey, white, AnyView, App, AppContext as _, Context, Entity, EventEmitter,
|
||||
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, PromptLevel, Render,
|
||||
StatefulInteractiveElement, Styled,
|
||||
};
|
||||
|
||||
use super::Window;
|
||||
|
||||
/// The event emitted when a prompt's option is selected.
|
||||
/// The usize is the index of the selected option, from the actions
|
||||
/// passed to the prompt.
|
||||
pub struct PromptResponse(pub usize);
|
||||
|
||||
/// A prompt that can be rendered in the window.
|
||||
pub trait Prompt: EventEmitter<PromptResponse> + FocusableView {}
|
||||
pub trait Prompt: EventEmitter<PromptResponse> + Focusable {}
|
||||
|
||||
impl<V: EventEmitter<PromptResponse> + FocusableView> Prompt for V {}
|
||||
impl<V: EventEmitter<PromptResponse> + Focusable> Prompt for V {}
|
||||
|
||||
/// A handle to a prompt that can be used to interact with it.
|
||||
pub struct PromptHandle {
|
||||
|
@ -29,25 +31,31 @@ impl PromptHandle {
|
|||
}
|
||||
|
||||
/// Construct a new prompt handle from a view of the appropriate types
|
||||
pub fn with_view<V: Prompt>(
|
||||
pub fn with_view<V: Prompt + Render>(
|
||||
self,
|
||||
view: View<V>,
|
||||
cx: &mut WindowContext,
|
||||
view: Entity<V>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> RenderablePromptHandle {
|
||||
let mut sender = Some(self.sender);
|
||||
let previous_focus = cx.focused();
|
||||
cx.subscribe(&view, move |_, e: &PromptResponse, cx| {
|
||||
let previous_focus = window.focused(cx);
|
||||
let window_handle = window.window_handle();
|
||||
cx.subscribe(&view, move |_: Entity<V>, e: &PromptResponse, cx| {
|
||||
if let Some(sender) = sender.take() {
|
||||
sender.send(e.0).ok();
|
||||
cx.window.prompt.take();
|
||||
if let Some(previous_focus) = &previous_focus {
|
||||
cx.focus(previous_focus);
|
||||
}
|
||||
window_handle
|
||||
.update(cx, |_, window, _cx| {
|
||||
window.prompt.take();
|
||||
if let Some(previous_focus) = &previous_focus {
|
||||
window.focus(previous_focus);
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.focus_view(&view);
|
||||
window.focus(&view.focus_handle(cx));
|
||||
|
||||
RenderablePromptHandle {
|
||||
view: Box::new(view),
|
||||
|
@ -68,19 +76,18 @@ pub fn fallback_prompt_renderer(
|
|||
detail: Option<&str>,
|
||||
actions: &[&str],
|
||||
handle: PromptHandle,
|
||||
cx: &mut WindowContext,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> RenderablePromptHandle {
|
||||
let renderer = cx.new_view({
|
||||
|cx| FallbackPromptRenderer {
|
||||
_level: level,
|
||||
message: message.to_string(),
|
||||
detail: detail.map(ToString::to_string),
|
||||
actions: actions.iter().map(ToString::to_string).collect(),
|
||||
focus: cx.focus_handle(),
|
||||
}
|
||||
let renderer = cx.new(|cx| FallbackPromptRenderer {
|
||||
_level: level,
|
||||
message: message.to_string(),
|
||||
detail: detail.map(ToString::to_string),
|
||||
actions: actions.iter().map(ToString::to_string).collect(),
|
||||
focus: cx.focus_handle(),
|
||||
});
|
||||
|
||||
handle.with_view(renderer, cx)
|
||||
handle.with_view(renderer, window, cx)
|
||||
}
|
||||
|
||||
/// The default GPUI fallback for rendering prompts, when the platform doesn't support it.
|
||||
|
@ -93,7 +100,7 @@ pub struct FallbackPromptRenderer {
|
|||
}
|
||||
|
||||
impl Render for FallbackPromptRenderer {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let prompt = div()
|
||||
.cursor_default()
|
||||
.track_focus(&self.focus)
|
||||
|
@ -133,7 +140,7 @@ impl Render for FallbackPromptRenderer {
|
|||
.text_sm()
|
||||
.child(action.clone())
|
||||
.id(ix)
|
||||
.on_click(cx.listener(move |_, _, cx| {
|
||||
.on_click(cx.listener(move |_, _, _, cx| {
|
||||
cx.emit(PromptResponse(ix));
|
||||
}))
|
||||
}));
|
||||
|
@ -171,8 +178,8 @@ impl Render for FallbackPromptRenderer {
|
|||
|
||||
impl EventEmitter<PromptResponse> for FallbackPromptRenderer {}
|
||||
|
||||
impl FocusableView for FallbackPromptRenderer {
|
||||
fn focus_handle(&self, _: &crate::AppContext) -> FocusHandle {
|
||||
impl Focusable for FallbackPromptRenderer {
|
||||
fn focus_handle(&self, _: &crate::App) -> FocusHandle {
|
||||
self.focus.clone()
|
||||
}
|
||||
}
|
||||
|
@ -181,7 +188,7 @@ pub(crate) trait PromptViewHandle {
|
|||
fn any_view(&self) -> AnyView;
|
||||
}
|
||||
|
||||
impl<V: Prompt> PromptViewHandle for View<V> {
|
||||
impl<V: Prompt + Render> PromptViewHandle for Entity<V> {
|
||||
fn any_view(&self) -> AnyView {
|
||||
self.clone().into()
|
||||
}
|
||||
|
@ -197,7 +204,8 @@ pub(crate) enum PromptBuilder {
|
|||
Option<&str>,
|
||||
&[&str],
|
||||
PromptHandle,
|
||||
&mut WindowContext,
|
||||
&mut Window,
|
||||
&mut App,
|
||||
) -> RenderablePromptHandle,
|
||||
>,
|
||||
),
|
||||
|
@ -210,7 +218,8 @@ impl Deref for PromptBuilder {
|
|||
Option<&str>,
|
||||
&[&str],
|
||||
PromptHandle,
|
||||
&mut WindowContext,
|
||||
&mut Window,
|
||||
&mut App,
|
||||
) -> RenderablePromptHandle;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue