use std::{cell::RefCell, rc::Rc};
use gpui::{
AccessibleAction, AnyElement, App, BorderStyle, Bounds, ClickEvent, Corners, ElementId,
FocusHandle, Hsla, IntoElement, MouseButton, ParentElement, Pixels, Point,
StatefulInteractiveElement, Styled, Window, canvas, div, prelude::FluentBuilder, px, quad,
};
use gpui_base::{Button, Toggle};
use gpui_material_foundation::{
InteractionFeedback, InteractionState, Ripple, Shape, animate, color, elevation_shadows,
window_material_theme,
};
use gpui_material_theme::{
ColorRole, CornerRole, CornerShape, Elevation, MotionKind, MotionSpec, MotionSpeed,
};
pub(crate) type Activate = Rc<dyn Fn(&mut Window, &mut App)>;
pub(crate) type Change = Rc<dyn Fn(bool, &mut Window, &mut App)>;
pub(crate) enum Action {
Invoke(Option<Activate>),
Toggle {
selected: bool,
on_change: Option<Change>,
},
}
#[derive(Clone, Copy)]
pub(crate) struct ActionStyle {
pub(crate) background: Option<ColorRole>,
pub(crate) background_color: Option<Hsla>,
pub(crate) foreground: ColorRole,
pub(crate) disabled: DisabledStyle,
pub(crate) height: f32,
pub(crate) min_width: f32,
pub(crate) corner: CornerRole,
pub(crate) pressed_corner: CornerRole,
pub(crate) selected_corner: Option<CornerRole>,
pub(crate) corner_motion: (MotionKind, MotionSpeed),
pub(crate) outline: f32,
pub(crate) elevation: Elevation,
pub(crate) hover_elevation: Elevation,
}
#[derive(Clone, Copy)]
pub(crate) struct DisabledStyle {
pub(crate) foreground: ColorRole,
pub(crate) background: Option<(ColorRole, f32)>,
pub(crate) outline: (ColorRole, f32),
}
impl ActionStyle {
pub(crate) fn foreground(self, disabled: bool, window: &Window, cx: &App) -> Hsla {
let colors = &window_material_theme(window, cx).colors;
if disabled {
color(colors.color(self.disabled.foreground)).opacity(0.38)
} else {
color(colors.color(self.foreground))
}
}
}
#[derive(Clone, Copy, Default)]
pub(crate) struct ActionLayout {
pub(crate) segment: Option<SegmentShape>,
pub(crate) width: Option<Pixels>,
pub(crate) tab_stop: bool,
pub(crate) semantic_only: bool,
}
#[derive(Clone, Copy)]
pub(crate) struct SegmentShape {
pub(crate) leading: bool,
pub(crate) trailing: bool,
pub(crate) inner: CornerRole,
pub(crate) pressed: CornerRole,
}
pub(crate) struct ActionControl {
pub(crate) id: ElementId,
pub(crate) label: gpui::SharedString,
pub(crate) focus: Option<FocusHandle>,
pub(crate) disabled: bool,
pub(crate) action: Action,
pub(crate) style: ActionStyle,
pub(crate) content: AnyElement,
}
#[derive(Default)]
pub(crate) struct FeedbackState {
hovered: bool,
pressed: bool,
keyboard_focus: Option<FocusHandle>,
shape: Option<Shape>,
ripple: Option<Ripple>,
}
impl FeedbackState {
fn cancel_unavailable(&mut self, enabled: bool, window: &Window) {
if !enabled || !window.is_window_active() {
self.hovered = false;
self.cancel();
} else if self
.keyboard_focus
.as_ref()
.is_some_and(|focus| !focus.is_focused(window))
{
self.cancel();
}
}
fn cancel(&mut self) {
self.pressed = false;
self.keyboard_focus = None;
self.ripple = None;
}
pub(crate) fn pressed(&self) -> bool {
self.pressed
}
}
impl ActionControl {
pub(crate) fn render(self, window: &mut Window, cx: &mut App) -> AnyElement {
self.render_scaled(1., window, cx)
}
pub(crate) fn render_scaled(self, scale: f32, window: &mut Window, cx: &mut App) -> AnyElement {
self.render_layout(
scale,
ActionLayout {
tab_stop: true,
..Default::default()
},
window,
cx,
)
}
pub(crate) fn render_layout(
self,
scale: f32,
layout: ActionLayout,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let feedback_id = (self.id.clone(), "material-feedback");
let (feedback, focus) = window
.use_keyed_state(feedback_id.clone(), cx, |_, cx| {
(
Rc::new(RefCell::new(FeedbackState::default())),
cx.focus_handle(),
)
})
.read(cx)
.clone();
let focus = self.focus.unwrap_or(focus);
let style = self.style;
let theme = window_material_theme(window, cx);
let density = theme.layout.density;
let direction = theme.layout.direction;
let reduced = cx.reduce_motion();
let (corner_kind, corner_speed) = style.corner_motion;
let motion = theme.motion.spec(corner_kind, corner_speed, reduced);
let effects = theme
.motion
.spec(MotionKind::Effects, MotionSpeed::Fast, reduced);
let selected = matches!(&self.action, Action::Toggle { selected: true, .. });
let foreground = color(theme.colors.color(style.foreground));
let background = style.background.map(|role| color(theme.colors.color(role)));
let outline = color(theme.colors.color(ColorRole::OutlineVariant));
let ring = color(theme.colors.color(ColorRole::Secondary));
let shadow = color(theme.colors.color(ColorRole::Shadow));
let disabled_foreground =
color(theme.colors.color(style.disabled.foreground)).opacity(0.38);
let disabled_background = style
.disabled
.background
.map_or_else(Hsla::default, |(role, opacity)| {
color(theme.colors.color(role)).opacity(opacity)
});
let disabled_outline =
color(theme.colors.color(style.disabled.outline.0)).opacity(style.disabled.outline.1);
let visual_height = px(density.visual_size(style.height) * scale);
let visual_width = layout
.width
.unwrap_or_else(|| px(density.visual_size(style.min_width) * scale));
let target_height = px(density.interactive_size(style.height) * scale);
let target_width = layout
.width
.unwrap_or_else(|| px(density.interactive_size(style.min_width.max(1.)) * scale));
let enabled = !self.disabled;
let (hovered, pressed) = {
let mut state = feedback.borrow_mut();
state.cancel_unavailable(enabled, window);
(
state.hovered,
enabled
&& state.pressed
&& (state.hovered
|| focus.is_focused(window) && window.last_input_was_keyboard()),
)
};
let progress = animate(
(self.id.clone(), "press-shape"),
f32::from(pressed),
motion,
window,
cx,
)
.clamp(0., 1.);
let selection = if style.selected_corner.is_some() {
animate(
(self.id.clone(), "selected-shape"),
f32::from(selected),
motion,
window,
cx,
)
.clamp(0., 1.)
} else {
0.
};
let focus_visible = focus.is_focused(window) && window.last_input_was_keyboard();
let mut interaction = InteractionState::default();
interaction.disabled = !enabled;
interaction.hovered = hovered;
interaction.pressed = pressed;
interaction.focus_visible = focus_visible;
let layer = animate(
(self.id.clone(), "state-layer"),
interaction
.layer_with_ripple(reduced)
.map_or(0., |layer| layer.opacity()),
if enabled {
effects
} else {
MotionSpec::Instant
},
window,
cx,
)
.clamp(0., 1.);
let outline_width = animate(
(self.id.clone(), "outline-width"),
style.outline,
effects,
window,
cx,
)
.max(0.);
let (foreground, background) = if enabled {
(
foreground,
style.background_color.or(background).unwrap_or_default(),
)
} else {
(disabled_foreground, disabled_background)
};
let elevation = if !enabled {
0.
} else if hovered && !pressed {
style.hover_elevation.dp()
} else {
style.elevation.dp()
};
let elevation = animate(
(self.id.clone(), "elevation"),
elevation,
effects,
window,
cx,
)
.max(0.)
* scale;
let mesh = feedback.clone();
let paint_feedback = feedback.clone();
let visual = canvas(
move |bounds, _, _| {
let corners =
interpolated_corners(bounds, style, selection, progress, scale, layout.segment);
let shape = Shape::rounded(bounds, corners, direction);
mesh.borrow_mut().shape = shape;
corners
.radii(
f32::from(bounds.size.width),
f32::from(bounds.size.height),
direction,
)
.map(px)
},
move |bounds, radii, window, cx| {
let [top_left, top_right, bottom_right, bottom_left] = radii;
let radii = Corners {
top_left,
top_right,
bottom_left,
bottom_right,
};
if elevation > 0. {
window.paint_drop_shadows(
bounds,
radii,
&elevation_shadows(elevation, scale, shadow),
);
}
let outline = if enabled { outline } else { disabled_outline };
if background.a > 0. || outline_width > 0. {
window.paint_quad(quad(
bounds,
radii,
background,
px(outline_width),
if outline_width > 0. {
outline
} else {
gpui::transparent_black()
},
BorderStyle::Solid,
));
}
let feedback = paint_feedback.borrow();
if let Some(shape) = &feedback.shape {
InteractionFeedback::new(interaction, foreground, ring).paint_with_opacity(
shape,
feedback.ripple.as_ref(),
layer,
cx.background_executor().now(),
reduced,
window,
);
}
},
)
.absolute()
.inset_0()
.size_full();
let content = div()
.relative()
.flex()
.items_center()
.justify_center()
.min_w(visual_width)
.max_w_full()
.min_h(visual_height)
.child(visual)
.child(self.content);
let id = self.id;
let label = self.label;
let disabled = self.disabled;
match self.action {
Action::Invoke(on_activate) => {
let control = Button::new(id)
.track_focus(&focus)
.tab_stop(layout.tab_stop)
.disabled(disabled)
.accessibility_label(label);
let control = match on_activate {
Some(on_activate) if enabled => {
let feedback = feedback.clone();
let invoke = on_activate.clone();
let control = control
.on_a11y_action(AccessibleAction::Click, move |_, window, cx| {
invoke(window, cx)
});
control.when(!layout.semantic_only, |control| {
control.on_click(move |event, window, cx| {
activate_feedback(event, &feedback, window, cx);
cx.stop_propagation();
on_activate(window, cx);
})
})
}
_ => control,
};
decorate(
control,
content,
feedback,
foreground,
target_height,
(target_width, layout.width.is_some()),
enabled,
)
}
Action::Toggle {
selected,
on_change,
} => {
let control = Toggle::new(id)
.track_focus(&focus)
.tab_stop(layout.tab_stop)
.pressed(selected)
.disabled(disabled)
.accessibility_label(label);
let control = match on_change {
Some(on_change) if enabled => {
let feedback = feedback.clone();
let toggle = on_change.clone();
control
.on_a11y_action(AccessibleAction::Click, move |_, window, cx| {
toggle(!selected, window, cx)
})
.on_change(move |selected, event, window, cx| {
activate_feedback(event, &feedback, window, cx);
cx.stop_propagation();
on_change(selected, window, cx);
})
}
_ => control,
};
decorate(
control,
content,
feedback,
foreground,
target_height,
(target_width, layout.width.is_some()),
enabled,
)
}
}
}
}
fn decorate(
control: impl Styled + gpui::StatefulInteractiveElement + ParentElement + IntoElement,
content: impl IntoElement,
feedback: Rc<RefCell<FeedbackState>>,
foreground: Hsla,
height: Pixels,
(width, fixed_width): (Pixels, bool),
enabled: bool,
) -> AnyElement {
let control = control
.min_h(height)
.min_w(width)
.when(fixed_width, |control| control.w(width))
.max_w_full()
.text_color(foreground)
.child(content);
control
.when(enabled, |control| feedback_target(control, feedback, true))
.into_any_element()
}
fn activate_feedback(
event: &ClickEvent,
state: &RefCell<FeedbackState>,
window: &mut Window,
cx: &App,
) {
let pointer = match event {
ClickEvent::Mouse(_) => return,
ClickEvent::Keyboard(_) => None,
ClickEvent::Touch(event) => Some(event.position),
};
let mut state = state.borrow_mut();
if !state.pressed {
start_ripple(&mut state, pointer, cx);
release_ripple(&mut state, cx);
}
window.refresh();
}
fn release_ripple(state: &mut FeedbackState, cx: &App) {
state.pressed = false;
state.keyboard_focus = None;
if let Some(ripple) = &mut state.ripple {
ripple.release(cx.background_executor().now());
}
}
fn start_ripple(state: &mut FeedbackState, pointer: Option<Point<Pixels>>, cx: &App) {
state.ripple = state.shape.as_ref().and_then(|shape| {
let pointer = pointer.filter(|pointer| shape.contains(*pointer));
Ripple::press(shape, pointer, cx.background_executor().now())
});
}
fn interpolated_corners(
bounds: Bounds<Pixels>,
style: ActionStyle,
selection: f32,
press: f32,
scale: f32,
segment: Option<SegmentShape>,
) -> CornerShape {
let width = f32::from(bounds.size.width) / scale;
let height = f32::from(bounds.size.height) / scale;
let direction = gpui_material_theme::Direction::Ltr;
let mut rest = style.corner.shape().radii(width, height, direction);
let mut selected = style
.selected_corner
.unwrap_or(style.corner)
.shape()
.radii(width, height, direction);
let mut pressed = style.pressed_corner.shape().radii(width, height, direction);
if let Some(segment) = segment {
let inner = segment.inner.shape().radii(width, height, direction)[0];
let inner_pressed = segment.pressed.shape().radii(width, height, direction)[0];
let outer = width.min(height) * 0.5;
rest = [
segment.leading,
segment.trailing,
segment.trailing,
segment.leading,
]
.map(|outer_edge| if outer_edge { outer } else { inner });
selected = [outer; 4];
pressed = [
segment.leading,
segment.trailing,
segment.trailing,
segment.leading,
]
.map(|outer_edge| if outer_edge { outer } else { inner_pressed });
}
CornerShape::Radii(std::array::from_fn(|ix| {
let resting = rest[ix] + (selected[ix] - rest[ix]) * selection;
(resting + (pressed[ix] - resting) * press) * scale
}))
}
pub(crate) fn ripple_target<E>(control: E, state: Rc<RefCell<FeedbackState>>) -> E
where
E: StatefulInteractiveElement,
{
feedback_target(control, state, false)
}
fn feedback_target<E>(control: E, state: Rc<RefCell<FeedbackState>>, track_hover: bool) -> E
where
E: StatefulInteractiveElement,
{
let press = state.clone();
let release = state.clone();
let outside = state.clone();
let hover = state.clone();
let keyboard = state.clone();
control
.on_mouse_down(MouseButton::Left, move |event, window, cx| {
let mut state = press.borrow_mut();
state.pressed = true;
state.keyboard_focus = None;
if track_hover {
state.hovered = true;
}
start_ripple(&mut state, Some(event.position), cx);
window.refresh();
})
.on_mouse_up(MouseButton::Left, move |_, window, cx| {
release_ripple(&mut release.borrow_mut(), cx);
window.refresh();
})
.on_mouse_up_out(MouseButton::Left, move |_, window, cx| {
release_ripple(&mut outside.borrow_mut(), cx);
window.refresh();
})
.on_hover(move |hovering, window, cx| {
let mut state = hover.borrow_mut();
let was_pressed = state.pressed;
if track_hover {
state.hovered = *hovering;
}
if !*hovering && (track_hover || was_pressed) {
release_ripple(&mut state, cx);
}
if track_hover || !*hovering && was_pressed {
window.refresh();
}
})
.capture_key_down(move |event, window, cx| {
if event.keystroke.key == "escape" {
let mut state = keyboard.borrow_mut();
state.cancel();
window.refresh();
} else if matches!(event.keystroke.key.as_str(), "space" | "enter") && !event.is_held {
let mut state = keyboard.borrow_mut();
state.pressed = true;
state.keyboard_focus = window.focused(cx);
start_ripple(&mut state, None, cx);
window.refresh();
}
})
.capture_key_up(move |event, window, cx| {
if matches!(event.keystroke.key.as_str(), "space" | "enter") {
release_ripple(&mut state.borrow_mut(), cx);
window.refresh();
}
})
}
pub(crate) fn ripple_state(
id: &ElementId,
enabled: bool,
window: &mut Window,
cx: &mut App,
) -> Rc<RefCell<FeedbackState>> {
let state = window
.use_keyed_state((id.clone(), "ripple"), cx, |_, _| {
Rc::new(RefCell::new(FeedbackState::default()))
})
.read(cx)
.clone();
state.borrow_mut().cancel_unavailable(enabled, window);
state
}
pub(crate) fn ripple_ink(
state: Rc<RefCell<FeedbackState>>,
tint: Hsla,
corners: CornerShape,
direction: gpui_material_theme::Direction,
) -> impl IntoElement {
let geometry = state.clone();
canvas(
move |bounds, _, _| {
geometry.borrow_mut().shape = Shape::rounded(bounds, corners, direction);
},
move |_, _, window, cx| {
let state = state.borrow();
if let (Some(shape), Some(ripple)) = (&state.shape, &state.ripple) {
ripple.paint(
shape,
tint,
cx.background_executor().now(),
cx.reduce_motion(),
window,
);
}
},
)
.absolute()
.inset_0()
.size_full()
}
pub(crate) fn icon_slot(icon: AnyElement, side: f32) -> impl IntoElement {
div()
.size(px(side))
.flex_none()
.flex()
.items_center()
.justify_center()
.text_size(px(side))
.line_height(px(side))
.child(icon)
}