chore: Upgrade to GPUI3 (#6)

* wip: gpui3

* wip: gpui3

* chore: fix clippy
This commit is contained in:
reya
2025-01-28 08:25:49 +07:00
committed by GitHub
parent 3c15e74e56
commit 72a6d79bc5
62 changed files with 2572 additions and 2511 deletions

View File

@@ -1,7 +1,7 @@
use gpui::{
canvas, div, relative, AnyElement, Div, Element, ElementId, EntityId, GlobalElementId,
canvas, div, relative, AnyElement, App, Div, Element, ElementId, EntityId, GlobalElementId,
InteractiveElement, IntoElement, ParentElement, Pixels, Position, ScrollHandle, SharedString,
Size, Stateful, StatefulInteractiveElement, Style, StyleRefinement, Styled, WindowContext,
Size, Stateful, StatefulInteractiveElement, Style, StyleRefinement, Styled, Window,
};
use std::{cell::Cell, rc::Rc};
@@ -58,14 +58,18 @@ where
fn with_element_state<R>(
&mut self,
id: &GlobalElementId,
cx: &mut WindowContext,
f: impl FnOnce(&mut Self, &mut ScrollViewState, &mut WindowContext) -> R,
window: &mut Window,
cx: &mut App,
f: impl FnOnce(&mut Self, &mut ScrollViewState, &mut Window, &mut App) -> R,
) -> R {
cx.with_optional_element_state::<ScrollViewState, _>(Some(id), |element_state, cx| {
let mut element_state = element_state.unwrap().unwrap_or_default();
let result = f(self, &mut element_state, cx);
(result, Some(element_state))
})
window.with_optional_element_state::<ScrollViewState, _>(
Some(id),
|element_state, window| {
let mut element_state = element_state.unwrap().unwrap_or_default();
let result = f(self, &mut element_state, window, cx);
(result, Some(element_state))
},
)
}
}
@@ -149,14 +153,16 @@ where
fn request_layout(
&mut self,
id: Option<&gpui::GlobalElementId>,
cx: &mut gpui::WindowContext,
window: &mut Window,
cx: &mut App,
) -> (gpui::LayoutId, Self::RequestLayoutState) {
let style = Style {
flex_grow: 1.0,
position: Position::Relative,
flex_grow: 1.0,
flex_shrink: 1.0,
size: Size {
width: relative(1.0).into(),
height: relative(1.0).into(),
width: relative(1.).into(),
height: relative(1.).into(),
},
..Default::default()
};
@@ -167,7 +173,7 @@ where
let scroll_id = self.id.clone();
let content = self.element.take().map(|c| c.into_any_element());
self.with_element_state(id.unwrap(), cx, |_, element_state, cx| {
self.with_element_state(id.unwrap(), window, cx, |_, element_state, window, cx| {
let handle = element_state.handle.clone();
let state = element_state.state.clone();
let scroll_size = element_state.scroll_size.clone();
@@ -185,7 +191,7 @@ where
.size_full()
.child(div().children(content).child({
let scroll_size = element_state.scroll_size.clone();
canvas(move |b, _| scroll_size.set(b.size), |_, _, _| {})
canvas(move |b, _, _| scroll_size.set(b.size), |_, _, _, _| {})
.absolute()
.size_full()
})),
@@ -203,9 +209,9 @@ where
),
)
.into_any_element();
let element_id = element.request_layout(window, cx);
let element_id = element.request_layout(cx);
let layout_id = cx.request_layout(style, vec![element_id]);
let layout_id = window.request_layout(style, vec![element_id], cx);
(layout_id, element)
})
@@ -216,9 +222,10 @@ where
_: Option<&gpui::GlobalElementId>,
_: gpui::Bounds<Pixels>,
element: &mut Self::RequestLayoutState,
cx: &mut gpui::WindowContext,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
element.prepaint(cx);
element.prepaint(window, cx);
// do nothing
ScrollViewState::default()
}
@@ -229,8 +236,9 @@ where
_: gpui::Bounds<Pixels>,
element: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState,
cx: &mut gpui::WindowContext,
window: &mut Window,
cx: &mut App,
) {
element.paint(cx)
element.paint(window, cx)
}
}

View File

@@ -1,7 +1,7 @@
use gpui::{
px, relative, Axis, Bounds, ContentMask, Corners, Edges, Element, ElementId, EntityId,
px, relative, App, Axis, Bounds, ContentMask, Corners, Edges, Element, ElementId, EntityId,
GlobalElementId, Hitbox, Hsla, IntoElement, IsZero as _, LayoutId, PaintQuad, Pixels, Point,
Position, ScrollHandle, ScrollWheelEvent, Size, Style, WindowContext,
Position, ScrollHandle, ScrollWheelEvent, Size, Style, Window,
};
use crate::AxisExt;
@@ -56,9 +56,9 @@ impl Element for ScrollableMask {
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
// Set the layout style relative to the table view to get same size.
let style = Style {
position: Position::Absolute,
flex_grow: 1.0,
@@ -70,7 +70,7 @@ impl Element for ScrollableMask {
..Default::default()
};
(cx.request_layout(style, None), ())
(window.request_layout(style, None, cx), ())
}
fn prepaint(
@@ -78,7 +78,8 @@ impl Element for ScrollableMask {
_: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
cx: &mut WindowContext,
window: &mut Window,
_: &mut App,
) -> Self::PrepaintState {
// Move y to bounds height to cover the parent view.
let cover_bounds = Bounds {
@@ -89,7 +90,7 @@ impl Element for ScrollableMask {
size: bounds.size,
};
cx.insert_hitbox(cover_bounds, false)
window.insert_hitbox(cover_bounds, false)
}
fn paint(
@@ -98,14 +99,15 @@ impl Element for ScrollableMask {
_: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
hitbox: &mut Self::PrepaintState,
cx: &mut WindowContext,
window: &mut Window,
_: &mut App,
) {
let line_height = cx.line_height();
let line_height = window.line_height();
let bounds = hitbox.bounds;
cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
window.with_content_mask(Some(ContentMask { bounds }), |window| {
if let Some(color) = self.debug {
cx.paint_quad(PaintQuad {
window.paint_quad(PaintQuad {
bounds,
border_widths: Edges::all(px(1.0)),
border_color: color,
@@ -114,16 +116,19 @@ impl Element for ScrollableMask {
});
}
cx.on_mouse_event({
window.on_mouse_event({
let view_id = self.view_id;
let is_horizontal = self.axis.is_horizontal();
let scroll_handle = self.scroll_handle.clone();
let hitbox = hitbox.clone();
let mouse_position = cx.mouse_position();
let mouse_position = window.mouse_position();
let last_offset = scroll_handle.offset();
move |event: &ScrollWheelEvent, phase, cx| {
if bounds.contains(&mouse_position) && phase.bubble() && hitbox.is_hovered(cx) {
move |event: &ScrollWheelEvent, phase, window, cx| {
if bounds.contains(&mouse_position)
&& phase.bubble()
&& hitbox.is_hovered(window)
{
let mut offset = scroll_handle.offset();
let mut delta = event.delta.pixel_delta(line_height);
@@ -146,7 +151,7 @@ impl Element for ScrollableMask {
if last_offset != offset {
scroll_handle.set_offset(offset);
cx.notify(Some(view_id));
cx.notify(view_id);
cx.stop_propagation();
}
}

View File

@@ -290,7 +290,7 @@ impl Scrollbar {
self
}
fn style_for_active(cx: &AppContext) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
fn style_for_active(cx: &App) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
(
cx.theme().scrollbar_thumb_hover,
cx.theme().scrollbar,
@@ -300,7 +300,7 @@ impl Scrollbar {
)
}
fn style_for_hovered_thumb(cx: &AppContext) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
fn style_for_hovered_thumb(cx: &App) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
(
cx.theme().scrollbar_thumb_hover,
cx.theme().scrollbar,
@@ -310,7 +310,7 @@ impl Scrollbar {
)
}
fn style_for_hovered_bar(cx: &AppContext) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
fn style_for_hovered_bar(cx: &App) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
let (inset, radius) = if cx.theme().scrollbar_show.is_hover() {
(THUMB_INSET, THUMB_RADIUS - px(1.))
} else {
@@ -326,7 +326,7 @@ impl Scrollbar {
)
}
fn style_for_idle(_: &AppContext) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
fn style_for_idle(_: &App) -> (Hsla, Hsla, Hsla, Pixels, Pixels) {
(
gpui::transparent_black(),
gpui::transparent_black(),
@@ -379,7 +379,8 @@ impl Element for Scrollbar {
fn request_layout(
&mut self,
_: Option<&gpui::GlobalElementId>,
cx: &mut gpui::WindowContext,
window: &mut Window,
cx: &mut App,
) -> (gpui::LayoutId, Self::RequestLayoutState) {
let style = Style {
position: Position::Absolute,
@@ -392,7 +393,7 @@ impl Element for Scrollbar {
..Default::default()
};
(cx.request_layout(style, None), ())
(window.request_layout(style, None, cx), ())
}
fn prepaint(
@@ -400,10 +401,11 @@ impl Element for Scrollbar {
_: Option<&gpui::GlobalElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
cx: &mut gpui::WindowContext,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
let hitbox = cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
cx.insert_hitbox(bounds, false)
let hitbox = window.with_content_mask(Some(ContentMask { bounds }), |window| {
window.insert_hitbox(bounds, false)
});
let mut states = vec![];
@@ -507,7 +509,7 @@ impl Element for Scrollbar {
idle_state.0 = cx.theme().scrollbar_thumb.opacity(opacity);
};
cx.request_animation_frame();
window.request_animation_frame();
}
}
}
@@ -550,8 +552,8 @@ impl Element for Scrollbar {
)
};
let bar_hitbox = cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
cx.insert_hitbox(bounds, false)
let bar_hitbox = window.with_content_mask(Some(ContentMask { bounds }), |window| {
window.insert_hitbox(bounds, false)
});
states.push(AxisPrepaintState {
@@ -580,7 +582,8 @@ impl Element for Scrollbar {
_: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
prepaint: &mut Self::PrepaintState,
cx: &mut gpui::WindowContext,
window: &mut Window,
cx: &mut App,
) {
let hitbox_bounds = prepaint.hitbox.bounds;
let is_visible = self.state.get().is_scrollbar_visible();
@@ -597,9 +600,9 @@ impl Element for Scrollbar {
let margin_end = state.margin_end;
let is_vertical = axis.is_vertical();
cx.set_cursor_style(CursorStyle::default(), &state.bar_hitbox);
window.set_cursor_style(CursorStyle::default(), &state.bar_hitbox);
cx.paint_layer(hitbox_bounds, |cx| {
window.paint_layer(hitbox_bounds, |cx| {
cx.paint_quad(fill(state.bounds, state.bg));
cx.paint_quad(PaintQuad {
@@ -627,12 +630,12 @@ impl Element for Scrollbar {
cx.paint_quad(fill(state.thumb_fill_bounds, state.thumb_bg).corner_radii(radius));
});
cx.on_mouse_event({
window.on_mouse_event({
let state = self.state.clone();
let view_id = self.view_id;
let scroll_handle = self.scroll_handle.clone();
move |event: &ScrollWheelEvent, phase, cx| {
move |event: &ScrollWheelEvent, phase, _, cx| {
if phase.bubble()
&& hitbox_bounds.contains(&event.position)
&& scroll_handle.offset() != state.get().last_scroll_offset
@@ -642,7 +645,7 @@ impl Element for Scrollbar {
.get()
.with_last_scroll(scroll_handle.offset(), Some(Instant::now())),
);
cx.notify(Some(view_id));
cx.notify(view_id);
}
}
});
@@ -650,12 +653,12 @@ impl Element for Scrollbar {
let safe_range = (-scroll_area_size + container_size)..px(0.);
if is_hover_to_show || is_visible {
cx.on_mouse_event({
window.on_mouse_event({
let state = self.state.clone();
let view_id = self.view_id;
let scroll_handle = self.scroll_handle.clone();
move |event: &MouseDownEvent, phase, cx| {
move |event: &MouseDownEvent, phase, _, cx| {
if phase.bubble() && bounds.contains(&event.position) {
cx.stop_propagation();
@@ -665,7 +668,7 @@ impl Element for Scrollbar {
state.set(state.get().with_drag_pos(axis, pos));
cx.notify(Some(view_id));
cx.notify(view_id);
} else {
// click on the scrollbar, jump to the position
// Set the thumb bar center to the click position
@@ -698,34 +701,34 @@ impl Element for Scrollbar {
});
}
cx.on_mouse_event({
window.on_mouse_event({
let scroll_handle = self.scroll_handle.clone();
let state = self.state.clone();
let view_id = self.view_id;
move |event: &MouseMoveEvent, _, cx| {
move |event: &MouseMoveEvent, _, _, cx| {
// Update hovered state for scrollbar
if bounds.contains(&event.position) {
if state.get().hovered_axis != Some(axis) {
state.set(state.get().with_hovered(Some(axis)));
cx.notify(Some(view_id));
cx.notify(view_id);
}
} else if state.get().hovered_axis == Some(axis)
&& state.get().hovered_axis.is_some()
{
state.set(state.get().with_hovered(None));
cx.notify(Some(view_id));
cx.notify(view_id);
}
// Update hovered state for scrollbar thumb
if thumb_bounds.contains(&event.position) {
if state.get().hovered_on_thumb != Some(axis) {
state.set(state.get().with_hovered_on_thumb(Some(axis)));
cx.notify(Some(view_id));
cx.notify(view_id);
}
} else if state.get().hovered_on_thumb == Some(axis) {
state.set(state.get().with_hovered_on_thumb(None));
cx.notify(Some(view_id));
cx.notify(view_id);
}
// Move thumb position on dragging
@@ -757,20 +760,24 @@ impl Element for Scrollbar {
)
};
scroll_handle.set_offset(offset);
cx.notify(Some(view_id));
if (scroll_handle.offset().y - offset.y).abs() > px(1.)
|| (scroll_handle.offset().x - offset.x).abs() > px(1.)
{
scroll_handle.set_offset(offset);
cx.notify(view_id);
}
}
}
});
cx.on_mouse_event({
window.on_mouse_event({
let view_id = self.view_id;
let state = self.state.clone();
move |_event: &MouseUpEvent, phase, cx| {
move |_event: &MouseUpEvent, phase, _, cx| {
if phase.bubble() {
state.set(state.get().with_unset_drag_pos());
cx.notify(Some(view_id));
cx.notify(view_id);
}
}
});