use crate::{
components::avatar::AvatarColor,
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::font,
};
use gpui_kit::{
AnyElement, App, Div, ElementId, FocusHandle, Hsla, IntoElement, ParentElement, Rems,
SharedString, Styled, Window, div, px, relative,
};
pub(crate) const BODY_LINE: f32 = 1.28572;
pub(crate) fn rem(px_value: f32) -> Rems {
crate::components::form::rem(px_value)
}
pub(crate) fn sans<E: Styled>(el: E, size: f32, weight: u16) -> E {
el.font(font(FontFamilyKey::Sans, weight))
.text_size(rem(size))
.line_height(relative(BODY_LINE))
}
pub(crate) fn sized<E: Styled>(el: E, size: f32) -> E {
el.text_size(rem(size)).line_height(relative(BODY_LINE))
}
pub(crate) fn mono<E: Styled>(el: E, size: f32, weight: u16) -> E {
el.font(font(FontFamilyKey::Mono, weight))
.text_size(rem(size))
.line_height(relative(BODY_LINE))
}
pub(crate) fn text(s: impl Into<SharedString>, size: f32, role: ColorRole) -> Div {
sans(div(), size, 400)
.text_color(color(role))
.child(s.into())
}
pub(crate) fn focus_ring() -> AnyElement {
focus_ring_offset(0.)
}
pub(crate) fn focus_ring_offset(offset: f32) -> AnyElement {
div()
.absolute()
.top(px(-offset))
.left(px(-offset))
.right(px(-offset))
.bottom(px(-offset))
.border_2()
.border_color(color(ColorRole::Focus))
.into_any_element()
}
pub(crate) fn title_tooltip(
text: SharedString,
) -> impl Fn(&mut Window, &mut App) -> gpui_kit::AnyView {
move |window, cx| gpui_kit::component::tooltip::Tooltip::new(text.clone()).build(window, cx)
}
pub(crate) fn titled(
el: Div,
kind: &str,
text: impl Into<SharedString>,
) -> gpui_kit::Stateful<Div> {
use gpui_kit::{InteractiveElement as _, StatefulInteractiveElement as _};
let text: SharedString = text.into();
el.id(ElementId::Name(format!("t3-{kind}-{text}").into()))
.tooltip(title_tooltip(text))
}
pub(crate) fn sub(id: &ElementId, name: &str, i: usize) -> ElementId {
(id.clone(), SharedString::from(format!("{name}-{i}"))).into()
}
pub(crate) fn focus_handle(id: &ElementId, window: &mut Window, cx: &mut App) -> FocusHandle {
window
.use_keyed_state((id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone()
}
pub(crate) fn focus_visible(focus: &FocusHandle, window: &Window) -> bool {
focus.is_focused(window) && crate::focus::focus_visible_mode(window)
}
pub(crate) fn press(id: impl Into<ElementId>, focus: &FocusHandle) -> gpui_kit::base::Button {
gpui_kit::base::Button::new(id)
.track_focus(focus)
.relative()
.justify_start()
.line_height(relative(BODY_LINE))
.cursor_pointer()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AvatarTone {
Avatar(AvatarColor),
Plain,
}
impl AvatarTone {
pub fn colors(self, ink: Hsla) -> (Hsla, Hsla) {
match self {
Self::Avatar(c) => c.colors(),
Self::Plain => (Hsla::transparent_black(), ink),
}
}
}
pub(crate) fn initials(text: impl Into<SharedString>, tone: AvatarTone, diameter: f32) -> Div {
let (bg, fg) = tone.colors(color(ColorRole::TextPrimary));
sans(div(), 12., 600)
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(diameter))
.rounded_full()
.overflow_hidden()
.whitespace_nowrap()
.bg(bg)
.text_color(fg)
.child(text.into())
}
pub fn actor_tone(actor: &str) -> AvatarTone {
const TONES: [AvatarTone; 6] = [
AvatarTone::Avatar(AvatarColor::Purple),
AvatarTone::Plain, AvatarTone::Avatar(AvatarColor::Teal),
AvatarTone::Avatar(AvatarColor::Magenta),
AvatarTone::Avatar(AvatarColor::Blue),
AvatarTone::Plain, ];
let h = actor
.encode_utf16()
.fold(17u32, |h, c| h.wrapping_mul(33).wrapping_add(c as u32));
TONES[(h % 6) as usize]
}
pub fn two_letters(name: &str) -> String {
name.chars().take(2).collect::<String>().to_uppercase()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn actor_tones_hash_like_the_reference() {
assert_eq!(actor_tone("jd"), AvatarTone::Plain);
assert_eq!(actor_tone("ak"), AvatarTone::Plain);
assert_eq!(actor_tone("mr"), AvatarTone::Avatar(AvatarColor::Purple));
assert_eq!(actor_tone(""), AvatarTone::Plain);
}
#[test]
fn initials_take_two_upper_case_letters() {
assert_eq!(two_letters("jd"), "JD");
assert_eq!(two_letters("you"), "YO");
assert_eq!(two_letters("a"), "A");
}
}