use crate::{
components::{form::inset_outline, overlay::floating_shadow},
theme::color,
tokens::{self, ColorRole, TokenLength, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, CursorStyle, ElementId, Hsla, InteractiveElement as _, IntoElement,
KeyDownEvent, ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled,
Window, div, prelude::FluentBuilder as _, px, rems,
};
use std::rc::Rc;
pub const PANEL_MAX_WIDTH: f32 = 18.;
pub const LINK_PADDING: (TokenLength, TokenLength) = (tokens::SPACING_04, tokens::SPACING_05);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LinkLook {
pub text: Hsla,
pub hover: Hsla,
pub focus: Hsla,
}
impl LinkLook {
pub fn current() -> Self {
Self {
text: color(ColorRole::TextPrimary),
hover: color(ColorRole::LayerHover01),
focus: color(ColorRole::Focus),
}
}
}
pub fn rule_color() -> Hsla {
color(ColorRole::BorderSubtle01)
}
#[derive(IntoElement, Default)]
pub struct PatternPopover {
children: Vec<AnyElement>,
}
impl PatternPopover {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for PatternPopover {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for PatternPopover {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_col()
.w_full()
.max_w(rems(PANEL_MAX_WIDTH))
.bg(color(ColorRole::Layer01))
.shadow(floating_shadow())
.text_color(color(ColorRole::TextPrimary))
.type_style(TypeStyle::BodyCompact01)
.children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct PatternRule;
impl PatternRule {
pub fn new() -> Self {
Self
}
}
impl RenderOnce for PatternRule {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div().w_full().h(px(1.)).flex_none().bg(rule_color())
}
}
type ClickHandler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct PatternLink {
id: ElementId,
label: SharedString,
on_click: Option<ClickHandler>,
}
impl PatternLink {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
on_click: None,
}
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl RenderOnce for PatternLink {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let look = LinkLook::current();
let focus = window
.use_keyed_state((self.id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let focused = focus.is_focused(window);
let on_key = self.on_click.clone();
let on_click = self.on_click;
div()
.id(self.id)
.track_focus(&focus.tab_index(0).tab_stop(true))
.relative()
.w_full()
.py(LINK_PADDING.0)
.px(LINK_PADDING.1)
.text_color(look.text)
.type_style(TypeStyle::BodyCompact01)
.cursor(CursorStyle::PointingHand)
.hover(move |s| s.bg(look.hover))
.on_click(move |e, window, cx| {
if let Some(f) = on_click.as_ref().filter(|_| !e.is_keyboard()) {
f(window, cx)
}
})
.on_key_down(move |e: &KeyDownEvent, window, cx| {
if e.keystroke.key == "enter" && !e.is_held {
if let Some(f) = &on_key {
f(window, cx);
}
cx.stop_propagation();
}
})
.child(self.label)
.when(focused, |d| d.child(inset_outline(2., look.focus)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn geometry_matches_the_css() {
assert_eq!(PANEL_MAX_WIDTH * tokens::REM_PX, 288.);
assert_eq!(LINK_PADDING.0.px_value(), 12.);
assert_eq!(LINK_PADDING.1.px_value(), 16.);
}
#[test]
fn colors_come_from_the_css_roles() {
let look = LinkLook::current();
assert_eq!(look.text, color(ColorRole::TextPrimary));
assert_eq!(look.hover, color(ColorRole::LayerHover01));
assert_eq!(look.focus, color(ColorRole::Focus));
assert_eq!(rule_color(), color(ColorRole::BorderSubtle01));
}
}