chore: fix menu anchoring to trigger bounds
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

This commit is contained in:
2026-11-28 14:30:57 +07:00
parent 3c5fb68f7e
commit cf81443411
14 changed files with 239 additions and 185 deletions
+11 -2
View File
@@ -41,7 +41,7 @@ use crate::components::common::{
};
use crate::components::empty_state::EmptyState;
use crate::components::input::{Input, InputState, InputStateEvent};
use crate::components::menu::anchored_overlay;
use crate::components::menu::{TriggerAnchor, TriggerBoundsCell, anchored_overlay};
use crate::icons::{Icon, IconRole};
use crate::theme::{ActiveTheme as _, COMBOBOX_MIN_WIDTH, COMBOBOX_WIDTH, POPUP_MAX_HEIGHT, Size};
@@ -588,7 +588,13 @@ where
let id = self.id.clone();
let open = self.highlight.is_some();
let weak = cx.weak_entity();
let wrapper_bounds: Rc<RefCell<Option<Bounds<Pixels>>>> = Rc::new(RefCell::new(None));
let wrapper_bounds: TriggerBoundsCell = window
.use_keyed_state((id.clone(), "wrapper-bounds"), cx, |_, _| {
Rc::new(RefCell::new(None)) as TriggerBoundsCell
})
.read(cx)
.clone();
div()
.id(id.clone())
@@ -725,6 +731,9 @@ where
wrapper.child(anchored_overlay(
None,
wrapper_bounds
.borrow()
.map(|bounds| TriggerAnchor::below(window, bounds)),
v_flex()
.id((id.clone(), "panel"))
.debug_selector({
+56 -12
View File
@@ -27,8 +27,8 @@ use std::time::Duration;
use gpui::prelude::FluentBuilder;
use gpui::{
Animation, AnimationExt as _, AnyElement, App, Bounds, DefiniteLength, DispatchPhase, Div,
ElementId, Entity, FocusHandle, Hsla, InteractiveElement as _, IntoElement, KeyBinding,
Anchor, Animation, AnimationExt as _, AnyElement, App, Bounds, DefiniteLength, DispatchPhase,
Div, ElementId, Entity, FocusHandle, Hsla, InteractiveElement as _, IntoElement, KeyBinding,
ListAlignment, ListSizingBehavior, ListState, MouseButton, MouseDownEvent, ParentElement as _,
Pixels, Point, RenderOnce, Role, SharedString, Stateful, StatefulInteractiveElement as _,
StyleRefinement, Styled, Window, actions, anchored, canvas, deferred, div, ease_out_quint,
@@ -368,6 +368,7 @@ impl MenuItem {
.role(Role::MenuItem)
.debug_selector(move || format!("{}-item-{index}", env.debug_prefix))
.flex()
.w_full()
.items_center()
.gap_2()
.h_8()
@@ -608,7 +609,13 @@ impl Styled for Menu {
impl RenderOnce for Menu {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let (chrome, enter_duration) = MenuChrome::read(cx);
let trigger_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None));
// Keyed so the panel can anchor to bounds captured by the last paint.
let trigger_bounds: TriggerBoundsCell = window
.use_keyed_state((self.id.clone(), "trigger-bounds"), cx, |_, _| {
Rc::new(RefCell::new(None)) as TriggerBoundsCell
})
.read(cx)
.clone();
let on_change = self.on_change.clone();
let panel_focus = ElementId::from((self.id.clone(), "panel")).focus_handle(window, cx);
@@ -706,6 +713,10 @@ impl MenuPanelSpec {
return None;
}
let target = self
.trigger_bounds
.borrow()
.map(|bounds| TriggerAnchor::below(window, bounds));
let state = MenuPanelState::new(id, window, cx);
let panel = state.panel(
window,
@@ -729,7 +740,7 @@ impl MenuPanelSpec {
},
);
Some(anchored_overlay(self.position, panel, None))
Some(anchored_overlay(self.position, target, panel, None))
}
}
@@ -1245,9 +1256,39 @@ pub(crate) fn trigger_toggle(
.inset_0()
}
/// Where an overlay hangs off its trigger.
#[derive(Clone, Copy)]
pub(crate) struct TriggerAnchor {
target: Bounds<Pixels>,
/// The panel's corner that meets the trigger.
anchor: Anchor,
/// The trigger's corner the panel hangs from.
corner: Anchor,
}
impl TriggerAnchor {
/// Hangs the panel below the trigger, aligned to the trigger edge nearer the window's center.
pub(crate) fn below(window: &Window, target: Bounds<Pixels>) -> Self {
if target.center().x > window.viewport_size().width / 2.0 {
Self {
target,
anchor: Anchor::TopRight,
corner: Anchor::BottomRight,
}
} else {
Self {
target,
anchor: Anchor::TopLeft,
corner: Anchor::BottomLeft,
}
}
}
}
/// Deferred anchored overlay on OVERLAY_LAYER, optionally fading in.
pub(crate) fn anchored_overlay<E: IntoElement>(
position: Option<Point<Pixels>>,
trigger: Option<TriggerAnchor>,
panel: E,
fade: Option<(&'static str, Duration)>,
) -> AnyElement {
@@ -1263,14 +1304,17 @@ pub(crate) fn anchored_overlay<E: IntoElement>(
None => div().child(panel).into_any_element(),
};
deferred(
anchored()
.when_some(position, |anchored, position| anchored.position(position))
.offset(point(px(0.), px(4.)))
.child(wrapper),
)
.with_priority(OVERLAY_LAYER)
.into_any_element()
let overlay = match (position, trigger) {
(Some(position), _) => anchored().position(position),
(None, Some(trigger)) => anchored()
.anchor(trigger.anchor)
.attach(trigger.target, trigger.corner),
(None, None) => anchored(),
};
deferred(overlay.offset(point(px(0.), px(4.))).child(wrapper))
.with_priority(OVERLAY_LAYER)
.into_any_element()
}
/// Canvas that grabs focus when `should_grab` holds.
+14 -2
View File
@@ -32,7 +32,8 @@ use gpui::{
use crate::components::common::{ElementIdExt as _, StyledExt as _, v_flex};
use crate::components::dialog::Dismiss;
use crate::components::menu::{
ChangeHandler, TriggerBoundsCell, anchored_overlay, focus_capture, trigger_toggle,
ChangeHandler, TriggerAnchor, TriggerBoundsCell, anchored_overlay, focus_capture,
trigger_toggle,
};
use crate::theme::{ActiveTheme as _, POPOVER_MIN_WIDTH};
@@ -135,7 +136,12 @@ impl RenderOnce for Popover {
)
};
let trigger_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None));
let trigger_bounds: TriggerBoundsCell = window
.use_keyed_state((self.id.clone(), "trigger-bounds"), cx, |_, _| {
Rc::new(RefCell::new(None)) as TriggerBoundsCell
})
.read(cx)
.clone();
// Keyboard focus rides the panel across frames.
let focus = self.id.focus_handle(window, cx);
@@ -163,8 +169,13 @@ impl RenderOnce for Popover {
let label = fallback_label.clone();
let content = self.content.map(|content| content(window, cx));
let target = trigger_bounds
.borrow()
.map(|bounds| TriggerAnchor::below(window, bounds));
wrapper.child(anchored_overlay(
None,
target,
v_flex()
.id((self.id.clone(), "popover-panel"))
.debug_selector(|| "popover-panel".to_string())
@@ -180,6 +191,7 @@ impl RenderOnce for Popover {
.on_mouse_down_out({
let on_change = on_change.clone();
let trigger_bounds = trigger_bounds.clone();
move |event, window, cx| {
if trigger_bounds
.borrow()
+7 -26
View File
@@ -1,13 +1,11 @@
//! Signed's icon set: application-defined SVG glyphs.
use std::borrow::Cow;
use std::time::Duration;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AbsoluteLength, App, AssetSource, Div, Hsla, IntoElement, ParentElement as _, Pixels, Rems,
RenderOnce, Result, SharedString, Styled as _, TimedTransformation, Transformation, Window,
div, rems, svg,
AbsoluteLength, App, Div, Hsla, IntoElement, ParentElement as _, Pixels, Rems, RenderOnce,
SharedString, Styled as _, TimedTransformation, Transformation, Window, div, rems, svg,
};
const SMALL_ICON_SIZE: Rems = rems(0.875);
@@ -16,6 +14,11 @@ const LARGE_ICON_SIZE: Rems = rems(1.25);
/// The spin presents at most this rate, rotating a sprite needs no more frames.
const SPIN_MAX_FPS: f32 = 60.0;
#[derive(rust_embed::RustEmbed)]
#[folder = "src/icons/assets"]
#[exclude = "*.DS_Store"]
pub struct Assets;
/// The semantic glyphs UI components need.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IconRole {
@@ -313,25 +316,3 @@ impl RenderOnce for Icon {
Self::render_svg_path(size, Some(color), transformation, rotation_period, path)
}
}
#[derive(rust_embed::RustEmbed)]
#[folder = "src/icons/assets"]
#[exclude = "*.DS_Store"]
pub struct Assets;
impl AssetSource for Assets {
fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
let name = match path.strip_prefix("icons/") {
Some(name) => name,
None => return Ok(None),
};
Ok(Assets::get(name).map(|file| file.data))
}
fn list(&self, path: &str) -> gpui::Result<Vec<SharedString>> {
Ok(Self::iter()
.map(|file| SharedString::from(format!("icons/{file}")))
.filter(|file| file.starts_with(path))
.collect())
}
}