use crate::tokens::{
DENSITY_FIELD_MD, DENSITY_FIELD_SM, DENSITY_ROW_LG, DENSITY_ROW_MD, DENSITY_ROW_SM, SIZE_LG,
SIZE_MD, TokenLength,
};
use gpui_kit::{App, Global};
use std::cell::Cell;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Density {
#[default]
Compact,
Comfortable,
}
impl Density {
pub const ALL: [Density; 2] = [Self::Compact, Self::Comfortable];
pub const fn id(self) -> &'static str {
match self {
Self::Compact => "compact",
Self::Comfortable => "comfortable",
}
}
pub const fn row(self) -> TokenLength {
match self {
Self::Compact => DENSITY_ROW_MD,
Self::Comfortable => SIZE_LG,
}
}
pub const fn row_sm(self) -> TokenLength {
match self {
Self::Compact => DENSITY_ROW_SM,
Self::Comfortable => SIZE_LG,
}
}
pub const fn row_lg(self) -> TokenLength {
match self {
Self::Compact => DENSITY_ROW_LG,
Self::Comfortable => SIZE_LG,
}
}
pub const fn field(self) -> TokenLength {
match self {
Self::Compact => DENSITY_FIELD_MD,
Self::Comfortable => SIZE_MD,
}
}
pub const fn field_sm(self) -> TokenLength {
match self {
Self::Compact => DENSITY_FIELD_SM,
Self::Comfortable => SIZE_MD,
}
}
}
thread_local! {
static CURRENT: Cell<Density> = const { Cell::new(Density::Compact) };
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ActiveDensity(pub Density);
impl Global for ActiveDensity {}
pub fn current() -> Density {
CURRENT.get()
}
pub const fn palette_input() -> TokenLength {
crate::tokens::PALETTE_INPUT_HEIGHT
}
pub fn zone(
density: Density,
build: impl FnOnce(&mut gpui_kit::Window, &mut App) -> gpui_kit::AnyElement + 'static,
) -> DensityZone {
DensityZone {
density,
build: Some(Box::new(build)),
child: None,
}
}
type ZoneBuild = Box<dyn FnOnce(&mut gpui_kit::Window, &mut App) -> gpui_kit::AnyElement>;
pub struct DensityZone {
density: Density,
build: Option<ZoneBuild>,
child: Option<gpui_kit::AnyElement>,
}
impl DensityZone {
fn scoped<R>(density: Density, f: impl FnOnce() -> R) -> R {
let previous = CURRENT.replace(density);
let result = f();
CURRENT.set(previous);
result
}
}
impl gpui_kit::Element for DensityZone {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<gpui_kit::ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&gpui_kit::GlobalElementId>,
_: Option<&gpui_kit::InspectorElementId>,
window: &mut gpui_kit::Window,
cx: &mut App,
) -> (gpui_kit::LayoutId, ()) {
let build = self.build.take();
let child = &mut self.child;
let id = Self::scoped(self.density, || {
let mut element = match build {
Some(build) => build(window, cx),
None => gpui_kit::IntoElement::into_any_element(gpui_kit::Empty),
};
let id = element.request_layout(window, cx);
*child = Some(element);
id
});
(id, ())
}
fn prepaint(
&mut self,
_: Option<&gpui_kit::GlobalElementId>,
_: Option<&gpui_kit::InspectorElementId>,
_: gpui_kit::Bounds<gpui_kit::Pixels>,
_: &mut (),
window: &mut gpui_kit::Window,
cx: &mut App,
) {
if let Some(child) = &mut self.child {
Self::scoped(self.density, || child.prepaint(window, cx));
}
}
fn paint(
&mut self,
_: Option<&gpui_kit::GlobalElementId>,
_: Option<&gpui_kit::InspectorElementId>,
_: gpui_kit::Bounds<gpui_kit::Pixels>,
_: &mut (),
_: &mut (),
window: &mut gpui_kit::Window,
cx: &mut App,
) {
if let Some(child) = &mut self.child {
Self::scoped(self.density, || child.paint(window, cx));
}
}
}
pub fn set_density(cx: &mut App, density: Density) {
CURRENT.set(density);
cx.set_global(ActiveDensity(density));
cx.refresh_windows();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn heights_follow_agents_md() {
let c = Density::Compact;
assert_eq!(
[c.row_sm(), c.row(), c.row_lg()].map(|l| l.px_value()),
[28., 32., 36.]
);
assert_eq!([c.field_sm(), c.field()].map(|l| l.px_value()), [28., 32.]);
let w = Density::Comfortable;
assert_eq!((w.field().px_value(), w.row().px_value()), (40., 48.));
assert_eq!(palette_input().px_value(), 44.);
assert_eq!(Density::default(), Density::Compact);
}
}