| 1 | use gpui_kit::*; |
| 2 | use store::Store; |
| 3 | |
| 4 | use crate::appearance::{self, AppIcon, AppearanceSettings, SystemTheme, ThemeChoice}; |
| 5 | use crate::theme::{self, ColorScheme}; |
| 6 | use crate::zoom; |
| 7 | |
| 8 | #[derive(Default)] |
| 9 | pub struct ActiveAppearance { |
| 10 | settings: AppearanceSettings, |
| 11 | system: SystemTheme, |
| 12 | } |
| 13 | |
| 14 | impl Global for ActiveAppearance {} |
| 15 | |
| 16 | pub fn system_theme(appearance: WindowAppearance) -> SystemTheme { |
| 17 | SystemTheme { |
| 18 | dark: matches!( |
| 19 | appearance, |
| 20 | WindowAppearance::Dark | WindowAppearance::VibrantDark |
| 21 | ), |
| 22 | high_contrast: appearance::system_high_contrast(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn install(store: &Store, cx: &mut App) { |
| 27 | let settings = appearance::load(store); |
| 28 | let system = system_theme(cx.window_appearance()); |
| 29 | theme::set_active_scheme(settings.color_scheme.id); |
| 30 | theme::set_active_shade(settings.theme.shade(system)); |
| 31 | zoom::set_factor(settings.zoom); |
| 32 | cx.set_global(ActiveAppearance { settings, system }); |
| 33 | } |
| 34 | |
| 35 | pub fn current(cx: &App) -> AppearanceSettings { |
| 36 | cx.try_global::<ActiveAppearance>() |
| 37 | .map(|active| active.settings) |
| 38 | .unwrap_or_default() |
| 39 | } |
| 40 | |
| 41 | fn system(cx: &App) -> SystemTheme { |
| 42 | cx.try_global::<ActiveAppearance>() |
| 43 | .map(|active| active.system) |
| 44 | .unwrap_or_default() |
| 45 | } |
| 46 | |
| 47 | fn store_settings(settings: AppearanceSettings, store: &Store, cx: &mut App) { |
| 48 | appearance::save(store, &settings); |
| 49 | let system = system(cx); |
| 50 | cx.set_global(ActiveAppearance { settings, system }); |
| 51 | } |
| 52 | |
| 53 | fn restyle(cx: &mut App) { |
| 54 | let active = cx.global::<ActiveAppearance>(); |
| 55 | let shade = active.settings.theme.shade(active.system); |
| 56 | let scheme = active.settings.color_scheme.id; |
| 57 | if theme::scheme().id != scheme || theme::shade() != shade { |
| 58 | theme::set_active_scheme(scheme); |
| 59 | theme::set_active_shade(shade); |
| 60 | theme::apply(cx); |
| 61 | } |
| 62 | cx.refresh_windows(); |
| 63 | } |
| 64 | |
| 65 | pub fn set_color_scheme(scheme: &'static ColorScheme, store: &Store, cx: &mut App) { |
| 66 | let settings = AppearanceSettings { |
| 67 | color_scheme: scheme, |
| 68 | ..current(cx) |
| 69 | }; |
| 70 | store_settings(settings, store, cx); |
| 71 | restyle(cx); |
| 72 | } |
| 73 | |
| 74 | pub fn set_theme(choice: ThemeChoice, store: &Store, cx: &mut App) { |
| 75 | let settings = AppearanceSettings { |
| 76 | theme: choice, |
| 77 | ..current(cx) |
| 78 | }; |
| 79 | store_settings(settings, store, cx); |
| 80 | restyle(cx); |
| 81 | } |
| 82 | |
| 83 | pub fn system_appearance_changed(appearance: WindowAppearance, cx: &mut App) { |
| 84 | let system = system_theme(appearance); |
| 85 | if cx.try_global::<ActiveAppearance>().is_none() || self::system(cx) == system { |
| 86 | return; |
| 87 | } |
| 88 | let settings = current(cx); |
| 89 | cx.set_global(ActiveAppearance { settings, system }); |
| 90 | restyle(cx); |
| 91 | } |
| 92 | |
| 93 | pub fn set_app_icon(icon: AppIcon, store: &Store, cx: &mut App) { |
| 94 | let settings = AppearanceSettings { |
| 95 | app_icon: icon, |
| 96 | ..current(cx) |
| 97 | }; |
| 98 | store_settings(settings, store, cx); |
| 99 | cx.refresh_windows(); |
| 100 | } |
| 101 | |
| 102 | pub fn set_zoom(zoom: f32, store: &Store, cx: &mut App) -> bool { |
| 103 | let current = current(cx); |
| 104 | if (current.zoom - zoom).abs() < f32::EPSILON { |
| 105 | return false; |
| 106 | } |
| 107 | let settings = AppearanceSettings { zoom, ..current }; |
| 108 | store_settings(settings, store, cx); |
| 109 | zoom::set_factor(zoom); |
| 110 | cx.refresh_windows(); |
| 111 | true |
| 112 | } |
| 113 | |
| 114 | #[cfg(test)] |
| 115 | mod tests { |
| 116 | use gpui_kit::{TestAppContext, WindowAppearance}; |
| 117 | use store::Store; |
| 118 | |
| 119 | use super::{ |
| 120 | current, install, set_app_icon, set_color_scheme, set_theme, set_zoom, |
| 121 | system_appearance_changed, |
| 122 | }; |
| 123 | use crate::appearance::{self, AppIcon, AppearanceSettings, ThemeChoice}; |
| 124 | use crate::theme::{self, Shade}; |
| 125 | |
| 126 | #[gpui_kit::test] |
| 127 | fn switching_saves_and_applies_live(cx: &mut TestAppContext) { |
| 128 | let store = Store::open_in_memory().expect("in-memory store"); |
| 129 | cx.update(|cx| { |
| 130 | gpui_kit::init(cx); |
| 131 | install(&store, cx); |
| 132 | theme::set_thread_scheme(None); |
| 133 | theme::set_thread_shade(None); |
| 134 | assert_eq!(current(cx), AppearanceSettings::default()); |
| 135 | let teams = theme::scheme_by_id("teams").expect("teams scheme"); |
| 136 | set_color_scheme(teams, &store, cx); |
| 137 | set_app_icon(AppIcon::MicrosoftTeams, &store, cx); |
| 138 | assert_eq!(theme::scheme().id, "teams"); |
| 139 | assert_eq!(theme::accent(), theme::color(teams.dark.accent)); |
| 140 | assert_eq!(appearance::load(&store), current(cx)); |
| 141 | assert_eq!(current(cx).app_icon, AppIcon::MicrosoftTeams); |
| 142 | set_theme(ThemeChoice::Light, &store, cx); |
| 143 | assert_eq!(theme::shade(), Shade::Light); |
| 144 | assert_eq!(theme::background(), theme::color(teams.light.background)); |
| 145 | assert_eq!(appearance::load(&store).theme, ThemeChoice::Light); |
| 146 | set_color_scheme(theme::default_scheme(), &store, cx); |
| 147 | set_theme(ThemeChoice::Dark, &store, cx); |
| 148 | assert_eq!(theme::scheme().id, "rusty-teams"); |
| 149 | assert_eq!(theme::shade(), Shade::Dark); |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | #[gpui_kit::test] |
| 154 | fn follow_os_tracks_the_window_appearance_at_runtime(cx: &mut TestAppContext) { |
| 155 | let store = Store::open_in_memory().expect("in-memory store"); |
| 156 | cx.update(|cx| { |
| 157 | gpui_kit::init(cx); |
| 158 | install(&store, cx); |
| 159 | set_theme(ThemeChoice::FollowOperatingSystem, &store, cx); |
| 160 | system_appearance_changed(WindowAppearance::Dark, cx); |
| 161 | assert_eq!(theme::shade(), Shade::Dark); |
| 162 | system_appearance_changed(WindowAppearance::Light, cx); |
| 163 | assert_eq!(theme::shade(), Shade::Light); |
| 164 | set_theme(ThemeChoice::Dark, &store, cx); |
| 165 | system_appearance_changed(WindowAppearance::Light, cx); |
| 166 | assert_eq!(theme::shade(), Shade::Dark); |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | #[gpui_kit::test] |
| 171 | fn zoom_is_saved_and_unchanged_steps_report_nothing(cx: &mut TestAppContext) { |
| 172 | let store = Store::open_in_memory().expect("in-memory store"); |
| 173 | cx.update(|cx| { |
| 174 | gpui_kit::init(cx); |
| 175 | install(&store, cx); |
| 176 | assert!(set_zoom(1.25, &store, cx)); |
| 177 | assert!(!set_zoom(1.25, &store, cx)); |
| 178 | assert_eq!(appearance::load(&store).zoom, 1.25); |
| 179 | assert!(set_zoom(1., &store, cx)); |
| 180 | }); |
| 181 | } |
| 182 | } |