Apply the logo palette and Unbounded headings across the app (#82)
CI / core (pull_request) Successful in 1m33s
CI / core (pull_request) Successful in 1m33s
This commit is contained in:
1 parent
48729f83ad
commit
bfb29e02fe
10 files changed
+394
-154
No files matched your search
@@ -0,0 +1,204 @@
|
||||
//! Arena Tracker's two palettes and the fonts registered once at startup.
|
||||
|
||||
use std::{borrow::Cow, rc::Rc};
|
||||
|
||||
use gpui_kit::component::{ActiveTheme as _, Theme};
|
||||
use gpui_kit::*;
|
||||
|
||||
/// DirectWrite exposes Inter's text optical size as its own family.
|
||||
const INTER: &str = "Inter Variable Text";
|
||||
const UNBOUNDED: &str = "Unbounded";
|
||||
|
||||
struct HeadingFamily(SharedString);
|
||||
impl Global for HeadingFamily {}
|
||||
|
||||
pub fn heading(cx: &App) -> Div {
|
||||
let family = cx
|
||||
.try_global::<HeadingFamily>()
|
||||
.map_or_else(|| &cx.theme().font_family, |heading| &heading.0);
|
||||
div()
|
||||
.font_family(family.clone())
|
||||
.font_weight(FontWeight::SEMIBOLD)
|
||||
}
|
||||
|
||||
fn load_font(cx: &App, bytes: &'static [u8], family: &'static str, weight: FontWeight) -> bool {
|
||||
let text = cx.text_system();
|
||||
if let Err(error) = text.add_fonts(vec![Cow::Borrowed(bytes)]) {
|
||||
eprintln!("Failed to load {family}: {error}");
|
||||
return false;
|
||||
}
|
||||
// Naming a missing family incurs a fallback lookup on every text run.
|
||||
let mut requested = font(family);
|
||||
requested.weight = weight;
|
||||
text.get_font_for_id(text.resolve_font(&requested)) == Some(requested)
|
||||
}
|
||||
|
||||
pub fn init_theme(cx: &mut App) {
|
||||
let inter = load_font(
|
||||
cx,
|
||||
include_bytes!("../assets/InterVariable.ttf"),
|
||||
INTER,
|
||||
FontWeight::NORMAL,
|
||||
);
|
||||
let heading = if load_font(
|
||||
cx,
|
||||
include_bytes!("../assets/Unbounded-SemiBold.ttf"),
|
||||
UNBOUNDED,
|
||||
FontWeight::SEMIBOLD,
|
||||
) {
|
||||
UNBOUNDED.into()
|
||||
} else {
|
||||
cx.theme().font_family.clone()
|
||||
};
|
||||
cx.set_global(HeadingFamily(heading));
|
||||
|
||||
let theme = Theme::global_mut(cx);
|
||||
for config in [&mut theme.light_theme, &mut theme.dark_theme] {
|
||||
let config = Rc::make_mut(config);
|
||||
if inter {
|
||||
config.font_family = Some(INTER.into());
|
||||
}
|
||||
// Several kit tokens share each palette role. Register them on the saved configs
|
||||
// so changing appearance reapplies the same palette as startup.
|
||||
macro_rules! role {
|
||||
($dark:literal, $light:literal => $($token:ident),+ ) => {{
|
||||
let value = if config.mode.is_dark() { $dark } else { $light };
|
||||
$(config.colors.$token = Some(value.into());)+
|
||||
}};
|
||||
}
|
||||
role!("#11152a", "#eef0f7" => background);
|
||||
role!("#151a33", "#e5e8f3" => title_bar, table_head, list_head);
|
||||
role!("#181e38", "#ffffff" => table, list, popover, muted, button, list_even, table_even);
|
||||
role!("#20274a", "#e4e8f4" => secondary, secondary_hover, accent, button_hover, table_hover,
|
||||
list_hover, tab_bar, tab_bar_segmented);
|
||||
role!("#283053", "#d2d7e9" => border, input, title_bar_border, window_border, table_row_border,
|
||||
chart_grid, secondary_active, button_active);
|
||||
role!("#edeaf7", "#161b2e" => foreground, popover_foreground, accent_foreground, secondary_foreground);
|
||||
role!("#8b91b1", "#596082" => muted_foreground, tab_foreground, table_head_foreground, scrollbar_thumb_hover);
|
||||
role!("#3a4166", "#c5cbe0" => switch, scrollbar_thumb);
|
||||
role!("#f5b83d", "#e3a21a" => primary, tab_active, chart_2, ring, progress_bar, list_active_border, table_active_border);
|
||||
role!("#f5b83d", "#8f5d00" => link, link_hover, link_active, caret, warning);
|
||||
role!("#ff7a7a", "#c62f2f" => danger);
|
||||
role!("#67e8f9", "#11667b" => info);
|
||||
role!("#4ade80", "#196638" => success);
|
||||
role!("#161b2e", "#161b2e" => primary_foreground, tab_active_foreground);
|
||||
role!("#f5b83d38", "#e3a21a38" => selection, list_active, table_active);
|
||||
// Let the kit derive pressed and hovered primary buttons from our gold.
|
||||
config.colors.primary_active = None;
|
||||
config.colors.primary_hover = None;
|
||||
}
|
||||
Theme::change(theme.mode, None, cx);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::INTER;
|
||||
use gpui_kit::component::{Theme, ThemeMode};
|
||||
use gpui_kit::{FontWeight, TestAppContext, font, px};
|
||||
|
||||
// Not #[gpui::test]: its text system is a no-op, and this checks DirectWrite's real one.
|
||||
#[test]
|
||||
fn identity_fonts_and_colors_are_used_in_both_themes() {
|
||||
let platform = gpui_kit::platform::current_platform(false);
|
||||
let cx = TestAppContext::build_with_text_system(
|
||||
gpui::TestDispatcher::new(0),
|
||||
None,
|
||||
platform.text_system(),
|
||||
);
|
||||
cx.update(|cx| {
|
||||
gpui_kit::init(cx);
|
||||
super::init_theme(cx);
|
||||
assert_eq!(
|
||||
Theme::global(cx).background,
|
||||
if Theme::global(cx).mode.is_dark() {
|
||||
gpui::rgb(0x11152a)
|
||||
} else {
|
||||
gpui::rgb(0xeef0f7)
|
||||
}
|
||||
.into()
|
||||
);
|
||||
for (mode, palette) in [
|
||||
(
|
||||
ThemeMode::Light,
|
||||
[
|
||||
0xeef0f7, 0xe5e8f3, 0xffffff, 0xe4e8f4, 0xd2d7e9, 0x161b2e, 0x596082,
|
||||
0xc5cbe0, 0xe3a21a, 0x8f5d00,
|
||||
],
|
||||
),
|
||||
(
|
||||
ThemeMode::Dark,
|
||||
[
|
||||
0x11152a, 0x151a33, 0x181e38, 0x20274a, 0x283053, 0xedeaf7, 0x8b91b1,
|
||||
0x3a4166, 0xf5b83d, 0xf5b83d,
|
||||
],
|
||||
),
|
||||
] {
|
||||
Theme::change(mode, None, cx);
|
||||
let theme = Theme::global(cx);
|
||||
assert_eq!(theme.font_family.as_str(), INTER);
|
||||
let family = &cx.global::<super::HeadingFamily>().0;
|
||||
assert_eq!(family.as_str(), "Unbounded");
|
||||
let mut heading = font(family.clone());
|
||||
heading.weight = FontWeight::SEMIBOLD;
|
||||
let text = cx.text_system();
|
||||
assert_eq!(
|
||||
text.get_font_for_id(text.resolve_font(&heading)),
|
||||
Some(heading)
|
||||
);
|
||||
assert_eq!(
|
||||
[
|
||||
theme.background,
|
||||
theme.title_bar,
|
||||
theme.table,
|
||||
theme.secondary,
|
||||
theme.border,
|
||||
theme.foreground,
|
||||
theme.muted_foreground,
|
||||
theme.switch,
|
||||
theme.primary,
|
||||
theme.link
|
||||
],
|
||||
palette.map(|color| gpui::rgb(color).into())
|
||||
);
|
||||
for accent in [
|
||||
theme.tab_active,
|
||||
theme.ring,
|
||||
theme.chart_2,
|
||||
theme.progress_bar,
|
||||
] {
|
||||
assert_eq!(accent, theme.primary);
|
||||
}
|
||||
assert_eq!(theme.tab_active_foreground, gpui::rgb(0x161b2e).into());
|
||||
for ink in [theme.warning, theme.danger, theme.info, theme.success] {
|
||||
// Alert paints its text over a 4% tint of the same colour.
|
||||
let background = theme.background.blend(ink.opacity(0.04));
|
||||
let [a, b] = [ink, background].map(|color| {
|
||||
let rgb = color.to_rgb();
|
||||
let [r, g, b] = [rgb.r, rgb.g, rgb.b].map(|v| {
|
||||
if v <= 0.04045 {
|
||||
v / 12.92
|
||||
} else {
|
||||
((v + 0.055) / 1.055).powf(2.4)
|
||||
}
|
||||
});
|
||||
0.2126 * r + 0.7152 * g + 0.0722 * b
|
||||
});
|
||||
assert!(
|
||||
(a.max(b) + 0.05) / (a.min(b) + 0.05) >= 4.5,
|
||||
"{mode:?} alert {ink:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
// DirectWrite returns the nearest weight, so only a wider glyph proves a semibold face.
|
||||
let text = cx.text_system();
|
||||
let [regular, semibold] = [FontWeight::NORMAL, FontWeight::SEMIBOLD].map(|weight| {
|
||||
let mut requested = font(INTER);
|
||||
requested.weight = weight;
|
||||
let id = text.resolve_font(&requested);
|
||||
assert_eq!(text.get_font_for_id(id), Some(requested));
|
||||
text.advance(id, px(14.), 'm').unwrap().width
|
||||
});
|
||||
assert!(semibold > regular, "{semibold:?} <= {regular:?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user