Vendor the native UI kit as crates/ui
- crates/ui holds the theme, icons, components, data widgets, editor surfaces, layout, and utilities; cargo feature gates are gone. - One merged icon set of 154 SVGs where the app file wins every duplicate name, served by a single rust-embed Assets source. - IconRole grows the 25 app glyphs, so assets::CustomIconName and crates/assets are deleted. - ui::init owns the theme and key bindings that desktop used to bind in three calls. - docs/gpui-native-kit-gaps.md becomes docs/ui-gaps.md; the icon gap is closed.
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
use crate::theme::{ActiveTheme as _, FOCUS_RING_ALPHA, FOCUS_RING_SPREAD};
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, Bounds, BoxShadow, Div, ElementId, FocusHandle, Global, Hsla, IntoElement, Pixels,
|
||||
Refineable as _, StatefulInteractiveElement, StyleRefinement, Styled, Window, WindowId, div,
|
||||
px,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// A row whose children center on the cross axis.
|
||||
#[must_use]
|
||||
pub fn h_flex() -> Div {
|
||||
div().flex().flex_row().items_center()
|
||||
}
|
||||
|
||||
/// A column whose children stack on the main axis.
|
||||
#[must_use]
|
||||
pub fn v_flex() -> Div {
|
||||
div().flex().flex_col()
|
||||
}
|
||||
|
||||
/// Logs the error and yields the success value, if any.
|
||||
pub trait ResultLogErr<T> {
|
||||
/// Logs the error and yields the success value, if any.
|
||||
fn log_err(self) -> Option<T>;
|
||||
}
|
||||
|
||||
impl<T, E: std::fmt::Display> ResultLogErr<T> for Result<T, E> {
|
||||
fn log_err(self) -> Option<T> {
|
||||
match self {
|
||||
Ok(value) => Some(value),
|
||||
Err(error) => {
|
||||
log::warn!("{error}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extensions over [`Styled`], shared by the components.
|
||||
pub trait StyledExt: Styled + Sized {
|
||||
#[must_use]
|
||||
fn refine_style(mut self, style: &StyleRefinement) -> Self {
|
||||
self.style().refine(style);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Styled> StyledExt for E {}
|
||||
|
||||
/// Returns a focus handle for the given element ID.
|
||||
pub fn focus_handle(id: &ElementId, window: &mut Window, cx: &mut App) -> FocusHandle {
|
||||
window
|
||||
.with_element_namespace(id.clone(), |window| {
|
||||
window.use_keyed_state("focus-handle", cx, |_, cx| cx.focus_handle())
|
||||
})
|
||||
.read(cx)
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// The painted bounds of elements that tooltips anchor to, per window and element id.
|
||||
/// Entries are never evicted: bounds are bounded by live windows, though long-lived processes minting dynamic per-item ids could accumulate.
|
||||
#[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()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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))
|
||||
}
|
||||
|
||||
/// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a focus ring shadow with the given color at half ink.
|
||||
pub fn focus_ring(color: Hsla) -> Vec<BoxShadow> {
|
||||
vec![
|
||||
BoxShadow::new(px(0.), px(0.), color.alpha(FOCUS_RING_ALPHA))
|
||||
.spread_radius(FOCUS_RING_SPREAD),
|
||||
]
|
||||
}
|
||||
|
||||
/// The next selectable row in the direction, or `None` at the ends.
|
||||
pub fn adjacent_selectable(
|
||||
selectable: &[usize],
|
||||
selected: Option<usize>,
|
||||
forward: bool,
|
||||
) -> Option<usize> {
|
||||
if forward {
|
||||
selectable
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|index| Some(*index) > selected)
|
||||
} else {
|
||||
selectable
|
||||
.iter()
|
||||
.copied()
|
||||
.rev()
|
||||
.find(|index| Some(*index) < selected)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the given key is a vertical navigation key.
|
||||
pub fn nav_key<S>(selectable: &[usize], selected: Option<usize>, key: &str, select: S) -> bool
|
||||
where
|
||||
S: FnOnce(Option<usize>),
|
||||
{
|
||||
let target = match key {
|
||||
"down" => adjacent_selectable(selectable, selected, true),
|
||||
"up" => adjacent_selectable(selectable, selected, false),
|
||||
"home" => selectable.first().copied(),
|
||||
"end" => selectable.last().copied(),
|
||||
_ => return false,
|
||||
};
|
||||
if let Some(next) = target
|
||||
&& Some(next) != selected
|
||||
{
|
||||
select(Some(next));
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// A selectable row's label and icon color.
|
||||
pub fn selectable_label_color(cx: &App, selected: bool, disabled: bool) -> Hsla {
|
||||
let theme = cx.theme();
|
||||
if disabled {
|
||||
theme.colors.muted_foreground
|
||||
} else if selected {
|
||||
theme.colors.primary_foreground
|
||||
} else {
|
||||
theme.colors.foreground
|
||||
}
|
||||
}
|
||||
|
||||
/// A selectable row's background.
|
||||
#[must_use]
|
||||
pub fn paint_selectable_row<R>(cx: &App, row: R, selected: bool, disabled: bool) -> R
|
||||
where
|
||||
R: IntoElement + Styled + StatefulInteractiveElement,
|
||||
{
|
||||
let theme = cx.theme();
|
||||
row.map(|row| {
|
||||
if disabled {
|
||||
row
|
||||
} else if selected {
|
||||
row.bg(theme.ramps.primary.base)
|
||||
} else {
|
||||
row.hover(|row| row.bg(theme.colors.muted))
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user