Add Corner to geometry and make names of corner methods consistent (#22119)

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2024-12-16 22:57:15 -07:00 committed by GitHub
parent 3052fc2565
commit fc5a810408
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 325 additions and 254 deletions

View file

@ -1,5 +1,5 @@
#![allow(missing_docs)]
use gpui::{AnchorCorner, ClickEvent, CursorStyle, MouseButton, View};
use gpui::{ClickEvent, Corner, CursorStyle, MouseButton, View};
use crate::{prelude::*, ContextMenu, PopoverMenu};
@ -46,7 +46,7 @@ impl RenderOnce for DropdownMenu {
.full_width(self.full_width)
.menu(move |_cx| Some(self.menu.clone()))
.trigger(DropdownMenuTrigger::new(self.label).full_width(self.full_width))
.attach(AnchorCorner::BottomLeft)
.attach(Corner::BottomLeft)
}
}

View file

@ -3,10 +3,10 @@
use std::{cell::RefCell, rc::Rc};
use gpui::{
anchored, deferred, div, point, prelude::FluentBuilder, px, size, AnchorCorner, AnyElement,
Bounds, DismissEvent, DispatchPhase, Element, ElementId, GlobalElementId, HitboxId,
InteractiveElement, IntoElement, LayoutId, Length, ManagedView, MouseDownEvent, ParentElement,
Pixels, Point, Style, View, VisualContext, WindowContext,
anchored, deferred, div, point, prelude::FluentBuilder, px, size, AnyElement, Bounds, Corner,
DismissEvent, DispatchPhase, Element, ElementId, GlobalElementId, HitboxId, InteractiveElement,
IntoElement, LayoutId, Length, ManagedView, MouseDownEvent, ParentElement, Pixels, Point,
Style, View, VisualContext, WindowContext,
};
use crate::prelude::*;
@ -89,8 +89,8 @@ pub struct PopoverMenu<M: ManagedView> {
>,
>,
menu_builder: Option<Rc<dyn Fn(&mut WindowContext) -> Option<View<M>> + 'static>>,
anchor: AnchorCorner,
attach: Option<AnchorCorner>,
anchor: Corner,
attach: Option<Corner>,
offset: Option<Point<Pixels>>,
trigger_handle: Option<PopoverMenuHandle<M>>,
full_width: bool,
@ -103,7 +103,7 @@ impl<M: ManagedView> PopoverMenu<M> {
id: id.into(),
child_builder: None,
menu_builder: None,
anchor: AnchorCorner::TopLeft,
anchor: Corner::TopLeft,
attach: None,
offset: None,
trigger_handle: None,
@ -140,13 +140,13 @@ impl<M: ManagedView> PopoverMenu<M> {
/// anchor defines which corner of the menu to anchor to the attachment point
/// (by default the cursor position, but see attach)
pub fn anchor(mut self, anchor: AnchorCorner) -> Self {
pub fn anchor(mut self, anchor: Corner) -> Self {
self.anchor = anchor;
self
}
/// attach defines which corner of the handle to attach the menu's anchor to
pub fn attach(mut self, attach: AnchorCorner) -> Self {
pub fn attach(mut self, attach: Corner) -> Self {
self.attach = Some(attach);
self
}
@ -157,12 +157,12 @@ impl<M: ManagedView> PopoverMenu<M> {
self
}
fn resolved_attach(&self) -> AnchorCorner {
fn resolved_attach(&self) -> Corner {
self.attach.unwrap_or(match self.anchor {
AnchorCorner::TopLeft => AnchorCorner::BottomLeft,
AnchorCorner::TopRight => AnchorCorner::BottomRight,
AnchorCorner::BottomLeft => AnchorCorner::TopLeft,
AnchorCorner::BottomRight => AnchorCorner::TopRight,
Corner::TopLeft => Corner::BottomLeft,
Corner::TopRight => Corner::BottomRight,
Corner::BottomLeft => Corner::TopLeft,
Corner::BottomRight => Corner::TopRight,
})
}
@ -171,8 +171,8 @@ impl<M: ManagedView> PopoverMenu<M> {
// Default offset = 4px padding + 1px border
let offset = rems_from_px(5.) * cx.rem_size();
match self.anchor {
AnchorCorner::TopRight | AnchorCorner::BottomRight => point(offset, px(0.)),
AnchorCorner::TopLeft | AnchorCorner::BottomLeft => point(-offset, px(0.)),
Corner::TopRight | Corner::BottomRight => point(offset, px(0.)),
Corner::TopLeft | Corner::BottomLeft => point(-offset, px(0.)),
}
})
}
@ -259,7 +259,7 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
.anchor(self.anchor);
if let Some(child_bounds) = element_state.child_bounds {
anchored = anchored.position(
self.resolved_attach().corner(child_bounds) + self.resolved_offset(cx),
child_bounds.corner(self.resolved_attach()) + self.resolved_offset(cx),
);
}
let mut element = deferred(anchored.child(div().occlude().child(menu.clone())))

View file

@ -3,18 +3,17 @@
use std::{cell::RefCell, rc::Rc};
use gpui::{
anchored, deferred, div, px, AnchorCorner, AnyElement, Bounds, DismissEvent, DispatchPhase,
Element, ElementId, GlobalElementId, Hitbox, InteractiveElement, IntoElement, LayoutId,
ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, View, VisualContext,
WindowContext,
anchored, deferred, div, px, AnyElement, Bounds, Corner, DismissEvent, DispatchPhase, Element,
ElementId, GlobalElementId, Hitbox, InteractiveElement, IntoElement, LayoutId, ManagedView,
MouseButton, MouseDownEvent, ParentElement, Pixels, Point, View, VisualContext, WindowContext,
};
pub struct RightClickMenu<M: ManagedView> {
id: ElementId,
child_builder: Option<Box<dyn FnOnce(bool) -> AnyElement + 'static>>,
menu_builder: Option<Rc<dyn Fn(&mut WindowContext) -> View<M> + 'static>>,
anchor: Option<AnchorCorner>,
attach: Option<AnchorCorner>,
anchor: Option<Corner>,
attach: Option<Corner>,
}
impl<M: ManagedView> RightClickMenu<M> {
@ -30,13 +29,13 @@ impl<M: ManagedView> RightClickMenu<M> {
/// anchor defines which corner of the menu to anchor to the attachment point
/// (by default the cursor position, but see attach)
pub fn anchor(mut self, anchor: AnchorCorner) -> Self {
pub fn anchor(mut self, anchor: Corner) -> Self {
self.anchor = Some(anchor);
self
}
/// attach defines which corner of the handle to attach the menu's anchor to
pub fn attach(mut self, attach: AnchorCorner) -> Self {
pub fn attach(mut self, attach: Corner) -> Self {
self.attach = Some(attach);
self
}
@ -238,7 +237,7 @@ impl<M: ManagedView> Element for RightClickMenu<M> {
*menu.borrow_mut() = Some(new_menu);
*position.borrow_mut() = if let Some(child_bounds) = child_bounds {
if let Some(attach) = attach {
attach.corner(child_bounds)
child_bounds.corner(attach)
} else {
cx.mouse_position()
}

View file

@ -236,12 +236,12 @@ impl Element for Scrollbar {
let padded_bounds = if is_vertical {
Bounds::from_corners(
bounds.origin + point(Pixels::ZERO, extra_padding),
bounds.lower_right() - point(Pixels::ZERO, extra_padding * 3),
bounds.bottom_right() - point(Pixels::ZERO, extra_padding * 3),
)
} else {
Bounds::from_corners(
bounds.origin + point(extra_padding, Pixels::ZERO),
bounds.lower_right() - point(extra_padding * 3, Pixels::ZERO),
bounds.bottom_right() - point(extra_padding * 3, Pixels::ZERO),
)
};

View file

@ -1,4 +1,4 @@
use gpui::{actions, AnchorCorner, Render, View};
use gpui::{actions, Corner, Render, View};
use story::Story;
use crate::prelude::*;
@ -47,8 +47,8 @@ impl Render for ContextMenuStory {
.child(
right_click_menu("test1")
.trigger(Label::new("BOTTOM LEFT"))
.anchor(AnchorCorner::BottomLeft)
.attach(AnchorCorner::TopLeft)
.anchor(Corner::BottomLeft)
.attach(Corner::TopLeft)
.menu(move |cx| build_menu(cx, "bottom left")),
),
)
@ -60,14 +60,14 @@ impl Render for ContextMenuStory {
.child(
right_click_menu("test3")
.trigger(Label::new("TOP RIGHT"))
.anchor(AnchorCorner::TopRight)
.anchor(Corner::TopRight)
.menu(move |cx| build_menu(cx, "top right")),
)
.child(
right_click_menu("test4")
.trigger(Label::new("BOTTOM RIGHT"))
.anchor(AnchorCorner::BottomRight)
.attach(AnchorCorner::TopRight)
.anchor(Corner::BottomRight)
.attach(Corner::TopRight)
.menu(move |cx| build_menu(cx, "bottom right")),
),
)