use super::button::{Button, ButtonSize, ButtonVariant};
use super::form::{controlled, rem};
use crate::{
icons::CarbonIcon,
motion::{self, Curve, Motion, Speed},
theme::color,
tokens::{ColorRole, SIZE_MD, SPACING_02, SPACING_03, SPACING_04, SPACING_06},
};
use gpui_kit::{
AnyElement, App, CursorStyle, ElementId, Hsla, InteractiveElement, IntoElement, KeyDownEvent,
ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled,
Window, div, prelude::FluentBuilder as _, px, relative,
};
use std::{rc::Rc, time::Instant};
pub const SLIDE: Motion = Motion::new(Curve::ProductiveStandard, Speed::Moderate02).spatial(true);
pub fn reduced_fade(elapsed: f32) -> Option<(bool, f32)> {
let duration = SLIDE.speed.duration().as_secs_f32();
if elapsed >= duration {
return None;
}
let half = duration / 2.;
let ease = |t: f32| SLIDE.curve.ease(t.clamp(0., 1.));
Some(if elapsed < half {
(true, 1. - ease(elapsed / half))
} else {
(false, ease((elapsed - half) / half))
})
}
#[derive(Clone, Copy, Default)]
struct Fade {
shown: usize,
from: Option<(usize, Instant)>,
}
pub const DOT_HEIGHT: f32 = 4.;
pub const DOT_FOCUS: (f32, f32) = (2., 2.);
pub const GAP: f32 = 1.;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CarouselOrientation {
#[default]
Horizontal,
Vertical,
}
impl CarouselOrientation {
pub const ALL: [CarouselOrientation; 2] = [Self::Horizontal, Self::Vertical];
pub const fn name(self) -> &'static str {
match self {
Self::Horizontal => "horizontal",
Self::Vertical => "vertical",
}
}
}
pub const fn button_margin_top() -> f32 {
-(SIZE_MD.px_value() + SPACING_02.px_value())
}
pub fn dot_color(current: bool) -> Hsla {
color(if current {
ColorRole::Interactive
} else {
ColorRole::LayerAccent01
})
}
pub fn key_index(key: &str, index: usize, count: usize) -> Option<usize> {
match key {
"right" => Some((index + 1).min(count.saturating_sub(1))),
"left" => Some(index.saturating_sub(1)),
_ => None,
}
}
pub fn ends(index: usize, count: usize) -> (bool, bool) {
(index == 0, index + 1 >= count)
}
#[derive(Default)]
pub struct CarouselItem {
children: Vec<AnyElement>,
}
impl CarouselItem {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for CarouselItem {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
type DotLabel = Rc<dyn Fn(usize) -> SharedString>;
#[derive(Default)]
pub struct CarouselContent {
items: Vec<CarouselItem>,
dot_label: Option<DotLabel>,
}
impl CarouselContent {
pub fn new() -> Self {
Self::default()
}
pub fn item(mut self, item: CarouselItem) -> Self {
self.items.push(item);
self
}
pub fn items(mut self, items: impl IntoIterator<Item = CarouselItem>) -> Self {
self.items.extend(items);
self
}
pub fn dot_labels(mut self, label: impl Fn(usize) -> SharedString + 'static) -> Self {
self.dot_label = Some(Rc::new(label));
self
}
}
pub struct CarouselPrevious {
label: SharedString,
}
impl CarouselPrevious {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
}
}
}
pub struct CarouselNext {
label: SharedString,
}
impl CarouselNext {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
}
}
}
type IndexHandler = Rc<dyn Fn(usize, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct Carousel {
id: ElementId,
orientation: CarouselOrientation,
label: Option<SharedString>,
content: CarouselContent,
previous: Option<CarouselPrevious>,
next: Option<CarouselNext>,
index: Option<usize>,
default_index: usize,
on_index_change: Option<IndexHandler>,
}
impl Carousel {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
orientation: CarouselOrientation::Horizontal,
label: None,
content: CarouselContent::new(),
previous: None,
next: None,
index: None,
default_index: 0,
on_index_change: None,
}
}
pub fn orientation(mut self, orientation: CarouselOrientation) -> Self {
self.orientation = orientation;
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn content(mut self, content: CarouselContent) -> Self {
self.content = content;
self
}
pub fn previous(mut self, previous: CarouselPrevious) -> Self {
self.previous = Some(previous);
self
}
pub fn next(mut self, next: CarouselNext) -> Self {
self.next = Some(next);
self
}
pub fn index(mut self, index: usize) -> Self {
self.index = Some(index);
self
}
pub fn default_index(mut self, index: usize) -> Self {
self.default_index = index;
self
}
pub fn on_index_change(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
self.on_index_change = Some(Rc::new(f));
self
}
}
impl RenderOnce for Carousel {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let count = self.content.items.len();
let (index, store) = controlled(
(id.clone(), "index"),
self.index,
self.default_index,
window,
cx,
);
let index = index.min(count.saturating_sub(1));
let on_change = self.on_index_change.clone();
let set: IndexHandler = Rc::new(move |i, window, cx| {
store.update(cx, |v, cx| {
*v = i;
cx.notify();
});
if let Some(f) = on_change.as_ref() {
f(i, window, cx);
}
});
let vertical = self.orientation == CarouselOrientation::Vertical;
let position = motion::animate((id.clone(), "slide"), index as f32, SLIDE, window, cx);
let fade = window.use_keyed_state((id.clone(), "fade"), cx, move |_, _| Fade {
shown: index,
from: None,
});
let reduced = motion::reduced_motion(cx);
let (position, track_opacity) = {
let now = Instant::now();
let f = *fade.read(cx);
let next = if f.shown != index {
Fade {
shown: index,
from: reduced.then_some((f.shown, now)),
}
} else {
f
};
if next.shown != f.shown || next.from.is_some() != f.from.is_some() {
fade.update(cx, |v, _| *v = next);
}
match next.from.and_then(|(from, at)| {
reduced_fade((now - at).as_secs_f32()).map(|step| (from, step))
}) {
Some((from, (show_from, opacity))) if reduced => {
window.request_animation_frame();
(if show_from { from as f32 } else { index as f32 }, opacity)
}
_ => (position, 1.),
}
};
let slide_height =
window.use_keyed_state((id.clone(), "slide-height"), cx, |_, _| None::<Pixels>);
let measured = *slide_height.read(cx);
let track = div()
.relative()
.opacity(track_opacity)
.flex()
.when(vertical, |this| this.flex_col())
.map(|this| {
if vertical {
this.top(measured.unwrap_or(px(0.)) * -position)
} else {
this.left(relative(-position))
}
})
.on_children_prepainted({
let slide_height = slide_height.clone();
move |bounds, _, cx| {
let h = bounds.first().map(|b| b.size.height);
if h.is_some() && h != *slide_height.read(cx) {
slide_height.update(cx, |v, cx| {
*v = h;
cx.notify();
});
}
}
})
.children(self.content.items.into_iter().map(|item| {
div()
.flex_none()
.flex()
.flex_col()
.min_w_0()
.when(!vertical, |this| this.w_full())
.children(item.children)
}));
let dot_label = self.content.dot_label.clone();
let dots = div()
.flex()
.flex_row()
.justify_start()
.gap(SPACING_03)
.py(SPACING_04)
.children((0..count).map(|k| {
let set = set.clone();
let focus = window
.use_keyed_state((id.clone(), format!("dot-{k}")), cx, |_, cx| {
cx.focus_handle()
})
.read(cx)
.clone();
let focus_visible =
focus.is_focused(window) && crate::focus::focus_visible_mode(window);
let (width, offset) = (DOT_FOCUS.0, DOT_FOCUS.1);
gpui_kit::base::Button::new((id.clone(), format!("dot-{k}")))
.track_focus(&focus)
.when_some(dot_label.as_ref(), |this, label| {
this.accessibility_label(label(k))
})
.selected(k == index)
.relative()
.flex_none()
.w(SPACING_06)
.h(rem(DOT_HEIGHT))
.bg(dot_color(k == index))
.cursor(CursorStyle::PointingHand)
.on_click(move |_, window, cx| set(k, window, cx))
.when(focus_visible, |this| {
this.child(
div()
.absolute()
.top(px(-(width + offset)))
.left(px(-(width + offset)))
.right(px(-(width + offset)))
.bottom(px(-(width + offset)))
.border(px(width))
.border_color(color(ColorRole::Focus)),
)
})
}));
let viewport = div()
.flex_none()
.w_full()
.flex()
.flex_col()
.child(
div()
.w_full()
.overflow_hidden()
.when_some(measured.filter(|_| vertical), |this, h| this.h(h))
.child(track),
)
.child(dots);
let (at_start, at_end) = ends(index, count);
let nav = |icon: CarbonIcon, label: SharedString, disabled: bool, to: usize, key: &str| {
let set = set.clone();
div().mt(rem(button_margin_top())).child(
Button::new((id.clone(), key.to_string()), label)
.variant(ButtonVariant::Secondary)
.size(ButtonSize::Md)
.icon(icon)
.icon_only(true)
.disabled(disabled)
.on_click(move |_, window, cx| set(to, window, cx)),
)
};
let previous = self.previous.map(|p| {
nav(
CarbonIcon::ChevronLeft,
p.label,
at_start,
index.saturating_sub(1),
"previous",
)
});
let next = self.next.map(|n| {
nav(
CarbonIcon::ChevronRight,
n.label,
at_end,
(index + 1).min(count.saturating_sub(1)),
"next",
)
});
let on_key = {
let set = set.clone();
move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
if let Some(next) = key_index(event.keystroke.key.as_str(), index, count) {
cx.stop_propagation();
if next != index {
set(next, window, cx);
}
}
}
};
div()
.id(id.clone())
.relative()
.w_full()
.flex()
.flex_row()
.flex_wrap()
.justify_end()
.gap(px(GAP))
.when_some(self.label, |this, label| this.aria_label(label))
.on_key_down(on_key)
.child(viewport)
.when_some(previous, |this, b| this.child(b))
.when_some(next, |this, b| this.child(b))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn dots_follow_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(dot_color(false), color(ColorRole::LayerAccent01));
assert_eq!(dot_color(true), color(ColorRole::Interactive));
assert_eq!((SPACING_06.px_value(), DOT_HEIGHT), (24., 4.));
assert_eq!(DOT_FOCUS, (2., 2.));
assert_eq!((SPACING_03.px_value(), SPACING_04.px_value()), (8., 12.));
}
#[test]
fn layout_and_motion_follow_custom_css() {
assert_eq!(button_margin_top(), -44.);
assert_eq!(GAP, 1.);
assert_eq!(SLIDE.speed.millis(), 240);
assert_eq!(SLIDE.curve, Curve::ProductiveStandard);
const { assert!(SLIDE.spatial) };
}
#[test]
fn keys_and_ends_clamp() {
assert_eq!(key_index("right", 0, 3), Some(1));
assert_eq!(key_index("right", 2, 3), Some(2));
assert_eq!(key_index("left", 0, 3), Some(0));
assert_eq!(key_index("left", 2, 3), Some(1));
assert_eq!(key_index("up", 1, 3), None);
assert_eq!(ends(0, 3), (true, false));
assert_eq!(ends(2, 3), (false, true));
assert_eq!(ends(1, 3), (false, false));
}
#[test]
fn reduced_motion_fades_instead_of_cutting() {
assert_eq!(reduced_fade(0.), Some((true, 1.)));
let (old, o) = reduced_fade(0.06).unwrap();
assert!(old && o > 0. && o < 1.);
let (old, o) = reduced_fade(0.18).unwrap();
assert!(!old && o > 0. && o < 1.);
assert_eq!(reduced_fade(0.24), None);
}
}