Port the rest of the app

This commit is contained in:
Mikayla 2024-01-21 20:26:33 -08:00
parent df4566fd1e
commit c05edee2b5
No known key found for this signature in database
11 changed files with 207 additions and 164 deletions

View file

@ -1584,27 +1584,34 @@ mod tests {
} }
fn editor_blocks(editor: &View<Editor>, cx: &mut WindowContext) -> Vec<(u32, SharedString)> { fn editor_blocks(editor: &View<Editor>, cx: &mut WindowContext) -> Vec<(u32, SharedString)> {
let editor_view = editor.clone();
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
snapshot snapshot
.blocks_in_range(0..snapshot.max_point().row()) .blocks_in_range(0..snapshot.max_point().row())
.enumerate() .enumerate()
.filter_map(|(ix, (row, block))| { .filter_map(|(ix, (row, block))| {
let name = match block { let name: SharedString = match block {
TransformBlock::Custom(block) => block TransformBlock::Custom(block) => cx.with_element_context({
let editor_view = editor_view.clone();
|cx| -> Option<SharedString> {
block
.render(&mut BlockContext { .render(&mut BlockContext {
view_context: cx, context: cx,
anchor_x: px(0.), anchor_x: px(0.),
gutter_padding: px(0.), gutter_padding: px(0.),
gutter_width: px(0.), gutter_width: px(0.),
line_height: px(0.), line_height: px(0.),
em_width: px(0.), em_width: px(0.),
block_id: ix, block_id: ix,
view: editor_view,
editor_style: &editor::EditorStyle::default(), editor_style: &editor::EditorStyle::default(),
}) })
.inner_id()? .inner_id()?
.try_into() .try_into()
.ok()?, .ok()
}
})?,
TransformBlock::ExcerptHeader { TransformBlock::ExcerptHeader {
starts_new_buffer, .. starts_new_buffer, ..

View file

@ -4,7 +4,7 @@ use super::{
}; };
use crate::{Anchor, Editor, EditorStyle, ExcerptId, ExcerptRange, ToPoint as _}; use crate::{Anchor, Editor, EditorStyle, ExcerptId, ExcerptRange, ToPoint as _};
use collections::{Bound, HashMap, HashSet}; use collections::{Bound, HashMap, HashSet};
use gpui::{AnyElement, Pixels, ViewContext}; use gpui::{AnyElement, ElementContext, Pixels, View};
use language::{BufferSnapshot, Chunk, Patch, Point}; use language::{BufferSnapshot, Chunk, Patch, Point};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
@ -81,7 +81,8 @@ pub enum BlockStyle {
} }
pub struct BlockContext<'a, 'b> { pub struct BlockContext<'a, 'b> {
pub view_context: &'b mut ViewContext<'a, Editor>, pub context: &'b mut ElementContext<'a>,
pub view: View<Editor>,
pub anchor_x: Pixels, pub anchor_x: Pixels,
pub gutter_width: Pixels, pub gutter_width: Pixels,
pub gutter_padding: Pixels, pub gutter_padding: Pixels,
@ -933,16 +934,16 @@ impl BlockDisposition {
} }
impl<'a> Deref for BlockContext<'a, '_> { impl<'a> Deref for BlockContext<'a, '_> {
type Target = ViewContext<'a, Editor>; type Target = ElementContext<'a>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
self.view_context self.context
} }
} }
impl DerefMut for BlockContext<'_, '_> { impl DerefMut for BlockContext<'_, '_> {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
self.view_context self.context
} }
} }

View file

@ -3912,7 +3912,7 @@ impl Editor {
gutter_hovered: bool, gutter_hovered: bool,
_line_height: Pixels, _line_height: Pixels,
_gutter_margin: Pixels, _gutter_margin: Pixels,
cx: &mut ViewContext<Self>, editor_view: View<Editor>,
) -> Vec<Option<IconButton>> { ) -> Vec<Option<IconButton>> {
fold_data fold_data
.iter() .iter()
@ -3922,14 +3922,19 @@ impl Editor {
.map(|(fold_status, buffer_row, active)| { .map(|(fold_status, buffer_row, active)| {
(active || gutter_hovered || fold_status == FoldStatus::Folded).then(|| { (active || gutter_hovered || fold_status == FoldStatus::Folded).then(|| {
IconButton::new(ix as usize, ui::IconName::ChevronDown) IconButton::new(ix as usize, ui::IconName::ChevronDown)
.on_click(cx.listener(move |editor, _e, cx| match fold_status { .on_click({
let view = editor_view.clone();
move |_e, cx| {
view.update(cx, |editor, cx| match fold_status {
FoldStatus::Folded => { FoldStatus::Folded => {
editor.unfold_at(&UnfoldAt { buffer_row }, cx); editor.unfold_at(&UnfoldAt { buffer_row }, cx);
} }
FoldStatus::Foldable => { FoldStatus::Foldable => {
editor.fold_at(&FoldAt { buffer_row }, cx); editor.fold_at(&FoldAt { buffer_row }, cx);
} }
})) })
}
})
.icon_color(ui::Color::Muted) .icon_color(ui::Color::Muted)
.icon_size(ui::IconSize::Small) .icon_size(ui::IconSize::Small)
.selected(fold_status == FoldStatus::Folded) .selected(fold_status == FoldStatus::Folded)
@ -9575,10 +9580,10 @@ pub fn diagnostic_block_renderer(diagnostic: Diagnostic, _is_valid: bool) -> Ren
.size(ButtonSize::Compact) .size(ButtonSize::Compact)
.style(ButtonStyle::Transparent) .style(ButtonStyle::Transparent)
.visible_on_hover(group_id) .visible_on_hover(group_id)
.on_click(cx.listener({ .on_click({
let message = diagnostic.message.clone(); let message = diagnostic.message.clone();
move |_, _, cx| cx.write_to_clipboard(ClipboardItem::new(message.clone())) move |_click, cx| cx.write_to_clipboard(ClipboardItem::new(message.clone()))
})) })
.tooltip(|cx| Tooltip::text("Copy diagnostic message", cx)), .tooltip(|cx| Tooltip::text("Copy diagnostic message", cx)),
) )
.into_any_element() .into_any_element()

View file

@ -25,12 +25,12 @@ use collections::{BTreeMap, HashMap};
use git::diff::DiffHunkStatus; use git::diff::DiffHunkStatus;
use gpui::{ use gpui::{
div, fill, outline, overlay, point, px, quad, relative, size, transparent_black, Action, div, fill, outline, overlay, point, px, quad, relative, size, transparent_black, Action,
AnchorCorner, AnyElement, AvailableSpace, BorrowWindow, Bounds, ContentMask, Corners, AnchorCorner, AnyElement, AvailableSpace, Bounds, ContentMask, Corners, CursorStyle,
CursorStyle, DispatchPhase, Edges, Element, ElementInputHandler, Entity, Hsla, DispatchPhase, Edges, Element, ElementInputHandler, Entity, Hsla, InteractiveBounds,
InteractiveBounds, InteractiveElement, IntoElement, ModifiersChangedEvent, MouseButton, InteractiveElement, IntoElement, ModifiersChangedEvent, MouseButton, MouseDownEvent,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, ScrollDelta, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, ScrollDelta, ScrollWheelEvent, ShapedLine,
ScrollWheelEvent, ShapedLine, SharedString, Size, StackingOrder, StatefulInteractiveElement, SharedString, Size, StackingOrder, StatefulInteractiveElement, Style, Styled, TextRun,
Style, Styled, TextRun, TextStyle, View, ViewContext, WindowContext, TextStyle, View, ViewContext, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting; use language::language_settings::ShowWhitespaceSetting;
@ -330,7 +330,7 @@ impl EditorElement {
register_action(view, cx, Editor::display_cursor_names); register_action(view, cx, Editor::display_cursor_names);
} }
fn register_key_listeners(&self, cx: &mut WindowContext) { fn register_key_listeners(&self, cx: &mut ElementContext) {
cx.on_key_event({ cx.on_key_event({
let editor = self.editor.clone(); let editor = self.editor.clone();
move |event: &ModifiersChangedEvent, phase, cx| { move |event: &ModifiersChangedEvent, phase, cx| {
@ -628,7 +628,7 @@ impl EditorElement {
gutter_bounds: Bounds<Pixels>, gutter_bounds: Bounds<Pixels>,
text_bounds: Bounds<Pixels>, text_bounds: Bounds<Pixels>,
layout: &LayoutState, layout: &LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let bounds = gutter_bounds.union(&text_bounds); let bounds = gutter_bounds.union(&text_bounds);
let scroll_top = let scroll_top =
@ -711,7 +711,7 @@ impl EditorElement {
&mut self, &mut self,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
layout: &mut LayoutState, layout: &mut LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let line_height = layout.position_map.line_height; let line_height = layout.position_map.line_height;
@ -782,7 +782,7 @@ impl EditorElement {
}); });
} }
fn paint_diff_hunks(bounds: Bounds<Pixels>, layout: &LayoutState, cx: &mut WindowContext) { fn paint_diff_hunks(bounds: Bounds<Pixels>, layout: &LayoutState, cx: &mut ElementContext) {
let line_height = layout.position_map.line_height; let line_height = layout.position_map.line_height;
let scroll_position = layout.position_map.snapshot.scroll_position(); let scroll_position = layout.position_map.snapshot.scroll_position();
@ -886,7 +886,7 @@ impl EditorElement {
&mut self, &mut self,
text_bounds: Bounds<Pixels>, text_bounds: Bounds<Pixels>,
layout: &mut LayoutState, layout: &mut LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let start_row = layout.visible_display_row_range.start; let start_row = layout.visible_display_row_range.start;
let content_origin = text_bounds.origin + point(layout.gutter_margin, Pixels::ZERO); let content_origin = text_bounds.origin + point(layout.gutter_margin, Pixels::ZERO);
@ -1153,7 +1153,7 @@ impl EditorElement {
&mut self, &mut self,
text_bounds: Bounds<Pixels>, text_bounds: Bounds<Pixels>,
layout: &mut LayoutState, layout: &mut LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let content_origin = text_bounds.origin + point(layout.gutter_margin, Pixels::ZERO); let content_origin = text_bounds.origin + point(layout.gutter_margin, Pixels::ZERO);
let start_row = layout.visible_display_row_range.start; let start_row = layout.visible_display_row_range.start;
@ -1268,7 +1268,7 @@ impl EditorElement {
&mut self, &mut self,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
layout: &mut LayoutState, layout: &mut LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
if layout.mode != EditorMode::Full { if layout.mode != EditorMode::Full {
return; return;
@ -1512,7 +1512,7 @@ impl EditorElement {
layout: &LayoutState, layout: &LayoutState,
content_origin: gpui::Point<Pixels>, content_origin: gpui::Point<Pixels>,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let start_row = layout.visible_display_row_range.start; let start_row = layout.visible_display_row_range.start;
let end_row = layout.visible_display_row_range.end; let end_row = layout.visible_display_row_range.end;
@ -1564,7 +1564,7 @@ impl EditorElement {
&mut self, &mut self,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
layout: &mut LayoutState, layout: &mut LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let scroll_position = layout.position_map.snapshot.scroll_position(); let scroll_position = layout.position_map.snapshot.scroll_position();
let scroll_left = scroll_position.x * layout.position_map.em_width; let scroll_left = scroll_position.x * layout.position_map.em_width;
@ -1814,7 +1814,7 @@ impl EditorElement {
} }
} }
fn compute_layout(&mut self, bounds: Bounds<Pixels>, cx: &mut WindowContext) -> LayoutState { fn compute_layout(&mut self, bounds: Bounds<Pixels>, cx: &mut ElementContext) -> LayoutState {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let style = self.style.clone(); let style = self.style.clone();
@ -2083,7 +2083,9 @@ impl EditorElement {
.width; .width;
let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.width; let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.width;
let (scroll_width, blocks) = cx.with_element_id(Some("editor_blocks"), |cx| { let editor_view = cx.view().clone();
let (scroll_width, blocks) = cx.with_element_context(|cx| {
cx.with_element_id(Some("editor_blocks"), |cx| {
self.layout_blocks( self.layout_blocks(
start_row..end_row, start_row..end_row,
&snapshot, &snapshot,
@ -2097,8 +2099,10 @@ impl EditorElement {
&style, &style,
&line_layouts, &line_layouts,
editor, editor,
editor_view,
cx, cx,
) )
})
}); });
let scroll_max = point( let scroll_max = point(
@ -2174,15 +2178,19 @@ impl EditorElement {
cx, cx,
); );
let fold_indicators = cx.with_element_id(Some("gutter_fold_indicators"), |cx| { let editor_view = cx.view().clone();
let fold_indicators = cx.with_element_context(|cx| {
cx.with_element_id(Some("gutter_fold_indicators"), |_cx| {
editor.render_fold_indicators( editor.render_fold_indicators(
fold_statuses, fold_statuses,
&style, &style,
editor.gutter_hovered, editor.gutter_hovered,
line_height, line_height,
gutter_margin, gutter_margin,
cx, editor_view,
) )
})
}); });
let invisible_symbol_font_size = font_size / 2.; let invisible_symbol_font_size = font_size / 2.;
@ -2273,7 +2281,8 @@ impl EditorElement {
style: &EditorStyle, style: &EditorStyle,
line_layouts: &[LineWithInvisibles], line_layouts: &[LineWithInvisibles],
editor: &mut Editor, editor: &mut Editor,
cx: &mut ViewContext<Editor>, editor_view: View<Editor>,
cx: &mut ElementContext,
) -> (Pixels, Vec<BlockLayout>) { ) -> (Pixels, Vec<BlockLayout>) {
let mut block_id = 0; let mut block_id = 0;
let (fixed_blocks, non_fixed_blocks) = snapshot let (fixed_blocks, non_fixed_blocks) = snapshot
@ -2287,7 +2296,7 @@ impl EditorElement {
available_space: Size<AvailableSpace>, available_space: Size<AvailableSpace>,
block_id: usize, block_id: usize,
editor: &mut Editor, editor: &mut Editor,
cx: &mut ViewContext<Editor>| { cx: &mut ElementContext| {
let mut element = match block { let mut element = match block {
TransformBlock::Custom(block) => { TransformBlock::Custom(block) => {
let align_to = block let align_to = block
@ -2306,13 +2315,14 @@ impl EditorElement {
}; };
block.render(&mut BlockContext { block.render(&mut BlockContext {
view_context: cx, context: cx,
anchor_x, anchor_x,
gutter_padding, gutter_padding,
line_height, line_height,
gutter_width, gutter_width,
em_width, em_width,
block_id, block_id,
view: editor_view.clone(),
editor_style: &self.style, editor_style: &self.style,
}) })
} }
@ -2504,7 +2514,7 @@ impl EditorElement {
&mut self, &mut self,
interactive_bounds: &InteractiveBounds, interactive_bounds: &InteractiveBounds,
layout: &LayoutState, layout: &LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
cx.on_mouse_event({ cx.on_mouse_event({
let position_map = layout.position_map.clone(); let position_map = layout.position_map.clone();
@ -2564,7 +2574,7 @@ impl EditorElement {
gutter_bounds: Bounds<Pixels>, gutter_bounds: Bounds<Pixels>,
text_bounds: Bounds<Pixels>, text_bounds: Bounds<Pixels>,
layout: &LayoutState, layout: &LayoutState,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let interactive_bounds = InteractiveBounds { let interactive_bounds = InteractiveBounds {
bounds: bounds.intersect(&cx.content_mask().bounds), bounds: bounds.intersect(&cx.content_mask().bounds),
@ -2787,7 +2797,7 @@ impl LineWithInvisibles {
content_origin: gpui::Point<Pixels>, content_origin: gpui::Point<Pixels>,
whitespace_setting: ShowWhitespaceSetting, whitespace_setting: ShowWhitespaceSetting,
selection_ranges: &[Range<DisplayPoint>], selection_ranges: &[Range<DisplayPoint>],
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let line_height = layout.position_map.line_height; let line_height = layout.position_map.line_height;
let line_y = line_height * row as f32 - layout.position_map.scroll_position.y; let line_y = line_height * row as f32 - layout.position_map.scroll_position.y;
@ -2821,7 +2831,7 @@ impl LineWithInvisibles {
row: u32, row: u32,
line_height: Pixels, line_height: Pixels,
whitespace_setting: ShowWhitespaceSetting, whitespace_setting: ShowWhitespaceSetting,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let allowed_invisibles_regions = match whitespace_setting { let allowed_invisibles_regions = match whitespace_setting {
ShowWhitespaceSetting::None => return, ShowWhitespaceSetting::None => return,
@ -2870,7 +2880,7 @@ impl Element for EditorElement {
fn request_layout( fn request_layout(
&mut self, &mut self,
_element_state: Option<Self::State>, _element_state: Option<Self::State>,
cx: &mut gpui::WindowContext, cx: &mut gpui::ElementContext,
) -> (gpui::LayoutId, Self::State) { ) -> (gpui::LayoutId, Self::State) {
cx.with_view_id(self.editor.entity_id(), |cx| { cx.with_view_id(self.editor.entity_id(), |cx| {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
@ -2882,12 +2892,13 @@ impl Element for EditorElement {
let mut style = Style::default(); let mut style = Style::default();
style.size.width = relative(1.).into(); style.size.width = relative(1.).into();
style.size.height = self.style.text.line_height_in_pixels(rem_size).into(); style.size.height = self.style.text.line_height_in_pixels(rem_size).into();
cx.request_layout(&style, None) cx.with_element_context(|cx| cx.request_layout(&style, None))
} }
EditorMode::AutoHeight { max_lines } => { EditorMode::AutoHeight { max_lines } => {
let editor_handle = cx.view().clone(); let editor_handle = cx.view().clone();
let max_line_number_width = let max_line_number_width =
self.max_line_number_width(&editor.snapshot(cx), cx); self.max_line_number_width(&editor.snapshot(cx), cx);
cx.with_element_context(|cx| {
cx.request_measured_layout( cx.request_measured_layout(
Style::default(), Style::default(),
move |known_dimensions, _, cx| { move |known_dimensions, _, cx| {
@ -2904,12 +2915,13 @@ impl Element for EditorElement {
.unwrap_or_default() .unwrap_or_default()
}, },
) )
})
} }
EditorMode::Full => { EditorMode::Full => {
let mut style = Style::default(); let mut style = Style::default();
style.size.width = relative(1.).into(); style.size.width = relative(1.).into();
style.size.height = relative(1.).into(); style.size.height = relative(1.).into();
cx.request_layout(&style, None) cx.with_element_context(|cx| cx.request_layout(&style, None))
} }
}; };
@ -2922,7 +2934,7 @@ impl Element for EditorElement {
&mut self, &mut self,
bounds: Bounds<gpui::Pixels>, bounds: Bounds<gpui::Pixels>,
_element_state: &mut Self::State, _element_state: &mut Self::State,
cx: &mut gpui::WindowContext, cx: &mut gpui::ElementContext,
) { ) {
let editor = self.editor.clone(); let editor = self.editor.clone();
@ -3204,7 +3216,7 @@ impl Cursor {
} }
} }
pub fn paint(&self, origin: gpui::Point<Pixels>, cx: &mut WindowContext) { pub fn paint(&self, origin: gpui::Point<Pixels>, cx: &mut ElementContext) {
let bounds = match self.shape { let bounds = match self.shape {
CursorShape::Bar => Bounds { CursorShape::Bar => Bounds {
origin: self.origin + origin, origin: self.origin + origin,
@ -3284,7 +3296,7 @@ pub struct HighlightedRangeLine {
} }
impl HighlightedRange { impl HighlightedRange {
pub fn paint(&self, bounds: Bounds<Pixels>, cx: &mut WindowContext) { pub fn paint(&self, bounds: Bounds<Pixels>, cx: &mut ElementContext) {
if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x { if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x {
self.paint_lines(self.start_y, &self.lines[0..1], bounds, cx); self.paint_lines(self.start_y, &self.lines[0..1], bounds, cx);
self.paint_lines( self.paint_lines(
@ -3303,7 +3315,7 @@ impl HighlightedRange {
start_y: Pixels, start_y: Pixels,
lines: &[HighlightedRangeLine], lines: &[HighlightedRangeLine],
_bounds: Bounds<Pixels>, _bounds: Bounds<Pixels>,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
if lines.is_empty() { if lines.is_empty() {
return; return;
@ -3521,6 +3533,7 @@ mod tests {
.unwrap(); .unwrap();
let state = cx let state = cx
.update_window(window.into(), |view, cx| { .update_window(window.into(), |view, cx| {
cx.with_element_context(|cx| {
cx.with_view_id(view.entity_id(), |cx| { cx.with_view_id(view.entity_id(), |cx| {
element.compute_layout( element.compute_layout(
Bounds { Bounds {
@ -3531,6 +3544,7 @@ mod tests {
) )
}) })
}) })
})
.unwrap(); .unwrap();
assert_eq!(state.selections.len(), 1); assert_eq!(state.selections.len(), 1);
@ -3615,6 +3629,7 @@ mod tests {
let state = cx let state = cx
.update_window(window.into(), |view, cx| { .update_window(window.into(), |view, cx| {
cx.with_element_context(|cx| {
cx.with_view_id(view.entity_id(), |cx| { cx.with_view_id(view.entity_id(), |cx| {
element.compute_layout( element.compute_layout(
Bounds { Bounds {
@ -3625,6 +3640,7 @@ mod tests {
) )
}) })
}) })
})
.unwrap(); .unwrap();
assert_eq!(state.selections.len(), 1); assert_eq!(state.selections.len(), 1);
let local_selections = &state.selections[0].1; let local_selections = &state.selections[0].1;
@ -3679,6 +3695,7 @@ mod tests {
let mut element = EditorElement::new(&editor, style); let mut element = EditorElement::new(&editor, style);
let state = cx let state = cx
.update_window(window.into(), |view, cx| { .update_window(window.into(), |view, cx| {
cx.with_element_context(|cx| {
cx.with_view_id(view.entity_id(), |cx| { cx.with_view_id(view.entity_id(), |cx| {
element.compute_layout( element.compute_layout(
Bounds { Bounds {
@ -3689,6 +3706,7 @@ mod tests {
) )
}) })
}) })
})
.unwrap(); .unwrap();
let size = state.position_map.size; let size = state.position_map.size;
@ -3704,7 +3722,9 @@ mod tests {
// Don't panic. // Don't panic.
let bounds = Bounds::<Pixels>::new(Default::default(), size); let bounds = Bounds::<Pixels>::new(Default::default(), size);
cx.update_window(window.into(), |_, cx| element.paint(bounds, &mut (), cx)) cx.update_window(window.into(), |_, cx| {
cx.with_element_context(|cx| element.paint(bounds, &mut (), cx))
})
.unwrap() .unwrap()
} }
@ -3880,6 +3900,7 @@ mod tests {
.unwrap(); .unwrap();
let layout_state = cx let layout_state = cx
.update_window(window.into(), |_, cx| { .update_window(window.into(), |_, cx| {
cx.with_element_context(|cx| {
element.compute_layout( element.compute_layout(
Bounds { Bounds {
origin: point(px(500.), px(500.)), origin: point(px(500.), px(500.)),
@ -3888,6 +3909,7 @@ mod tests {
cx, cx,
) )
}) })
})
.unwrap(); .unwrap();
layout_state layout_state

View file

@ -30,10 +30,7 @@ pub struct ElementContext<'a> {
} }
impl<'a> WindowContext<'a> { impl<'a> WindowContext<'a> {
pub(crate) fn with_element_context<R>( pub fn with_element_context<R>(&mut self, f: impl FnOnce(&mut ElementContext) -> R) -> R {
&mut self,
f: impl FnOnce(&mut ElementContext) -> R,
) -> R {
f(&mut ElementContext { f(&mut ElementContext {
cx: WindowContext::new(self.app, self.window), cx: WindowContext::new(self.app, self.window),
}) })
@ -176,7 +173,7 @@ impl<'a> ElementContext<'a> {
/// with a `GlobalElementId`, which disambiguates the given id in the context of its ancestor /// with a `GlobalElementId`, which disambiguates the given id in the context of its ancestor
/// ids. Because elements are discarded and recreated on each frame, the `GlobalElementId` is /// ids. Because elements are discarded and recreated on each frame, the `GlobalElementId` is
/// used to associate state with identified elements across separate frames. /// used to associate state with identified elements across separate frames.
pub(crate) fn with_element_id<R>( pub fn with_element_id<R>(
&mut self, &mut self,
id: Option<impl Into<ElementId>>, id: Option<impl Into<ElementId>>,
f: impl FnOnce(&mut Self) -> R, f: impl FnOnce(&mut Self) -> R,
@ -195,7 +192,7 @@ impl<'a> ElementContext<'a> {
/// Invoke the given function with the given content mask after intersecting it /// Invoke the given function with the given content mask after intersecting it
/// with the current mask. /// with the current mask.
pub(crate) fn with_content_mask<R>( pub fn with_content_mask<R>(
&mut self, &mut self,
mask: Option<ContentMask<Pixels>>, mask: Option<ContentMask<Pixels>>,
f: impl FnOnce(&mut Self) -> R, f: impl FnOnce(&mut Self) -> R,
@ -213,7 +210,7 @@ impl<'a> ElementContext<'a> {
/// Invoke the given function with the content mask reset to that /// Invoke the given function with the content mask reset to that
/// of the window. /// of the window.
pub(crate) fn break_content_mask<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R { pub fn break_content_mask<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
let mask = ContentMask { let mask = ContentMask {
bounds: Bounds { bounds: Bounds {
origin: Point::default(), origin: Point::default(),
@ -238,7 +235,7 @@ impl<'a> ElementContext<'a> {
/// Called during painting to invoke the given closure in a new stacking context. The given /// Called during painting to invoke the given closure in a new stacking context. The given
/// z-index is interpreted relative to the previous call to `stack`. /// z-index is interpreted relative to the previous call to `stack`.
pub(crate) fn with_z_index<R>(&mut self, z_index: u8, f: impl FnOnce(&mut Self) -> R) -> R { pub fn with_z_index<R>(&mut self, z_index: u8, f: impl FnOnce(&mut Self) -> R) -> R {
let new_stacking_order_id = let new_stacking_order_id =
post_inc(&mut self.window_mut().next_frame.next_stacking_order_id); post_inc(&mut self.window_mut().next_frame.next_stacking_order_id);
let old_stacking_order_id = mem::replace( let old_stacking_order_id = mem::replace(
@ -255,7 +252,7 @@ impl<'a> ElementContext<'a> {
/// Updates the global element offset relative to the current offset. This is used to implement /// Updates the global element offset relative to the current offset. This is used to implement
/// scrolling. /// scrolling.
pub(crate) fn with_element_offset<R>( pub fn with_element_offset<R>(
&mut self, &mut self,
offset: Point<Pixels>, offset: Point<Pixels>,
f: impl FnOnce(&mut Self) -> R, f: impl FnOnce(&mut Self) -> R,
@ -270,7 +267,7 @@ impl<'a> ElementContext<'a> {
/// Updates the global element offset based on the given offset. This is used to implement /// Updates the global element offset based on the given offset. This is used to implement
/// drag handles and other manual painting of elements. /// drag handles and other manual painting of elements.
pub(crate) fn with_absolute_element_offset<R>( pub fn with_absolute_element_offset<R>(
&mut self, &mut self,
offset: Point<Pixels>, offset: Point<Pixels>,
f: impl FnOnce(&mut Self) -> R, f: impl FnOnce(&mut Self) -> R,
@ -285,7 +282,7 @@ impl<'a> ElementContext<'a> {
} }
/// Obtain the current element offset. /// Obtain the current element offset.
pub(crate) fn element_offset(&self) -> Point<Pixels> { pub fn element_offset(&self) -> Point<Pixels> {
self.window() self.window()
.next_frame .next_frame
.element_offset_stack .element_offset_stack
@ -295,7 +292,7 @@ impl<'a> ElementContext<'a> {
} }
/// Obtain the current content mask. /// Obtain the current content mask.
pub(crate) fn content_mask(&self) -> ContentMask<Pixels> { pub fn content_mask(&self) -> ContentMask<Pixels> {
self.window() self.window()
.next_frame .next_frame
.content_mask_stack .content_mask_stack
@ -311,7 +308,7 @@ impl<'a> ElementContext<'a> {
/// The size of an em for the base font of the application. Adjusting this value allows the /// The size of an em for the base font of the application. Adjusting this value allows the
/// UI to scale, just like zooming a web page. /// UI to scale, just like zooming a web page.
pub(crate) fn rem_size(&self) -> Pixels { pub fn rem_size(&self) -> Pixels {
self.window().rem_size self.window().rem_size
} }
@ -319,7 +316,7 @@ impl<'a> ElementContext<'a> {
/// frames. If an element with this ID existed in the rendered frame, its state will be passed /// frames. If an element with this ID existed in the rendered frame, its state will be passed
/// to the given closure. The state returned by the closure will be stored so it can be referenced /// to the given closure. The state returned by the closure will be stored so it can be referenced
/// when drawing the next frame. /// when drawing the next frame.
pub(crate) fn with_element_state<S, R>( pub fn with_element_state<S, R>(
&mut self, &mut self,
id: ElementId, id: ElementId,
f: impl FnOnce(Option<S>, &mut Self) -> (R, S), f: impl FnOnce(Option<S>, &mut Self) -> (R, S),

View file

@ -1,11 +1,11 @@
use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; use editor::{Cursor, HighlightedRange, HighlightedRangeLine};
use gpui::{ use gpui::{
div, fill, point, px, relative, AnyElement, AvailableSpace, BorrowWindow, Bounds, div, fill, point, px, relative, AnyElement, AvailableSpace, Bounds, DispatchPhase, Element,
DispatchPhase, Element, ElementId, FocusHandle, Font, FontStyle, FontWeight, HighlightStyle, ElementContext, ElementId, FocusHandle, Font, FontStyle, FontWeight, HighlightStyle, Hsla,
Hsla, InputHandler, InteractiveBounds, InteractiveElement, InteractiveElementState, InputHandler, InteractiveBounds, InteractiveElement, InteractiveElementState, Interactivity,
Interactivity, IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton, IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton, MouseMoveEvent,
MouseMoveEvent, Pixels, Point, ShapedLine, StatefulInteractiveElement, Styled, TextRun, Pixels, Point, ShapedLine, StatefulInteractiveElement, Styled, TextRun, TextStyle, TextSystem,
TextStyle, TextSystem, UnderlineStyle, WeakView, WhiteSpace, WindowContext, UnderlineStyle, WeakView, WhiteSpace, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::CursorShape; use language::CursorShape;
@ -81,7 +81,7 @@ impl LayoutCell {
origin: Point<Pixels>, origin: Point<Pixels>,
layout: &LayoutState, layout: &LayoutState,
_visible_bounds: Bounds<Pixels>, _visible_bounds: Bounds<Pixels>,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let pos = { let pos = {
let point = self.point; let point = self.point;
@ -120,7 +120,7 @@ impl LayoutRect {
} }
} }
fn paint(&self, origin: Point<Pixels>, layout: &LayoutState, cx: &mut WindowContext) { fn paint(&self, origin: Point<Pixels>, layout: &LayoutState, cx: &mut ElementContext) {
let position = { let position = {
let alac_point = self.point; let alac_point = self.point;
point( point(
@ -590,7 +590,7 @@ impl TerminalElement {
origin: Point<Pixels>, origin: Point<Pixels>,
mode: TermMode, mode: TermMode,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let focus = self.focus.clone(); let focus = self.focus.clone();
let terminal = self.terminal.clone(); let terminal = self.terminal.clone();
@ -722,7 +722,7 @@ impl Element for TerminalElement {
fn request_layout( fn request_layout(
&mut self, &mut self,
element_state: Option<Self::State>, element_state: Option<Self::State>,
cx: &mut WindowContext<'_>, cx: &mut ElementContext<'_>,
) -> (LayoutId, Self::State) { ) -> (LayoutId, Self::State) {
let (layout_id, interactive_state) = let (layout_id, interactive_state) =
self.interactivity self.interactivity
@ -741,7 +741,7 @@ impl Element for TerminalElement {
&mut self, &mut self,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
state: &mut Self::State, state: &mut Self::State,
cx: &mut WindowContext<'_>, cx: &mut ElementContext<'_>,
) { ) {
let mut layout = self.compute_layout(bounds, cx); let mut layout = self.compute_layout(bounds, cx);

View file

@ -2,8 +2,9 @@ use std::{cell::RefCell, rc::Rc};
use gpui::{ use gpui::{
overlay, point, prelude::FluentBuilder, px, rems, AnchorCorner, AnyElement, Bounds, overlay, point, prelude::FluentBuilder, px, rems, AnchorCorner, AnyElement, Bounds,
DismissEvent, DispatchPhase, Element, ElementId, InteractiveBounds, IntoElement, LayoutId, DismissEvent, DispatchPhase, Element, ElementContext, ElementId, InteractiveBounds,
ManagedView, MouseDownEvent, ParentElement, Pixels, Point, View, VisualContext, WindowContext, IntoElement, LayoutId, ManagedView, MouseDownEvent, ParentElement, Pixels, Point, View,
VisualContext, WindowContext,
}; };
use crate::{Clickable, Selectable}; use crate::{Clickable, Selectable};
@ -134,7 +135,7 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
fn request_layout( fn request_layout(
&mut self, &mut self,
element_state: Option<Self::State>, element_state: Option<Self::State>,
cx: &mut WindowContext, cx: &mut ElementContext,
) -> (gpui::LayoutId, Self::State) { ) -> (gpui::LayoutId, Self::State) {
let mut menu_layout_id = None; let mut menu_layout_id = None;
@ -188,7 +189,7 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
&mut self, &mut self,
_: Bounds<gpui::Pixels>, _: Bounds<gpui::Pixels>,
element_state: &mut Self::State, element_state: &mut Self::State,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
if let Some(mut child) = element_state.child_element.take() { if let Some(mut child) = element_state.child_element.take() {
child.paint(cx); child.paint(cx);

View file

@ -1,9 +1,9 @@
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use gpui::{ use gpui::{
overlay, AnchorCorner, AnyElement, BorrowWindow, Bounds, DismissEvent, DispatchPhase, Element, overlay, AnchorCorner, AnyElement, Bounds, DismissEvent, DispatchPhase, Element,
ElementId, InteractiveBounds, IntoElement, LayoutId, ManagedView, MouseButton, MouseDownEvent, ElementContext, ElementId, InteractiveBounds, IntoElement, LayoutId, ManagedView, MouseButton,
ParentElement, Pixels, Point, View, VisualContext, WindowContext, MouseDownEvent, ParentElement, Pixels, Point, View, VisualContext, WindowContext,
}; };
pub struct RightClickMenu<M: ManagedView> { pub struct RightClickMenu<M: ManagedView> {
@ -64,7 +64,7 @@ impl<M: ManagedView> Element for RightClickMenu<M> {
fn request_layout( fn request_layout(
&mut self, &mut self,
element_state: Option<Self::State>, element_state: Option<Self::State>,
cx: &mut WindowContext, cx: &mut ElementContext,
) -> (gpui::LayoutId, Self::State) { ) -> (gpui::LayoutId, Self::State) {
let (menu, position) = if let Some(element_state) = element_state { let (menu, position) = if let Some(element_state) = element_state {
(element_state.menu, element_state.position) (element_state.menu, element_state.position)
@ -116,7 +116,7 @@ impl<M: ManagedView> Element for RightClickMenu<M> {
&mut self, &mut self,
bounds: Bounds<gpui::Pixels>, bounds: Bounds<gpui::Pixels>,
element_state: &mut Self::State, element_state: &mut Self::State,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
if let Some(mut child) = element_state.child_element.take() { if let Some(mut child) = element_state.child_element.take() {
child.paint(cx); child.paint(cx);

View file

@ -2,9 +2,9 @@
pub use gpui::prelude::*; pub use gpui::prelude::*;
pub use gpui::{ pub use gpui::{
div, px, relative, rems, AbsoluteLength, DefiniteLength, Div, Element, ElementId, div, px, relative, rems, AbsoluteLength, DefiniteLength, Div, Element, ElementContext,
InteractiveElement, ParentElement, Pixels, Rems, RenderOnce, SharedString, Styled, ViewContext, ElementId, InteractiveElement, ParentElement, Pixels, Rems, RenderOnce, SharedString, Styled,
WindowContext, ViewContext, WindowContext,
}; };
pub use crate::clickable::*; pub use crate::clickable::*;

View file

@ -710,7 +710,7 @@ mod element {
pane_bounds: Bounds<Pixels>, pane_bounds: Bounds<Pixels>,
axis_bounds: Bounds<Pixels>, axis_bounds: Bounds<Pixels>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
cx: &mut WindowContext, cx: &mut ElementContext,
) { ) {
let handle_bounds = Bounds { let handle_bounds = Bounds {
origin: pane_bounds.origin.apply_along(axis, |origin| { origin: pane_bounds.origin.apply_along(axis, |origin| {
@ -803,7 +803,7 @@ mod element {
fn request_layout( fn request_layout(
&mut self, &mut self,
state: Option<Self::State>, state: Option<Self::State>,
cx: &mut ui::prelude::WindowContext, cx: &mut ui::prelude::ElementContext,
) -> (gpui::LayoutId, Self::State) { ) -> (gpui::LayoutId, Self::State) {
let mut style = Style::default(); let mut style = Style::default();
style.flex_grow = 1.; style.flex_grow = 1.;
@ -820,7 +820,7 @@ mod element {
&mut self, &mut self,
bounds: gpui::Bounds<ui::prelude::Pixels>, bounds: gpui::Bounds<ui::prelude::Pixels>,
state: &mut Self::State, state: &mut Self::State,
cx: &mut ui::prelude::WindowContext, cx: &mut ui::prelude::ElementContext,
) { ) {
let flexes = self.flexes.lock().clone(); let flexes = self.flexes.lock().clone();
let len = self.children.len(); let len = self.children.len();

View file

@ -26,12 +26,12 @@ use futures::{
}; };
use gpui::{ use gpui::{
actions, canvas, div, impl_actions, point, px, size, Action, AnyElement, AnyModel, AnyView, actions, canvas, div, impl_actions, point, px, size, Action, AnyElement, AnyModel, AnyView,
AnyWeakView, AppContext, AsyncAppContext, AsyncWindowContext, BorrowWindow, Bounds, Context, AnyWeakView, AppContext, AsyncAppContext, AsyncWindowContext, Bounds, Context, Div,
Div, DragMoveEvent, Element, Entity, EntityId, EventEmitter, FocusHandle, FocusableView, DragMoveEvent, Element, ElementContext, Entity, EntityId, EventEmitter, FocusHandle,
GlobalPixels, InteractiveElement, IntoElement, KeyContext, LayoutId, ManagedView, Model, FocusableView, GlobalPixels, InteractiveElement, IntoElement, KeyContext, LayoutId,
ModelContext, ParentElement, PathPromptOptions, Pixels, Point, PromptLevel, Render, Size, ManagedView, Model, ModelContext, ParentElement, PathPromptOptions, Pixels, Point, PromptLevel,
Styled, Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds, Render, Size, Styled, Subscription, Task, View, ViewContext, VisualContext, WeakView,
WindowContext, WindowHandle, WindowOptions, WindowBounds, WindowContext, WindowHandle, WindowOptions,
}; };
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem}; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
use itertools::Itertools; use itertools::Itertools;
@ -3539,9 +3539,14 @@ impl Render for Workspace {
.border_b() .border_b()
.border_color(colors.border) .border_color(colors.border)
.child( .child(
canvas(cx.listener(|workspace, bounds, _| { canvas({
workspace.bounds = *bounds; let this = cx.view().clone();
})) move |bounds, cx| {
this.update(cx, |this, _cx| {
this.bounds = *bounds;
})
}
})
.absolute() .absolute()
.size_full(), .size_full(),
) )
@ -4293,7 +4298,7 @@ impl Element for DisconnectedOverlay {
fn request_layout( fn request_layout(
&mut self, &mut self,
_: Option<Self::State>, _: Option<Self::State>,
cx: &mut WindowContext, cx: &mut ElementContext,
) -> (LayoutId, Self::State) { ) -> (LayoutId, Self::State) {
let mut background = cx.theme().colors().elevated_surface_background; let mut background = cx.theme().colors().elevated_surface_background;
background.fade_out(0.2); background.fade_out(0.2);
@ -4315,7 +4320,12 @@ impl Element for DisconnectedOverlay {
(overlay.request_layout(cx), overlay) (overlay.request_layout(cx), overlay)
} }
fn paint(&mut self, bounds: Bounds<Pixels>, overlay: &mut Self::State, cx: &mut WindowContext) { fn paint(
&mut self,
bounds: Bounds<Pixels>,
overlay: &mut Self::State,
cx: &mut ElementContext,
) {
cx.with_z_index(u8::MAX, |cx| { cx.with_z_index(u8::MAX, |cx| {
cx.add_opaque_layer(bounds); cx.add_opaque_layer(bounds);
overlay.paint(cx); overlay.paint(cx);