From c3094b7c3de560b6ee9f5e07b1792c910d43204d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 15 Nov 2023 09:45:23 +0100 Subject: [PATCH] Introduce gpui::render_view --- crates/editor2/src/editor.rs | 13 +++--- crates/gpui2/src/style.rs | 4 +- crates/gpui2/src/view.rs | 82 +++++++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index b37c5e5756b8bdd760de001b50b88fd39b2a6d48..e5c7b0e4a2109a6d10f4859405ef354528bce8b2 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -39,9 +39,9 @@ use futures::FutureExt; use fuzzy::{StringMatch, StringMatchCandidate}; use git::diff_hunk_to_display; use gpui::{ - action, actions, div, point, px, relative, rems, size, uniform_list, AnyElement, AppContext, - AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Component, Context, Entity, - EventEmitter, FocusHandle, FontFeatures, FontStyle, FontWeight, HighlightStyle, Hsla, + action, actions, div, point, px, relative, rems, render_view, size, uniform_list, AnyElement, + AppContext, AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Component, Context, + Entity, EventEmitter, FocusHandle, FontFeatures, FontStyle, FontWeight, HighlightStyle, Hsla, InputHandler, KeyContext, Model, MouseButton, ParentElement, Pixels, Render, StatefulInteractive, StatelessInteractive, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, View, ViewContext, VisualContext, WeakView, WindowContext, @@ -7806,7 +7806,8 @@ impl Editor { } div() .pl(cx.anchor_x) - .child(with_view(&rename_editor, |_, _| { + .child(render_view( + &rename_editor, EditorElement::new(EditorStyle { background: cx.theme().system().transparent, local_player: cx.editor_style.local_player, @@ -7817,8 +7818,8 @@ impl Editor { .editor_style .diagnostic_style .clone(), - }) - })) + }), + )) .render() } }), diff --git a/crates/gpui2/src/style.rs b/crates/gpui2/src/style.rs index 0819ba9255e65a1645cb751fc8d2c99f4fe70824..20972ca84655fcf1f8fe8d1d7137a4a22003787d 100644 --- a/crates/gpui2/src/style.rs +++ b/crates/gpui2/src/style.rs @@ -1,8 +1,8 @@ use crate::{ black, phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, CornersRefinement, CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font, - FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Result, - Rgba, SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext, WindowContext, + FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rgba, + SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext, WindowContext, }; use refineable::{Cascade, Refineable}; use smallvec::SmallVec; diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index d12d84f43b424f83c5785d7dd0b33c776054bbb3..38120a3e37919e8e6ddac33214f2098bdaa06602 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -1,7 +1,7 @@ use crate::{ private::Sealed, AnyBox, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace, - Bounds, Component, Element, ElementId, Entity, EntityId, Flatten, LayoutId, Model, Pixels, - Size, ViewContext, VisualContext, WeakModel, WindowContext, + BorrowWindow, Bounds, Component, Element, ElementId, Entity, EntityId, Flatten, LayoutId, + Model, Pixels, Size, ViewContext, VisualContext, WeakModel, WindowContext, }; use anyhow::{Context, Result}; use std::{ @@ -281,6 +281,84 @@ where } } +pub struct RenderView { + view: View, + component: Option, +} + +impl Component for RenderView +where + C: 'static + Component, + ParentViewState: 'static, + ViewState: 'static, +{ + fn render(self) -> AnyElement { + AnyElement::new(self) + } +} + +impl Element for RenderView +where + C: 'static + Component, + ParentViewState: 'static, + ViewState: 'static, +{ + type ElementState = AnyElement; + + fn id(&self) -> Option { + Some(self.view.entity_id().into()) + } + + fn initialize( + &mut self, + _: &mut ParentViewState, + _: Option, + cx: &mut ViewContext, + ) -> Self::ElementState { + cx.with_element_id(self.view.entity_id(), |_, cx| { + self.view.update(cx, |view, cx| { + let mut element = self.component.take().unwrap().render(); + element.initialize(view, cx); + element + }) + }) + } + + fn layout( + &mut self, + _: &mut ParentViewState, + element: &mut Self::ElementState, + cx: &mut ViewContext, + ) -> LayoutId { + cx.with_element_id(self.view.entity_id(), |_, cx| { + self.view.update(cx, |view, cx| element.layout(view, cx)) + }) + } + + fn paint( + &mut self, + _: Bounds, + _: &mut ParentViewState, + element: &mut Self::ElementState, + cx: &mut ViewContext, + ) { + cx.with_element_id(self.view.entity_id(), |_, cx| { + self.view.update(cx, |view, cx| element.paint(view, cx)) + }) + } +} + +pub fn render_view(view: &View, component: C) -> RenderView +where + C: 'static + Component, + V: 'static, +{ + RenderView { + view: view.clone(), + component: Some(component), + } +} + mod any_view { use crate::{AnyElement, AnyView, BorrowWindow, LayoutId, Render, WindowContext}; use std::any::Any;