use super::{Handler, float_shadow, inset_frame};
use crate::{
icons::{CarbonIcon, Icon},
motion::{self, Curve, Motion, Speed},
t3::craft::{self, focus_handle, focus_visible, press, rem, sans, sub},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
AnyElement, App, AppContext as _, Bounds, BoxShadow, Element, ElementId, GlobalElementId,
InspectorElementId, InteractiveElement, IntoElement, LayoutId, ParentElement, Pixels,
RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window,
component::{Sizable as _, Size, input::Input as KitInput, input::InputState},
div,
prelude::FluentBuilder as _,
px, relative,
};
use std::{cell::Cell, rc::Rc};
type BoolHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
type ValueHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
pub const PHONE_BEZEL: f32 = 10.;
pub const PHONE_SCREEN_RADIUS: f32 = 36.;
pub const PHONE_RADIUS: f32 = 44.;
#[derive(IntoElement)]
pub struct PhoneFrame {
height: f32,
children: Vec<AnyElement>,
}
impl PhoneFrame {
pub fn new() -> Self {
Self {
height: 560.,
children: Vec::new(),
}
}
pub fn height(mut self, height: f32) -> Self {
self.height = height;
self
}
}
impl Default for PhoneFrame {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for PhoneFrame {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
thread_local! {
static SCREEN_DEPTH: Cell<u32> = const { Cell::new(0) };
}
pub fn in_phone_screen() -> bool {
SCREEN_DEPTH.with(|d| d.get() > 0)
}
fn with_phone_screen<R>(f: impl FnOnce() -> R) -> R {
SCREEN_DEPTH.with(|d| d.set(d.get() + 1));
struct Pop;
impl Drop for Pop {
fn drop(&mut self) {
SCREEN_DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
}
}
let _pop = Pop;
f()
}
pub fn screen_bottom_radius() -> Option<f32> {
in_phone_screen().then_some(PHONE_SCREEN_RADIUS)
}
struct ScreenScope(AnyElement);
impl Element for ScreenScope {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, ()) {
let child = &mut self.0;
(with_phone_screen(|| child.request_layout(window, cx)), ())
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
_: &mut (),
window: &mut Window,
cx: &mut App,
) {
let child = &mut self.0;
with_phone_screen(|| child.prepaint(window, cx));
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
_: &mut (),
_: &mut (),
window: &mut Window,
cx: &mut App,
) {
let child = &mut self.0;
with_phone_screen(|| child.paint(window, cx));
}
}
impl RenderOnce for PhoneFrame {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.relative()
.flex_none()
.w(rem(352.))
.h(rem(self.height))
.p(px(PHONE_BEZEL))
.rounded(rem(PHONE_RADIUS))
.bg(color(ColorRole::BackgroundInverse))
.shadow(float_shadow(2.))
.child(ScreenScope(
div()
.relative()
.flex()
.flex_col()
.size_full()
.overflow_hidden()
.pt(rem(44.))
.rounded(rem(PHONE_SCREEN_RADIUS))
.bg(color(ColorRole::Background))
.children(self.children)
.into_any_element(),
))
.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border(px(PHONE_BEZEL))
.border_color(color(ColorRole::BackgroundInverse))
.rounded(rem(PHONE_RADIUS)),
)
.child(
div()
.absolute()
.top(px(18.))
.left_0()
.right_0()
.flex()
.justify_center()
.child(
div()
.w(rem(88.))
.h(rem(24.))
.rounded(rem(16.))
.bg(gpui_kit::black()),
),
)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Leading {
#[default]
Back,
Menu,
None,
}
fn mbtn(
id: ElementId,
label: SharedString,
icon: CarbonIcon,
on_press: Option<Handler>,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let focus = focus_handle(&id, window, cx);
let ring = focus_visible(&focus, window);
press(id, &focus)
.accessibility_label(label)
.flex_none()
.justify_center()
.size(rem(44.))
.rounded_full()
.active(|s| s.bg(color(ColorRole::BackgroundHover)))
.when_some(on_press, |this, f| this.on_click(move |_, w, cx| f(w, cx)))
.child(
Icon::carbon(icon)
.size_length(rem(20.))
.color(color(ColorRole::LinkPrimary)),
)
.when(ring, |this| this.child(craft::focus_ring()))
.into_any_element()
}
#[derive(Clone)]
pub struct BarAction {
pub label: SharedString,
pub icon: CarbonIcon,
on_press: Option<Handler>,
}
impl BarAction {
pub fn new(label: impl Into<SharedString>, icon: CarbonIcon) -> Self {
Self {
label: label.into(),
icon,
on_press: None,
}
}
pub fn on_press(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_press = Some(Rc::new(f));
self
}
}
pub fn nav_slot(actions: usize) -> f32 {
if actions > 1 { 88. } else { 52. }
}
#[derive(IntoElement)]
pub struct MobileAppBar {
id: ElementId,
title: SharedString,
subtitle: Option<SharedString>,
leading: Leading,
actions: Vec<BarAction>,
large: bool,
searchable: bool,
phone: bool,
on_leading: Option<Handler>,
back_label: SharedString,
menu_label: SharedString,
search_label: SharedString,
}
impl MobileAppBar {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: title.into(),
subtitle: None,
leading: Leading::Back,
actions: Vec::new(),
large: false,
searchable: false,
phone: false,
on_leading: None,
back_label: "Back".into(),
menu_label: "Menu".into(),
search_label: "Search".into(),
}
}
pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
pub fn leading(mut self, leading: Leading) -> Self {
self.leading = leading;
self
}
pub fn actions(mut self, actions: impl IntoIterator<Item = BarAction>) -> Self {
self.actions = actions.into_iter().collect();
self
}
pub fn large(mut self, large: bool) -> Self {
self.large = large;
self
}
pub fn searchable(mut self, searchable: bool) -> Self {
self.searchable = searchable;
self
}
pub fn phone(mut self, phone: bool) -> Self {
self.phone = phone;
self
}
pub fn on_leading(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_leading = Some(Rc::new(f));
self
}
pub fn labels(
mut self,
back: impl Into<SharedString>,
menu: impl Into<SharedString>,
search: impl Into<SharedString>,
) -> Self {
self.back_label = back.into();
self.menu_label = menu.into();
self.search_label = search.into();
self
}
}
impl RenderOnce for MobileAppBar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let lead = match self.leading {
Leading::Back => Some((self.back_label.clone(), CarbonIcon::ChevronLeft)),
Leading::Menu => Some((self.menu_label.clone(), CarbonIcon::Menu)),
Leading::None => None,
};
let lead_el = match lead.clone() {
Some((label, icon)) => mbtn(
(id.clone(), "lead").into(),
label,
icon,
self.on_leading.clone(),
window,
cx,
),
None => div().flex_none().w(rem(44.)).into_any_element(),
};
let actions: Vec<AnyElement> = self
.actions
.iter()
.enumerate()
.map(|(i, a)| {
mbtn(
sub(&id, "act", i),
a.label.clone(),
a.icon,
a.on_press.clone(),
window,
cx,
)
})
.collect();
let n_actions = actions.len();
let title = (!self.large).then(|| {
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.items_center()
.text_center()
.child(
sans(div(), 16., 600)
.max_w_full()
.when(self.phone, |d| {
d.overflow_hidden().whitespace_nowrap().text_ellipsis()
})
.text_color(color(ColorRole::TextPrimary))
.child(self.title.clone()),
)
.when_some(self.subtitle.clone(), |this, s| {
this.child(
craft::text(s, 12., ColorRole::TextSecondary)
.max_w_full()
.when(self.phone, |d| {
d.overflow_hidden().whitespace_nowrap().text_ellipsis()
}),
)
})
});
let acts = div().flex().children(actions);
let row = if self.phone && lead.is_some() {
let slot = nav_slot(n_actions);
div()
.flex()
.items_center()
.h(rem(44.))
.child(div().flex_none().w(px(slot)).child(lead_el))
.child(title.unwrap_or_else(|| div().flex_1()))
.child(
div()
.flex()
.flex_none()
.w(px(slot))
.justify_end()
.child(acts),
)
} else {
div()
.flex()
.items_center()
.gap(rem(4.))
.h(rem(44.))
.child(lead_el)
.child(title.unwrap_or_else(|| div().flex_1()))
.child(acts)
};
let search = self.searchable.then(|| {
let state = window
.use_keyed_state((id.clone(), "search"), cx, |window, cx| {
let placeholder = self.search_label.clone();
cx.new(|cx| InputState::new(window, cx).placeholder(placeholder))
})
.read(cx)
.clone();
div()
.flex()
.items_center()
.gap(rem(8.))
.h(rem(if self.phone { 40. } else { 36. }))
.mt(rem(if self.phone { 12. } else { 8. }))
.mx(rem(8.))
.px(rem(12.))
.bg(color(if self.phone {
ColorRole::Field01
} else {
ColorRole::LayerAccent01
}))
.when(self.phone, |d| {
d.border_b_1()
.border_color(color(ColorRole::BorderStrong01))
})
.child(
Icon::carbon(CarbonIcon::Search)
.size_length(rem(16.))
.color(color(ColorRole::IconSecondary)),
)
.child(
KitInput::new(&state)
.appearance(false)
.with_size(Size::XSmall)
.p_0()
.flex_1()
.min_w_0()
.text_size(rem(16.))
.text_color(color(ColorRole::TextPrimary)),
)
});
div()
.id(id)
.flex()
.flex_col()
.px(rem(8.))
.pb(rem(8.))
.bg(color(ColorRole::Background))
.child(row)
.when(self.large, |this| {
this.child(
sans(div(), 32., 600)
.px(rem(8.))
.when(self.phone, |d| d.line_height(rem(40.)))
.text_color(color(ColorRole::TextPrimary))
.child(self.title.clone()),
)
})
.children(search)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum RowKind {
#[default]
Navigation,
Toggle,
Choice,
Action,
}
impl RowKind {
pub const ALL: [RowKind; 4] = [Self::Navigation, Self::Toggle, Self::Choice, Self::Action];
pub const fn name(self) -> &'static str {
match self {
Self::Navigation => "navigation",
Self::Toggle => "toggle",
Self::Choice => "choice",
Self::Action => "action",
}
}
}
pub fn switch_geometry(phone: bool) -> (f32, f32, f32, f32, f32) {
if phone {
(48., 24., 18., 3., 24.)
} else {
(48., 28., 24., 2., 20.)
}
}
#[derive(IntoElement)]
pub struct ListRow {
id: ElementId,
icon: Option<CarbonIcon>,
title: SharedString,
subtitle: Option<SharedString>,
value: Option<SharedString>,
kind: RowKind,
checked: bool,
on: Option<bool>,
default_on: bool,
destructive: bool,
on_press: Option<Handler>,
on_toggle: Option<BoolHandler>,
phone: bool,
divider: Option<f32>,
}
impl ListRow {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
icon: None,
title: title.into(),
subtitle: None,
value: None,
kind: RowKind::Navigation,
checked: false,
on: None,
default_on: false,
destructive: false,
on_press: None,
on_toggle: None,
phone: false,
divider: None,
}
}
pub fn icon(mut self, icon: CarbonIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
pub fn value(mut self, value: impl Into<SharedString>) -> Self {
self.value = Some(value.into());
self
}
pub fn kind(mut self, kind: RowKind) -> Self {
self.kind = kind;
self
}
pub fn checked(mut self, checked: bool) -> Self {
self.checked = checked;
self
}
pub fn on(mut self, on: bool) -> Self {
self.on = Some(on);
self
}
pub fn default_on(mut self, on: bool) -> Self {
self.default_on = on;
self
}
pub fn destructive(mut self, destructive: bool) -> Self {
self.destructive = destructive;
self
}
pub fn on_press(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_press = Some(Rc::new(f));
self
}
pub fn on_toggle(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_toggle = Some(Rc::new(f));
self
}
pub fn phone(mut self, phone: bool) -> Self {
self.phone = phone;
self
}
fn has_icon(&self) -> bool {
self.icon.is_some()
}
}
pub fn divider_inset(phone: bool, icon: bool, previous_icon: bool) -> f32 {
if icon || (phone && previous_icon) {
52.
} else {
16.
}
}
impl RenderOnce for ListRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let ink = color(if self.destructive {
ColorRole::TextError
} else if self.kind == RowKind::Action {
ColorRole::LinkPrimary
} else {
ColorRole::TextPrimary
});
let icon = self.icon.map(|icon| {
let (bg, fg) = if self.phone {
(
None,
color(if self.destructive {
ColorRole::TextError
} else {
ColorRole::IconPrimary
}),
)
} else {
(
Some(color(ColorRole::Interactive)),
color(ColorRole::TextOnColor),
)
};
div()
.flex()
.flex_none()
.items_center()
.justify_center()
.w(rem(if self.phone { 20. } else { 28. }))
.h(rem(28.))
.when_some(bg, |d, bg| d.bg(bg))
.child(Icon::carbon(icon).size_length(rem(20.)).color(fg))
});
let text = div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.when(self.phone, |d| d.gap(px(2.)))
.child(self.title.clone())
.when_some(self.subtitle.clone(), |d, s| {
d.child(craft::text(s, 13., ColorRole::TextSecondary))
});
let value = |v: SharedString| craft::text(v, 16., ColorRole::TextSecondary);
let focus = focus_handle(&id, window, cx);
let ring = focus_visible(&focus, window);
let row = sans(div(), 16., 400)
.relative()
.flex()
.items_center()
.gap(rem(12.))
.w_full()
.min_h(rem(if self.phone { 56. } else { 44. }))
.px(rem(16.))
.py(rem(8.))
.text_color(ink)
.children(icon)
.child(text);
let row = match self.kind {
RowKind::Toggle => {
let (on, store) = crate::components::form::controlled(
(id.clone(), "on"),
self.on,
self.default_on,
window,
cx,
);
let (w, h, knob, inset, travel) = switch_geometry(self.phone);
let fade = Motion::new(Curve::ProductiveStandard, Speed::Fast02);
let bg = motion::animate_color(
(id.clone(), "track"),
color(if on {
ColorRole::SupportSuccess
} else {
ColorRole::ToggleOff
}),
fade,
window,
cx,
);
let x = motion::animate(
(id.clone(), "knob"),
if on { travel } else { 0. },
fade.spatial(true),
window,
cx,
);
let on_toggle = self.on_toggle.clone();
let phone = self.phone;
row.child(
press(id.clone(), &focus)
.role(gpui_kit::Role::Switch)
.accessibility_label(self.title.clone())
.flex_none()
.w(rem(w))
.h(rem(h))
.rounded(rem(16.))
.bg(bg)
.on_click(move |_, window, cx| {
store.update(cx, |v, cx| {
*v = !on;
cx.notify();
});
if let Some(f) = &on_toggle {
f(!on, window, cx);
}
})
.child(
div()
.absolute()
.top(px(inset))
.left(px(inset + x))
.size(rem(knob))
.rounded_full()
.bg(gpui_kit::white())
.when(!phone, |d| {
d.shadow(vec![
BoxShadow::new(
px(0.),
px(1.),
gpui_kit::black().opacity(0.3),
)
.blur_radius(px(2.)),
])
}),
)
.when(ring, |this| this.child(craft::focus_ring())),
)
.into_any_element()
}
kind => {
let right: Option<AnyElement> = match kind {
RowKind::Navigation => Some(
div()
.flex()
.items_center()
.gap(rem(12.))
.children(self.value.clone().map(value))
.child(
Icon::carbon(CarbonIcon::ChevronRight)
.size_length(rem(16.))
.color(color(ColorRole::IconSecondary)),
)
.into_any_element(),
),
RowKind::Choice => self.checked.then(|| {
Icon::carbon(CarbonIcon::Checkmark)
.size_length(rem(20.))
.color(color(ColorRole::Interactive))
.into_any_element()
}),
_ => self.value.clone().map(|v| value(v).into_any_element()),
};
let on_press = self.on_press.clone();
press(id.clone(), &focus)
.when(kind == RowKind::Choice, |b| {
b.role(gpui_kit::Role::MenuItemRadio)
})
.accessibility_label(self.title.clone())
.w_full()
.active(|s| s.bg(color(ColorRole::LayerHover01)))
.when_some(on_press, |this, f| this.on_click(move |_, w, cx| f(w, cx)))
.child(row.children(right))
.when(ring, |this| this.child(craft::focus_ring()))
.into_any_element()
}
};
div()
.relative()
.w_full()
.child(row)
.when_some(self.divider, |this, left| {
this.child(
div()
.absolute()
.top_0()
.left(rem(left))
.right_0()
.h(px(1.))
.bg(color(if self.phone {
ColorRole::BorderSubtle00
} else {
ColorRole::BorderSubtle01
})),
)
})
}
}
#[derive(IntoElement)]
pub struct MobileListSection {
title: SharedString,
footer: Option<SharedString>,
rows: Vec<ListRow>,
raised: bool,
phone: bool,
}
impl MobileListSection {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
footer: None,
rows: Vec::new(),
raised: false,
phone: false,
}
}
pub fn footer(mut self, footer: impl Into<SharedString>) -> Self {
self.footer = Some(footer.into());
self
}
pub fn row(mut self, row: ListRow) -> Self {
self.rows.push(row);
self
}
pub fn rows(mut self, rows: impl IntoIterator<Item = ListRow>) -> Self {
self.rows.extend(rows);
self
}
pub fn raised(mut self, raised: bool) -> Self {
self.raised = raised;
self
}
pub fn phone(mut self, phone: bool) -> Self {
self.phone = phone;
self
}
}
impl RenderOnce for MobileListSection {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let phone = self.phone;
let mut previous_icon = false;
let rows: Vec<ListRow> = self
.rows
.into_iter()
.enumerate()
.map(|(i, mut row)| {
row.phone = row.phone || phone;
let icon = row.has_icon();
if i > 0 {
row.divider = Some(divider_inset(row.phone, icon, previous_icon));
}
previous_icon = icon;
row
})
.collect();
div()
.flex()
.flex_col()
.px(rem(16.))
.when(!phone, |d| d.pb(rem(24.)))
.child(if self.title.is_empty() {
div().pb(rem(MLIST_HEADING_GAP))
} else {
craft::text(self.title.to_uppercase(), 12., ColorRole::TextSecondary)
.pb(rem(MLIST_HEADING_GAP))
})
.child(
div()
.flex()
.flex_col()
.overflow_hidden()
.bg(color(if self.raised {
ColorRole::Layer02
} else {
ColorRole::Layer01
}))
.children(rows),
)
.when_some(self.footer, |this, f| {
this.child(craft::text(f, 12., ColorRole::TextSecondary).pt(rem(8.)))
})
}
}
pub const MLIST_HEADING_GAP: f32 = 8.;
pub const SHEET_INTRO_GAP: f32 = 12.;
pub const SHEET_INTRO_LINE: f32 = 1.5;
pub const SHEET_HEAD_HEIGHT: f32 = 8. + 5. + 44.;
#[derive(IntoElement)]
pub struct MobileSheet {
id: ElementId,
title: SharedString,
intro: Option<SharedString>,
primary: Option<SharedString>,
inline: bool,
on_close: Option<Handler>,
on_primary: Option<Handler>,
close_label: SharedString,
children: Vec<AnyElement>,
}
impl MobileSheet {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: title.into(),
intro: None,
primary: None,
inline: true,
on_close: None,
on_primary: None,
close_label: "Close".into(),
children: Vec::new(),
}
}
pub fn intro(mut self, intro: impl Into<SharedString>) -> Self {
self.intro = Some(intro.into());
self
}
pub fn primary(mut self, primary: impl Into<SharedString>) -> Self {
self.primary = Some(primary.into());
self
}
pub fn inline(mut self, inline: bool) -> Self {
self.inline = inline;
self
}
pub fn on_close(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_close = Some(Rc::new(f));
self
}
pub fn on_primary(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_primary = Some(Rc::new(f));
self
}
pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
self.close_label = label.into();
self
}
}
impl ParentElement for MobileSheet {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MobileSheet {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let close = mbtn(
(id.clone(), "close").into(),
self.close_label.clone(),
CarbonIcon::Close,
self.on_close.clone(),
window,
cx,
);
let primary = match self.primary.clone() {
Some(label) => {
let pid: ElementId = (id.clone(), "primary").into();
let focus = focus_handle(&pid, window, cx);
let ring = focus_visible(&focus, window);
let on_primary = self.on_primary.clone();
sans(press(pid, &focus), 16., 600)
.h(rem(44.))
.px(rem(12.))
.text_color(color(ColorRole::LinkPrimary))
.when_some(on_primary, |b, f| b.on_click(move |_, w, cx| f(w, cx)))
.child(label)
.when(ring, |b| b.child(craft::focus_ring()))
.into_any_element()
}
None => div().w(rem(44.)).into_any_element(),
};
div()
.id(id)
.flex()
.flex_col()
.w_full()
.when(self.inline, |d| d.mt_auto())
.when_some(screen_bottom_radius(), |d, r| d.rounded_b(rem(r)))
.bg(color(ColorRole::Layer01))
.shadow(float_shadow(-2.))
.child(
div()
.mt(rem(8.))
.mx_auto()
.w(rem(36.))
.h(px(5.))
.rounded(px(3.))
.bg(color(ColorRole::BorderStrong01)),
)
.child(
div()
.flex()
.items_center()
.px(rem(8.))
.child(div().flex().flex_1().flex_basis(px(0.)).child(close))
.child(
sans(div(), 16., 600)
.flex_none()
.text_color(color(ColorRole::TextPrimary))
.child(self.title.clone()),
)
.child(
div()
.flex()
.flex_1()
.flex_basis(px(0.))
.justify_end()
.child(primary),
),
)
.when_some(self.intro.clone(), |this, intro| {
this.child(
craft::text(intro, 14., ColorRole::TextSecondary)
.line_height(relative(SHEET_INTRO_LINE))
.px(rem(24.))
.pb(rem(SHEET_INTRO_GAP))
.text_center(),
)
})
.child(div().pb(rem(32.)).children(self.children))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DockTab {
pub id: SharedString,
pub label: SharedString,
pub icon: CarbonIcon,
pub badge: Option<usize>,
}
impl DockTab {
pub fn new(
id: impl Into<SharedString>,
label: impl Into<SharedString>,
icon: CarbonIcon,
) -> Self {
Self {
id: id.into(),
label: label.into(),
icon,
badge: None,
}
}
pub fn badge(mut self, badge: usize) -> Self {
self.badge = Some(badge);
self
}
}
#[derive(IntoElement)]
pub struct MobileTabDock {
id: ElementId,
tabs: Vec<DockTab>,
value: Option<SharedString>,
on_change: Option<ValueHandler>,
phone: bool,
}
impl MobileTabDock {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
tabs: Vec::new(),
value: None,
on_change: None,
phone: false,
}
}
pub fn tabs(mut self, tabs: impl IntoIterator<Item = DockTab>) -> Self {
self.tabs = tabs.into_iter().collect();
self
}
pub fn value(mut self, value: impl Into<SharedString>) -> Self {
self.value = Some(value.into());
self
}
pub fn on_value_change(
mut self,
f: impl Fn(SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_change = Some(Rc::new(f));
self
}
pub fn phone(mut self, phone: bool) -> Self {
self.phone = phone;
self
}
}
pub fn home_indicator() -> AnyElement {
div()
.absolute()
.bottom(px(8.))
.left_0()
.right_0()
.flex()
.justify_center()
.child(
div()
.w(px(134.))
.h(px(5.))
.rounded(px(3.))
.bg(color(ColorRole::TextPrimary)),
)
.into_any_element()
}
impl RenderOnce for MobileTabDock {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let first = self.tabs.first().map(|t| t.id.clone()).unwrap_or_default();
let (current, store) = crate::components::form::controlled(
(id.clone(), "value"),
self.value.clone(),
first,
window,
cx,
);
let phone = self.phone;
let tabs = self.tabs.iter().enumerate().map(|(i, t)| {
let on = t.id == current;
let tid = sub(&id, "tab", i);
let focus = focus_handle(&tid, window, cx);
let ring = focus_visible(&focus, window);
let ink = color(if !on {
ColorRole::TextSecondary
} else if phone {
ColorRole::Interactive
} else {
ColorRole::LinkPrimary
});
let value = t.id.clone();
let store = store.clone();
let on_change = self.on_change.clone();
let badge = t.badge.map(|n| {
let b = sans(
div(),
if phone { 11. } else { 10. },
if phone { 600 } else { 400 },
)
.absolute()
.min_w(rem(if phone { 18. } else { 16. }))
.px(px(if phone { 5. } else { 4. }))
.h(rem(if phone { 18. } else { 16. }))
.line_height(rem(if phone { 18. } else { 16. }))
.rounded(rem(if phone { 9. } else { 8. }))
.text_center()
.bg(color(ColorRole::SupportWarning))
.text_color(crate::t3::base::ink())
.child(n.to_string());
if phone {
b.top(px(3.)).left(relative(0.5)).ml(px(6.)).shadow(vec![
BoxShadow::new(px(0.), px(0.), color(ColorRole::Background))
.spread_radius(px(2.)),
])
} else {
b.top(px(2.)).right(rem(14.4))
}
});
sans(
press(tid, &focus),
if phone { 12. } else { 11. },
if on && phone { 600 } else { 400 },
)
.role(gpui_kit::Role::Tab)
.accessibility_label(t.label.clone())
.flex_col()
.items_center()
.justify_center()
.gap(px(if phone { 4. } else { 2. }))
.when(phone, |b| b.flex_1().min_w_0().h(px(56.)))
.when(!phone, |b| {
b.min_w(rem(64.))
.px(rem(12.))
.py(rem(4.))
.when(on, |b| b.bg(color(ColorRole::LayerSelected01)))
})
.text_color(ink)
.on_click(move |_, window, cx| {
store.update(cx, |v, cx| {
*v = value.clone();
cx.notify();
});
if let Some(f) = &on_change {
f(value.clone(), window, cx);
}
})
.child(Icon::carbon(t.icon).size_length(rem(20.)).color(ink))
.child(t.label.clone())
.children(badge)
.when(ring, |b| b.child(craft::focus_ring()))
.into_any_element()
});
let tabs: Vec<AnyElement> = tabs.collect();
if phone {
div()
.id(id)
.relative()
.flex()
.w_full()
.px(rem(8.))
.pb(px(34.))
.bg(color(ColorRole::Background))
.border_t_1()
.border_color(color(ColorRole::BorderSubtle01))
.children(tabs)
.child(home_indicator())
} else {
div()
.id(id)
.relative()
.flex()
.self_center()
.mx_auto()
.mt(rem(8.))
.mb(rem(16.))
.gap(rem(2.))
.p(rem(4.))
.bg(color(ColorRole::Layer02))
.shadow(float_shadow(2.))
.children(tabs)
.child(inset_frame(color(ColorRole::BorderSubtle01)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_phone_screen_mask_stays_in_the_bezel() {
use std::f32::consts::FRAC_1_SQRT_2;
assert_eq!(
(PHONE_BEZEL, PHONE_RADIUS, PHONE_SCREEN_RADIUS),
(10., 44., 36.)
);
let ring = PHONE_RADIUS - (PHONE_RADIUS - PHONE_BEZEL) * FRAC_1_SQRT_2;
let screen = PHONE_BEZEL + PHONE_SCREEN_RADIUS * (1. - FRAC_1_SQRT_2);
assert!((ring - screen).abs() < 1., "{ring} vs {screen}");
}
#[test]
fn full_bleed_corners_stay_in_the_bezel() {
let centre = PHONE_RADIUS;
let corner = (centre - PHONE_BEZEL) * std::f32::consts::SQRT_2;
assert!(corner > PHONE_RADIUS, "{corner}");
let r = screen_bottom_radius_px();
let child_centre = PHONE_BEZEL + r;
let farthest = (child_centre - centre) * std::f32::consts::SQRT_2 + r;
assert!(farthest <= PHONE_RADIUS, "{farthest}");
assert_eq!(screen_bottom_radius(), None);
assert_eq!(
with_phone_screen(screen_bottom_radius),
Some(PHONE_SCREEN_RADIUS)
);
assert!(!in_phone_screen());
}
fn screen_bottom_radius_px() -> f32 {
with_phone_screen(screen_bottom_radius).unwrap()
}
#[test]
fn sheet_geometry_follows_the_reference() {
assert_eq!(SHEET_HEAD_HEIGHT, 57.);
assert_eq!(14. * SHEET_INTRO_LINE, 21.);
assert_eq!(
SHEET_HEAD_HEIGHT + 14. * SHEET_INTRO_LINE + SHEET_INTRO_GAP + MLIST_HEADING_GAP,
98.
);
}
#[test]
fn list_and_bar_geometry() {
assert_eq!(divider_inset(false, true, false), 52.);
assert_eq!(divider_inset(false, false, true), 16.);
assert_eq!(divider_inset(true, false, true), 52.);
assert_eq!(nav_slot(2), 88.);
assert_eq!(nav_slot(1), 52.);
assert_eq!(switch_geometry(false), (48., 28., 24., 2., 20.));
assert_eq!(switch_geometry(true), (48., 24., 18., 3., 24.));
}
}