1use gpui::{
10 Animation, AnimationExt, App, Bounds, Context, Global, Hsla, Pixels, SharedString, TextRun,
11 Window, WindowAppearance, WindowBounds, prelude::*, px,
12};
13
14pub use gpui::FontWeight;
15
16use std::borrow::Cow;
17use std::path::Path;
18use std::sync::Arc;
19use std::sync::atomic::{AtomicU64, Ordering};
20use std::time::Duration;
21
22pub mod fonts;
23pub mod locales;
24
25pub use fonts::*;
26pub use locales::*;
27
28static NEXT_UNIQUE_ID: AtomicU64 = AtomicU64::new(1);
29
30#[derive(Clone, Debug, Default, PartialEq, Eq)]
41pub struct FontConfig {
42 pub ui_families: Vec<SharedString>,
46 pub code_families: Vec<SharedString>,
50 pub ui_weight: Option<FontWeight>,
54 pub code_weight: Option<FontWeight>,
57}
58
59impl FontConfig {
60 pub fn system() -> Self {
62 Self::default()
63 }
64
65 pub fn with_ui_families(
67 mut self,
68 families: impl IntoIterator<Item = impl Into<SharedString>>,
69 ) -> Self {
70 self.ui_families = families.into_iter().map(Into::into).collect();
71 self
72 }
73
74 pub fn with_code_families(
76 mut self,
77 families: impl IntoIterator<Item = impl Into<SharedString>>,
78 ) -> Self {
79 self.code_families = families.into_iter().map(Into::into).collect();
80 self
81 }
82
83 pub fn with_ui_weight(mut self, weight: FontWeight) -> Self {
85 self.ui_weight = Some(weight);
86 self
87 }
88
89 pub fn with_code_weight(mut self, weight: FontWeight) -> Self {
91 self.code_weight = Some(weight);
92 self
93 }
94
95 pub fn ui_families(&self) -> &[SharedString] {
97 &self.ui_families
98 }
99
100 pub fn code_families(&self) -> &[SharedString] {
102 &self.code_families
103 }
104
105 pub fn ui_weight(&self) -> Option<FontWeight> {
107 self.ui_weight
108 }
109
110 pub fn code_weight(&self) -> Option<FontWeight> {
112 self.code_weight
113 }
114}
115
116#[derive(Clone, Debug, Default, PartialEq, Eq)]
118pub struct Options {
119 pub theme_mode: ThemeMode,
121 pub fonts: FontConfig,
123 pub locales: LocalesConfig,
125}
126
127impl Options {
128 pub fn system() -> Self {
130 Self::default()
131 }
132
133 pub fn with_theme_mode(mut self, mode: ThemeMode) -> Self {
135 self.theme_mode = mode;
136 self
137 }
138
139 pub fn with_fonts(mut self, fonts: FontConfig) -> Self {
141 self.fonts = fonts;
142 self
143 }
144
145 pub fn with_locale(mut self, locale: impl Into<LocaleId>) -> Self {
147 self.locales = self.locales.with_locale(locale);
148 self
149 }
150
151 pub fn with_fallback_locale(mut self, locale: impl Into<LocaleId>) -> Self {
153 self.locales = self.locales.with_fallback_locale(locale);
154 self
155 }
156
157 pub fn with_locales(mut self, locales: LocalesConfig) -> Self {
159 self.locales = locales;
160 self
161 }
162
163 pub fn with_locales_resources(mut self, resources: LocalesMap) -> Self {
165 self.locales = self.locales.with_resources(resources);
166 self
167 }
168
169 pub fn try_with_locales_dir(mut self, dir: impl AsRef<Path>) -> Result<Self, LocalesLoadError> {
171 self.locales = self.locales.try_with_locales_dir(dir)?;
172 Ok(self)
173 }
174
175 pub fn with_translator(mut self, translator: impl Translator + 'static) -> Self {
177 self.locales = self.locales.with_translator(translator);
178 self
179 }
180
181 pub fn with_shared_translator(mut self, translator: Arc<dyn Translator>) -> Self {
183 self.locales = self.locales.with_shared_translator(translator);
184 self
185 }
186}
187
188pub fn load_custom_fonts(
196 cx: &mut App,
197 fonts: impl IntoIterator<Item = Cow<'static, [u8]>>,
198) -> gpui::Result<()> {
199 cx.text_system().add_fonts(fonts.into_iter().collect())
200}
201
202pub fn next_unique_id() -> u64 {
204 NEXT_UNIQUE_ID.fetch_add(1, Ordering::Relaxed)
205}
206
207pub fn unique_id(prefix: &str) -> gpui::SharedString {
214 format!("{}-{}", prefix, next_unique_id()).into()
215}
216
217pub fn stable_unique_id(
223 key: impl Into<gpui::SharedString>,
224 prefix: &str,
225 window: &mut Window,
226 cx: &mut App,
227) -> gpui::SharedString {
228 let prefix = prefix.to_string();
229 let key = gpui::ElementId::from(key.into());
230 window
231 .use_keyed_state(key, cx, move |_, _| unique_id(&prefix))
232 .read(cx)
233 .clone()
234}
235
236pub mod popper;
238
239pub use popper::*;
240
241pub use liora_theme::Theme;
242
243#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
244pub enum ThemeMode {
246 #[default]
247 System,
249 Light,
251 Dark,
253}
254
255impl ThemeMode {
256 pub fn label(self) -> &'static str {
258 match self {
259 Self::System => "System",
260 Self::Light => "Light",
261 Self::Dark => "Dark",
262 }
263 }
264
265 pub fn value(self) -> &'static str {
267 match self {
268 Self::System => "system",
269 Self::Light => "light",
270 Self::Dark => "dark",
271 }
272 }
273
274 pub fn from_value(value: &str) -> Option<Self> {
276 match value {
277 "system" => Some(Self::System),
278 "light" => Some(Self::Light),
279 "dark" => Some(Self::Dark),
280 _ => None,
281 }
282 }
283
284 pub fn resolve(self, appearance: WindowAppearance) -> Theme {
286 match self {
287 Self::System => theme_for_window_appearance(appearance),
288 Self::Light => Theme::light(),
289 Self::Dark => Theme::dark(),
290 }
291 }
292
293 pub fn from_theme(theme: &Theme) -> Self {
295 match theme.name.as_str() {
296 "dark" => Self::Dark,
297 _ => Self::Light,
298 }
299 }
300}
301
302pub fn startup_maximized_window_bounds(
309 cx: &App,
310 fallback_size: gpui::Size<Pixels>,
311) -> WindowBounds {
312 let bounds = cx
313 .primary_display()
314 .map(|display| display.bounds())
315 .unwrap_or(Bounds {
316 origin: gpui::Point::default(),
317 size: fallback_size,
318 });
319 WindowBounds::Maximized(bounds)
320}
321
322#[derive(Clone, Debug)]
331pub struct LinuxDesktopIdentity<'a> {
332 pub app_id: &'a str,
334 pub desktop_entry: Cow<'a, str>,
336 pub png_icons: &'a [LinuxDesktopPngIcon<'a>],
338 pub svg_icon: &'a [u8],
340}
341
342#[derive(Clone, Copy, Debug)]
344pub struct LinuxDesktopPngIcon<'a> {
345 pub size: u16,
347 pub bytes: &'a [u8],
349}
350
351pub fn linux_desktop_entry(
358 name: &str,
359 generic_name: &str,
360 comment: &str,
361 executable: &std::path::Path,
362 icon: &str,
363 categories: &str,
364 keywords: &str,
365) -> String {
366 format!(
367 "[Desktop Entry]\nVersion=1.0\nType=Application\nName={name}\nGenericName={generic_name}\nComment={comment}\nExec={}\nIcon={icon}\nCategories={categories}\nKeywords={keywords}\nStartupNotify=true\nTerminal=false\n",
368 desktop_exec_value(executable),
369 )
370}
371
372pub fn linux_desktop_png_icon_path(app_id: &str, size: u16) -> Option<std::path::PathBuf> {
378 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
379 {
380 linux_xdg_data_home().ok().map(|data_home| {
381 data_home
382 .join("icons")
383 .join("hicolor")
384 .join(format!("{size}x{size}"))
385 .join("apps")
386 .join(format!("{app_id}.png"))
387 })
388 }
389
390 #[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
391 {
392 let _ = (app_id, size);
393 None
394 }
395}
396
397fn desktop_exec_value(executable: &std::path::Path) -> String {
398 let value = executable.to_string_lossy();
399 if value
400 .chars()
401 .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '/' | '.' | '_' | '-' | '+'))
402 {
403 value.into_owned()
404 } else {
405 format!("\"{}\"", value.replace('\\', "\\\\").replace('\"', "\\\""))
406 }
407}
408
409pub fn ensure_linux_desktop_identity(identity: LinuxDesktopIdentity<'_>) -> std::io::Result<()> {
418 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
419 {
420 ensure_linux_desktop_identity_impl(linux_xdg_data_home()?, identity, true)
421 }
422
423 #[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
424 {
425 let _ = identity;
426 Ok(())
427 }
428}
429
430#[cfg(any(target_os = "linux", target_os = "freebsd"))]
431fn ensure_linux_desktop_identity_impl(
432 data_home: std::path::PathBuf,
433 identity: LinuxDesktopIdentity<'_>,
434 refresh_cache: bool,
435) -> std::io::Result<()> {
436 let mut changed = write_if_changed(
437 &data_home
438 .join("applications")
439 .join(format!("{}.desktop", identity.app_id)),
440 identity.desktop_entry.as_bytes(),
441 )?;
442 for icon in identity.png_icons {
443 changed |= write_if_changed(
444 &data_home
445 .join("icons")
446 .join("hicolor")
447 .join(format!("{}x{}", icon.size, icon.size))
448 .join("apps")
449 .join(format!("{}.png", identity.app_id)),
450 icon.bytes,
451 )?;
452 }
453 changed |= write_if_changed(
454 &data_home
455 .join("icons")
456 .join("hicolor")
457 .join("scalable")
458 .join("apps")
459 .join(format!("{}.svg", identity.app_id)),
460 identity.svg_icon,
461 )?;
462 changed |= ensure_hicolor_index_theme(&data_home)?;
463 if changed && refresh_cache {
464 refresh_linux_desktop_identity_cache(&data_home);
465 }
466 Ok(())
467}
468
469#[cfg(any(target_os = "linux", target_os = "freebsd"))]
470fn refresh_linux_desktop_identity_cache(data_home: &std::path::Path) {
471 let apps_dir = data_home.join("applications");
472 let _ = std::fs::remove_file(
473 data_home
474 .join("icons")
475 .join("hicolor")
476 .join(".icon-theme.cache"),
477 );
478 let _ = std::process::Command::new("update-desktop-database")
479 .arg(&apps_dir)
480 .status();
481 for command in ["kbuildsycoca6", "kbuildsycoca5"] {
482 let _ = std::process::Command::new(command)
483 .arg("--noincremental")
484 .status();
485 }
486}
487
488#[cfg(any(target_os = "linux", target_os = "freebsd"))]
489fn ensure_hicolor_index_theme(data_home: &std::path::Path) -> std::io::Result<bool> {
490 write_if_changed(
491 &data_home.join("icons").join("hicolor").join("index.theme"),
492 HICOLOR_INDEX_THEME.as_bytes(),
493 )
494}
495
496#[cfg(any(target_os = "linux", target_os = "freebsd"))]
497const HICOLOR_INDEX_THEME: &str = "[Icon Theme]\nName=Hicolor\nComment=Fallback icon theme\nHidden=true\nDirectories=16x16/apps,24x24/apps,32x32/apps,48x48/apps,64x64/apps,128x128/apps,256x256/apps,512x512/apps,scalable/apps\n\n[16x16/apps]\nSize=16\nContext=Applications\nType=Threshold\n\n[24x24/apps]\nSize=24\nContext=Applications\nType=Threshold\n\n[32x32/apps]\nSize=32\nContext=Applications\nType=Threshold\n\n[48x48/apps]\nSize=48\nContext=Applications\nType=Threshold\n\n[64x64/apps]\nSize=64\nContext=Applications\nType=Threshold\n\n[128x128/apps]\nSize=128\nContext=Applications\nType=Threshold\n\n[256x256/apps]\nSize=256\nContext=Applications\nType=Threshold\n\n[512x512/apps]\nSize=512\nContext=Applications\nType=Threshold\n\n[scalable/apps]\nSize=512\nMinSize=16\nMaxSize=512\nContext=Applications\nType=Scalable\n";
498
499#[cfg(any(target_os = "linux", target_os = "freebsd"))]
500fn linux_xdg_data_home() -> std::io::Result<std::path::PathBuf> {
501 if let Some(data_home) = std::env::var_os("XDG_DATA_HOME") {
502 return Ok(std::path::PathBuf::from(data_home));
503 }
504 std::env::var_os("HOME")
505 .map(std::path::PathBuf::from)
506 .map(|home| home.join(".local").join("share"))
507 .ok_or_else(|| {
508 std::io::Error::new(
509 std::io::ErrorKind::NotFound,
510 "neither XDG_DATA_HOME nor HOME is set",
511 )
512 })
513}
514
515#[cfg(any(target_os = "linux", target_os = "freebsd"))]
516fn write_if_changed(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<bool> {
517 if std::fs::read(path).is_ok_and(|existing| existing == bytes) {
518 return Ok(false);
519 }
520 if let Some(parent) = path.parent() {
521 std::fs::create_dir_all(parent)?;
522 }
523 std::fs::write(path, bytes)?;
524 Ok(true)
525}
526
527fn startup_system_appearance(cx: &App) -> WindowAppearance {
528 platform_system_appearance().unwrap_or_else(|| cx.window_appearance())
529}
530
531fn current_system_appearance(window: &Window, _cx: &App) -> WindowAppearance {
532 platform_system_appearance().unwrap_or_else(|| window.appearance())
533}
534
535#[cfg(any(target_os = "linux", target_os = "freebsd"))]
536fn platform_system_appearance() -> Option<WindowAppearance> {
537 gtk_theme_env_appearance()
538 .or_else(gtk_settings_appearance)
539 .or_else(gsettings_color_scheme_appearance)
540}
541
542#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
543fn platform_system_appearance() -> Option<WindowAppearance> {
544 None
545}
546
547#[cfg(any(target_os = "linux", target_os = "freebsd"))]
548fn gtk_theme_env_appearance() -> Option<WindowAppearance> {
549 std::env::var("GTK_THEME")
550 .ok()
551 .and_then(|theme| appearance_from_theme_name(&theme))
552}
553
554#[cfg(any(target_os = "linux", target_os = "freebsd"))]
555fn gtk_settings_appearance() -> Option<WindowAppearance> {
556 ["gtk-4.0", "gtk-3.0"]
557 .into_iter()
558 .filter_map(|version| {
559 std::env::var_os("HOME").map(|home| {
560 std::path::PathBuf::from(home)
561 .join(".config")
562 .join(version)
563 .join("settings.ini")
564 })
565 })
566 .filter_map(|path| std::fs::read_to_string(path).ok())
567 .find_map(|settings| appearance_from_gtk_settings(&settings))
568}
569
570#[cfg(any(target_os = "linux", target_os = "freebsd"))]
571fn gsettings_color_scheme_appearance() -> Option<WindowAppearance> {
572 let output = std::process::Command::new("gsettings")
573 .args(["get", "org.gnome.desktop.interface", "color-scheme"])
574 .output()
575 .ok()?;
576 if !output.status.success() {
577 return None;
578 }
579 let value = String::from_utf8_lossy(&output.stdout);
580 appearance_from_color_scheme(&value)
581}
582
583#[cfg(any(target_os = "linux", target_os = "freebsd"))]
584fn appearance_from_color_scheme(value: &str) -> Option<WindowAppearance> {
585 let value = value
586 .trim()
587 .trim_matches('\'')
588 .trim_matches('"')
589 .to_ascii_lowercase();
590 if value.contains("prefer-dark") {
591 Some(WindowAppearance::Dark)
592 } else if value.contains("prefer-light") || value == "default" {
593 Some(WindowAppearance::Light)
594 } else {
595 None
596 }
597}
598
599#[cfg(any(target_os = "linux", target_os = "freebsd"))]
600fn appearance_from_gtk_settings(settings: &str) -> Option<WindowAppearance> {
601 for line in settings.lines() {
602 let line = line.trim();
603 if line.starts_with('#') || line.starts_with(';') || line.is_empty() {
604 continue;
605 }
606 let Some((key, value)) = line.split_once('=') else {
607 continue;
608 };
609 let key = key.trim();
610 let value = value.trim();
611 if key == "gtk-application-prefer-dark-theme" {
612 return match value.to_ascii_lowercase().as_str() {
613 "true" | "1" => Some(WindowAppearance::Dark),
614 "false" | "0" => Some(WindowAppearance::Light),
615 _ => None,
616 };
617 }
618 if key == "gtk-theme-name"
619 && let Some(appearance) = appearance_from_theme_name(value)
620 {
621 return Some(appearance);
622 }
623 }
624 None
625}
626
627#[cfg(any(target_os = "linux", target_os = "freebsd"))]
628fn appearance_from_theme_name(theme: &str) -> Option<WindowAppearance> {
629 let theme = theme.to_ascii_lowercase();
630 if theme.contains("dark") {
631 Some(WindowAppearance::Dark)
632 } else if theme.contains("light") {
633 Some(WindowAppearance::Light)
634 } else {
635 None
636 }
637}
638
639pub fn theme_for_window_appearance(appearance: WindowAppearance) -> Theme {
641 match appearance {
642 WindowAppearance::Light | WindowAppearance::VibrantLight => Theme::light(),
643 WindowAppearance::Dark | WindowAppearance::VibrantDark => Theme::dark(),
644 }
645}
646
647pub struct Config {
649 pub theme: Theme,
651 pub theme_mode: ThemeMode,
653 pub fonts: FontConfig,
655 pub locales: LocalesConfig,
657 pub z_index_base: u32,
659}
660
661impl Global for Config {}
662
663impl Config {
664 pub fn set_theme_mode(&mut self, mode: ThemeMode, appearance: WindowAppearance) {
666 self.theme_mode = mode;
667 self.theme = mode.resolve(appearance);
668 }
669
670 pub fn set_font_config(&mut self, fonts: FontConfig) {
672 self.fonts = fonts;
673 }
674
675 pub fn set_locales_config(&mut self, locales: LocalesConfig) {
677 self.locales = locales;
678 }
679
680 pub fn sync_system_theme(&mut self, appearance: WindowAppearance) -> bool {
682 if self.theme_mode != ThemeMode::System {
683 return false;
684 }
685 let theme = ThemeMode::System.resolve(appearance);
686 let changed = self.theme.name != theme.name;
687 self.theme = theme;
688 changed
689 }
690}
691
692pub fn init_liora(cx: &mut App, theme: Theme) {
694 let theme_mode = ThemeMode::from_theme(&theme);
695 set_core_config(
696 cx,
697 Config {
698 theme,
699 theme_mode,
700 fonts: FontConfig::default(),
701 locales: LocalesConfig::default(),
702 z_index_base: 1000,
703 },
704 );
705}
706
707fn set_core_config(cx: &mut App, config: Config) {
708 cx.set_global(config);
709 cx.set_global(crate::popper::ZIndexStack::default());
710 cx.set_global(crate::popper::ActiveTooltip(Vec::new()));
711 cx.set_global(crate::popper::ActivePopover(Vec::new()));
712 cx.set_global(crate::popper::ActiveModal(Vec::new()));
713 cx.set_global(crate::popper::ActiveDrawer(Vec::new()));
714}
715
716pub fn init_liora_with_mode(cx: &mut App, mode: ThemeMode) {
718 init_liora_with_options(cx, Options::default().with_theme_mode(mode));
719}
720
721pub fn init_liora_with_options(cx: &mut App, options: Options) {
723 let appearance = startup_system_appearance(cx);
724 set_core_config(
725 cx,
726 Config {
727 theme: options.theme_mode.resolve(appearance),
728 theme_mode: options.theme_mode,
729 fonts: options.fonts,
730 locales: options.locales,
731 z_index_base: 1000,
732 },
733 );
734}
735
736pub fn set_font_config(cx: &mut App, fonts: FontConfig) {
738 cx.global_mut::<Config>().set_font_config(fonts);
739}
740
741pub fn resolve_font_family_from_available(
748 candidates: &[SharedString],
749 available: &[String],
750 default: Option<SharedString>,
751) -> Option<SharedString> {
752 if candidates.is_empty() {
753 return default;
754 }
755
756 candidates
757 .iter()
758 .find(|candidate| available.iter().any(|family| family == candidate.as_ref()))
759 .cloned()
760 .or_else(|| candidates.last().cloned())
761}
762
763pub fn ui_font_family(cx: &App) -> Option<SharedString> {
765 let config = cx.global::<Config>();
766 let available = cx.text_system().all_font_names();
767 resolve_font_family_from_available(&config.fonts.ui_families, &available, None)
768}
769
770pub fn ui_font_weight(cx: &App) -> Option<FontWeight> {
772 cx.global::<Config>().fonts.ui_weight
773}
774
775pub fn code_font_family(cx: &App) -> SharedString {
777 let config = cx.global::<Config>();
778 let available = cx.text_system().all_font_names();
779 resolve_font_family_from_available(
780 &config.fonts.code_families,
781 &available,
782 Some("Monospace".into()),
783 )
784 .unwrap_or_else(|| "Monospace".into())
785}
786
787pub fn code_font_weight(cx: &App) -> Option<FontWeight> {
789 cx.global::<Config>().fonts.code_weight
790}
791
792pub fn apply_theme_mode(window: &mut Window, cx: &mut App, mode: ThemeMode) {
794 let appearance = current_system_appearance(window, cx);
795 cx.global_mut::<Config>().set_theme_mode(mode, appearance);
796 window.refresh();
797}
798
799pub fn sync_system_theme(window: &mut Window, cx: &mut App) {
801 let appearance = current_system_appearance(window, cx);
802 if cx.global_mut::<Config>().sync_system_theme(appearance) {
803 window.refresh();
804 }
805}
806
807pub fn attach_system_theme_observer(window: &mut Window, cx: &mut App) {
815 sync_system_theme(window, cx);
816 window
817 .observe_window_appearance(|window, cx| sync_system_theme(window, cx))
818 .detach();
819}
820
821pub fn render_active_popover_in_window(_window: &mut gpui::Window, cx: &mut App) {
823 for entry in cx.global::<crate::popper::ActivePopover>().0.clone() {
824 push_portal(
825 move |_window, _cx| entry.view.clone().into_any_element(),
826 cx,
827 );
828 }
829}
830
831pub fn render_active_modal_in_window(_window: &mut gpui::Window, cx: &mut App) {
833 for entry in cx.global::<crate::popper::ActiveModal>().0.clone() {
834 push_portal(
835 move |_window, _cx| entry.view.clone().into_any_element(),
836 cx,
837 );
838 }
839}
840
841pub fn render_active_drawer_in_window(_window: &mut gpui::Window, cx: &mut App) {
843 for entry in cx.global::<crate::popper::ActiveDrawer>().0.clone() {
844 push_portal(
845 move |_window, _cx| entry.view.clone().into_any_element(),
846 cx,
847 );
848 }
849}
850
851pub fn tooltip_content_lines(content: &SharedString) -> Vec<SharedString> {
858 let lines: Vec<SharedString> = content
859 .split('\n')
860 .map(|line| line.trim_end_matches('\r'))
861 .map(SharedString::from)
862 .collect();
863
864 if lines.is_empty() {
865 vec![SharedString::default()]
866 } else {
867 lines
868 }
869}
870
871pub fn render_active_tooltip_in_window(window: &mut gpui::Window, cx: &mut App) {
873 let mouse_pos = window.mouse_position();
874 cx.global_mut::<crate::popper::ActiveTooltip>()
875 .0
876 .retain(|data| data.anchor_bounds.contains(&mouse_pos));
877
878 let active = cx.global::<crate::popper::ActiveTooltip>().0.clone();
879 for (tooltip_index, data) in active.into_iter().enumerate() {
880 let theme = cx.global::<Config>().theme.clone();
881
882 let font_size = px(theme.font_size.sm);
885 let text_style = window.text_style();
886 let lines = tooltip_content_lines(&data.content);
887 let max_line_width = lines
888 .iter()
889 .map(|line| {
890 let run = TextRun {
891 len: line.len(),
892 font: text_style.font(),
893 color: theme.neutral.card,
894 background_color: None,
895 underline: None,
896 strikethrough: None,
897 };
898 window
899 .text_system()
900 .shape_line(line.clone(), font_size, &[run], None)
901 .width
902 })
903 .fold(px(0.0), |max_width, width| max_width.max(width));
904
905 let padding_h = px(12.0);
906 let padding_v = px(6.0);
907 let line_height = window.line_height();
908 let content_size = gpui::Size {
909 width: max_line_width + padding_h * 2.0,
910 height: line_height * lines.len() + padding_v * 2.0,
911 };
912
913 push_passive_portal(
914 move |window, _cx| {
915 let viewport = Bounds {
916 origin: gpui::Point::default(),
917 size: window.viewport_size(),
918 };
919
920 let popper = Popper {
921 anchor_bounds: data.anchor_bounds,
922 placement: data.placement,
923 offset: data.offset,
924 };
925
926 let (pos, _final_placement) =
927 popper.calculate_position_with_flip(content_size, viewport);
928
929 gpui::div()
930 .absolute()
931 .cursor_default()
932 .top(pos.y)
933 .left(pos.x)
934 .w(content_size.width)
935 .h(content_size.height)
936 .bg(theme.neutral.text_1)
937 .text_color(theme.neutral.card)
938 .px(padding_h)
939 .py(padding_v)
940 .flex()
941 .flex_col()
942 .items_start()
943 .justify_center()
944 .gap(px(2.0))
945 .rounded(px(theme.radius.sm))
946 .shadow_lg()
947 .text_size(font_size)
948 .children(lines.iter().cloned())
949 .with_animation(
950 ("liora-tooltip-motion", tooltip_index),
951 Animation::new(Duration::from_millis(220))
952 .with_easing(gpui::ease_out_quint()),
953 |tooltip, delta| tooltip.opacity(delta),
954 )
955 .into_any_element()
956 },
957 cx,
958 );
959 }
960}
961
962#[cfg(test)]
963mod tooltip_text_tests {
964 use super::*;
965
966 #[test]
967 fn tooltip_renderer_never_shapes_multiline_content_as_one_line() {
968 let source = include_str!("lib.rs").split("#[cfg(test)]").next().unwrap();
969
970 assert!(
971 !source.contains(".shape_line(data.content.clone()"),
972 "GPUI shape_line panics when the text argument contains newlines"
973 );
974 assert!(
975 source.contains("tooltip_content_lines(&data.content)")
976 || source.contains("tooltip_content_lines("),
977 "tooltip rendering should split multiline content before measuring and rendering"
978 );
979 }
980
981 #[test]
982 fn tooltip_content_lines_preserves_multiline_tooltips_without_newline_lines() {
983 let lines = tooltip_content_lines(&SharedString::from("Mon\nO 100 H 110\nL 96 C 108"));
984
985 assert_eq!(lines, vec!["Mon", "O 100 H 110", "L 96 C 108"]);
986 assert!(lines.iter().all(|line| !line.contains('\n')));
987 }
988}
989
990#[cfg(test)]
991mod theme_mode_tests {
992 use super::*;
993
994 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
995 #[test]
996 fn linux_startup_appearance_parses_synchronous_dark_preferences() {
997 assert_eq!(
998 appearance_from_color_scheme("'prefer-dark'"),
999 Some(WindowAppearance::Dark)
1000 );
1001 assert_eq!(
1002 appearance_from_color_scheme("prefer-light"),
1003 Some(WindowAppearance::Light)
1004 );
1005 assert_eq!(
1006 appearance_from_gtk_settings(
1007 "[Settings]\ngtk-application-prefer-dark-theme=true\ngtk-theme-name=Breeze\n"
1008 ),
1009 Some(WindowAppearance::Dark)
1010 );
1011 assert_eq!(
1012 appearance_from_theme_name("Adwaita-dark"),
1013 Some(WindowAppearance::Dark)
1014 );
1015 }
1016
1017 #[test]
1018 fn theme_mode_values_and_labels_are_stable() {
1019 assert_eq!(ThemeMode::System.value(), "system");
1020 assert_eq!(ThemeMode::Light.label(), "Light");
1021 assert_eq!(ThemeMode::from_value("dark"), Some(ThemeMode::Dark));
1022 assert_eq!(ThemeMode::from_theme(&Theme::dark()), ThemeMode::Dark);
1023 assert_eq!(ThemeMode::from_theme(&Theme::light()), ThemeMode::Light);
1024 assert_eq!(ThemeMode::from_value("unknown"), None);
1025 }
1026
1027 #[test]
1028 fn system_theme_resolves_from_window_appearance() {
1029 assert_eq!(
1030 ThemeMode::System.resolve(WindowAppearance::Light).name,
1031 Theme::light().name
1032 );
1033 assert_eq!(
1034 ThemeMode::System
1035 .resolve(WindowAppearance::VibrantDark)
1036 .name,
1037 Theme::dark().name
1038 );
1039 }
1040
1041 #[test]
1042 fn config_syncs_only_in_system_mode() {
1043 let mut config = Config {
1044 theme: Theme::light(),
1045 theme_mode: ThemeMode::Light,
1046 fonts: FontConfig::default(),
1047 locales: LocalesConfig::default(),
1048 z_index_base: 1000,
1049 };
1050 assert!(!config.sync_system_theme(WindowAppearance::Dark));
1051 assert_eq!(config.theme.name, "light");
1052
1053 config.set_theme_mode(ThemeMode::System, WindowAppearance::Dark);
1054 assert_eq!(config.theme.name, "dark");
1055 assert!(!config.sync_system_theme(WindowAppearance::VibrantDark));
1056 assert!(config.sync_system_theme(WindowAppearance::Light));
1057 assert_eq!(config.theme.name, "light");
1058 }
1059
1060 #[test]
1061 fn font_config_defaults_to_system_fonts_and_keeps_custom_families_explicit() {
1062 let default = FontConfig::default();
1063 assert!(default.ui_families.is_empty());
1064 assert!(default.code_families.is_empty());
1065 assert_eq!(default.ui_weight(), None);
1066 assert_eq!(default.code_weight(), None);
1067
1068 let custom = FontConfig::system()
1069 .with_ui_families(["Inter", "Segoe UI"])
1070 .with_code_families(["JetBrains Mono", "Monospace"])
1071 .with_ui_weight(FontWeight::MEDIUM)
1072 .with_code_weight(FontWeight::NORMAL);
1073 assert_eq!(
1074 custom.ui_families(),
1075 &[SharedString::from("Inter"), SharedString::from("Segoe UI")]
1076 );
1077 assert_eq!(
1078 custom.code_families(),
1079 &[
1080 SharedString::from("JetBrains Mono"),
1081 SharedString::from("Monospace")
1082 ]
1083 );
1084 assert_eq!(custom.ui_weight(), Some(FontWeight::MEDIUM));
1085 assert_eq!(custom.code_weight(), Some(FontWeight::NORMAL));
1086 let options = Options::system()
1087 .with_theme_mode(ThemeMode::Dark)
1088 .with_fonts(custom.clone());
1089 assert_eq!(options.theme_mode, ThemeMode::Dark);
1090 assert_eq!(options.fonts, custom);
1091 assert_eq!(options.locales.locale.as_str(), "en-US");
1092 }
1093
1094 #[test]
1095 fn font_config_accepts_ordered_fallback_family_lists() {
1096 let custom = FontConfig::system()
1097 .with_ui_families(["MiSans", "Segoe UI", "Arial"])
1098 .with_code_families(["JetBrains Mono", "SF Mono", "Monospace"])
1099 .with_ui_weight(FontWeight::MEDIUM);
1100
1101 assert_eq!(
1102 custom.ui_families(),
1103 &[
1104 SharedString::from("MiSans"),
1105 SharedString::from("Segoe UI"),
1106 SharedString::from("Arial")
1107 ]
1108 );
1109 assert_eq!(
1110 custom.code_families(),
1111 &[
1112 SharedString::from("JetBrains Mono"),
1113 SharedString::from("SF Mono"),
1114 SharedString::from("Monospace")
1115 ]
1116 );
1117 assert_eq!(custom.ui_weight(), Some(FontWeight::MEDIUM));
1118 }
1119
1120 #[test]
1121 fn font_family_resolution_uses_first_available_then_final_fallback() {
1122 let candidates = [
1123 SharedString::from("MiSans"),
1124 SharedString::from("Segoe UI"),
1125 SharedString::from("Arial"),
1126 ];
1127 let available = [String::from("Segoe UI")];
1128
1129 assert_eq!(
1130 resolve_font_family_from_available(&candidates, &available, None).as_deref(),
1131 Some("Segoe UI")
1132 );
1133 assert_eq!(
1134 resolve_font_family_from_available(&candidates, &[], None).as_deref(),
1135 Some("Arial")
1136 );
1137 assert_eq!(
1138 resolve_font_family_from_available(&[], &available, Some("Monospace".into()))
1139 .as_deref(),
1140 Some("Monospace")
1141 );
1142 }
1143
1144 #[test]
1145 fn system_theme_observer_syncs_immediately_and_stays_attached() {
1146 let source = include_str!("lib.rs");
1147 let start = source
1148 .find("pub fn attach_system_theme_observer")
1149 .expect("system theme observer helper should exist");
1150 let body = &source[start
1151 ..source[start..]
1152 .find("pub fn render_active_popover_in_window")
1153 .expect("next function should follow observer helper")
1154 + start];
1155
1156 let sync_call = format!("{}(window, cx);", "sync_system_theme");
1157 let observe_call = format!("{}", "observe_window_appearance");
1158 let sync_index = body
1159 .find(&sync_call)
1160 .expect("observer helper should sync the current window appearance immediately");
1161 let observe_index = body
1162 .find(&observe_call)
1163 .expect("observer helper should observe later appearance changes");
1164 assert!(sync_index < observe_index);
1165 assert!(body.contains(".detach();"));
1166 }
1167}
1168
1169#[cfg(test)]
1170mod motion_tests {
1171 #[test]
1172 fn tooltip_rendering_uses_gpui_motion() {
1173 let source = include_str!("lib.rs").split("#[cfg(test)]").next().unwrap();
1174
1175 assert!(source.contains("tooltip-motion"));
1176 assert!(source.contains("with_animation("));
1177 }
1178}
1179
1180pub fn liora_theme<'a, V>(cx: &'a Context<'a, V>) -> &'a Theme {
1182 &cx.global::<Config>().theme
1183}
1184
1185pub trait ContextExt {
1187 fn liora(&self) -> &Theme;
1189}
1190
1191impl<'a, V> ContextExt for Context<'a, V> {
1192 fn liora(&self) -> &Theme {
1193 liora_theme(self)
1194 }
1195}
1196
1197pub trait ElementExt {
1199 fn liora(self, cx: &mut App) -> Self;
1201}
1202
1203impl ElementExt for gpui::Div {
1204 fn liora(self, _cx: &mut App) -> Self {
1205 self
1206 }
1207}
1208
1209pub fn z_index_popup<V>(cx: &Context<'_, V>) -> u32 {
1211 cx.global::<Config>().z_index_base + 100
1212}
1213
1214pub fn z_index_modal<V>(cx: &Context<'_, V>) -> u32 {
1216 cx.global::<Config>().z_index_base + 200
1217}
1218
1219pub fn z_index_notification<V>(cx: &Context<'_, V>) -> u32 {
1221 cx.global::<Config>().z_index_base + 300
1222}
1223
1224pub fn z_index_tooltip<V>(cx: &Context<'_, V>) -> u32 {
1226 cx.global::<Config>().z_index_base + 400
1227}
1228
1229pub fn hex_color(hex: u32) -> Hsla {
1231 gpui::rgb(hex).into()
1232}
1233
1234#[cfg(test)]
1235mod unique_id_tests {
1236 use super::*;
1237
1238 #[test]
1239 fn generated_ids_are_prefixed_and_unique() {
1240 let first = unique_id("component");
1241 let second = unique_id("component");
1242
1243 assert!(first.as_ref().starts_with("component-"));
1244 assert!(second.as_ref().starts_with("component-"));
1245 assert_ne!(first, second);
1246 }
1247}
1248
1249#[cfg(test)]
1250mod desktop_identity_tests {
1251 use super::*;
1252
1253 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
1254 #[test]
1255 fn linux_desktop_identity_installs_desktop_entry_and_hicolor_icons() {
1256 let temp_root = std::env::temp_dir().join(format!(
1257 "liora-desktop-identity-test-{}",
1258 std::process::id()
1259 ));
1260 let _ = std::fs::remove_dir_all(&temp_root);
1261 std::fs::create_dir_all(&temp_root).expect("test temp root should be creatable");
1262
1263 ensure_linux_desktop_identity_impl(
1264 temp_root.clone(),
1265 LinuxDesktopIdentity {
1266 app_id: "liora-test",
1267 desktop_entry: Cow::Borrowed(
1268 "[Desktop Entry]\nType=Application\nName=Liora Test\nIcon=liora-test\n",
1269 ),
1270 png_icons: &[LinuxDesktopPngIcon {
1271 size: 48,
1272 bytes: b"png",
1273 }],
1274 svg_icon: b"svg",
1275 },
1276 false,
1277 )
1278 .expect("identity registration should write into the supplied XDG data home");
1279
1280 assert_eq!(
1281 std::fs::read_to_string(temp_root.join("applications/liora-test.desktop"))
1282 .expect("desktop entry should be installed"),
1283 "[Desktop Entry]\nType=Application\nName=Liora Test\nIcon=liora-test\n"
1284 );
1285 assert_eq!(
1286 std::fs::read(temp_root.join("icons/hicolor/48x48/apps/liora-test.png"))
1287 .expect("PNG hicolor icon should be installed"),
1288 b"png"
1289 );
1290 assert_eq!(
1291 std::fs::read(temp_root.join("icons/hicolor/scalable/apps/liora-test.svg"))
1292 .expect("SVG hicolor icon should be installed"),
1293 b"svg"
1294 );
1295 assert!(
1296 std::fs::read_to_string(temp_root.join("icons/hicolor/index.theme"))
1297 .expect("hicolor index theme should be installed")
1298 .contains("Directories=16x16/apps")
1299 );
1300
1301 let _ = std::fs::remove_dir_all(temp_root);
1302 }
1303}