use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use gpui::{Hsla, Pixels, Rems, Rgba, SharedString, Styled, hsla, px, rems, rgb, rgba};
use kuizer::config::{Accent, Density, Theme};
use kuizer::detect::Category;
use kuizer::units::ByteSize;
pub use kuizer::units::age;
pub const REM: f32 = 16.;
pub fn unit(value: f32) -> Rems {
rems(value / REM)
}
pub mod space {
pub const XXS: f32 = 2.;
pub const XS: f32 = 4.;
pub const S: f32 = 6.;
pub const M: f32 = 8.;
pub const L: f32 = 12.;
pub const XL: f32 = 16.;
pub const XXL: f32 = 24.;
pub const GUTTER: f32 = XL;
pub const INSET: f32 = M;
pub const SURFACE: f32 = M;
use super::compact;
pub fn row_y() -> f32 {
if compact() { XS } else { M }
}
pub fn bar_y() -> f32 {
if compact() { XXS } else { XS }
}
pub fn section() -> f32 {
if compact() { M } else { L }
}
}
pub mod font {
pub const CAPTION: f32 = 11.;
pub const SMALL: f32 = 12.;
pub const BODY: f32 = 13.;
pub const TITLE: f32 = 15.;
pub const HEADING: f32 = 18.;
pub const FIGURE: f32 = 24.;
pub const DISPLAY: f32 = 32.;
}
pub mod size {
pub const HAIR: f32 = 1.;
pub const MARKER: f32 = 2.;
pub const DOT: f32 = 6.;
pub const ICON: f32 = 16.;
pub const CHECK: f32 = 14.;
pub const GLYPH: f32 = 10.;
pub const KEY: f32 = 16.;
pub const BADGE: f32 = 18.;
pub const CHIP: f32 = 20.;
pub const SEGMENT: f32 = 24.;
pub const CONTROL: f32 = 28.;
pub const TILE: f32 = 30.;
pub const INPUT: f32 = 32.;
pub const STATUS: f32 = 32.;
pub const BAR: f32 = 36.;
pub const SIDEBAR: f32 = 220.;
pub const SIDEBAR_COLLAPSED: f32 = 48.;
pub const METER: f32 = 12.;
pub const TRACK: f32 = 4.;
pub const SLIVER: f32 = 2.;
pub const STRIP: f32 = 6.;
pub const GAUGE: f32 = 44.;
pub const MODE: f32 = 68.;
pub const SWATCH: f32 = 8.;
pub const LEGEND: f32 = 10.;
pub const CARET: f32 = 14.;
pub const COLUMNS: f32 = 88.;
pub const CHART: f32 = 120.;
pub const MAP: f32 = 200.;
pub const STAT: f32 = 140.;
pub const ACTIONS: f32 = 196.;
pub const LABEL: f32 = 240.;
pub const KEYS: f32 = 250.;
pub const FILTER: f32 = 300.;
pub const TRIGGER: f32 = 340.;
pub const LIST: f32 = 340.;
pub const MENU: f32 = 220.;
pub const DIALOG: f32 = 520.;
pub const SUMMARY: f32 = 540.;
pub const SUMMARY_HEIGHT: f32 = TILE + 2. * super::space::S + 2. * HAIR;
pub const PALETTE: f32 = 560.;
pub const PAGE: f32 = 760.;
pub const SHORTCUTS: f32 = 820.;
pub const SETTINGS: f32 = 840.;
pub const SETTINGS_HEIGHT: f32 = 560.;
pub const SETTINGS_CONTENT: f32 = 600.;
pub const CARD: f32 = 168.;
pub const PREVIEW: f32 = 72.;
pub const SETTINGS_NAV: f32 = 200.;
pub const SWITCH: f32 = 32.;
pub const SWITCH_HEIGHT: f32 = 18.;
pub const DROP: f32 = 72.;
use super::compact;
pub fn row() -> f32 {
if compact() { 24. } else { 28. }
}
pub fn row_tall() -> f32 {
if compact() { 32. } else { 40. }
}
pub fn toolbar() -> f32 {
if compact() { 32. } else { 40. }
}
}
pub mod col {
pub const XS: f32 = 56.;
pub const S: f32 = 72.;
pub const M: f32 = 96.;
pub const L: f32 = 112.;
pub const XL: f32 = 152.;
pub const XXL: f32 = 176.;
pub const WIDE: f32 = 240.;
}
pub const DISABLED: f32 = 0.4;
pub const DIM: f32 = 0.8;
static LIGHT: AtomicBool = AtomicBool::new(false);
static ACCENT: AtomicUsize = AtomicUsize::new(0);
static COMPACT: AtomicBool = AtomicBool::new(false);
pub fn set_density(density: Density) {
COMPACT.store(density == Density::Compact, Ordering::Relaxed);
}
fn compact() -> bool {
COMPACT.load(Ordering::Relaxed)
}
pub fn configure(theme: Theme, accent: Accent) {
LIGHT.store(theme == Theme::Light, Ordering::Relaxed);
let slot = Accent::ALL
.iter()
.position(|known| *known == accent)
.unwrap_or(0);
ACCENT.store(slot, Ordering::Relaxed);
}
struct Tint {
accent: (u32, u32),
focus: (u32, u32),
selected: (u32, u32),
tile: (u32, u32),
}
fn tint_of(accent: Accent) -> Tint {
match accent {
Accent::Purple => Tint {
accent: (0x007e_64fb, 0x006c_35f5),
focus: (0x0096_8dfa, 0x006c_35f5),
selected: (0x001a_1726, 0x00f5_f5fd),
tile: (0x0033_2d5d, 0x00dc_dcfa),
},
Accent::Blue => Tint {
accent: (0x003b_82f6, 0x0014_63e6),
focus: (0x005a_96ff, 0x0014_63e6),
selected: (0x0015_1a24, 0x00f0_f5ff),
tile: (0x002c_3a55, 0x00bc_d2f5),
},
Accent::Teal => Tint {
accent: (0x000f_9f92, 0x000a_8577),
focus: (0x0022_b8a9, 0x000a_8577),
selected: (0x0014_1e1e, 0x00ed_f9f7),
tile: (0x0027_474a, 0x00b3_e3dc),
},
Accent::Pink => Tint {
accent: (0x00e8_508f, 0x00d6_246e),
focus: (0x00f0_66a0, 0x00d6_246e),
selected: (0x0022_1720, 0x00ff_f1f6),
tile: (0x004f_2f40, 0x00f2_bdd2),
},
}
}
fn tint() -> Tint {
tint_of(Accent::ALL[ACCENT.load(Ordering::Relaxed)])
}
fn shade(theme: Theme, (dark, light_value): (u32, u32)) -> Rgba {
pick_in(theme, dark, light_value)
}
pub fn accent_of(accent: Accent) -> Rgba {
shade(current(), tint_of(accent).accent)
}
fn light() -> bool {
LIGHT.load(Ordering::Relaxed)
}
fn current() -> Theme {
if light() { Theme::Light } else { Theme::Dark }
}
fn pick_in(theme: Theme, dark: u32, light_value: u32) -> Rgba {
rgb(if theme == Theme::Light {
light_value
} else {
dark
})
}
fn pick(dark: u32, light_value: u32) -> Rgba {
pick_in(current(), dark, light_value)
}
pub struct Swatch {
pub background: Rgba,
pub panel: Rgba,
pub text: Rgba,
pub muted: Rgba,
pub accent: Rgba,
}
pub fn swatch(theme: Theme) -> Swatch {
Swatch {
background: background_in(theme),
panel: panel_in(theme),
text: text_in(theme),
muted: muted_in(theme),
accent: bar_in(theme),
}
}
fn pick_alpha(dark: u32, light_value: u32) -> Rgba {
rgba(if light() { light_value } else { dark })
}
fn background_in(theme: Theme) -> Rgba {
pick_in(theme, 0x000f_1011, 0x00ff_ffff)
}
pub fn background() -> Rgba {
background_in(current())
}
pub fn transparent() -> Rgba {
rgba(0x0000_0000)
}
pub fn shadow() -> Hsla {
hsla(0., 0., 0., if light() { 0.14 } else { 0.55 })
}
pub fn shadow_contact() -> Hsla {
hsla(0., 0., 0., if light() { 0.1 } else { 0.5 })
}
pub fn scrim() -> Rgba {
pick_alpha(0x0000_00a0, 0x1111_1466)
}
fn panel_in(theme: Theme) -> Rgba {
pick_in(theme, 0x0018_191b, 0x00fa_fafa)
}
pub fn panel() -> Rgba {
panel_in(current())
}
pub fn elevated() -> Rgba {
pick(0x001d_1e21, 0x00ff_ffff)
}
pub fn hover() -> Rgba {
pick(0x0025_262a, 0x00f0_f0f0)
}
pub fn hover_row() -> Rgba {
pick_alpha(0xffff_ff09, 0x0000_0008)
}
pub fn kbd() -> Rgba {
pick_alpha(0xffff_ff12, 0x0000_000f)
}
pub fn selected() -> Rgba {
shade(current(), tint().selected)
}
pub fn focus() -> Rgba {
shade(current(), tint().focus)
}
pub fn cursor() -> Rgba {
pick_alpha(0xffff_ff14, 0x0000_0012)
}
pub fn border() -> Rgba {
pick(0x0026_272b, 0x00e3_e3e3)
}
pub fn border_strong() -> Rgba {
pick(0x003a_3c42, 0x00c7_c7c7)
}
fn text_in(theme: Theme) -> Rgba {
pick_in(theme, 0x00f7_f8f8, 0x000d_0d0d)
}
pub fn text() -> Rgba {
text_in(current())
}
fn muted_in(theme: Theme) -> Rgba {
pick_in(theme, 0x00a3_a7ae, 0x004d_4d4d)
}
pub fn muted() -> Rgba {
muted_in(current())
}
pub fn faint() -> Rgba {
pick(0x006b_6f76, 0x008f_8f8f)
}
fn bar_in(theme: Theme) -> Rgba {
shade(theme, tint().accent)
}
pub fn bar() -> Rgba {
bar_in(current())
}
pub fn on_accent() -> Rgba {
rgb(0x00ff_ffff)
}
pub fn ok() -> Rgba {
pick(0x003f_cf8e, 0x0015_9a5c)
}
pub fn bar_strong() -> Rgba {
pick(0x00c4_c7cc, 0x004d_4d4d)
}
pub fn bar_mid() -> Rgba {
pick(0x0072_767d, 0x008f_8f8f)
}
pub fn bar_soft() -> Rgba {
pick(0x005e_6168, 0x00b0_b0b0)
}
pub fn bar_rest() -> Rgba {
pick(0x0035_373c, 0x00d4_d4d4)
}
pub fn track() -> Rgba {
pick(0x0023_2428, 0x00ec_ecec)
}
pub fn review() -> Rgba {
pick_alpha(0xffd2_36e6, 0x9a63_08f2)
}
pub fn review_soft() -> Rgba {
pick_alpha(0xc2a3_6e24, 0x9a63_081a)
}
pub fn danger() -> Rgba {
pick_alpha(0xff66_7fbf, 0xc826_46e6)
}
pub fn danger_solid() -> Rgba {
pick(0x00ff_667f, 0x00c8_2646)
}
pub fn danger_hover() -> Rgba {
pick(0x00ff_8fa1, 0x00a8_1e3a)
}
pub fn on_danger() -> Rgba {
pick(0x000d_0d0d, 0x00ff_ffff)
}
pub fn kbd_on_danger() -> Rgba {
rgba(0x0d0d_0d29)
}
pub fn primary() -> Rgba {
pick(0x00ff_ffff, 0x000d_0d0d)
}
pub fn on_primary() -> Rgba {
pick(0x000d_0d0d, 0x00ff_ffff)
}
pub fn tile_folder() -> Rgba {
pick(0x001b_1b1b, 0x00f0_f0f0)
}
pub fn tile_rest() -> Rgba {
pick(0x0017_1717, 0x00f4_f4f4)
}
pub fn tile(category: Category) -> Rgba {
match category {
Category::Dependencies => pick(0x002a_2a2a, 0x00d3_d3d3),
Category::Build => pick(0x0027_2727, 0x00d9_d9d9),
Category::Cache => pick(0x0024_2424, 0x00df_dfdf),
Category::Temp => pick(0x0021_2121, 0x00e5_e5e5),
Category::Logs => pick(0x0020_2020, 0x00e8_e8e8),
Category::Trash => pick(0x001e_1e1e, 0x00eb_ebeb),
Category::Files => pick(0x001c_1c1c, 0x00ee_eeee),
}
}
pub fn tile_edge(category: Category) -> Rgba {
match category {
Category::Dependencies => pick(0x006e_6e6e, 0x008a_8a8a),
Category::Build => pick(0x0056_5656, 0x009f_9f9f),
Category::Cache => pick(0x0044_4444, 0x00b1_b1b1),
Category::Temp => pick(0x0036_3636, 0x00c2_c2c2),
Category::Logs => pick(0x0030_3030, 0x00ca_caca),
Category::Trash => pick(0x002a_2a2a, 0x00d2_d2d2),
Category::Files => pick(0x0026_2626, 0x00da_dada),
}
}
pub fn tile_selected() -> Rgba {
shade(current(), tint().tile)
}
pub fn radius_button() -> Pixels {
px(4.)
}
pub fn radius_control() -> Pixels {
px(4.)
}
pub fn radius_panel() -> Pixels {
px(4.)
}
pub fn radius_dialog() -> Pixels {
px(6.)
}
pub fn radius_surface() -> Pixels {
px(8.)
}
pub fn radius_tag() -> Pixels {
px(2.)
}
pub fn radius_checkbox() -> Pixels {
px(2.)
}
pub fn radius_dot() -> Pixels {
px(1.)
}
pub fn radius_bar() -> Pixels {
px(1.)
}
pub fn radius_pill() -> Pixels {
px(999.)
}
pub const FONT_CANDIDATES: [&str; 7] = [
"Schibsted Grotesk",
"PP Neue Montreal",
"Geist",
"Inter",
"IBM Plex Sans",
"Zed Plex Sans",
"Noto Sans",
];
pub const MONO_CANDIDATES: [&str; 7] = [
"IBM Plex Mono",
"PP Neue Montreal Mono",
"Geist Mono",
"JetBrains Mono",
"Noto Sans Mono",
"DejaVu Sans Mono",
"Menlo",
];
static MONO: OnceLock<SharedString> = OnceLock::new();
pub fn set_mono(font: SharedString) {
let _ = MONO.set(font);
}
pub fn label<E: Styled>(element: E) -> E {
mono(element.text_size(unit(font::CAPTION)))
}
pub fn mono_family() -> Option<SharedString> {
MONO.get().cloned()
}
pub fn mono<E: Styled>(element: E) -> E {
match MONO.get() {
Some(font) => element.font_family(font.clone()),
None => element,
}
}
pub fn bytes(value: u64) -> String {
ByteSize(value).to_string()
}
#[allow(
clippy::cast_precision_loss,
reason = "bar widths only need a few significant digits"
)]
pub fn share(part: u64, whole: u64) -> f32 {
if whole == 0 {
return 0.0;
}
(part as f64 / whole as f64).clamp(0.0, 1.0) as f32
}