chore: stabilize element ids and overlay focus tracking
This commit is contained in:
@@ -17,12 +17,10 @@ use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyElement, AnyView, App, ClickEvent, ElementId, Hsla, InteractiveElement as _, IntoElement,
|
||||
ParentElement, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
|
||||
StyleRefinement, Styled as _, Window, canvas, relative, transparent_black,
|
||||
StyleRefinement, Styled as _, Window, relative, transparent_black,
|
||||
};
|
||||
|
||||
use crate::components::common::{
|
||||
ControlSizing, Sizable, StyledExt as _, focus_ring, h_flex, record_anchor_bounds,
|
||||
};
|
||||
use crate::components::common::{ControlSizing, Sizable, StyledExt as _, focus_ring, h_flex};
|
||||
use crate::components::spinner::Spinner;
|
||||
use crate::icons::Icon;
|
||||
use crate::theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
|
||||
@@ -386,11 +384,6 @@ impl RenderOnce for Button {
|
||||
),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {})
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
)
|
||||
.when(self.disabled, |button| button.opacity(DISABLED_OPACITY))
|
||||
.when_some(self.tooltip, |button, tooltip| {
|
||||
button.tooltip(move |window, cx| tooltip(window, cx))
|
||||
|
||||
@@ -15,7 +15,7 @@ use std::time::Duration;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyView, App, ClipboardItem, ElementId, Entity, IntoElement, ParentElement as _, RenderOnce,
|
||||
AnyView, App, ClipboardItem, ElementId, IntoElement, ParentElement as _, RenderOnce,
|
||||
SharedString, StyleRefinement, Styled as _, Task, Window, div,
|
||||
};
|
||||
|
||||
@@ -108,18 +108,17 @@ impl Sizable for ClipboardButton {
|
||||
/// The transient copied marker keyed per button.
|
||||
struct CopiedState {
|
||||
generation: u64,
|
||||
tasks: Vec<Task<()>>,
|
||||
task: Option<Task<()>>,
|
||||
}
|
||||
|
||||
impl RenderOnce for ClipboardButton {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let copied: Entity<CopiedState> =
|
||||
window.use_keyed_state((self.id.clone(), "clipboard-copied"), cx, |_, _| {
|
||||
CopiedState {
|
||||
generation: 0,
|
||||
tasks: Vec::new(),
|
||||
}
|
||||
});
|
||||
let copied = window.use_keyed_state((self.id.clone(), "clipboard-copied"), cx, |_, _| {
|
||||
CopiedState {
|
||||
generation: 0,
|
||||
task: None,
|
||||
}
|
||||
});
|
||||
let is_copied = copied.read(cx).generation != 0;
|
||||
|
||||
let selector = if is_copied {
|
||||
@@ -172,7 +171,7 @@ impl RenderOnce for ClipboardButton {
|
||||
});
|
||||
|
||||
copied.update(cx, |state, _| {
|
||||
state.tasks.push(task);
|
||||
state.task = Some(task);
|
||||
});
|
||||
|
||||
// Invoke the callback after copying
|
||||
|
||||
@@ -10,13 +10,11 @@
|
||||
//! let column = v_flex().gap_2().child(row);
|
||||
//! ```
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, Bounds, BoxShadow, Div, ElementId, FocusHandle, FontWeight, Global, Hsla, IntoElement,
|
||||
Pixels, Refineable as _, Rems, StatefulInteractiveElement, StyleRefinement, Styled, Window,
|
||||
WindowId, div, px, rems,
|
||||
App, BoxShadow, Div, ElementId, FocusHandle, FontWeight, Hsla, IntoElement, Pixels,
|
||||
Refineable as _, Rems, SharedString, StatefulInteractiveElement, StyleRefinement, Styled,
|
||||
Window, div, px, rems,
|
||||
};
|
||||
|
||||
use crate::theme::{ActiveTheme as _, FOCUS_RING_ALPHA, FOCUS_RING_SPREAD, Size};
|
||||
@@ -31,22 +29,6 @@ pub const TOAST_LAYER: usize = 3;
|
||||
/// A handler told to clean up after a dismissed surface.
|
||||
pub type DismissHandler = std::rc::Rc<dyn Fn(&mut gpui::App)>;
|
||||
|
||||
/// The painted bounds of elements that tooltips anchor to, per window and element id.
|
||||
#[derive(Default)]
|
||||
struct AnchorBoundsRegistry(HashMap<(WindowId, ElementId), Bounds<Pixels>>);
|
||||
|
||||
impl Global for AnchorBoundsRegistry {}
|
||||
|
||||
impl AnchorBoundsRegistry {
|
||||
fn record(&mut self, window: WindowId, id: ElementId, bounds: Bounds<Pixels>) {
|
||||
self.0.insert((window, id), bounds);
|
||||
}
|
||||
|
||||
fn get(&self, window: WindowId, id: &ElementId) -> Option<Bounds<Pixels>> {
|
||||
self.0.get(&(window, id.clone())).copied()
|
||||
}
|
||||
}
|
||||
|
||||
/// A row whose children center on the cross axis.
|
||||
#[must_use]
|
||||
pub fn h_flex() -> Div {
|
||||
@@ -112,20 +94,47 @@ pub fn focus_handle(id: &ElementId, window: &mut Window, cx: &mut App) -> FocusH
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// The last bounds an element painted at, in the given window.
|
||||
pub fn anchor_bounds(window: WindowId, id: &ElementId, cx: &App) -> Option<Bounds<Pixels>> {
|
||||
cx.try_global::<AnchorBoundsRegistry>()
|
||||
.and_then(|registry| registry.get(window, id))
|
||||
}
|
||||
/// Saves the focused handle while an overlay is open and restores it when the
|
||||
/// overlay closes, unless another surface already took focus.
|
||||
pub(crate) fn track_overlay_focus(
|
||||
id: &ElementId,
|
||||
overlay_focus: &FocusHandle,
|
||||
open: bool,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let was_open = window.use_keyed_state(
|
||||
SharedString::from(format!("overlay-was-open:{id}")),
|
||||
cx,
|
||||
|_, _| false,
|
||||
);
|
||||
let previous = window.use_keyed_state(
|
||||
SharedString::from(format!("overlay-previous-focus:{id}")),
|
||||
cx,
|
||||
|_, _| None::<FocusHandle>,
|
||||
);
|
||||
|
||||
/// A canvas prepaint closure recording the element's painted bounds.
|
||||
pub fn record_anchor_bounds(
|
||||
id: ElementId,
|
||||
) -> impl Fn(Bounds<Pixels>, &mut Window, &mut App) + 'static {
|
||||
move |bounds, window, cx| {
|
||||
let window_id = window.window_handle().window_id();
|
||||
cx.default_global::<AnchorBoundsRegistry>()
|
||||
.record(window_id, id.clone(), bounds);
|
||||
let was_open_now = *was_open.read(cx);
|
||||
if open && !was_open_now {
|
||||
let focused = window.focused(cx);
|
||||
previous.update(cx, |previous, _| *previous = focused);
|
||||
was_open.update(cx, |was_open, _| *was_open = true);
|
||||
} else if !open && was_open_now {
|
||||
was_open.update(cx, |was_open, _| *was_open = false);
|
||||
let Some(restore) = previous.update(cx, |previous, _| previous.take()) else {
|
||||
return;
|
||||
};
|
||||
// Only take focus back from the closing overlay itself; a surface that
|
||||
// replaced it owns focus now.
|
||||
let overlay_focus = overlay_focus.clone();
|
||||
window.defer(cx, move |window, cx| {
|
||||
let holds_focus = window
|
||||
.focused(cx)
|
||||
.is_none_or(|current| current == overlay_focus);
|
||||
if holds_focus && restore.is_rendered(window) {
|
||||
window.focus(&restore, cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+148
-144
@@ -34,7 +34,8 @@ use gpui::{
|
||||
|
||||
use crate::components::button::{Button, ButtonVariant};
|
||||
use crate::components::common::{
|
||||
DIALOG_LAYER, Sizable, StyledExt as _, floating_panel, h_flex, v_flex,
|
||||
DIALOG_LAYER, Sizable, StyledExt as _, floating_panel, focus_handle, h_flex,
|
||||
track_overlay_focus, v_flex,
|
||||
};
|
||||
use crate::components::grain::grain;
|
||||
use crate::components::scroll_area::ScrollArea;
|
||||
@@ -254,6 +255,10 @@ impl RenderOnce for Dialog {
|
||||
.as_ref()
|
||||
.map_or(self.open, DialogHandle::is_open);
|
||||
|
||||
// Keyboard focus rides the panel across frames.
|
||||
let focus = focus_handle(&self.id, window, cx);
|
||||
track_overlay_focus(&self.id, &focus, open, window, cx);
|
||||
|
||||
if !open {
|
||||
return root.into_any_element();
|
||||
}
|
||||
@@ -296,163 +301,162 @@ impl RenderOnce for Dialog {
|
||||
let close_on_backdrop = self.close_on_backdrop;
|
||||
let show_close = !alert && on_change.is_some() && (close_on_escape || close_on_backdrop);
|
||||
|
||||
// Keyboard focus rides the panel across frames.
|
||||
let focus = crate::components::common::focus_handle(&self.id, window, cx);
|
||||
|
||||
let content = self.content.map(|build| build(window, cx));
|
||||
let actions = self.actions.map(|build| build(window, cx));
|
||||
let height = self.height;
|
||||
|
||||
let scroll = window
|
||||
.use_keyed_state("dialog-content-scroll", cx, |_, _| ScrollHandle::new())
|
||||
.use_keyed_state((self.id.clone(), "dialog-content-scroll"), cx, |_, _| {
|
||||
ScrollHandle::new()
|
||||
})
|
||||
.read(cx)
|
||||
.clone();
|
||||
|
||||
let panel = floating_panel(
|
||||
cx,
|
||||
radius,
|
||||
v_flex()
|
||||
.id("dialog-panel")
|
||||
.track_focus(&focus)
|
||||
.role(role)
|
||||
.aria_label(label)
|
||||
.when_some(self.width, |panel, width| panel.w(width))
|
||||
.when(self.width.is_none() && self.min_width.is_none(), |panel| {
|
||||
panel.min_w(DIALOG_MIN_WIDTH)
|
||||
})
|
||||
.when_some(self.min_width, |panel, width| panel.min_w(width))
|
||||
.max_w(relative(0.9))
|
||||
.when_some(height, |panel, height| panel.h(height))
|
||||
.text_color(foreground),
|
||||
shadows,
|
||||
)
|
||||
// The panel behind the dialog is `background` (pure white/black), so
|
||||
// the dialog matches it instead of the muted `surface` tint.
|
||||
.bg(background)
|
||||
.overflow_hidden()
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_key_down({
|
||||
let on_change = on_change.clone();
|
||||
move |event: &KeyDownEvent, window, cx| {
|
||||
if event.keystroke.key == "escape"
|
||||
&& !alert
|
||||
&& close_on_escape
|
||||
&& let Some(on_change) = &on_change
|
||||
{
|
||||
on_change(false, window, cx);
|
||||
}
|
||||
cx.stop_propagation();
|
||||
}
|
||||
})
|
||||
.when(self.title.is_some() || show_close, |panel| {
|
||||
panel.child(
|
||||
h_flex()
|
||||
.h_12()
|
||||
.px_2()
|
||||
.gap_2()
|
||||
.justify_between()
|
||||
.when(show_close, |row| row.child(div().w_7()))
|
||||
.when_some(self.title.clone(), |row, title| {
|
||||
row.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.text_center()
|
||||
.text_sm()
|
||||
.font_semibold()
|
||||
.child(title),
|
||||
)
|
||||
})
|
||||
.when(show_close, |row| {
|
||||
row.child(
|
||||
Button::new("dialog-close")
|
||||
.icon(IconRole::Close)
|
||||
.variant(ButtonVariant::Ghost)
|
||||
.small()
|
||||
.debug_selector(|| "dialog-close".to_string())
|
||||
.accessibility_label("Close")
|
||||
.on_click({
|
||||
let on_change = on_change.clone();
|
||||
move |_, window, cx| {
|
||||
if let Some(on_change) = &on_change {
|
||||
on_change(false, window, cx);
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
}),
|
||||
)
|
||||
})
|
||||
.when_some(content, |panel, content| {
|
||||
panel.map(|this| {
|
||||
if height.is_some() {
|
||||
this.child(
|
||||
ScrollArea::new("dialog-content")
|
||||
.handle(scroll)
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.child(div().p_2().child(content)),
|
||||
)
|
||||
} else {
|
||||
this.child(div().p_2().child(content))
|
||||
}
|
||||
})
|
||||
})
|
||||
.when_some(actions, |panel, actions| {
|
||||
panel.child(
|
||||
h_flex()
|
||||
.px_2()
|
||||
.h_12()
|
||||
.justify_end()
|
||||
.gap_2()
|
||||
.children(actions),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
canvas(|_, _, _| {}, {
|
||||
move |_, _, window, cx| {
|
||||
if !focus.contains_focused(window, cx) {
|
||||
window.focus(&focus, cx);
|
||||
}
|
||||
}
|
||||
})
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
);
|
||||
|
||||
let scrim = div()
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.bg(scrim_color)
|
||||
.occlude()
|
||||
.when(self.grain, |scrim| scrim.child(grain()))
|
||||
.when(!alert && close_on_backdrop, |scrim| {
|
||||
scrim.on_mouse_down(MouseButton::Left, {
|
||||
let on_change = on_change.clone();
|
||||
move |_, _window, cx| {
|
||||
if let Some(on_change) = &on_change {
|
||||
on_change(false, _window, cx);
|
||||
}
|
||||
cx.stop_propagation();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
deferred(
|
||||
root.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.p_4()
|
||||
.refine_style(&self.style)
|
||||
.child(scrim.with_animation(
|
||||
"dialog-scrim-enter",
|
||||
Animation::new(enter_duration).with_easing(ease_out_quint()),
|
||||
|scrim, delta| scrim.opacity(delta),
|
||||
))
|
||||
.child(panel.with_animation(
|
||||
"dialog-enter",
|
||||
Animation::new(enter_duration).with_easing(ease_out_quint()),
|
||||
|panel, delta| panel.opacity(delta),
|
||||
)),
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.bg(scrim_color)
|
||||
.occlude()
|
||||
.when(self.grain, |scrim| scrim.child(grain()))
|
||||
.when(!alert && close_on_backdrop, |scrim| {
|
||||
scrim.on_mouse_down(MouseButton::Left, {
|
||||
let on_change = on_change.clone();
|
||||
move |_, _window, cx| {
|
||||
if let Some(on_change) = &on_change {
|
||||
on_change(false, _window, cx);
|
||||
}
|
||||
cx.stop_propagation();
|
||||
}
|
||||
})
|
||||
})
|
||||
.with_animation(
|
||||
"dialog-scrim-enter",
|
||||
Animation::new(enter_duration).with_easing(ease_out_quint()),
|
||||
|scrim, delta| scrim.opacity(delta),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
floating_panel(
|
||||
cx,
|
||||
radius,
|
||||
v_flex()
|
||||
.id((self.id.clone(), "dialog-panel"))
|
||||
.track_focus(&focus)
|
||||
.role(role)
|
||||
.aria_label(label)
|
||||
.when_some(self.width, |panel, width| panel.w(width))
|
||||
.when(self.width.is_none() && self.min_width.is_none(), |panel| {
|
||||
panel.min_w(DIALOG_MIN_WIDTH)
|
||||
})
|
||||
.when_some(self.min_width, |panel, width| panel.min_w(width))
|
||||
.max_w(relative(0.9))
|
||||
.when_some(height, |panel, height| panel.h(height))
|
||||
.text_color(foreground),
|
||||
shadows,
|
||||
)
|
||||
.bg(background)
|
||||
.overflow_hidden()
|
||||
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
|
||||
.on_key_down({
|
||||
let on_change = on_change.clone();
|
||||
move |event: &KeyDownEvent, window, cx| {
|
||||
if event.keystroke.key == "escape"
|
||||
&& !alert
|
||||
&& close_on_escape
|
||||
&& let Some(on_change) = &on_change
|
||||
{
|
||||
on_change(false, window, cx);
|
||||
}
|
||||
cx.stop_propagation();
|
||||
}
|
||||
})
|
||||
.when(self.title.is_some() || show_close, |panel| {
|
||||
panel.child(
|
||||
h_flex()
|
||||
.h_12()
|
||||
.px_2()
|
||||
.gap_2()
|
||||
.justify_between()
|
||||
.when(show_close, |row| row.child(div().w_7()))
|
||||
.when_some(self.title.clone(), |row, title| {
|
||||
row.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.text_center()
|
||||
.text_sm()
|
||||
.font_semibold()
|
||||
.child(title),
|
||||
)
|
||||
})
|
||||
.when(show_close, |row| {
|
||||
row.child(
|
||||
Button::new((self.id.clone(), "dialog-close"))
|
||||
.icon(IconRole::Close)
|
||||
.variant(ButtonVariant::Ghost)
|
||||
.small()
|
||||
.debug_selector(|| "dialog-close".to_string())
|
||||
.accessibility_label("Close")
|
||||
.on_click({
|
||||
let on_change = on_change.clone();
|
||||
move |_, window, cx| {
|
||||
if let Some(on_change) = &on_change {
|
||||
on_change(false, window, cx);
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
}),
|
||||
)
|
||||
})
|
||||
.when_some(content, |panel, content| {
|
||||
panel.map(|this| {
|
||||
if height.is_some() {
|
||||
this.child(
|
||||
ScrollArea::new((self.id.clone(), "dialog-content"))
|
||||
.handle(scroll)
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.child(div().p_2().child(content)),
|
||||
)
|
||||
} else {
|
||||
this.child(div().p_2().child(content))
|
||||
}
|
||||
})
|
||||
})
|
||||
.when_some(actions, |panel, actions| {
|
||||
panel.child(
|
||||
h_flex()
|
||||
.px_2()
|
||||
.h_12()
|
||||
.justify_end()
|
||||
.gap_2()
|
||||
.children(actions),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
canvas(|_, _, _| {}, {
|
||||
move |_, _, window, cx| {
|
||||
if !focus.contains_focused(window, cx) {
|
||||
window.focus(&focus, cx);
|
||||
}
|
||||
}
|
||||
})
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
)
|
||||
.with_animation(
|
||||
"dialog-enter",
|
||||
Animation::new(enter_duration).with_easing(ease_out_quint()),
|
||||
|panel, delta| panel.opacity(delta),
|
||||
),
|
||||
),
|
||||
)
|
||||
.with_priority(DIALOG_LAYER)
|
||||
.into_any_element()
|
||||
|
||||
@@ -33,7 +33,7 @@ use gpui::{
|
||||
};
|
||||
|
||||
use crate::components::common::{
|
||||
OVERLAY_LAYER, StyledExt as _, floating_panel, focus_handle, v_flex,
|
||||
OVERLAY_LAYER, StyledExt as _, floating_panel, focus_handle, track_overlay_focus, v_flex,
|
||||
wrapping_adjacent_selectable,
|
||||
};
|
||||
use crate::components::scroll_area::{ScrollArea, ScrollAxes};
|
||||
@@ -557,6 +557,9 @@ impl RenderOnce for Menu {
|
||||
let trigger_bounds: Rc<RefCell<Option<Bounds<Pixels>>>> = Rc::new(RefCell::new(None));
|
||||
let on_change = self.on_change.clone();
|
||||
|
||||
let panel_focus = focus_handle(&(self.id.clone(), "panel").into(), window, cx);
|
||||
track_overlay_focus(&self.id, &panel_focus, self.open, window, cx);
|
||||
|
||||
let fallback_label = self
|
||||
.a11y_label
|
||||
.clone()
|
||||
|
||||
@@ -29,7 +29,9 @@ use gpui::{
|
||||
StyleRefinement, Styled as _, Window, div,
|
||||
};
|
||||
|
||||
use crate::components::common::{StyledExt as _, floating_panel, v_flex};
|
||||
use crate::components::common::{
|
||||
StyledExt as _, floating_panel, focus_handle, track_overlay_focus, v_flex,
|
||||
};
|
||||
use crate::components::menu::{
|
||||
ChangeHandler, TriggerBoundsCell, anchored_overlay, focus_capture, trigger_toggle,
|
||||
};
|
||||
@@ -133,6 +135,10 @@ impl RenderOnce for Popover {
|
||||
|
||||
let trigger_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None));
|
||||
|
||||
// Keyboard focus rides the panel across frames.
|
||||
let focus = focus_handle(&self.id, window, cx);
|
||||
track_overlay_focus(&self.id, &focus, self.open, window, cx);
|
||||
|
||||
let fallback_label = self
|
||||
.a11y_label
|
||||
.clone()
|
||||
@@ -153,7 +159,6 @@ impl RenderOnce for Popover {
|
||||
.when(self.open, |wrapper| {
|
||||
let on_change = self.on_change;
|
||||
let label = fallback_label.clone();
|
||||
let focus = crate::components::common::focus_handle(&self.id, window, cx);
|
||||
let content = self.content.map(|content| content(window, cx));
|
||||
|
||||
wrapper.child(anchored_overlay(
|
||||
|
||||
@@ -18,10 +18,10 @@ use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use gpui::{
|
||||
App, Bounds, DispatchPhase, ElementId, Entity, EntityId, IntoElement, ListState, MouseButton,
|
||||
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, RenderOnce, ScrollEvent,
|
||||
ScrollHandle, SharedString, Styled as _, Task, UniformListScrollHandle, Window, canvas, fill,
|
||||
point,
|
||||
App, Bounds, DispatchPhase, ElementId, Entity, EntityId, Hitbox, HitboxBehavior, IntoElement,
|
||||
ListState, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point,
|
||||
RenderOnce, ScrollEvent, ScrollHandle, SharedString, Styled as _, Task,
|
||||
UniformListScrollHandle, Window, canvas, fill, point,
|
||||
};
|
||||
|
||||
use crate::components::common::ResultLogErr as _;
|
||||
@@ -232,6 +232,98 @@ impl ScrollbarHandle for ListState {
|
||||
}
|
||||
}
|
||||
|
||||
/// One frame's painted scrollbar: track, thumb, and the track's hitbox.
|
||||
struct Metrics {
|
||||
viewport: Bounds<Pixels>,
|
||||
thumb_bounds: Bounds<Pixels>,
|
||||
track_bounds: Bounds<Pixels>,
|
||||
track_length: Pixels,
|
||||
thumb_length: Pixels,
|
||||
max_offset: Pixels,
|
||||
hitbox: Hitbox,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
/// Measures the track and thumb, inserting the track's hitbox.
|
||||
fn new(
|
||||
handle: &dyn ScrollbarHandle,
|
||||
axis: Axis,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Option<Self> {
|
||||
let theme = cx.theme();
|
||||
let width = theme.scrollbar.width;
|
||||
let inset = theme.scrollbar.inset;
|
||||
let min_thumb = theme.scrollbar.min_thumb;
|
||||
|
||||
let viewport = handle.viewport();
|
||||
let viewport_along = axis.viewport_along(&viewport);
|
||||
let max_offset = axis.along(handle.max_offset());
|
||||
|
||||
if max_offset <= Pixels::ZERO || viewport_along <= Pixels::ZERO {
|
||||
return None;
|
||||
}
|
||||
|
||||
let track_length = viewport_along - inset * 2.0;
|
||||
let (thumb_length, thumb_travel) = Scrollbar::thumb_geometry(
|
||||
viewport_along,
|
||||
viewport_along + max_offset,
|
||||
axis.along(handle.offset()),
|
||||
track_length,
|
||||
min_thumb,
|
||||
);
|
||||
|
||||
// The track strip is the thumb's rail plus the padding the thumb rides inside.
|
||||
let (thumb_bounds, track_bounds) = match axis {
|
||||
Axis::Vertical => {
|
||||
let thumb_x = viewport.right() - width - inset;
|
||||
(
|
||||
Bounds::from_corners(
|
||||
point(thumb_x, viewport.top() + inset + thumb_travel),
|
||||
point(
|
||||
thumb_x + width,
|
||||
viewport.top() + inset + thumb_travel + thumb_length,
|
||||
),
|
||||
),
|
||||
Bounds::from_corners(
|
||||
point(viewport.right() - width, viewport.top() + inset),
|
||||
point(viewport.right(), viewport.top() + inset + track_length),
|
||||
),
|
||||
)
|
||||
}
|
||||
Axis::Horizontal => {
|
||||
let thumb_y = viewport.bottom() - width - inset;
|
||||
(
|
||||
Bounds::from_corners(
|
||||
point(viewport.left() + inset + thumb_travel, thumb_y),
|
||||
point(
|
||||
viewport.left() + inset + thumb_travel + thumb_length,
|
||||
thumb_y + width,
|
||||
),
|
||||
),
|
||||
Bounds::from_corners(
|
||||
point(viewport.left() + inset, viewport.bottom() - width),
|
||||
point(viewport.left() + inset + track_length, viewport.bottom()),
|
||||
),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// A covering surface that blocks mouse input keeps the track unhovered.
|
||||
let hitbox = window.insert_hitbox(track_bounds, HitboxBehavior::Normal);
|
||||
|
||||
Some(Self {
|
||||
viewport,
|
||||
thumb_bounds,
|
||||
track_bounds,
|
||||
track_length,
|
||||
thumb_length,
|
||||
max_offset,
|
||||
hitbox,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// An overlay scrollbar, rendered over the scrolled surface.
|
||||
#[derive(IntoElement)]
|
||||
#[must_use]
|
||||
@@ -244,13 +336,18 @@ pub struct Scrollbar {
|
||||
|
||||
impl RenderOnce for Scrollbar {
|
||||
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
|
||||
canvas(|_, _, _| {}, {
|
||||
let handle = self.handle;
|
||||
let axis = self.axis;
|
||||
let visibility = self.visibility;
|
||||
let id = self.id;
|
||||
move |_, _, window, cx| Self::paint(&handle, axis, visibility, &id, window, cx)
|
||||
})
|
||||
let axis = self.axis;
|
||||
let visibility = self.visibility;
|
||||
let id = self.id;
|
||||
let measured_handle = self.handle.clone();
|
||||
let painted_handle = self.handle;
|
||||
|
||||
canvas(
|
||||
move |_, window, cx| Metrics::new(measured_handle.as_ref(), axis, window, cx),
|
||||
move |_, metrics, window, cx| {
|
||||
Self::paint(&painted_handle, axis, visibility, &id, metrics, window, cx)
|
||||
},
|
||||
)
|
||||
.absolute()
|
||||
.inset_0()
|
||||
}
|
||||
@@ -327,24 +424,28 @@ impl Scrollbar {
|
||||
axis: Axis,
|
||||
visibility: ScrollbarVisibility,
|
||||
id: &ElementId,
|
||||
metrics: Option<Metrics>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let Some(Metrics {
|
||||
viewport,
|
||||
thumb_bounds,
|
||||
track_bounds,
|
||||
track_length,
|
||||
thumb_length,
|
||||
max_offset,
|
||||
hitbox,
|
||||
}) = metrics
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let view = window.current_view();
|
||||
let theme = cx.theme();
|
||||
|
||||
let color = theme.colors.scrollbar_thumb;
|
||||
let width = theme.scrollbar.width;
|
||||
let inset = theme.scrollbar.inset;
|
||||
let min_thumb = theme.scrollbar.min_thumb;
|
||||
|
||||
let viewport = handle.viewport();
|
||||
let viewport_along = axis.viewport_along(&viewport);
|
||||
let max_offset = axis.along(handle.max_offset());
|
||||
|
||||
if max_offset <= Pixels::ZERO || viewport_along <= Pixels::ZERO {
|
||||
return;
|
||||
}
|
||||
|
||||
let autohide = match visibility {
|
||||
ScrollbarVisibility::Platform => theme.scrollbar.autohide,
|
||||
@@ -355,51 +456,6 @@ impl Scrollbar {
|
||||
let autohide_state =
|
||||
autohide.then(|| Autohide::new(Self::state_key("autohide", id), window, cx));
|
||||
|
||||
let track_length = viewport_along - inset * 2.0;
|
||||
let (thumb_length, thumb_travel) = Self::thumb_geometry(
|
||||
viewport_along,
|
||||
viewport_along + max_offset,
|
||||
axis.along(handle.offset()),
|
||||
track_length,
|
||||
min_thumb,
|
||||
);
|
||||
|
||||
// The track strip is the thumb's rail plus the padding the thumb rides inside.
|
||||
let (thumb_bounds, track_bounds) = match axis {
|
||||
Axis::Vertical => {
|
||||
let thumb_x = viewport.right() - width - inset;
|
||||
(
|
||||
Bounds::from_corners(
|
||||
point(thumb_x, viewport.top() + inset + thumb_travel),
|
||||
point(
|
||||
thumb_x + width,
|
||||
viewport.top() + inset + thumb_travel + thumb_length,
|
||||
),
|
||||
),
|
||||
Bounds::from_corners(
|
||||
point(viewport.right() - width, viewport.top() + inset),
|
||||
point(viewport.right(), viewport.top() + inset + track_length),
|
||||
),
|
||||
)
|
||||
}
|
||||
Axis::Horizontal => {
|
||||
let thumb_y = viewport.bottom() - width - inset;
|
||||
(
|
||||
Bounds::from_corners(
|
||||
point(viewport.left() + inset + thumb_travel, thumb_y),
|
||||
point(
|
||||
viewport.left() + inset + thumb_travel + thumb_length,
|
||||
thumb_y + width,
|
||||
),
|
||||
),
|
||||
Bounds::from_corners(
|
||||
point(viewport.left() + inset, viewport.bottom() - width),
|
||||
point(viewport.left() + inset + track_length, viewport.bottom()),
|
||||
),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let drag = window.use_keyed_state(Self::state_key("thumb-drag", id), cx, |_, _| Drag::None);
|
||||
let dragging = drag.read(cx).is_some();
|
||||
|
||||
@@ -433,6 +489,11 @@ impl Scrollbar {
|
||||
return;
|
||||
}
|
||||
|
||||
// A covering surface blocks the pointer, so the track is not hovered.
|
||||
if !hitbox.is_hovered_at(event.position, window) {
|
||||
return;
|
||||
}
|
||||
|
||||
let in_thumb = thumb_bounds.contains(&event.position);
|
||||
let in_track = !in_thumb && track_bounds.contains(&event.position);
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
//! Renders a small text panel next to a hovered element.
|
||||
//!
|
||||
//! Most call sites hand [`Tooltip::text`] or [`Tooltip::anchored`] to gpui's
|
||||
//! `.tooltip(...)` method; [`Tooltip::new`] creates the view for callers that
|
||||
//! hold the [`gpui::Entity`] themselves.
|
||||
//! Most call sites hand [`Tooltip::text`] to gpui's `.tooltip(...)` method;
|
||||
//! [`Tooltip::new`] creates the view for callers that hold the [`gpui::Entity`]
|
||||
//! themselves. The panel follows the pointer.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
@@ -14,22 +14,17 @@
|
||||
//! ```
|
||||
|
||||
use gpui::{
|
||||
AnyView, App, AppContext as _, Bounds, Context, ElementId, Entity, InteractiveElement as _,
|
||||
IntoElement, ParentElement as _, Pixels, Render, Role, SharedString,
|
||||
StatefulInteractiveElement as _, Styled as _, Window, canvas, div, point, px,
|
||||
AnyView, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, Role, SharedString, StatefulInteractiveElement as _, Styled as _,
|
||||
Window, div,
|
||||
};
|
||||
|
||||
use crate::components::common::{anchor_bounds, floating_panel, record_anchor_bounds};
|
||||
use crate::theme::{
|
||||
ActiveTheme as _, BORDER_WIDTH, TOOLTIP_ANCHOR_GAP, TOOLTIP_MAX_WIDTH, TOOLTIP_OFFSET,
|
||||
};
|
||||
|
||||
const PANEL_BOUNDS_ID: &str = "tooltip-panel-bounds";
|
||||
use crate::components::common::floating_panel;
|
||||
use crate::theme::{ActiveTheme as _, TOOLTIP_MAX_WIDTH};
|
||||
|
||||
/// A small text panel gpui shows next to a hovered element.
|
||||
pub struct Tooltip {
|
||||
text: SharedString,
|
||||
anchor: Option<Bounds<Pixels>>,
|
||||
}
|
||||
|
||||
impl Tooltip {
|
||||
@@ -38,34 +33,7 @@ impl Tooltip {
|
||||
where
|
||||
L: Into<SharedString>,
|
||||
{
|
||||
cx.new(|_| Self {
|
||||
text: text.into(),
|
||||
anchor: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the `.tooltip(...)` closure that pins the panel next to the
|
||||
/// element identified by `anchor_id`.
|
||||
///
|
||||
/// [`Tooltip::text`] places the panel at the pointer instead.
|
||||
pub fn anchored<L, I>(
|
||||
text: L,
|
||||
anchor_id: I,
|
||||
) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static
|
||||
where
|
||||
L: Into<SharedString>,
|
||||
I: Into<ElementId>,
|
||||
{
|
||||
let text = text.into();
|
||||
let anchor_id = anchor_id.into();
|
||||
move |window, cx| {
|
||||
let anchor = anchor_bounds(window.window_handle().window_id(), &anchor_id, cx);
|
||||
cx.new(|_| Self {
|
||||
text: text.clone(),
|
||||
anchor,
|
||||
})
|
||||
.into()
|
||||
}
|
||||
cx.new(|_| Self { text: text.into() })
|
||||
}
|
||||
|
||||
/// Returns the `.tooltip(...)` closure for a plain text tooltip.
|
||||
@@ -79,15 +47,17 @@ impl Tooltip {
|
||||
}
|
||||
|
||||
impl Render for Tooltip {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
// Copied out: the mutable reads below need `cx` free of this borrow.
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let surface_foreground = theme.colors.surface_foreground;
|
||||
let radius_sm = theme.radius.sm;
|
||||
let shadows = theme.elevations.popover.shadows();
|
||||
|
||||
let build_panel = || {
|
||||
floating_panel(
|
||||
div()
|
||||
.debug_selector(|| "tooltip-anchor".to_string())
|
||||
.pl_2()
|
||||
.pt_2()
|
||||
.child(floating_panel(
|
||||
cx,
|
||||
radius_sm,
|
||||
div()
|
||||
@@ -100,53 +70,7 @@ impl Render for Tooltip {
|
||||
.text_sm()
|
||||
.text_color(surface_foreground)
|
||||
.child(self.text.clone()),
|
||||
shadows.clone(),
|
||||
)
|
||||
};
|
||||
|
||||
let (wrapper, panel) = match self.anchor {
|
||||
Some(parent) => {
|
||||
let origin = window.mouse_position() + point(TOOLTIP_OFFSET, TOOLTIP_OFFSET);
|
||||
let viewport_height = window.viewport_size().height;
|
||||
|
||||
let height = anchor_bounds(
|
||||
window.window_handle().window_id(),
|
||||
&ElementId::from(PANEL_BOUNDS_ID),
|
||||
cx,
|
||||
)
|
||||
.map(|bounds| bounds.size.height)
|
||||
.unwrap_or_else(|| window.line_height() + px(8.) + BORDER_WIDTH * 2.0);
|
||||
|
||||
let below = viewport_height - TOOLTIP_ANCHOR_GAP - parent.bottom();
|
||||
let above = parent.top() - TOOLTIP_ANCHOR_GAP;
|
||||
let hang_below = below >= height || below >= above;
|
||||
|
||||
let panel = build_panel()
|
||||
.ml(parent.left() - origin.x)
|
||||
.mt(if hang_below {
|
||||
parent.bottom() + TOOLTIP_ANCHOR_GAP - origin.y
|
||||
} else {
|
||||
parent.top() - TOOLTIP_ANCHOR_GAP - height - origin.y
|
||||
})
|
||||
.child(
|
||||
canvas(
|
||||
record_anchor_bounds(ElementId::from(PANEL_BOUNDS_ID)),
|
||||
|_, _, _, _| {},
|
||||
)
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
);
|
||||
(div().debug_selector(|| "tooltip-anchor".to_string()), panel)
|
||||
}
|
||||
None => {
|
||||
let wrapper = div()
|
||||
.debug_selector(|| "tooltip-anchor".to_string())
|
||||
.pl_2()
|
||||
.pt_2();
|
||||
(wrapper, build_panel())
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.child(panel)
|
||||
shadows,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,9 +126,18 @@ impl Root {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Mounts a dialog view above the content.
|
||||
/// Mounts a dialog view above the content, replacing an open dialog of the same view type.
|
||||
fn mount_dialog(&mut self, view: impl Into<AnyView>, cx: &mut Context<Self>) {
|
||||
self.dialogs.push(view.into());
|
||||
let view = view.into();
|
||||
if let Some(existing) = self
|
||||
.dialogs
|
||||
.iter_mut()
|
||||
.find(|dialog| dialog.entity_type() == view.entity_type())
|
||||
{
|
||||
*existing = view;
|
||||
} else {
|
||||
self.dialogs.push(view);
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
|
||||
@@ -35,8 +35,6 @@ pub const BORDER_WIDTH: Pixels = px(1.0);
|
||||
pub const OVERLAY_ICON_SIZE: Rems = rems(0.75);
|
||||
/// The draggable width of a resize seam and its panel-row gutter.
|
||||
pub const RESIZE_HANDLE_WIDTH: Pixels = px(8.0);
|
||||
/// The gap between an anchored tooltip and its anchor.
|
||||
pub const TOOLTIP_ANCHOR_GAP: Pixels = px(4.0);
|
||||
/// The widest a titlebar tab grows before its label fades.
|
||||
pub const TAB_MAX_WIDTH: Pixels = px(240.0);
|
||||
/// The narrowest a titlebar tab shrinks, keeping short labels clear of the fade.
|
||||
@@ -69,8 +67,6 @@ pub const CODE_LINE_HEIGHT: f32 = 1.5;
|
||||
pub const POPUP_MAX_HEIGHT: Pixels = px(320.0);
|
||||
/// The widest a tooltip may grow.
|
||||
pub const TOOLTIP_MAX_WIDTH: Pixels = px(360.0);
|
||||
/// How far a tooltip sits from the pointer.
|
||||
pub const TOOLTIP_OFFSET: Pixels = px(1.0);
|
||||
/// The caret's thickness.
|
||||
pub const CARET_WIDTH: Pixels = px(1.5);
|
||||
/// The IME preedit underline's thickness.
|
||||
|
||||
Reference in New Issue
Block a user