chore: bump gpui

This commit is contained in:
2026-04-25 07:01:14 +07:00
parent 80186a79e5
commit 6d60726f27
15 changed files with 342 additions and 775 deletions

490
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1421,7 +1421,7 @@ impl ChatPanel {
.icon(IconName::Emoji) .icon(IconName::Emoji)
.ghost() .ghost()
.large() .large()
.dropdown_menu_with_anchor(gpui::Corner::BottomLeft, move |this, _window, _cx| { .dropdown_menu_with_anchor(gpui::Anchor::BottomLeft, move |this, _window, _cx| {
this.horizontal() this.horizontal()
.menu("👍", Box::new(Command::Insert("👍"))) .menu("👍", Box::new(Command::Insert("👍")))
.menu("👎", Box::new(Command::Insert("👎"))) .menu("👎", Box::new(Command::Insert("👎")))

View File

@@ -1,6 +1,6 @@
use std::fmt::{self, Debug, Display, Formatter}; use std::fmt::{self, Debug, Display, Formatter};
use gpui::{AbsoluteLength, Axis, Corner, Length, Pixels}; use gpui::{AbsoluteLength, Axis, Length, Pixels};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// A enum for defining the placement of the element. /// A enum for defining the placement of the element.
@@ -49,141 +49,6 @@ impl Placement {
} }
} }
/// The anchor position of an element.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum Anchor {
#[default]
#[serde(rename = "top-left")]
TopLeft,
#[serde(rename = "top-center")]
TopCenter,
#[serde(rename = "top-right")]
TopRight,
#[serde(rename = "bottom-left")]
BottomLeft,
#[serde(rename = "bottom-center")]
BottomCenter,
#[serde(rename = "bottom-right")]
BottomRight,
}
impl Display for Anchor {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Anchor::TopLeft => write!(f, "TopLeft"),
Anchor::TopCenter => write!(f, "TopCenter"),
Anchor::TopRight => write!(f, "TopRight"),
Anchor::BottomLeft => write!(f, "BottomLeft"),
Anchor::BottomCenter => write!(f, "BottomCenter"),
Anchor::BottomRight => write!(f, "BottomRight"),
}
}
}
impl Anchor {
/// Returns true if the anchor is at the top.
#[inline]
pub fn is_top(&self) -> bool {
matches!(self, Self::TopLeft | Self::TopCenter | Self::TopRight)
}
/// Returns true if the anchor is at the bottom.
#[inline]
pub fn is_bottom(&self) -> bool {
matches!(
self,
Self::BottomLeft | Self::BottomCenter | Self::BottomRight
)
}
/// Returns true if the anchor is at the left.
#[inline]
pub fn is_left(&self) -> bool {
matches!(self, Self::TopLeft | Self::BottomLeft)
}
/// Returns true if the anchor is at the right.
#[inline]
pub fn is_right(&self) -> bool {
matches!(self, Self::TopRight | Self::BottomRight)
}
/// Returns true if the anchor is at the center.
#[inline]
pub fn is_center(&self) -> bool {
matches!(self, Self::TopCenter | Self::BottomCenter)
}
/// Swaps the vertical position of the anchor.
pub fn swap_vertical(&self) -> Self {
match self {
Anchor::TopLeft => Anchor::BottomLeft,
Anchor::TopCenter => Anchor::BottomCenter,
Anchor::TopRight => Anchor::BottomRight,
Anchor::BottomLeft => Anchor::TopLeft,
Anchor::BottomCenter => Anchor::TopCenter,
Anchor::BottomRight => Anchor::TopRight,
}
}
/// Swaps the horizontal position of the anchor.
pub fn swap_horizontal(&self) -> Self {
match self {
Anchor::TopLeft => Anchor::TopRight,
Anchor::TopCenter => Anchor::TopCenter,
Anchor::TopRight => Anchor::TopLeft,
Anchor::BottomLeft => Anchor::BottomRight,
Anchor::BottomCenter => Anchor::BottomCenter,
Anchor::BottomRight => Anchor::BottomLeft,
}
}
pub fn other_side_corner_along(&self, axis: Axis) -> Anchor {
match axis {
Axis::Vertical => match self {
Self::TopLeft => Self::BottomLeft,
Self::TopCenter => Self::BottomCenter,
Self::TopRight => Self::BottomRight,
Self::BottomLeft => Self::TopLeft,
Self::BottomCenter => Self::TopCenter,
Self::BottomRight => Self::TopRight,
},
Axis::Horizontal => match self {
Self::TopLeft => Self::TopRight,
Self::TopCenter => Self::TopCenter,
Self::TopRight => Self::TopLeft,
Self::BottomLeft => Self::BottomRight,
Self::BottomCenter => Self::BottomCenter,
Self::BottomRight => Self::BottomLeft,
},
}
}
}
impl From<Corner> for Anchor {
fn from(corner: Corner) -> Self {
match corner {
Corner::TopLeft => Anchor::TopLeft,
Corner::TopRight => Anchor::TopRight,
Corner::BottomLeft => Anchor::BottomLeft,
Corner::BottomRight => Anchor::BottomRight,
}
}
}
impl From<Anchor> for Corner {
fn from(anchor: Anchor) -> Self {
match anchor {
Anchor::TopLeft => Corner::TopLeft,
Anchor::TopRight => Corner::TopRight,
Anchor::BottomLeft => Corner::BottomLeft,
Anchor::BottomRight => Corner::BottomRight,
Anchor::TopCenter => Corner::TopLeft,
Anchor::BottomCenter => Corner::BottomLeft,
}
}
}
/// A enum for defining the side of the element. /// A enum for defining the side of the element.
/// ///
/// See also: [`Placement`] if you need to define the 4 edges. /// See also: [`Placement`] if you need to define the 4 edges.

View File

@@ -1,10 +1,9 @@
use gpui::{Pixels, px}; use gpui::{Anchor, Pixels, px};
use serde::{Deserialize, Serialize};
use crate::{Anchor, Edges, TITLEBAR_HEIGHT}; use crate::{Edges, TITLEBAR_HEIGHT};
/// The settings for notifications. /// The settings for notifications.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone)]
pub struct NotificationSettings { pub struct NotificationSettings {
/// The placement of the notification, default: [`Anchor::TopRight`] /// The placement of the notification, default: [`Anchor::TopRight`]
pub placement: Anchor, pub placement: Anchor,

View File

@@ -1,332 +0,0 @@
//! This is a fork of gpui's anchored element that adds support for offsetting
//! https://github.com/zed-industries/zed/blob/b06f4088a3565c5e30663106ff79c1ced645d87a/crates/gpui/src/elements/anchored.rs
use gpui::{
AnyElement, App, Axis, Bounds, Display, Edges, Element, GlobalElementId, Half,
InspectorElementId, IntoElement, LayoutId, ParentElement, Pixels, Point, Position, Size, Style,
Window, point, px,
};
use smallvec::SmallVec;
use theme::Anchor;
/// The state that the anchored element element uses to track its children.
pub struct AnchoredState {
child_layout_ids: SmallVec<[LayoutId; 4]>,
}
/// An anchored element that can be used to display UI that
/// will avoid overflowing the window bounds.
pub(crate) struct Anchored {
children: SmallVec<[AnyElement; 2]>,
anchor_corner: Anchor,
fit_mode: AnchoredFitMode,
anchor_position: Option<Point<Pixels>>,
position_mode: AnchoredPositionMode,
offset: Option<Point<Pixels>>,
}
/// anchored gives you an element that will avoid overflowing the window bounds.
/// Its children should have no margin to avoid measurement issues.
pub(crate) fn anchored() -> Anchored {
Anchored {
children: SmallVec::new(),
anchor_corner: Anchor::TopLeft,
fit_mode: AnchoredFitMode::SwitchAnchor,
anchor_position: None,
position_mode: AnchoredPositionMode::Window,
offset: None,
}
}
#[allow(dead_code)]
impl Anchored {
/// Sets which corner of the anchored element should be anchored to the current position.
pub fn anchor(mut self, anchor: Anchor) -> Self {
self.anchor_corner = anchor;
self
}
/// Sets the position in window coordinates
/// (otherwise the location the anchored element is rendered is used)
pub fn position(mut self, anchor: Point<Pixels>) -> Self {
self.anchor_position = Some(anchor);
self
}
/// Offset the final position by this amount.
/// Useful when you want to anchor to an element but offset from it, such as in PopoverMenu.
pub fn offset(mut self, offset: Point<Pixels>) -> Self {
self.offset = Some(offset);
self
}
/// Sets the position mode for this anchored element. Local will have this
/// interpret its [`Anchored::position`] as relative to the parent element.
/// While Window will have it interpret the position as relative to the window.
pub fn position_mode(mut self, mode: AnchoredPositionMode) -> Self {
self.position_mode = mode;
self
}
/// Snap to window edge instead of switching anchor corner when an overflow would occur.
pub fn snap_to_window(mut self) -> Self {
self.fit_mode = AnchoredFitMode::SnapToWindow;
self
}
/// Snap to window edge and leave some margins.
pub fn snap_to_window_with_margin(mut self, edges: impl Into<Edges<Pixels>>) -> Self {
self.fit_mode = AnchoredFitMode::SnapToWindowWithMargin(edges.into());
self
}
}
impl ParentElement for Anchored {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements)
}
}
impl Element for Anchored {
type PrepaintState = ();
type RequestLayoutState = AnchoredState;
fn id(&self) -> Option<gpui::ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (gpui::LayoutId, Self::RequestLayoutState) {
let child_layout_ids = self
.children
.iter_mut()
.map(|child| child.request_layout(window, cx))
.collect::<SmallVec<_>>();
let anchored_style = Style {
position: Position::Absolute,
display: Display::Flex,
..Style::default()
};
let layout_id = window.request_layout(anchored_style, child_layout_ids.iter().copied(), cx);
(layout_id, AnchoredState { child_layout_ids })
}
fn prepaint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
bounds: Bounds<Pixels>,
request_layout: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) {
if request_layout.child_layout_ids.is_empty() {
return;
}
let mut child_min = point(Pixels::MAX, Pixels::MAX);
let mut child_max = Point::default();
for child_layout_id in &request_layout.child_layout_ids {
let child_bounds = window.layout_bounds(*child_layout_id);
child_min = child_min.min(&child_bounds.origin);
child_max = child_max.max(&child_bounds.bottom_right());
}
let size: Size<Pixels> = (child_max - child_min).into();
let (origin, mut desired) = self.position_mode.get_position_and_bounds(
self.anchor_position,
self.anchor_corner,
size,
bounds,
self.offset,
);
let limits = Bounds {
origin: Point::default(),
size: window.viewport_size(),
};
if self.fit_mode == AnchoredFitMode::SwitchAnchor {
let mut anchor_corner = self.anchor_corner;
if desired.left() < limits.left() || desired.right() > limits.right() {
let switched = Bounds::from_corner_and_size(
anchor_corner
.other_side_corner_along(Axis::Horizontal)
.into(),
origin,
size,
);
if !(switched.left() < limits.left() || switched.right() > limits.right()) {
anchor_corner = anchor_corner.other_side_corner_along(Axis::Horizontal);
desired = switched
}
}
if desired.top() < limits.top() || desired.bottom() > limits.bottom() {
let switched = Bounds::from_corner_and_size(
anchor_corner.other_side_corner_along(Axis::Vertical).into(),
origin,
size,
);
if !(switched.top() < limits.top() || switched.bottom() > limits.bottom()) {
desired = switched;
}
}
}
let client_inset = window.client_inset().unwrap_or(px(0.));
let edges = match self.fit_mode {
AnchoredFitMode::SnapToWindowWithMargin(edges) => edges,
_ => Edges::default(),
}
.map(|edge| *edge + client_inset);
// Snap the horizontal edges of the anchored element to the horizontal edges of the window if
// its horizontal bounds overflow, aligning to the left if it is wider than the limits.
if desired.right() > limits.right() {
desired.origin.x -= desired.right() - limits.right() + edges.right;
}
if desired.left() < limits.left() {
desired.origin.x = limits.origin.x + edges.left;
}
// Snap the vertical edges of the anchored element to the vertical edges of the window if
// its vertical bounds overflow, aligning to the top if it is taller than the limits.
if desired.bottom() > limits.bottom() {
desired.origin.y -= desired.bottom() - limits.bottom() + edges.bottom;
}
if desired.top() < limits.top() {
desired.origin.y = limits.origin.y + edges.top;
}
let offset = desired.origin - bounds.origin;
let offset = point(offset.x.round(), offset.y.round());
window.with_element_offset(offset, |window| {
for child in &mut self.children {
child.prepaint(window, cx);
}
})
}
fn paint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
_bounds: Bounds<Pixels>,
_request_layout: &mut Self::RequestLayoutState,
_prepaint: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
for child in &mut self.children {
child.paint(window, cx);
}
}
}
impl IntoElement for Anchored {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}
/// Which algorithm to use when fitting the anchored element to be inside the window.
#[allow(dead_code)]
#[derive(Copy, Clone, PartialEq)]
pub enum AnchoredFitMode {
/// Snap the anchored element to the window edge.
SnapToWindow,
/// Snap to window edge and leave some margins.
SnapToWindowWithMargin(Edges<Pixels>),
/// Switch which corner anchor this anchored element is attached to.
SwitchAnchor,
}
/// Which algorithm to use when positioning the anchored element.
#[allow(dead_code)]
#[derive(Copy, Clone, PartialEq)]
pub enum AnchoredPositionMode {
/// Position the anchored element relative to the window.
Window,
/// Position the anchored element relative to its parent.
Local,
}
impl AnchoredPositionMode {
fn get_position_and_bounds(
&self,
anchor_position: Option<Point<Pixels>>,
anchor_corner: Anchor,
size: Size<Pixels>,
bounds: Bounds<Pixels>,
offset: Option<Point<Pixels>>,
) -> (Point<Pixels>, Bounds<Pixels>) {
let offset = offset.unwrap_or_default();
match self {
AnchoredPositionMode::Window => {
let anchor_position = anchor_position.unwrap_or(bounds.origin);
let bounds =
Self::from_corner_and_size(anchor_corner, anchor_position + offset, size);
(anchor_position, bounds)
}
AnchoredPositionMode::Local => {
let anchor_position = anchor_position.unwrap_or_default();
let bounds = Self::from_corner_and_size(
anchor_corner,
bounds.origin + anchor_position + offset,
size,
);
(anchor_position, bounds)
}
}
}
// Ref https://github.com/zed-industries/zed/blob/b06f4088a3565c5e30663106ff79c1ced645d87a/crates/gpui/src/geometry.rs#L863
fn from_corner_and_size(
anchor: Anchor,
origin: Point<Pixels>,
size: Size<Pixels>,
) -> Bounds<Pixels> {
let origin = match anchor {
Anchor::TopLeft => origin,
Anchor::TopCenter => Point {
x: origin.x - size.width.half(),
y: origin.y,
},
Anchor::TopRight => Point {
x: origin.x - size.width,
y: origin.y,
},
Anchor::BottomLeft => Point {
x: origin.x,
y: origin.y - size.height,
},
Anchor::BottomCenter => Point {
x: origin.x - size.width.half(),
y: origin.y - size.height,
},
Anchor::BottomRight => Point {
x: origin.x - size.width,
y: origin.y - size.height,
},
};
Bounds { origin, size }
}
}

View File

@@ -2,7 +2,7 @@ use std::sync::Arc;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
App, AppContext, Context, Corner, DefiniteLength, DismissEvent, DragMoveEvent, Empty, Entity, Anchor, App, AppContext, Context, DefiniteLength, DismissEvent, DragMoveEvent, Empty, Entity,
EventEmitter, FocusHandle, Focusable, InteractiveElement as _, IntoElement, MouseButton, EventEmitter, FocusHandle, Focusable, InteractiveElement as _, IntoElement, MouseButton,
ParentElement, Pixels, Render, ScrollHandle, SharedString, StatefulInteractiveElement, Styled, ParentElement, Pixels, Render, ScrollHandle, SharedString, StatefulInteractiveElement, Styled,
WeakEntity, Window, div, px, rems, WeakEntity, Window, div, px, rems,
@@ -460,7 +460,7 @@ impl TabPanel {
}) })
} }
}) })
.anchor(Corner::TopRight), .anchor(Anchor::TopRight),
) )
} }

View File

@@ -1,4 +1,3 @@
pub use anchored::*;
pub use element_ext::ElementExt; pub use element_ext::ElementExt;
pub use event::InteractiveElementExt; pub use event::InteractiveElementExt;
pub use focusable::FocusableCycle; pub use focusable::FocusableCycle;
@@ -34,7 +33,6 @@ pub mod switch;
pub mod tab; pub mod tab;
pub mod tooltip; pub mod tooltip;
mod anchored;
mod element_ext; mod element_ext;
mod event; mod event;
mod focusable; mod focusable;

View File

@@ -1,14 +1,15 @@
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
anchored, deferred, div, px, App, AppContext as _, ClickEvent, Context, DismissEvent, Entity, App, AppContext as _, ClickEvent, Context, DismissEvent, Entity, Focusable,
Focusable, InteractiveElement as _, IntoElement, KeyBinding, MouseButton, OwnedMenu, InteractiveElement as _, IntoElement, KeyBinding, MouseButton, OwnedMenu, ParentElement,
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Window, Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Window, anchored,
deferred, div, px,
}; };
use crate::actions::{Cancel, SelectLeft, SelectRight}; use crate::actions::{Cancel, SelectLeft, SelectRight};
use crate::button::{Button, ButtonVariants}; use crate::button::{Button, ButtonVariants};
use crate::menu::PopupMenu; use crate::menu::PopupMenu;
use crate::{h_flex, Selectable, Sizable}; use crate::{Selectable, Sizable, h_flex};
const CONTEXT: &str = "AppMenuBar"; const CONTEXT: &str = "AppMenuBar";
@@ -241,7 +242,7 @@ impl Render for AppMenu {
.when(is_selected, |this| { .when(is_selected, |this| {
this.child(deferred( this.child(deferred(
anchored() anchored()
.anchor(gpui::Corner::TopLeft) .anchor(gpui::Anchor::TopLeft)
.snap_to_window_with_margin(px(8.)) .snap_to_window_with_margin(px(8.))
.child( .child(
div() div()

View File

@@ -3,10 +3,10 @@ use std::rc::Rc;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
anchored, deferred, div, px, AnyElement, App, Context, Corner, DismissEvent, Element, Anchor, AnyElement, App, Context, DismissEvent, Element, ElementId, Entity, Focusable,
ElementId, Entity, Focusable, GlobalElementId, Hitbox, HitboxBehavior, InspectorElementId, GlobalElementId, Hitbox, HitboxBehavior, InspectorElementId, InteractiveElement, IntoElement,
InteractiveElement, IntoElement, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, StyleRefinement, Styled,
StyleRefinement, Styled, Subscription, Window, Subscription, Window, anchored, deferred, div, px,
}; };
use crate::menu::PopupMenu; use crate::menu::PopupMenu;
@@ -41,7 +41,7 @@ pub struct ContextMenu<E: ParentElement + Styled + Sized> {
menu: Option<Rc<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu>>, menu: Option<Rc<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu>>,
// This is not in use, just for style refinement forwarding. // This is not in use, just for style refinement forwarding.
_ignore_style: StyleRefinement, _ignore_style: StyleRefinement,
anchor: Corner, anchor: Anchor,
} }
impl<E: ParentElement + Styled> ContextMenu<E> { impl<E: ParentElement + Styled> ContextMenu<E> {
@@ -51,7 +51,7 @@ impl<E: ParentElement + Styled> ContextMenu<E> {
id: id.into(), id: id.into(),
element: Some(element), element: Some(element),
menu: None, menu: None,
anchor: Corner::TopLeft, anchor: Anchor::TopLeft,
_ignore_style: StyleRefinement::default(), _ignore_style: StyleRefinement::default(),
} }
} }

View File

@@ -1,7 +1,7 @@
use std::rc::Rc; use std::rc::Rc;
use gpui::{ use gpui::{
Context, Corner, DismissEvent, ElementId, Entity, Focusable, InteractiveElement, IntoElement, Anchor, Context, DismissEvent, ElementId, Entity, Focusable, InteractiveElement, IntoElement,
RenderOnce, SharedString, StyleRefinement, Styled, Window, RenderOnce, SharedString, StyleRefinement, Styled, Window,
}; };
@@ -18,13 +18,13 @@ pub trait DropdownMenu: Styled + Selectable + InteractiveElement + IntoElement +
self, self,
f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static, f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> DropdownMenuPopover<Self> { ) -> DropdownMenuPopover<Self> {
self.dropdown_menu_with_anchor(Corner::TopLeft, f) self.dropdown_menu_with_anchor(Anchor::TopLeft, f)
} }
/// Create a dropdown menu with the given items, anchored to the given corner /// Create a dropdown menu with the given items, anchored to the given corner
fn dropdown_menu_with_anchor( fn dropdown_menu_with_anchor(
mut self, mut self,
anchor: impl Into<Corner>, anchor: impl Into<Anchor>,
f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static, f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> DropdownMenuPopover<Self> { ) -> DropdownMenuPopover<Self> {
let style = self.style().clone(); let style = self.style().clone();
@@ -42,7 +42,7 @@ impl DropdownMenu for Avatar {}
pub struct DropdownMenuPopover<T: Selectable + IntoElement + 'static> { pub struct DropdownMenuPopover<T: Selectable + IntoElement + 'static> {
id: ElementId, id: ElementId,
style: StyleRefinement, style: StyleRefinement,
anchor: Corner, anchor: Anchor,
trigger: T, trigger: T,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
builder: Rc<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu>, builder: Rc<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu>,
@@ -54,7 +54,7 @@ where
{ {
fn new( fn new(
id: ElementId, id: ElementId,
anchor: impl Into<Corner>, anchor: impl Into<Anchor>,
trigger: T, trigger: T,
builder: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static, builder: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> Self { ) -> Self {
@@ -68,7 +68,7 @@ where
} }
/// Set the anchor corner for the dropdown menu popover. /// Set the anchor corner for the dropdown menu popover.
pub fn anchor(mut self, anchor: impl Into<Corner>) -> Self { pub fn anchor(mut self, anchor: impl Into<Anchor>) -> Self {
self.anchor = anchor.into(); self.anchor = anchor.into();
self self
} }

View File

@@ -2,7 +2,7 @@ use std::rc::Rc;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
Action, AnyElement, App, AppContext, Axis, Bounds, ClickEvent, Context, Corner, DismissEvent, Action, Anchor, AnyElement, App, AppContext, Axis, Bounds, ClickEvent, Context, DismissEvent,
Edges, Entity, EventEmitter, FocusHandle, Focusable, Half, InteractiveElement, IntoElement, Edges, Entity, EventEmitter, FocusHandle, Focusable, Half, InteractiveElement, IntoElement,
KeyBinding, MouseDownEvent, OwnedMenuItem, ParentElement, Pixels, Point, Render, ScrollHandle, KeyBinding, MouseDownEvent, OwnedMenuItem, ParentElement, Pixels, Point, Render, ScrollHandle,
SharedString, StatefulInteractiveElement, Styled, Subscription, WeakEntity, Window, anchored, SharedString, StatefulInteractiveElement, Styled, Subscription, WeakEntity, Window, anchored,
@@ -299,7 +299,7 @@ pub struct PopupMenu {
scroll_handle: ScrollHandle, scroll_handle: ScrollHandle,
/// This will update on render /// This will update on render
submenu_anchor: (Corner, Pixels), submenu_anchor: (Anchor, Pixels),
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
} }
@@ -322,7 +322,7 @@ impl PopupMenu {
scroll_handle: ScrollHandle::default(), scroll_handle: ScrollHandle::default(),
external_link_icon: true, external_link_icon: true,
size: Size::default(), size: Size::default(),
submenu_anchor: (Corner::TopLeft, Pixels::ZERO), submenu_anchor: (Anchor::TopLeft, Pixels::ZERO),
_subscriptions: vec![], _subscriptions: vec![],
} }
} }
@@ -840,7 +840,7 @@ impl PopupMenu {
} }
fn select_left(&mut self, _: &SelectLeft, window: &mut Window, cx: &mut Context<Self>) { fn select_left(&mut self, _: &SelectLeft, window: &mut Window, cx: &mut Context<Self>) {
let handled = if matches!(self.submenu_anchor.0, Corner::TopLeft | Corner::BottomLeft) { let handled = if matches!(self.submenu_anchor.0, Anchor::TopLeft | Anchor::BottomLeft) {
self._unselect_submenu(window, cx) self._unselect_submenu(window, cx)
} else { } else {
self._select_submenu(window, cx) self._select_submenu(window, cx)
@@ -861,7 +861,7 @@ impl PopupMenu {
} }
fn select_right(&mut self, _: &SelectRight, window: &mut Window, cx: &mut Context<Self>) { fn select_right(&mut self, _: &SelectRight, window: &mut Window, cx: &mut Context<Self>) {
let handled = if matches!(self.submenu_anchor.0, Corner::TopLeft | Corner::BottomLeft) { let handled = if matches!(self.submenu_anchor.0, Anchor::TopLeft | Anchor::BottomLeft) {
self._select_submenu(window, cx) self._select_submenu(window, cx)
} else { } else {
self._unselect_submenu(window, cx) self._unselect_submenu(window, cx)
@@ -930,8 +930,9 @@ impl PopupMenu {
}; };
match parent.read(cx).submenu_anchor.0 { match parent.read(cx).submenu_anchor.0 {
Corner::TopLeft | Corner::BottomLeft => Side::Left, Anchor::TopLeft | Anchor::BottomLeft => Side::Left,
Corner::TopRight | Corner::BottomRight => Side::Right, Anchor::TopRight | Anchor::BottomRight => Side::Right,
_ => Side::Left,
} }
} }
@@ -1041,14 +1042,14 @@ impl PopupMenu {
let bounds = self.bounds; let bounds = self.bounds;
let max_width = self.max_width(); let max_width = self.max_width();
let (anchor, left) = if max_width + bounds.origin.x > window.bounds().size.width { let (anchor, left) = if max_width + bounds.origin.x > window.bounds().size.width {
(Corner::TopRight, -px(16.)) (Anchor::TopRight, -px(16.))
} else { } else {
(Corner::TopLeft, bounds.size.width - px(8.)) (Anchor::TopLeft, bounds.size.width - px(8.))
}; };
let is_bottom_pos = bounds.origin.y + bounds.size.height > window.bounds().size.height; let is_bottom_pos = bounds.origin.y + bounds.size.height > window.bounds().size.height;
self.submenu_anchor = if is_bottom_pos { self.submenu_anchor = if is_bottom_pos {
(anchor.other_side_corner_along(gpui::Axis::Vertical), left) (anchor.other_side_along(gpui::Axis::Vertical), left)
} else { } else {
(anchor, left) (anchor, left)
}; };
@@ -1230,7 +1231,7 @@ impl PopupMenu {
this.child({ this.child({
let (anchor, left) = self.submenu_anchor; let (anchor, left) = self.submenu_anchor;
let is_bottom_pos = let is_bottom_pos =
matches!(anchor, Corner::BottomLeft | Corner::BottomRight); matches!(anchor, Anchor::BottomLeft | Anchor::BottomRight);
anchored() anchored()
.anchor(anchor) .anchor(anchor)
.child( .child(

View File

@@ -5,12 +5,12 @@ use std::time::Duration;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
Animation, AnimationExt, AnyElement, App, AppContext, ClickEvent, Context, DismissEvent, Anchor, Animation, AnimationExt, AnyElement, App, AppContext, ClickEvent, Context,
ElementId, Entity, EventEmitter, InteractiveElement as _, IntoElement, ParentElement as _, DismissEvent, ElementId, Entity, EventEmitter, InteractiveElement as _, IntoElement,
Render, SharedString, StatefulInteractiveElement, StyleRefinement, Styled, Subscription, ParentElement as _, Render, SharedString, StatefulInteractiveElement, StyleRefinement, Styled,
Window, div, px, relative, Subscription, Window, div, px, relative,
}; };
use theme::{ActiveTheme, Anchor}; use theme::ActiveTheme;
use crate::animation::cubic_bezier; use crate::animation::cubic_bezier;
use crate::button::{Button, ButtonVariants as _}; use crate::button::{Button, ButtonVariants as _};
@@ -288,6 +288,7 @@ impl Styled for Notification {
&mut self.style &mut self.style
} }
} }
impl Render for Notification { impl Render for Notification {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let content = self let content = self
@@ -423,12 +424,17 @@ impl Render for Notification {
let y_offset = px(0.) + delta * px(45.); let y_offset = px(0.) + delta * px(45.);
that.top(px(0.) + y_offset) that.top(px(0.) + y_offset)
} }
_ => that,
} }
} else { } else {
let opacity = delta; let opacity = delta;
let y_offset = match placement { let y_offset = match placement {
placement if placement.is_top() => px(-45.) + delta * px(45.), Anchor::TopLeft | Anchor::TopRight | Anchor::TopCenter => {
placement if placement.is_bottom() => px(45.) - delta * px(45.), px(-45.) + delta * px(45.)
}
Anchor::BottomLeft | Anchor::BottomRight | Anchor::BottomCenter => {
px(45.) - delta * px(45.)
}
_ => px(0.), _ => px(0.),
}; };
this.top(px(0.) + y_offset) this.top(px(0.) + y_offset)

View File

@@ -2,15 +2,14 @@ use std::rc::Rc;
use gpui::prelude::FluentBuilder as _; use gpui::prelude::FluentBuilder as _;
use gpui::{ use gpui::{
AnyElement, App, Bounds, Context, Deferred, DismissEvent, Div, ElementId, EventEmitter, Anchor, AnyElement, App, Bounds, Context, Deferred, DismissEvent, Div, ElementId, EventEmitter,
FocusHandle, Focusable, Half, InteractiveElement as _, IntoElement, KeyBinding, MouseButton, FocusHandle, Focusable, InteractiveElement as _, IntoElement, KeyBinding, MouseButton,
ParentElement, Pixels, Point, Render, RenderOnce, Stateful, StyleRefinement, Styled, ParentElement, Pixels, Point, Render, RenderOnce, Stateful, StyleRefinement, Styled,
Subscription, Window, deferred, div, px, Subscription, Window, anchored, deferred, div, px,
}; };
use theme::Anchor;
use crate::actions::Cancel; use crate::actions::Cancel;
use crate::{ElementExt, Selectable, StyledExt as _, anchored, v_flex}; use crate::{ElementExt, Selectable, StyledExt as _, v_flex};
const CONTEXT: &str = "Popover"; const CONTEXT: &str = "Popover";
@@ -175,19 +174,26 @@ impl Popover {
self self
} }
fn resolved_corner(anchor: Anchor, trigger_bounds: Bounds<Pixels>) -> Point<Pixels> { pub(crate) fn resolved_corner(anchor: Anchor, trigger_bounds: Bounds<Pixels>) -> Point<Pixels> {
let offset = if anchor.is_center() { match anchor {
gpui::point(trigger_bounds.size.width.half(), px(0.)) Anchor::TopLeft => trigger_bounds.origin,
} else { Anchor::TopCenter => trigger_bounds.top_center(),
Point::default() Anchor::TopRight => trigger_bounds.top_right(),
}; Anchor::BottomLeft => Point {
x: trigger_bounds.origin.x,
trigger_bounds.corner(anchor.swap_vertical().into()) y: trigger_bounds.origin.y - trigger_bounds.size.height,
+ offset },
+ Point { Anchor::BottomCenter => Point {
x: px(0.), x: trigger_bounds.top_center().x,
y: -trigger_bounds.size.height, y: trigger_bounds.origin.y - trigger_bounds.size.height,
} },
Anchor::BottomRight => Point {
x: trigger_bounds.top_right().x,
y: trigger_bounds.origin.y - trigger_bounds.size.height,
},
// Fallback for LeftCenter/RightCenter adjust as needed.
_ => trigger_bounds.origin,
}
} }
} }
@@ -330,6 +336,7 @@ impl Popover {
.map(|this| match anchor { .map(|this| match anchor {
Anchor::TopLeft | Anchor::TopCenter | Anchor::TopRight => this.top_1(), Anchor::TopLeft | Anchor::TopCenter | Anchor::TopRight => this.top_1(),
Anchor::BottomLeft | Anchor::BottomCenter | Anchor::BottomRight => this.bottom_1(), Anchor::BottomLeft | Anchor::BottomCenter | Anchor::BottomRight => this.bottom_1(),
Anchor::LeftCenter | Anchor::RightCenter => this.top_1(), // Fallback for centered
}) })
} }
} }

View File

@@ -5,7 +5,7 @@ use std::rc::Rc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use gpui::{ use gpui::{
App, Axis, BorderStyle, Bounds, ContentMask, Corner, CursorStyle, Edges, Element, ElementId, Anchor, App, Axis, BorderStyle, Bounds, ContentMask, CursorStyle, Edges, Element, ElementId,
GlobalElementId, Hitbox, HitboxBehavior, Hsla, InspectorElementId, IntoElement, IsZero, GlobalElementId, Hitbox, HitboxBehavior, Hsla, InspectorElementId, IntoElement, IsZero,
LayoutId, ListState, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PaintQuad, Pixels, Point, LayoutId, ListState, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PaintQuad, Pixels, Point,
Position, ScrollHandle, ScrollWheelEvent, Size, Style, UniformListScrollHandle, Window, fill, Position, ScrollHandle, ScrollWheelEvent, Size, Style, UniformListScrollHandle, Window, fill,
@@ -648,14 +648,14 @@ impl Element for Scrollbar {
// The clickable area of the thumb // The clickable area of the thumb
let thumb_length = thumb_end - thumb_start - inset * 2; let thumb_length = thumb_end - thumb_start - inset * 2;
let thumb_bounds = if is_vertical { let thumb_bounds = if is_vertical {
Bounds::from_corner_and_size( Bounds::from_anchor_and_size(
Corner::TopRight, Anchor::TopRight,
bounds.top_right() + point(-inset, inset + thumb_start), bounds.top_right() + point(-inset, inset + thumb_start),
size(WIDTH, thumb_length), size(WIDTH, thumb_length),
) )
} else { } else {
Bounds::from_corner_and_size( Bounds::from_anchor_and_size(
Corner::BottomLeft, Anchor::BottomLeft,
bounds.bottom_left() + point(inset + thumb_start, -inset), bounds.bottom_left() + point(inset + thumb_start, -inset),
size(thumb_length, WIDTH), size(thumb_length, WIDTH),
) )
@@ -663,14 +663,14 @@ impl Element for Scrollbar {
// The actual render area of the thumb // The actual render area of the thumb
let thumb_fill_bounds = if is_vertical { let thumb_fill_bounds = if is_vertical {
Bounds::from_corner_and_size( Bounds::from_anchor_and_size(
Corner::TopRight, Anchor::TopRight,
bounds.top_right() + point(-inset, inset + thumb_start), bounds.top_right() + point(-inset, inset + thumb_start),
size(thumb_width, thumb_length), size(thumb_width, thumb_length),
) )
} else { } else {
Bounds::from_corner_and_size( Bounds::from_anchor_and_size(
Corner::BottomLeft, Anchor::BottomLeft,
bounds.bottom_left() + point(inset + thumb_start, -inset), bounds.bottom_left() + point(inset + thumb_start, -inset),
size(thumb_length, thumb_width), size(thumb_length, thumb_width),
) )

View File

@@ -2,7 +2,7 @@ use std::rc::Rc;
use gpui::prelude::FluentBuilder as _; use gpui::prelude::FluentBuilder as _;
use gpui::{ use gpui::{
AnyElement, App, Corner, Div, Edges, ElementId, InteractiveElement, IntoElement, ParentElement, Anchor, AnyElement, App, Div, Edges, ElementId, InteractiveElement, IntoElement, ParentElement,
RenderOnce, ScrollHandle, Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled, RenderOnce, ScrollHandle, Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled,
Window, div, px, Window, div, px,
}; };
@@ -282,7 +282,7 @@ impl RenderOnce for TabBar {
this this
}) })
.anchor(Corner::TopRight), .anchor(Anchor::TopRight),
) )
}) })
.when_some(self.suffix, |this, suffix| this.child(suffix)) .when_some(self.suffix, |this, suffix| this.child(suffix))