Incorporate ElementId as part of the Element::id trait method and expose GlobalId (#11101)

We're planning to associate "selection sources" with global element ids
to allow arbitrary UI text to be selected in GPUI. Previously, global
ids were not exposed outside the framework and we entangled management
of the element id stack with element state access. This was more
acceptable when element state was the only place we used global element
ids, but now that we're planning to use them more places, it makes sense
to deal with element identity as a first-class part of the element
system. We now ensure that the stack of element ids which forms the
current global element id is correctly managed in every phase of element
layout and paint and make the global id available to each element
method. In a subsequent PR, we'll use the global element id as part of
implementing arbitrary selection for UI text.

Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2024-04-28 12:59:21 -07:00 committed by GitHub
parent 8b55494351
commit 39fb1d567d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 825 additions and 497 deletions

View file

@ -1,11 +1,11 @@
use editor::{CursorLayout, HighlightedRange, HighlightedRangeLine};
use gpui::{
div, fill, point, px, relative, AnyElement, Bounds, DispatchPhase, Element, FocusHandle, Font,
FontStyle, FontWeight, HighlightStyle, Hitbox, Hsla, InputHandler, InteractiveElement,
Interactivity, IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton,
MouseMoveEvent, Pixels, Point, ShapedLine, StatefulInteractiveElement, StrikethroughStyle,
Styled, TextRun, TextStyle, UnderlineStyle, WeakView, WhiteSpace, WindowContext,
WindowTextSystem,
div, fill, point, px, relative, AnyElement, Bounds, DispatchPhase, Element, ElementId,
FocusHandle, Font, FontStyle, FontWeight, GlobalElementId, HighlightStyle, Hitbox, Hsla,
InputHandler, InteractiveElement, Interactivity, IntoElement, LayoutId, Model, ModelContext,
ModifiersChangedEvent, MouseButton, MouseMoveEvent, Pixels, Point, ShapedLine,
StatefulInteractiveElement, StrikethroughStyle, Styled, TextRun, TextStyle, UnderlineStyle,
WeakView, WhiteSpace, WindowContext, WindowTextSystem,
};
use itertools::Itertools;
use language::CursorShape;
@ -544,26 +544,37 @@ impl Element for TerminalElement {
type RequestLayoutState = ();
type PrepaintState = LayoutState;
fn request_layout(&mut self, cx: &mut WindowContext) -> (LayoutId, Self::RequestLayoutState) {
self.interactivity.occlude_mouse();
let layout_id = self.interactivity.request_layout(cx, |mut style, cx| {
style.size.width = relative(1.).into();
style.size.height = relative(1.).into();
let layout_id = cx.request_layout(&style, None);
fn id(&self) -> Option<ElementId> {
self.interactivity.element_id.clone()
}
layout_id
});
fn request_layout(
&mut self,
global_id: Option<&GlobalElementId>,
cx: &mut WindowContext,
) -> (LayoutId, Self::RequestLayoutState) {
self.interactivity.occlude_mouse();
let layout_id = self
.interactivity
.request_layout(global_id, cx, |mut style, cx| {
style.size.width = relative(1.).into();
style.size.height = relative(1.).into();
let layout_id = cx.request_layout(&style, None);
layout_id
});
(layout_id, ())
}
fn prepaint(
&mut self,
global_id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
cx: &mut WindowContext,
) -> Self::PrepaintState {
self.interactivity
.prepaint(bounds, bounds.size, cx, |_, _, hitbox, cx| {
.prepaint(global_id, bounds, bounds.size, cx, |_, _, hitbox, cx| {
let hitbox = hitbox.unwrap();
let settings = ThemeSettings::get_global(cx).clone();
@ -775,6 +786,7 @@ impl Element for TerminalElement {
fn paint(
&mut self,
global_id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
layout: &mut Self::PrepaintState,
@ -802,7 +814,7 @@ impl Element for TerminalElement {
let cursor = layout.cursor.take();
let hyperlink_tooltip = layout.hyperlink_tooltip.take();
self.interactivity
.paint(bounds, Some(&layout.hitbox), cx, |_, cx| {
.paint(global_id, bounds, Some(&layout.hitbox), cx, |_, cx| {
cx.handle_input(&self.focus, terminal_input_handler);
cx.on_key_event({