Merge pull request #126 from jamerrq/follow-omarchy-font

This commit is contained in:
Tobi Lutke
2026-11-25 09:52:27 -05:00
9 changed files with 270 additions and 16 deletions
Generated
+2
View File
@@ -1423,10 +1423,12 @@ name = "disktree-app"
version = "0.11.0"
dependencies = [
"anyhow",
"async-channel",
"disktree-core",
"dunce",
"gpui-kit",
"gpui-omarchy",
"notify",
"rustc-hash 2.1.3",
"tempfile",
"windows-sys 0.61.2",
+1 -1
View File
@@ -14,7 +14,7 @@ path always asks first.
Built with [GPUI](https://gpui-kit.com/) through
[gpui-omarchy](https://github.com/huacnlee/gpui-omarchy), so it follows your
Omarchy theme and behaves like the rest of the desktop.
Omarchy theme and font, and behaves like the rest of the desktop.
## Install
+2
View File
@@ -18,7 +18,9 @@ gpui-kit = { version = "=0.6.6", default-features = false, features = ["assets"]
gpui-omarchy = "0.1.3"
disktree-core = { workspace = true }
anyhow = { workspace = true }
async-channel = "2"
dunce = "1.0.5"
notify = "7"
rustc-hash = { workspace = true }
tempfile = "3"
+227 -9
View File
@@ -1,26 +1,219 @@
//! Light or dark when there is no Omarchy theme to follow.
//! Light or dark when there is no Omarchy theme to follow, and the face to
//! draw with when there is.
//!
//! On Omarchy, gpui-omarchy follows the desktop's theme files. Elsewhere there
//! are none, and gpui-omarchy would then stay on its dark default whatever
//! the system is set to; there the app follows the system appearance instead,
//! with the crate's own dark and light palettes, and switches when it does.
//!
//! The face splits the same way. Omarchy has one font setting — the monospace
//! family `omarchy font set` writes to fontconfig — and its shell draws
//! everything in it. gpui-omarchy exposes that family as `mono_font`; `font`
//! is GPUI's system UI face, which no Omarchy setting reaches. On Omarchy the
//! app therefore draws with `mono_font`, so the desktop's font choice reaches
//! it too; elsewhere it keeps the platform's UI face.
//!
//! Two things need that face: the text this app lays out itself, which reads
//! it through [`font`], and the controls gpui-omarchy draws — buttons,
//! checkboxes, text fields, dialogs, tooltips — which style themselves from
//! `Theme::font` and so need [`follow_desktop_face`] to rewrite it. Both
//! follow fontconfig while the app runs: `omarchy font set` only rewrites
//! `~/.config/fontconfig`, so that file is what is watched.
use std::path::Path;
use std::sync::OnceLock;
use std::time::Duration;
use gpui_kit::{App, Window, WindowAppearance};
use gpui_omarchy::Theme;
use gpui_kit::{App, SharedString, Window, WindowAppearance};
use gpui_omarchy::{ActiveTheme, Theme};
use notify::{RecursiveMode, Watcher as _};
/// Whether Omarchy's theme files are installed, and gpui-omarchy therefore
/// follows the desktop instead of the system appearance.
pub fn follows_omarchy(home: Option<&Path>) -> bool {
home.is_some_and(|home| {
[".local/state/omarchy/current", ".config/omarchy/current"]
.iter()
.any(|theme| home.join(theme).exists())
})
}
/// Whether the system appearance, rather than an Omarchy theme, decides the
/// colours: unless an Omarchy theme is installed, on macOS and Windows, and
/// on a Linux desktop that states a preference.
pub fn follows_system(home: Option<&Path>) -> bool {
let desktop = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
states_a_preference(&desktop)
&& !home.is_some_and(|home| {
[".local/state/omarchy/current", ".config/omarchy/current"]
.iter()
.any(|theme| home.join(theme).exists())
})
states_a_preference(&desktop) && !follows_omarchy(home)
}
/// The family every label is drawn in.
///
/// Resolved once per process, like the theme's own `mono_font`: changing the
/// font takes effect on the next start, the same as the terminal.
pub fn font(theme: &Theme) -> SharedString {
font_for(theme, on_omarchy())
}
fn font_for(theme: &Theme, on_omarchy: bool) -> SharedString {
if on_omarchy {
theme.mono_font.clone()
} else {
theme.font.clone()
}
}
fn on_omarchy() -> bool {
static ON_OMARCHY: OnceLock<bool> = OnceLock::new();
*ON_OMARCHY.get_or_init(|| follows_omarchy(std::env::home_dir().as_deref()))
}
/// Point gpui-omarchy's own controls at the desktop face, and follow the font
/// from now on.
///
/// The crate draws buttons, checkboxes, text fields, dialogs and tooltips
/// itself, and each styles itself with `Theme::font`. On Omarchy that becomes
/// the family `omarchy font set` chose, so its controls match the text this
/// app lays out. `Theme::apply` resets `font` whenever the theme files change,
/// so an observer puts it back after every reload.
pub fn follow_desktop_face(cx: &mut App) {
let omarchy = on_omarchy();
follow_face(cx, omarchy);
if omarchy {
watch_desktop_face(cx);
}
}
/// [`follow_desktop_face`] with the desktop decision left to the caller, and
/// without the watcher, so a test can exercise it on a machine that is not
/// Omarchy.
pub fn follow_face(cx: &mut App, on_omarchy: bool) {
if !on_omarchy {
return;
}
desktop_face(cx);
cx.observe_global::<Theme>(desktop_face).detach();
}
/// Keeps the fontconfig watcher's task alive for as long as the app runs.
struct FaceWatcher {
_task: gpui_kit::Task<()>,
}
impl gpui_kit::Global for FaceWatcher {}
/// Repaint when `omarchy font set` rewrites fontconfig.
///
/// `omarchy font set` writes `~/.config/fontconfig/fonts.conf` and restarts
/// the shell; it never touches the theme files gpui-omarchy watches, and
/// gpui-omarchy resolves `mono_font` once per process. Watching the directory
/// (not the file, which the rewrite replaces) and asking fontconfig again is
/// what lets the change land without a restart.
fn watch_desktop_face(cx: &mut App) {
let Some(home) = std::env::home_dir() else {
return;
};
let directory = home.join(".config/fontconfig");
// A machine with no fontconfig directory yet has nothing to watch; its
// first `omarchy font set` still needs a restart.
if !directory.is_dir() {
return;
}
let (sender, events) = async_channel::bounded(1);
let watcher = notify::recommended_watcher(
move |event: notify::Result<notify::Event>| match event {
// `fc-match` reads the file this watches; reacting to access
// would let each read schedule the next one.
Ok(event) if event.kind.is_access() => {}
// A rewrite arrives as several events, and one pending wake-up
// is enough to handle all of them.
Ok(_) => {
let _ = sender.try_send(());
}
Err(error) => eprintln!("disktree font watcher error: {error}"),
},
);
let mut watcher = match watcher {
Ok(watcher) => watcher,
Err(error) => {
eprintln!("Cannot watch the Omarchy font: {error}");
return;
}
};
if let Err(error) = watcher.watch(&directory, RecursiveMode::Recursive) {
eprintln!("Cannot watch the Omarchy font: {error}");
return;
}
let task = cx.spawn(async move |cx| {
// The task owns the watcher, and the global below owns the task.
let _watcher = watcher;
while events.recv().await.is_ok() {
// `fonts.conf` is truncated before it is written, so let the
// rewrite finish rather than reading it empty.
cx.background_executor()
.timer(Duration::from_millis(50))
.await;
let face = cx
.background_executor()
.spawn(async { resolved_face() })
.await;
let Some(face) = face else { continue };
cx.update(|cx| set_desktop_face(cx, face));
}
});
cx.set_global(FaceWatcher { _task: task });
}
/// The family fontconfig resolves for `monospace` right now.
///
/// The same query gpui-omarchy makes once at startup, repeated so a change
/// can be picked up. `fc-match` lists aliases after the first family,
/// separated by commas.
#[cfg(all(unix, not(target_os = "macos")))]
#[allow(
clippy::literal_string_with_formatting_args,
reason = "%{family} is fontconfig's format, not a Rust one"
)]
fn resolved_face() -> Option<SharedString> {
let output = std::process::Command::new("fc-match")
.args(["monospace", "-f", "%{family}"])
.output()
.ok()
.filter(|output| output.status.success())?;
let family = String::from_utf8(output.stdout).ok()?;
let family = family.lines().next()?.split(',').next()?.trim();
(!family.is_empty()).then(|| SharedString::from(family.to_owned()))
}
#[cfg(not(all(unix, not(target_os = "macos"))))]
fn resolved_face() -> Option<SharedString> {
None
}
/// Put `face` in the theme. The app's own text reads `mono_font` and
/// gpui-omarchy's controls read `font`, so both move together.
fn set_desktop_face(cx: &mut App, face: SharedString) {
let mut theme = cx.omarchy().clone();
if theme.font == face && theme.mono_font == face {
return;
}
theme.mono_font = face.clone();
theme.font = face;
cx.set_global(theme);
cx.refresh_windows();
}
/// After a theme reload, `Theme::apply` has put `font` and `mono_font` back to
/// their startup values. Asking fontconfig again, rather than copying the
/// stale field, is what keeps a font change through a colour change.
fn desktop_face(cx: &mut App) {
// The rewrite notifies this observer a second time; equality ends that
// pass without asking fontconfig again.
if cx.omarchy().font == cx.omarchy().mono_font {
return;
}
let face =
resolved_face().unwrap_or_else(|| cx.omarchy().mono_font.clone());
set_desktop_face(cx, face);
}
/// Whether the system has a light or dark setting to follow. macOS and
@@ -68,4 +261,29 @@ mod tests {
assert_eq!(states_a_preference("Hyprland"), native);
assert_eq!(states_a_preference(""), native);
}
#[test]
fn either_omarchy_theme_location_marks_the_desktop() {
let bare = tempfile::tempdir().unwrap();
assert!(!follows_omarchy(Some(bare.path())));
assert!(!follows_omarchy(None));
for location in
[".local/state/omarchy/current", ".config/omarchy/current"]
{
let home = tempfile::tempdir().unwrap();
std::fs::create_dir_all(home.path().join(location)).unwrap();
assert!(follows_omarchy(Some(home.path())), "{location}");
}
}
/// Omarchy's one font setting must reach the labels: the desktop's
/// monospace face replaces GPUI's fixed system UI face.
#[test]
fn omarchy_draws_with_the_desktop_monospace_face() {
let mut theme = Theme::tokyo_night();
theme.font = "System UI".into();
theme.mono_font = "Desktop Mono".into();
assert_eq!(font_for(&theme, true).as_ref(), "Desktop Mono");
assert_eq!(font_for(&theme, false).as_ref(), "System UI");
}
}
+1
View File
@@ -184,6 +184,7 @@ fn run() -> Result<()> {
.with_assets(gpui_kit::assets::Assets)
.run(move |cx| {
gpui_omarchy::init(cx);
appearance::follow_desktop_face(cx);
app_menu::install(cx);
let home = std::env::home_dir();
let native_look = appearance::follows_system(home.as_deref());
+29 -1
View File
@@ -16,8 +16,9 @@ use gpui_kit::{
Bounds, Context, Entity, Pixels, Point, TestAppContext, VisualTestContext,
px,
};
use gpui_omarchy::Theme;
use gpui_omarchy::{ActiveTheme, Theme};
use crate::appearance;
use crate::state::{Disktree, Screen};
/// A small tree on disk: two directories, a nested file, and a hidden one.
@@ -1192,6 +1193,33 @@ fn both_appearances_draw(cx: &mut TestAppContext) {
}
}
/// gpui-omarchy's own controls read `Theme::font`, and a theme reload resets
/// it: the desktop face must be set at startup and survive the reload, or
/// buttons and checkboxes fall back to the system face.
#[gpui_kit::test]
fn the_desktop_face_survives_a_theme_reload(cx: &TestAppContext) {
cx.update(gpui_omarchy::init);
cx.update(|cx| {
let mut theme = Theme::tokyo_night();
theme.font = "System UI".into();
theme.mono_font = "Stale Mono".into();
theme.apply(cx);
appearance::follow_face(cx, true);
});
cx.update(|cx| {
assert_eq!(cx.omarchy().font, cx.omarchy().mono_font);
assert_ne!(cx.omarchy().font.as_ref(), "System UI");
});
// Reloading the theme is what `Theme::apply` does, and it puts both
// fields back to their startup values; the observer has to win.
cx.update(|cx| Theme::flexoki_light().apply(cx));
cx.update(|cx| {
assert_eq!(cx.omarchy().font, cx.omarchy().mono_font);
assert_ne!(cx.omarchy().font.as_ref(), "System UI");
});
}
/// A view built without a scan keeps working: the screens must not assume a
/// walk is in flight.
#[gpui_kit::test]
+2 -1
View File
@@ -23,6 +23,7 @@ use gpui_omarchy::{ActiveTheme, Theme};
use disktree_core::classify::Category;
use crate::appearance;
use crate::palette;
use crate::state::{Disktree, Filtered, Label, View};
@@ -142,7 +143,7 @@ fn mosaic_inner(
let measured = Rc::clone(&app.treemap_size);
let colors = Colors::new(&theme);
let font = theme.font.clone();
let font = appearance::font(&theme);
let name_size = rem_px(0.75, rem);
let size_size = rem_px(0.6875, rem);
+3 -2
View File
@@ -26,6 +26,7 @@ use gpui_omarchy::{
use gpui_kit::prelude::FluentBuilder as _;
use crate::appearance;
use crate::palette;
use crate::state::{
ColorMode, Crumb, Disktree, PANEL_REMS, Screen, panel_width,
@@ -112,7 +113,7 @@ pub fn root(
.size_full()
.bg(theme.background)
.text_color(theme.foreground)
.font_family(theme.font)
.font_family(appearance::font(&theme))
.text_size(text::BODY)
.child(body);
if let Some(tip) = cursor_tooltip(app, window, cx) {
@@ -3596,7 +3597,7 @@ fn card_surface(cx: &gpui_kit::App) -> Div {
.border_color(theme.control_border())
.bg(theme.background.opacity(0.93))
.text_color(theme.foreground)
.font_family(theme.font.clone())
.font_family(appearance::font(theme))
.text_size(text::CAPTION)
}
+3 -2
View File
@@ -13,6 +13,7 @@ use gpui_kit::{
};
use gpui_omarchy::{ActiveTheme, Status};
use crate::appearance;
use crate::ui::{space, text};
/// File counts, which is what a directory count is too.
@@ -92,7 +93,7 @@ pub fn chip(label: impl Into<SharedString>, color: Hsla, cx: &App) -> Div {
.bg(color.opacity(0.1))
.text_color(color)
.text_size(text::CAPTION)
.font_family(theme.font.clone())
.font_family(appearance::font(theme))
.child(label.into())
}
@@ -279,7 +280,7 @@ pub fn glyph_bar(
let theme = cx.omarchy();
div()
.text_size(text::CAPTION)
.font_family(theme.font.clone())
.font_family(appearance::font(theme))
.text_color(color)
.child(SharedString::from(share_bar(part, total, width)))
}