use crate::{
components::{DropdownMenu, DropdownMenuKind, DropdownMenuSize, MenuItem},
icons::{CarbonIcon, Icon},
t3::craft::{rem, sans},
theme::color,
tokens::{self, ColorRole},
u0::rows as row,
};
use gpui_kit::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Window, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;
#[derive(Clone)]
pub struct ScopeHeaderTexts {
pub applying_to: SharedString,
pub global: SharedString,
pub note: Rc<dyn Fn(&str) -> SharedString>,
}
impl Default for ScopeHeaderTexts {
fn default() -> Self {
Self {
applying_to: "Applying to".into(),
global: "All projects (global)".into(),
note: Rc::new(|s| {
format!("Values you change here override the global setting for {s} only.").into()
}),
}
}
}
type ScopeHandler = Rc<dyn Fn(Option<SharedString>, &mut Window, &mut App)>;
type AsideHandler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct ScopeHeader {
id: ElementId,
scope: Option<SharedString>,
projects: Vec<SharedString>,
texts: ScopeHeaderTexts,
on_scope_change: Option<ScopeHandler>,
aside: Option<SharedString>,
on_aside: Option<AsideHandler>,
page: bool,
}
impl ScopeHeader {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
scope: None,
projects: Vec::new(),
texts: ScopeHeaderTexts::default(),
on_scope_change: None,
aside: None,
on_aside: None,
page: false,
}
}
pub fn aside(mut self, aside: impl Into<SharedString>) -> Self {
self.aside = Some(aside.into());
self
}
pub fn on_aside(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_aside = Some(Rc::new(f));
self
}
pub fn page(mut self, page: bool) -> Self {
self.page = page;
self
}
pub fn scope(mut self, scope: Option<SharedString>) -> Self {
self.scope = scope;
self
}
pub fn projects(mut self, projects: impl IntoIterator<Item = impl Into<SharedString>>) -> Self {
self.projects = projects.into_iter().map(Into::into).collect();
self
}
pub fn texts(mut self, texts: ScopeHeaderTexts) -> Self {
self.texts = texts;
self
}
pub fn on_scope_change(
mut self,
f: impl Fn(Option<SharedString>, &mut Window, &mut App) + 'static,
) -> Self {
self.on_scope_change = Some(Rc::new(f));
self
}
pub fn button_label(&self) -> SharedString {
self.scope
.clone()
.unwrap_or_else(|| self.texts.global.clone())
}
}
impl RenderOnce for ScopeHeader {
fn render(self, window: &mut Window, _: &mut App) -> impl IntoElement {
let pick = |scope: Option<SharedString>, f: Option<ScopeHandler>| {
move |w: &mut Window, cx: &mut App| {
if let Some(f) = &f {
f(scope.clone(), w, cx);
}
}
};
let mut items = vec![
MenuItem::item(self.texts.global.clone())
.on_select(pick(None, self.on_scope_change.clone())),
MenuItem::divider(),
];
items.extend(self.projects.iter().map(|p| {
MenuItem::item(p.clone()).on_select(pick(Some(p.clone()), self.on_scope_change.clone()))
}));
let label = self.button_label();
sans(div(), 12., 400)
.flex()
.flex_wrap()
.items_center()
.gap(tokens::SPACING_03)
.py(tokens::SPACING_03)
.when(!self.page, |this| this.px(tokens::SPACING_04))
.when(self.page, |this| this.px(rem(20.)))
.bg(color(ColorRole::Layer01))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.text_color(color(ColorRole::TextPrimary))
.child(row::dim(self.texts.applying_to.clone(), 12.))
.child(
DropdownMenu::new((self.id.clone(), "menu"))
.label(label)
.kind(DropdownMenuKind::Ghost)
.size(DropdownMenuSize::Sm)
.items(items),
)
.when_some(self.aside.clone(), |this, aside| {
let link = self.on_aside.clone();
this.child(
div()
.id((self.id.clone(), "aside"))
.ml_auto()
.line_height(rem(16.))
.text_color(color(if link.is_some() {
ColorRole::LinkPrimary
} else {
ColorRole::TextSecondary
}))
.when_some(link, |this, f| {
this.cursor_pointer()
.hover(|s| s.text_color(color(ColorRole::LinkPrimaryHover)))
.on_click(move |_, w, cx| f(w, cx))
})
.child(aside),
)
})
.when_some(self.scope.clone(), |this, scope| {
this.child(
div()
.w_full()
.flex()
.items_center()
.gap(tokens::SPACING_02)
.text_color(color(ColorRole::TextSecondary))
.child(
div().flex_none().child(
Icon::carbon(CarbonIcon::InformationFilled)
.size_length(rem(14.))
.color(color(ColorRole::SupportInfo)),
),
)
.child({
let note = (self.texts.note)(&scope);
let least = crate::typography::longest_word_width(
¬e,
12.,
tokens::FontFamilyKey::Sans,
400,
window,
);
div().flex_1().min_w(least).child(note)
}),
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn label_and_note_follow_the_scope() {
let h = ScopeHeader::new("s");
assert_eq!(
h.button_label(),
SharedString::from("All projects (global)")
);
let h = ScopeHeader::new("s").scope(Some("carbon-ui".into()));
assert_eq!(h.button_label(), SharedString::from("carbon-ui"));
assert_eq!(
(h.texts.note)("carbon-ui"),
SharedString::from(
"Values you change here override the global setting for carbon-ui only."
)
);
}
}