use crate::{
icons::{CarbonIcon, Icon},
motion,
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::font,
u0::primitives::tree::CHEVRON_MOTION,
};
use gpui_kit::{App, ElementId, Entity, Hsla, Styled, Window};
pub(crate) use crate::t3::press::{focus, hover, rem, ring, track_hover};
pub fn head_ink(hovered: bool) -> Hsla {
color(if hovered {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
})
}
pub(crate) fn mono<E: Styled>(el: E) -> E {
el.font(font(FontFamilyKey::Mono, 400)).text_size(rem(12.))
}
pub(crate) fn open_state(
id: impl Into<ElementId>,
default_open: bool,
window: &mut Window,
cx: &mut App,
) -> Entity<bool> {
window.use_keyed_state(id, cx, move |_, _| default_open)
}
pub(crate) fn toggle(state: &Entity<bool>, cx: &mut App) {
state.update(cx, |open, cx| {
*open = !*open;
cx.notify();
});
}
pub(crate) fn chevron_right(
id: impl Into<ElementId>,
open: bool,
size: f32,
window: &mut Window,
cx: &mut App,
) -> Icon {
let id: ElementId = id.into();
let angle = motion::animate(
(id, "chev"),
if open {
std::f32::consts::FRAC_PI_2
} else {
0.
},
CHEVRON_MOTION,
window,
cx,
);
Icon::carbon(CarbonIcon::ChevronRight)
.size_length(rem(size))
.color(color(ColorRole::IconSecondary))
.rotate(gpui_kit::radians(angle))
}
pub(crate) fn chevron_down(open: bool, size: f32, ink: Hsla) -> Icon {
Icon::carbon(CarbonIcon::ChevronDown)
.size_length(rem(size))
.color(ink)
.rotate(gpui_kit::radians(chevron_down_angle(open)))
}
pub fn chevron_down_angle(open: bool) -> f32 {
if open {
0.
} else {
-std::f32::consts::FRAC_PI_2
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn heads_brighten_on_hover_and_chevrons_turn() {
crate::theme::tests_set(CarbonTheme::G100);
assert_eq!(head_ink(false), color(ColorRole::TextSecondary));
assert_eq!(head_ink(true), color(ColorRole::TextPrimary));
assert_eq!(chevron_down_angle(true), 0.);
assert_eq!(chevron_down_angle(false), -std::f32::consts::FRAC_PI_2);
}
}