mod control;
mod elevation;
mod passive;
use std::rc::Rc;
use gpui::{
AnyElement, App, Bounds, ElementId, Entity, FocusHandle, Hsla, IntoElement, ParentElement,
Pixels, RenderOnce, SharedString, StyleRefinement, Styled, Window, px,
};
use gpui_material_theme::{ColorRole, CornerShape, Direction, Elevation, MaterialTheme};
use crate::{InteractionSource, Shape, ShapeFit, ShapePolygon, current_material_theme};
pub use elevation::{
color, elevation_shadow_pair, elevation_shadows, resolve_color_spec, resolve_edges, scrim_color,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SurfaceBorder {
pub width: Pixels,
pub color: Hsla,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SurfaceFeedback {
pub state_layer: bool,
pub ripple: bool,
pub focus_ring: bool,
pub ink: Option<Hsla>,
pub focus_color: Option<Hsla>,
}
impl Default for SurfaceFeedback {
fn default() -> Self {
Self {
state_layer: true,
ripple: true,
focus_ring: true,
ink: None,
focus_color: None,
}
}
}
#[derive(Clone, Copy)]
enum SurfaceAction {
Activate,
Select(bool),
Check(bool),
}
type Activate = Rc<dyn Fn(&mut Window, &mut App)>;
struct Control {
id: ElementId,
label: SharedString,
action: SurfaceAction,
activate: Option<Activate>,
}
#[derive(IntoElement)]
pub struct Surface {
style: StyleRefinement,
children: Vec<AnyElement>,
role: ColorRole,
container_color: Option<Hsla>,
content_color: Option<Hsla>,
inherit_content_color: bool,
shape: Option<CornerShape>,
polygon: Option<ShapePolygon>,
shape_fit: ShapeFit,
clip_backdrop: Option<Hsla>,
shape_hit_test: Option<bool>,
border: Option<SurfaceBorder>,
tonal_elevation: Elevation,
shadow_elevation: Elevation,
shadow_scale: f32,
theme: Option<Rc<MaterialTheme>>,
control: Option<Control>,
disabled: bool,
focus: Option<FocusHandle>,
feedback: SurfaceFeedback,
interaction_source: Option<Entity<InteractionSource>>,
}
impl Surface {
pub fn new(role: ColorRole) -> Self {
Self {
style: StyleRefinement::default(),
children: Vec::new(),
role,
container_color: None,
content_color: None,
inherit_content_color: false,
shape: None,
polygon: None,
shape_fit: ShapeFit::Contain,
clip_backdrop: None,
shape_hit_test: None,
border: None,
tonal_elevation: Elevation::Level0,
shadow_elevation: Elevation::Level0,
shadow_scale: 1.,
theme: None,
control: None,
disabled: false,
focus: None,
feedback: SurfaceFeedback::default(),
interaction_source: None,
}
}
pub fn clickable(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
role: ColorRole,
) -> Self {
Self::interactive(id, label, role, SurfaceAction::Activate)
}
pub fn selectable(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
role: ColorRole,
selected: bool,
) -> Self {
Self::interactive(id, label, role, SurfaceAction::Select(selected))
}
pub fn toggleable(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
role: ColorRole,
checked: bool,
) -> Self {
Self::interactive(id, label, role, SurfaceAction::Check(checked))
}
fn interactive(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
role: ColorRole,
action: SurfaceAction,
) -> Self {
Self {
control: Some(Control {
id: id.into(),
label: label.into(),
action,
activate: None,
}),
..Self::new(role)
}
}
pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
if let Some(control) = &mut self.control {
control.activate = Some(Rc::new(handler));
}
self
}
pub fn on_checked_change(
mut self,
handler: impl Fn(bool, &mut Window, &mut App) + 'static,
) -> Self {
if let Some(control) = &mut self.control
&& let SurfaceAction::Check(checked) = control.action
{
control.activate = Some(Rc::new(move |window, cx| handler(!checked, window, cx)));
}
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
self.focus = Some(focus.clone());
self
}
pub fn feedback(mut self, feedback: SurfaceFeedback) -> Self {
self.feedback = feedback;
self
}
pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
self.interaction_source = Some(source.clone());
self
}
pub fn container_color(mut self, color: Hsla) -> Self {
self.container_color = Some(color);
self
}
pub fn content_color(mut self, color: Hsla) -> Self {
self.content_color = Some(color);
self
}
pub fn inherit_content_color(mut self, inherit: bool) -> Self {
self.inherit_content_color = inherit;
self
}
pub fn shape(mut self, shape: CornerShape) -> Self {
self.shape = Some(shape);
self.polygon = None;
self
}
pub fn polygon_shape(mut self, polygon: impl Into<ShapePolygon>) -> Self {
self.polygon = Some(polygon.into());
self.shape = None;
self
}
pub fn shape_fit(mut self, fit: ShapeFit) -> Self {
self.shape_fit = fit;
self
}
pub fn shape_clip_backdrop(mut self, backdrop: Hsla) -> Self {
self.clip_backdrop = Some(backdrop);
self
}
pub fn shape_hit_test(mut self, enabled: bool) -> Self {
self.shape_hit_test = Some(enabled);
self
}
pub fn border(mut self, border: SurfaceBorder) -> Self {
self.border = Some(border);
self
}
pub fn tonal_elevation(mut self, elevation: Elevation) -> Self {
self.tonal_elevation = elevation;
self
}
pub fn shadow_elevation(mut self, elevation: Elevation) -> Self {
self.shadow_elevation = elevation;
self
}
pub fn shadow_scale(mut self, scale: f32) -> Self {
self.shadow_scale = scale;
self
}
pub fn theme(mut self, theme: MaterialTheme) -> Self {
self.theme = Some(Rc::new(theme));
self
}
}
impl ParentElement for Surface {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl Styled for Surface {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
#[derive(Clone)]
pub(super) enum ResolvedShape {
Corners(CornerShape, Direction),
Polygon(ShapePolygon, ShapeFit),
}
impl ResolvedShape {
pub(super) fn build(&self, bounds: Bounds<Pixels>) -> Option<Shape> {
match self {
Self::Corners(shape, direction) => Shape::rounded(bounds, *shape, *direction),
Self::Polygon(polygon, fit) => Shape::polygon(bounds, polygon, *fit),
}
}
}
pub(super) struct PolygonPaint {
pub(super) background: Hsla,
pub(super) shadow_elevation: f32,
pub(super) shadow_scale: f32,
pub(super) shadow_color: Hsla,
pub(super) border: Option<(Pixels, Hsla)>,
}
impl RenderOnce for Surface {
fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let current;
let theme = match &self.theme {
Some(theme) => theme.as_ref(),
None => {
current = current_material_theme(window, cx);
&*current
}
};
let scheme = &theme.colors;
let default_focus = color(scheme.color(ColorRole::Secondary));
let shadow_color = color(scheme.color(ColorRole::Shadow));
let background = if self.role == ColorRole::Surface {
scheme.surface_at_elevation(self.tonal_elevation)
} else {
scheme.color(self.role)
};
let direction = theme.layout.direction;
let background = self.container_color.unwrap_or_else(|| color(background));
let foreground = self.content_color.or_else(|| {
if self.inherit_content_color || self.container_color.is_some() {
return None;
}
scheme.pair(self.role).map(|pair| color(pair.foreground))
});
if let Some(foreground) = foreground {
self.style.text.color = Some(foreground);
}
let border = self.border.map(|border| {
let border_width = if f32::from(border.width).is_finite() {
border.width.max(px(0.))
} else {
px(0.)
};
(border_width, border.color)
});
let polygon_paint = if self.polygon.is_some() {
Some(PolygonPaint {
background,
shadow_elevation: self.shadow_elevation.dp(),
shadow_scale: self.shadow_scale,
shadow_color,
border: border.filter(|(width, _)| *width > px(0.)),
})
} else {
self.style.background = Some(background.into());
let shadows =
elevation_shadows(self.shadow_elevation.dp(), self.shadow_scale, shadow_color);
if !shadows.is_empty() {
self.style.box_shadow = Some(shadows);
}
if let Some(shape) = self.shape {
if shape == CornerShape::Full {
self = self.rounded_full();
} else {
let [tl, tr, br, bl] = shape.radii(f32::MAX, f32::MAX, direction);
self = self
.rounded_tl(px(tl))
.rounded_tr(px(tr))
.rounded_br(px(br))
.rounded_bl(px(bl));
}
}
if let Some((border_width, border_color)) = border {
let width = Some(border_width.into());
self.style.border_widths.top = width;
self.style.border_widths.right = width;
self.style.border_widths.bottom = width;
self.style.border_widths.left = width;
self.style.border_color = Some(if border_width > px(0.) {
border_color
} else {
gpui::transparent_black()
});
}
None
};
let resolved_shape = self.resolve_shape(direction, window);
let Some(control) = self.control.take() else {
return self.render_passive(resolved_shape, polygon_paint);
};
self.render_control(
control,
resolved_shape,
polygon_paint,
default_focus,
window,
cx,
)
}
}
impl Surface {
fn resolve_shape(&self, direction: Direction, window: &Window) -> ResolvedShape {
if let Some(polygon) = &self.polygon {
ResolvedShape::Polygon(polygon.clone(), self.shape_fit)
} else if let Some(shape) = self.shape {
ResolvedShape::Corners(shape, direction)
} else {
let radii = &self.style.corner_radii;
let radius = |value: Option<gpui::AbsoluteLength>| {
value.map_or(0., |value| value.to_pixels(window.rem_size()).into())
};
ResolvedShape::Corners(
CornerShape::Radii([
radius(radii.top_left),
radius(radii.top_right),
radius(radii.bottom_right),
radius(radii.bottom_left),
]),
Direction::Ltr,
)
}
}
}