use super::{BackgroundPress, OverflowAnchor, OverflowToolbar, OverflowUnit};
use crate::icon_button::{IconButton, Metrics};
use crate::workspace::FOCUS_RING_PX;
use gpui_kit::base::ElementExt as _;
use gpui_kit::component::{Sizable as _, button::ButtonVariants as _, h_flex};
use gpui_kit::{
AnyElement, App, AppContext as _, AvailableSpace, Context, Entity, FocusHandle, Focusable as _,
InteractiveElement as _, IntoElement, ParentElement as _, Rems, Render, Role,
StatefulInteractiveElement as _, Styled as _, TestSupportExt as _, Window, div, px, size,
};
use subspace_lucide::IconName;
impl OverflowToolbar {
fn measure_units(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.widths = self
.units
.iter()
.map(|unit| {
let mut element = (unit.inline)(window, cx);
let measured = element.layout_as_root(
size(AvailableSpace::MaxContent, AvailableSpace::MaxContent),
window,
cx,
);
f32::from(measured.width)
})
.collect();
}
fn hidden_units(&self) -> Vec<OverflowUnit> {
let plan = self.plan();
self.units[plan.visible..].to_vec()
}
fn release_menu(&self, focus: &FocusHandle, window: &mut Window, cx: &mut App) {
let Some(anchor) = &self.anchor else {
return;
};
let chevron = anchor.focus_handle(cx);
anchor.update(cx, |anchor, cx| anchor.dismiss(window, cx));
if chevron.is_focused(window) {
focus.focus(window, cx);
}
}
fn refresh_popup(
&mut self,
anchor: &Entity<crate::anchored::Anchor>,
window: &mut Window,
cx: &mut Context<Self>,
) {
if !std::mem::take(&mut self.popup_dirty) {
return;
}
let Some(popup) = self.popup.as_ref().and_then(|popup| popup.upgrade()) else {
return;
};
let units = self.hidden_units();
let anchor = OverflowAnchor {
bounds: self.trigger_bounds,
host: anchor.clone(),
};
let (changed, items) = self.projection.build(&units, anchor);
popup.update(cx, |popup, cx| {
if changed {
popup.rebuild(window, cx, |menu, _, _| Self::popup(menu, items));
} else {
cx.notify();
}
});
}
fn more_button(
&self,
anchor: Entity<crate::anchored::Anchor>,
cx: &Context<Self>,
) -> impl IntoElement {
let entity = cx.weak_entity();
let measured = entity.clone();
div()
.id("overflow-toolbar-more")
.test_support()
.flex_shrink_0()
.w(px(self.chevron_width))
.on_prepaint(move |bounds, _, cx| {
_ = measured.update(cx, |toolbar, cx| {
if toolbar.trigger_bounds != bounds {
toolbar.trigger_bounds = bounds;
toolbar.popup_dirty = true;
cx.notify();
}
});
})
.child(
crate::anchored::dropdown_menu(
IconButton::new("overflow-toolbar-button")
.ghost()
.small()
.icon(IconName::ChevronDown)
.w(px(self.chevron_width))
.tooltip("More toolbar actions")
.accessibility_label("More toolbar actions"),
{
let anchor = anchor.clone();
move |menu, _, cx| {
let popup = cx.weak_entity();
let projection = entity.update(cx, |toolbar, _| {
toolbar.popup = Some(popup);
let units = toolbar.hidden_units();
let anchor = OverflowAnchor {
bounds: toolbar.trigger_bounds,
host: anchor.clone(),
};
toolbar.projection.build(&units, anchor).1
});
match projection {
Ok(items) => Self::popup(menu, items),
Err(_) => menu,
}
}
},
)
.with_anchor(&anchor),
)
}
}
fn inline_units(units: &[OverflowUnit], window: &mut Window, cx: &mut App) -> Vec<AnyElement> {
units
.iter()
.map(|unit| {
div()
.id(gpui_kit::SharedString::from(format!(
"overflow-unit:{}",
unit.key
)))
.test_support()
.flex_shrink_0()
.child((unit.inline)(window, cx))
.into_any_element()
})
.collect()
}
fn device_grid(value: Rems, window: &Window) -> f32 {
let scale = window.scale_factor();
(f32::from(value.to_pixels(window.rem_size())) * scale).round() / scale
}
impl Render for OverflowToolbar {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let size = Metrics::current(cx);
self.gap = device_grid(size.gap(), window);
self.chevron_width = device_grid(size.overflow_width(), window);
self.measure_units(window, cx);
let anchor = self
.anchor
.get_or_insert_with(|| cx.new(crate::anchored::Anchor::new))
.clone();
let focus = self
.focus
.get_or_insert_with(|| cx.focus_handle().tab_stop(false))
.clone();
self.refresh_popup(&anchor, window, cx);
let plan = self.plan();
let mut children = inline_units(&self.units[..plan.visible], window, cx);
if plan.has_overflow() {
children.push(self.more_button(anchor, cx).into_any_element());
} else {
self.release_menu(&focus, window, cx);
}
let entity = cx.weak_entity();
let row = h_flex()
.id("overflow-toolbar-row")
.test_support()
.flex_nowrap()
.w(px(self.content_width()))
.max_w_full()
.min_w_0()
.gap_x(px(self.gap))
.on_prepaint(move |bounds, window, cx| {
_ = entity.update(cx, |toolbar, cx| {
let width = f32::from(bounds.size.width);
let changed = toolbar
.viewport
.is_none_or(|viewport| crate::measure::changed(viewport, width));
if changed {
toolbar.viewport = Some(width);
toolbar.popup_dirty = true;
cx.notify();
window.request_animation_frame();
}
});
})
.children(children);
clip(row, focus, self.background_press.clone())
}
}
pub(super) const TOOLBAR_LABEL: &str = "Toolbar";
fn clip(
row: impl IntoElement,
focus: FocusHandle,
background_press: Option<BackgroundPress>,
) -> impl IntoElement {
let ring = px(FOCUS_RING_PX);
h_flex()
.id("overflow-toolbar-clip")
.role(Role::Toolbar)
.aria_label(TOOLBAR_LABEL)
.test_support()
.track_focus(&focus)
.on_any_mouse_down(move |event, window, _| {
if !window.default_prevented()
&& let Some(press) = &background_press
{
press(event, window);
}
window.prevent_default();
})
.min_w_0()
.m(-ring)
.p(ring)
.overflow_hidden()
.child(row)
}