1use crate::{
2 Appearance, DEFAULT_ICON_THEME_NAME, SyntaxTheme, Theme, status_colors_refinement,
3 syntax_overrides, theme_colors_refinement,
4};
5use collections::HashMap;
6use derive_more::{Deref, DerefMut};
7use gpui::{
8 App, Context, Font, FontFallbacks, FontStyle, FontWeight, Global, Pixels, Subscription, Window,
9 px,
10};
11use refineable::Refineable;
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14pub use settings::{FontFamilyName, IconThemeName, ThemeAppearanceMode, ThemeName};
15use settings::{RegisterSetting, Settings, SettingsContent};
16use std::sync::Arc;
17
18const MIN_FONT_SIZE: Pixels = px(6.0);
19const MAX_FONT_SIZE: Pixels = px(100.0);
20const MIN_LINE_HEIGHT: f32 = 1.0;
21
22#[derive(
23 Debug,
24 Default,
25 PartialEq,
26 Eq,
27 PartialOrd,
28 Ord,
29 Hash,
30 Clone,
31 Copy,
32 Serialize,
33 Deserialize,
34 JsonSchema,
35)]
36
37/// Specifies the density of the UI.
38/// Note: This setting is still experimental. See [this tracking issue](https://github.com/zed-industries/zed/issues/18078)
39#[serde(rename_all = "snake_case")]
40pub enum UiDensity {
41 /// A denser UI with tighter spacing and smaller elements.
42 #[serde(alias = "compact")]
43 Compact,
44 #[default]
45 #[serde(alias = "default")]
46 /// The default UI density.
47 Default,
48 #[serde(alias = "comfortable")]
49 /// A looser UI with more spacing and larger elements.
50 Comfortable,
51}
52
53impl UiDensity {
54 /// The spacing ratio of a given density.
55 /// TODO: Standardize usage throughout the app or remove
56 pub fn spacing_ratio(self) -> f32 {
57 match self {
58 UiDensity::Compact => 0.75,
59 UiDensity::Default => 1.0,
60 UiDensity::Comfortable => 1.25,
61 }
62 }
63}
64
65impl From<String> for UiDensity {
66 fn from(s: String) -> Self {
67 match s.as_str() {
68 "compact" => Self::Compact,
69 "default" => Self::Default,
70 "comfortable" => Self::Comfortable,
71 _ => Self::default(),
72 }
73 }
74}
75
76impl From<UiDensity> for String {
77 fn from(val: UiDensity) -> Self {
78 match val {
79 UiDensity::Compact => "compact".to_string(),
80 UiDensity::Default => "default".to_string(),
81 UiDensity::Comfortable => "comfortable".to_string(),
82 }
83 }
84}
85
86impl From<settings::UiDensity> for UiDensity {
87 fn from(val: settings::UiDensity) -> Self {
88 match val {
89 settings::UiDensity::Compact => Self::Compact,
90 settings::UiDensity::Default => Self::Default,
91 settings::UiDensity::Comfortable => Self::Comfortable,
92 }
93 }
94}
95
96/// Customizable settings for the UI and theme system.
97#[derive(Clone, PartialEq, RegisterSetting)]
98pub struct ThemeSettings {
99 /// The UI font size. Determines the size of text in the UI,
100 /// as well as the size of a [gpui::Rems] unit.
101 ///
102 /// Changing this will impact the size of all UI elements.
103 ui_font_size: Pixels,
104 /// The font used for UI elements.
105 pub ui_font: Font,
106 /// The font size used for buffers, and the terminal.
107 ///
108 /// The terminal font size can be overridden using it's own setting.
109 buffer_font_size: Pixels,
110 /// The font used for buffers, and the terminal.
111 ///
112 /// The terminal font family can be overridden using it's own setting.
113 pub buffer_font: Font,
114 /// The agent font size. Determines the size of text in the agent panel. Falls back to the UI font size if unset.
115 agent_ui_font_size: Option<Pixels>,
116 /// The agent buffer font size. Determines the size of user messages in the agent panel.
117 agent_buffer_font_size: Option<Pixels>,
118 /// The line height for buffers, and the terminal.
119 ///
120 /// Changing this may affect the spacing of some UI elements.
121 ///
122 /// The terminal font family can be overridden using it's own setting.
123 pub buffer_line_height: BufferLineHeight,
124 /// The current theme selection.
125 pub theme: ThemeSelection,
126 /// Manual overrides for the active theme.
127 ///
128 /// Note: This setting is still experimental. See [this tracking issue](https://github.com/zed-industries/zed/issues/18078)
129 pub experimental_theme_overrides: Option<settings::ThemeStyleContent>,
130 /// Manual overrides per theme
131 pub theme_overrides: HashMap<String, settings::ThemeStyleContent>,
132 /// The current icon theme selection.
133 pub icon_theme: IconThemeSelection,
134 /// The density of the UI.
135 /// Note: This setting is still experimental. See [this tracking issue](
136 pub ui_density: UiDensity,
137 /// The amount of fading applied to unnecessary code.
138 pub unnecessary_code_fade: f32,
139}
140
141pub(crate) const DEFAULT_LIGHT_THEME: &'static str = "One Light";
142pub(crate) const DEFAULT_DARK_THEME: &'static str = "One Dark";
143
144/// Returns the name of the default theme for the given [`Appearance`].
145pub fn default_theme(appearance: Appearance) -> &'static str {
146 match appearance {
147 Appearance::Light => DEFAULT_LIGHT_THEME,
148 Appearance::Dark => DEFAULT_DARK_THEME,
149 }
150}
151
152/// The appearance of the system.
153#[derive(Debug, Clone, Copy, Deref)]
154pub struct SystemAppearance(pub Appearance);
155
156impl Default for SystemAppearance {
157 fn default() -> Self {
158 Self(Appearance::Dark)
159 }
160}
161
162#[derive(Deref, DerefMut, Default)]
163struct GlobalSystemAppearance(SystemAppearance);
164
165impl Global for GlobalSystemAppearance {}
166
167impl SystemAppearance {
168 /// Initializes the [`SystemAppearance`] for the application.
169 pub fn init(cx: &mut App) {
170 *cx.default_global::<GlobalSystemAppearance>() =
171 GlobalSystemAppearance(SystemAppearance(cx.window_appearance().into()));
172 }
173
174 /// Returns the global [`SystemAppearance`].
175 pub fn global(cx: &App) -> Self {
176 cx.global::<GlobalSystemAppearance>().0
177 }
178
179 /// Returns a mutable reference to the global [`SystemAppearance`].
180 pub fn global_mut(cx: &mut App) -> &mut Self {
181 cx.global_mut::<GlobalSystemAppearance>()
182 }
183}
184
185#[derive(Default)]
186struct BufferFontSize(Pixels);
187
188impl Global for BufferFontSize {}
189
190#[derive(Default)]
191pub(crate) struct UiFontSize(Pixels);
192
193impl Global for UiFontSize {}
194
195/// In-memory override for the font size in the agent panel.
196#[derive(Default)]
197pub struct AgentFontSize(Pixels);
198
199impl Global for AgentFontSize {}
200
201/// Represents the selection of a theme, which can be either static or dynamic.
202#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
203#[serde(untagged)]
204pub enum ThemeSelection {
205 /// A static theme selection, represented by a single theme name.
206 Static(ThemeName),
207 /// A dynamic theme selection, which can change based the [ThemeMode].
208 Dynamic {
209 /// The mode used to determine which theme to use.
210 #[serde(default)]
211 mode: ThemeAppearanceMode,
212 /// The theme to use for light mode.
213 light: ThemeName,
214 /// The theme to use for dark mode.
215 dark: ThemeName,
216 },
217}
218
219impl From<settings::ThemeSelection> for ThemeSelection {
220 fn from(selection: settings::ThemeSelection) -> Self {
221 match selection {
222 settings::ThemeSelection::Static(theme) => ThemeSelection::Static(theme),
223 settings::ThemeSelection::Dynamic { mode, light, dark } => {
224 ThemeSelection::Dynamic { mode, light, dark }
225 }
226 }
227 }
228}
229
230impl ThemeSelection {
231 /// Returns the theme name for the selected [ThemeMode].
232 pub fn name(&self, system_appearance: Appearance) -> ThemeName {
233 match self {
234 Self::Static(theme) => theme.clone(),
235 Self::Dynamic { mode, light, dark } => match mode {
236 ThemeAppearanceMode::Light => light.clone(),
237 ThemeAppearanceMode::Dark => dark.clone(),
238 ThemeAppearanceMode::System => match system_appearance {
239 Appearance::Light => light.clone(),
240 Appearance::Dark => dark.clone(),
241 },
242 },
243 }
244 }
245
246 /// Returns the [ThemeMode] for the [ThemeSelection].
247 pub fn mode(&self) -> Option<ThemeAppearanceMode> {
248 match self {
249 ThemeSelection::Static(_) => None,
250 ThemeSelection::Dynamic { mode, .. } => Some(*mode),
251 }
252 }
253}
254
255/// Represents the selection of an icon theme, which can be either static or dynamic.
256#[derive(Clone, Debug, PartialEq, Eq)]
257pub enum IconThemeSelection {
258 /// A static icon theme selection, represented by a single icon theme name.
259 Static(IconThemeName),
260 /// A dynamic icon theme selection, which can change based on the [`ThemeMode`].
261 Dynamic {
262 /// The mode used to determine which theme to use.
263 mode: ThemeAppearanceMode,
264 /// The icon theme to use for light mode.
265 light: IconThemeName,
266 /// The icon theme to use for dark mode.
267 dark: IconThemeName,
268 },
269}
270
271impl From<settings::IconThemeSelection> for IconThemeSelection {
272 fn from(selection: settings::IconThemeSelection) -> Self {
273 match selection {
274 settings::IconThemeSelection::Static(theme) => IconThemeSelection::Static(theme),
275 settings::IconThemeSelection::Dynamic { mode, light, dark } => {
276 IconThemeSelection::Dynamic { mode, light, dark }
277 }
278 }
279 }
280}
281
282impl IconThemeSelection {
283 /// Returns the icon theme name based on the given [`Appearance`].
284 pub fn name(&self, system_appearance: Appearance) -> IconThemeName {
285 match self {
286 Self::Static(theme) => theme.clone(),
287 Self::Dynamic { mode, light, dark } => match mode {
288 ThemeAppearanceMode::Light => light.clone(),
289 ThemeAppearanceMode::Dark => dark.clone(),
290 ThemeAppearanceMode::System => match system_appearance {
291 Appearance::Light => light.clone(),
292 Appearance::Dark => dark.clone(),
293 },
294 },
295 }
296 }
297
298 /// Returns the [`ThemeMode`] for the [`IconThemeSelection`].
299 pub fn mode(&self) -> Option<ThemeAppearanceMode> {
300 match self {
301 IconThemeSelection::Static(_) => None,
302 IconThemeSelection::Dynamic { mode, .. } => Some(*mode),
303 }
304 }
305}
306
307/// Sets the theme for the given appearance to the theme with the specified name.
308///
309/// The caller should make sure that the [`Appearance`] matches the theme associated with the name.
310///
311/// If the current [`ThemeAppearanceMode`] is set to [`System`] and the user's system [`Appearance`]
312/// is different than the new theme's [`Appearance`], this function will update the
313/// [`ThemeAppearanceMode`] to the new theme's appearance in order to display the new theme.
314///
315/// [`System`]: ThemeAppearanceMode::System
316pub fn set_theme(
317 current: &mut SettingsContent,
318 theme_name: impl Into<Arc<str>>,
319 theme_appearance: Appearance,
320 system_appearance: Appearance,
321) {
322 let theme_name = ThemeName(theme_name.into());
323
324 let Some(selection) = current.theme.theme.as_mut() else {
325 current.theme.theme = Some(settings::ThemeSelection::Static(theme_name));
326 return;
327 };
328
329 match selection {
330 settings::ThemeSelection::Static(theme) => {
331 *theme = theme_name;
332 }
333 settings::ThemeSelection::Dynamic { mode, light, dark } => {
334 // Update the appropriate theme slot based on appearance.
335 match theme_appearance {
336 Appearance::Light => *light = theme_name,
337 Appearance::Dark => *dark = theme_name,
338 }
339
340 // Don't update the theme mode if it is set to system and the new theme has the same
341 // appearance.
342 let should_update_mode =
343 !(mode == &ThemeAppearanceMode::System && theme_appearance == system_appearance);
344
345 if should_update_mode {
346 // Update the mode to the specified appearance (otherwise we might set the theme and
347 // nothing gets updated because the system specified the other mode appearance).
348 *mode = ThemeAppearanceMode::from(theme_appearance);
349 }
350 }
351 }
352}
353
354/// Sets the icon theme for the given appearance to the icon theme with the specified name.
355pub fn set_icon_theme(
356 current: &mut SettingsContent,
357 icon_theme_name: IconThemeName,
358 appearance: Appearance,
359) {
360 if let Some(selection) = current.theme.icon_theme.as_mut() {
361 let icon_theme_to_update = match selection {
362 settings::IconThemeSelection::Static(theme) => theme,
363 settings::IconThemeSelection::Dynamic { mode, light, dark } => match mode {
364 ThemeAppearanceMode::Light => light,
365 ThemeAppearanceMode::Dark => dark,
366 ThemeAppearanceMode::System => match appearance {
367 Appearance::Light => light,
368 Appearance::Dark => dark,
369 },
370 },
371 };
372
373 *icon_theme_to_update = icon_theme_name;
374 } else {
375 current.theme.icon_theme = Some(settings::IconThemeSelection::Static(icon_theme_name));
376 }
377}
378
379/// Sets the mode for the theme.
380pub fn set_mode(content: &mut SettingsContent, mode: ThemeAppearanceMode) {
381 let theme = content.theme.as_mut();
382
383 if let Some(selection) = theme.theme.as_mut() {
384 match selection {
385 settings::ThemeSelection::Static(theme) => {
386 // If the theme was previously set to a single static theme,
387 // we don't know whether it was a light or dark theme, so we
388 // just use it for both.
389 *selection = settings::ThemeSelection::Dynamic {
390 mode,
391 light: theme.clone(),
392 dark: theme.clone(),
393 };
394 }
395 settings::ThemeSelection::Dynamic {
396 mode: mode_to_update,
397 ..
398 } => *mode_to_update = mode,
399 }
400 } else {
401 theme.theme = Some(settings::ThemeSelection::Dynamic {
402 mode,
403 light: ThemeName(DEFAULT_LIGHT_THEME.into()),
404 dark: ThemeName(DEFAULT_DARK_THEME.into()),
405 });
406 }
407
408 if let Some(selection) = theme.icon_theme.as_mut() {
409 match selection {
410 settings::IconThemeSelection::Static(icon_theme) => {
411 // If the icon theme was previously set to a single static
412 // theme, we don't know whether it was a light or dark
413 // theme, so we just use it for both.
414 *selection = settings::IconThemeSelection::Dynamic {
415 mode,
416 light: icon_theme.clone(),
417 dark: icon_theme.clone(),
418 };
419 }
420 settings::IconThemeSelection::Dynamic {
421 mode: mode_to_update,
422 ..
423 } => *mode_to_update = mode,
424 }
425 } else {
426 theme.icon_theme = Some(settings::IconThemeSelection::Static(IconThemeName(
427 DEFAULT_ICON_THEME_NAME.into(),
428 )));
429 }
430}
431// }
432
433/// The buffer's line height.
434#[derive(Clone, Copy, Debug, PartialEq, Default)]
435pub enum BufferLineHeight {
436 /// A less dense line height.
437 #[default]
438 Comfortable,
439 /// The default line height.
440 Standard,
441 /// A custom line height, where 1.0 is the font's height. Must be at least 1.0.
442 Custom(f32),
443}
444
445impl From<settings::BufferLineHeight> for BufferLineHeight {
446 fn from(value: settings::BufferLineHeight) -> Self {
447 match value {
448 settings::BufferLineHeight::Comfortable => BufferLineHeight::Comfortable,
449 settings::BufferLineHeight::Standard => BufferLineHeight::Standard,
450 settings::BufferLineHeight::Custom(line_height) => {
451 BufferLineHeight::Custom(line_height)
452 }
453 }
454 }
455}
456
457impl BufferLineHeight {
458 /// Returns the value of the line height.
459 pub fn value(&self) -> f32 {
460 match self {
461 BufferLineHeight::Comfortable => 1.618,
462 BufferLineHeight::Standard => 1.3,
463 BufferLineHeight::Custom(line_height) => *line_height,
464 }
465 }
466}
467
468impl ThemeSettings {
469 /// Returns the buffer font size.
470 pub fn buffer_font_size(&self, cx: &App) -> Pixels {
471 let font_size = cx
472 .try_global::<BufferFontSize>()
473 .map(|size| size.0)
474 .unwrap_or(self.buffer_font_size);
475 clamp_font_size(font_size)
476 }
477
478 /// Returns the UI font size.
479 pub fn ui_font_size(&self, cx: &App) -> Pixels {
480 let font_size = cx
481 .try_global::<UiFontSize>()
482 .map(|size| size.0)
483 .unwrap_or(self.ui_font_size);
484 clamp_font_size(font_size)
485 }
486
487 /// Returns the agent panel font size. Falls back to the UI font size if unset.
488 pub fn agent_ui_font_size(&self, cx: &App) -> Pixels {
489 cx.try_global::<AgentFontSize>()
490 .map(|size| size.0)
491 .or(self.agent_ui_font_size)
492 .map(clamp_font_size)
493 .unwrap_or_else(|| self.ui_font_size(cx))
494 }
495
496 /// Returns the agent panel buffer font size.
497 pub fn agent_buffer_font_size(&self, cx: &App) -> Pixels {
498 cx.try_global::<AgentFontSize>()
499 .map(|size| size.0)
500 .or(self.agent_buffer_font_size)
501 .map(clamp_font_size)
502 .unwrap_or_else(|| self.buffer_font_size(cx))
503 }
504
505 /// Returns the buffer font size, read from the settings.
506 ///
507 /// The real buffer font size is stored in-memory, to support temporary font size changes.
508 /// Use [`Self::buffer_font_size`] to get the real font size.
509 pub fn buffer_font_size_settings(&self) -> Pixels {
510 self.buffer_font_size
511 }
512
513 /// Returns the UI font size, read from the settings.
514 ///
515 /// The real UI font size is stored in-memory, to support temporary font size changes.
516 /// Use [`Self::ui_font_size`] to get the real font size.
517 pub fn ui_font_size_settings(&self) -> Pixels {
518 self.ui_font_size
519 }
520
521 /// Returns the agent font size, read from the settings.
522 ///
523 /// The real agent font size is stored in-memory, to support temporary font size changes.
524 /// Use [`Self::agent_ui_font_size`] to get the real font size.
525 pub fn agent_ui_font_size_settings(&self) -> Option<Pixels> {
526 self.agent_ui_font_size
527 }
528
529 /// Returns the agent buffer font size, read from the settings.
530 ///
531 /// The real agent buffer font size is stored in-memory, to support temporary font size changes.
532 /// Use [`Self::agent_buffer_font_size`] to get the real font size.
533 pub fn agent_buffer_font_size_settings(&self) -> Option<Pixels> {
534 self.agent_buffer_font_size
535 }
536
537 // TODO: Rename: `line_height` -> `buffer_line_height`
538 /// Returns the buffer's line height.
539 pub fn line_height(&self) -> f32 {
540 f32::max(self.buffer_line_height.value(), MIN_LINE_HEIGHT)
541 }
542
543 /// Applies the theme overrides, if there are any, to the current theme.
544 pub fn apply_theme_overrides(&self, mut arc_theme: Arc<Theme>) -> Arc<Theme> {
545 // Apply the old overrides setting first, so that the new setting can override those.
546 if let Some(experimental_theme_overrides) = &self.experimental_theme_overrides {
547 let mut theme = (*arc_theme).clone();
548 ThemeSettings::modify_theme(&mut theme, experimental_theme_overrides);
549 arc_theme = Arc::new(theme);
550 }
551
552 if let Some(theme_overrides) = self.theme_overrides.get(arc_theme.name.as_ref()) {
553 let mut theme = (*arc_theme).clone();
554 ThemeSettings::modify_theme(&mut theme, theme_overrides);
555 arc_theme = Arc::new(theme);
556 }
557
558 arc_theme
559 }
560
561 fn modify_theme(base_theme: &mut Theme, theme_overrides: &settings::ThemeStyleContent) {
562 if let Some(window_background_appearance) = theme_overrides.window_background_appearance {
563 base_theme.styles.window_background_appearance = window_background_appearance.into();
564 }
565 let status_color_refinement = status_colors_refinement(&theme_overrides.status);
566
567 base_theme.styles.colors.refine(&theme_colors_refinement(
568 &theme_overrides.colors,
569 &status_color_refinement,
570 ));
571 base_theme.styles.status.refine(&status_color_refinement);
572 base_theme.styles.player.merge(&theme_overrides.players);
573 base_theme.styles.accents.merge(&theme_overrides.accents);
574 base_theme.styles.syntax = SyntaxTheme::merge(
575 base_theme.styles.syntax.clone(),
576 syntax_overrides(&theme_overrides),
577 );
578 }
579}
580
581/// Observe changes to the adjusted buffer font size.
582pub fn observe_buffer_font_size_adjustment<V: 'static>(
583 cx: &mut Context<V>,
584 f: impl 'static + Fn(&mut V, &mut Context<V>),
585) -> Subscription {
586 cx.observe_global::<BufferFontSize>(f)
587}
588
589/// Gets the font size, adjusted by the difference between the current buffer font size and the one set in the settings.
590pub fn adjusted_font_size(size: Pixels, cx: &App) -> Pixels {
591 let adjusted_font_size =
592 if let Some(BufferFontSize(adjusted_size)) = cx.try_global::<BufferFontSize>() {
593 let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
594 let delta = *adjusted_size - buffer_font_size;
595 size + delta
596 } else {
597 size
598 };
599 clamp_font_size(adjusted_font_size)
600}
601
602/// Adjusts the buffer font size.
603pub fn adjust_buffer_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
604 let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
605 let adjusted_size = cx
606 .try_global::<BufferFontSize>()
607 .map_or(buffer_font_size, |adjusted_size| adjusted_size.0);
608 cx.set_global(BufferFontSize(clamp_font_size(f(adjusted_size))));
609 cx.refresh_windows();
610}
611
612/// Resets the buffer font size to the default value.
613pub fn reset_buffer_font_size(cx: &mut App) {
614 if cx.has_global::<BufferFontSize>() {
615 cx.remove_global::<BufferFontSize>();
616 cx.refresh_windows();
617 }
618}
619
620// TODO: Make private, change usages to use `get_ui_font_size` instead.
621#[allow(missing_docs)]
622pub fn setup_ui_font(window: &mut Window, cx: &mut App) -> gpui::Font {
623 let (ui_font, ui_font_size) = {
624 let theme_settings = ThemeSettings::get_global(cx);
625 let font = theme_settings.ui_font.clone();
626 (font, theme_settings.ui_font_size(cx))
627 };
628
629 window.set_rem_size(ui_font_size);
630 ui_font
631}
632
633/// Sets the adjusted UI font size.
634pub fn adjust_ui_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
635 let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
636 let adjusted_size = cx
637 .try_global::<UiFontSize>()
638 .map_or(ui_font_size, |adjusted_size| adjusted_size.0);
639 cx.set_global(UiFontSize(clamp_font_size(f(adjusted_size))));
640 cx.refresh_windows();
641}
642
643/// Resets the UI font size to the default value.
644pub fn reset_ui_font_size(cx: &mut App) {
645 if cx.has_global::<UiFontSize>() {
646 cx.remove_global::<UiFontSize>();
647 cx.refresh_windows();
648 }
649}
650
651/// Sets the adjusted font size of agent responses in the agent panel.
652pub fn adjust_agent_ui_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
653 let agent_ui_font_size = ThemeSettings::get_global(cx).agent_ui_font_size(cx);
654 let adjusted_size = cx
655 .try_global::<AgentFontSize>()
656 .map_or(agent_ui_font_size, |adjusted_size| adjusted_size.0);
657 cx.set_global(AgentFontSize(clamp_font_size(f(adjusted_size))));
658 cx.refresh_windows();
659}
660
661/// Resets the agent response font size in the agent panel to the default value.
662pub fn reset_agent_ui_font_size(cx: &mut App) {
663 if cx.has_global::<AgentFontSize>() {
664 cx.remove_global::<AgentFontSize>();
665 cx.refresh_windows();
666 }
667}
668
669/// Sets the adjusted font size of user messages in the agent panel.
670pub fn adjust_agent_buffer_font_size(cx: &mut App, f: impl FnOnce(Pixels) -> Pixels) {
671 let agent_buffer_font_size = ThemeSettings::get_global(cx).agent_buffer_font_size(cx);
672 let adjusted_size = cx
673 .try_global::<AgentFontSize>()
674 .map_or(agent_buffer_font_size, |adjusted_size| adjusted_size.0);
675 cx.set_global(AgentFontSize(clamp_font_size(f(adjusted_size))));
676 cx.refresh_windows();
677}
678
679/// Resets the user message font size in the agent panel to the default value.
680pub fn reset_agent_buffer_font_size(cx: &mut App) {
681 if cx.has_global::<AgentFontSize>() {
682 cx.remove_global::<AgentFontSize>();
683 cx.refresh_windows();
684 }
685}
686
687/// Ensures font size is within the valid range.
688pub fn clamp_font_size(size: Pixels) -> Pixels {
689 size.clamp(MIN_FONT_SIZE, MAX_FONT_SIZE)
690}
691
692fn clamp_font_weight(weight: f32) -> FontWeight {
693 FontWeight(weight.clamp(100., 950.))
694}
695
696/// font fallback from settings
697pub fn font_fallbacks_from_settings(
698 fallbacks: Option<Vec<settings::FontFamilyName>>,
699) -> Option<FontFallbacks> {
700 fallbacks.map(|fallbacks| {
701 FontFallbacks::from_fonts(
702 fallbacks
703 .into_iter()
704 .map(|font_family| font_family.0.to_string())
705 .collect(),
706 )
707 })
708}
709
710impl settings::Settings for ThemeSettings {
711 fn from_settings(content: &settings::SettingsContent) -> Self {
712 let content = &content.theme;
713 let theme_selection: ThemeSelection = content.theme.clone().unwrap().into();
714 let icon_theme_selection: IconThemeSelection = content.icon_theme.clone().unwrap().into();
715 Self {
716 ui_font_size: clamp_font_size(content.ui_font_size.unwrap().into()),
717 ui_font: Font {
718 family: content.ui_font_family.as_ref().unwrap().0.clone().into(),
719 features: content.ui_font_features.clone().unwrap(),
720 fallbacks: font_fallbacks_from_settings(content.ui_font_fallbacks.clone()),
721 weight: clamp_font_weight(content.ui_font_weight.unwrap().0),
722 style: Default::default(),
723 },
724 buffer_font: Font {
725 family: content
726 .buffer_font_family
727 .as_ref()
728 .unwrap()
729 .0
730 .clone()
731 .into(),
732 features: content.buffer_font_features.clone().unwrap(),
733 fallbacks: font_fallbacks_from_settings(content.buffer_font_fallbacks.clone()),
734 weight: clamp_font_weight(content.buffer_font_weight.unwrap().0),
735 style: FontStyle::default(),
736 },
737 buffer_font_size: clamp_font_size(content.buffer_font_size.unwrap().into()),
738 buffer_line_height: content.buffer_line_height.unwrap().into(),
739 agent_ui_font_size: content.agent_ui_font_size.map(Into::into),
740 agent_buffer_font_size: content.agent_buffer_font_size.map(Into::into),
741 theme: theme_selection,
742 experimental_theme_overrides: content.experimental_theme_overrides.clone(),
743 theme_overrides: content.theme_overrides.clone(),
744 icon_theme: icon_theme_selection,
745 ui_density: content.ui_density.unwrap_or_default().into(),
746 unnecessary_code_fade: content.unnecessary_code_fade.unwrap().0.clamp(0.0, 0.9),
747 }
748 }
749}