Skip to main content

par_term_config/config/
theme_methods.rs

1//! Theme and tab-style methods for `Config`.
2//!
3//! Covers:
4//! - Loading the active theme (`load_theme`)
5//! - Applying system dark/light theme (`apply_system_theme`)
6//! - Applying tab-style presets (`apply_tab_style`, `apply_system_tab_style`)
7
8use super::config_struct::Config;
9use crate::themes::Theme;
10use crate::types::TabStyle;
11
12impl Config {
13    /// Apply tab style preset, overwriting the tab bar color/size fields.
14    ///
15    /// This is called when the user changes `tab_style` in settings.
16    /// The `Dark` style corresponds to the existing defaults and does nothing.
17    pub fn apply_tab_style(&mut self) {
18        match self.tabs.tab_style {
19            TabStyle::Dark => {
20                // Default dark theme - restore original defaults
21                self.tab_colors.tab_bar_background = crate::defaults::tab_bar_background();
22                self.tab_colors.tab_active_background = crate::defaults::tab_active_background();
23                self.tab_colors.tab_inactive_background =
24                    crate::defaults::tab_inactive_background();
25                self.tab_colors.tab_hover_background = crate::defaults::tab_hover_background();
26                self.tab_colors.tab_active_text = crate::defaults::tab_active_text();
27                self.tab_colors.tab_inactive_text = crate::defaults::tab_inactive_text();
28                self.tab_colors.tab_active_indicator = crate::defaults::tab_active_indicator();
29                self.tab_colors.tab_border_color = crate::defaults::tab_border_color();
30                self.tab_colors.tab_border_width = crate::defaults::tab_border_width();
31                self.tabs.tab_bar_height = crate::defaults::tab_bar_height();
32            }
33            TabStyle::Light => {
34                self.tab_colors.tab_bar_background = [235, 235, 235];
35                self.tab_colors.tab_active_background = [255, 255, 255];
36                self.tab_colors.tab_inactive_background = [225, 225, 225];
37                self.tab_colors.tab_hover_background = [240, 240, 240];
38                self.tab_colors.tab_active_text = [30, 30, 30];
39                self.tab_colors.tab_inactive_text = [100, 100, 100];
40                self.tab_colors.tab_active_indicator = [50, 120, 220];
41                self.tab_colors.tab_border_color = [200, 200, 200];
42                self.tab_colors.tab_border_width = 1.0;
43                self.tabs.tab_bar_height = crate::defaults::tab_bar_height();
44            }
45            TabStyle::Compact => {
46                // Smaller tabs, tighter spacing
47                self.tab_colors.tab_bar_background = [35, 35, 35];
48                self.tab_colors.tab_active_background = [55, 55, 55];
49                self.tab_colors.tab_inactive_background = [35, 35, 35];
50                self.tab_colors.tab_hover_background = [45, 45, 45];
51                self.tab_colors.tab_active_text = [240, 240, 240];
52                self.tab_colors.tab_inactive_text = [160, 160, 160];
53                self.tab_colors.tab_active_indicator = [80, 140, 240];
54                self.tab_colors.tab_border_color = [60, 60, 60];
55                self.tab_colors.tab_border_width = 0.5;
56                self.tabs.tab_bar_height = 22.0;
57            }
58            TabStyle::Minimal => {
59                // Very clean, flat look with minimal decoration
60                self.tab_colors.tab_bar_background = [30, 30, 30];
61                self.tab_colors.tab_active_background = [30, 30, 30];
62                self.tab_colors.tab_inactive_background = [30, 30, 30];
63                self.tab_colors.tab_hover_background = [40, 40, 40];
64                self.tab_colors.tab_active_text = [255, 255, 255];
65                self.tab_colors.tab_inactive_text = [120, 120, 120];
66                self.tab_colors.tab_active_indicator = [100, 150, 255];
67                self.tab_colors.tab_border_color = [30, 30, 30]; // No visible border
68                self.tab_colors.tab_border_width = 0.0;
69                self.tabs.tab_bar_height = 26.0;
70            }
71            TabStyle::HighContrast => {
72                // Maximum contrast for accessibility
73                self.tab_colors.tab_bar_background = [0, 0, 0];
74                self.tab_colors.tab_active_background = [255, 255, 255];
75                self.tab_colors.tab_inactive_background = [30, 30, 30];
76                self.tab_colors.tab_hover_background = [60, 60, 60];
77                self.tab_colors.tab_active_text = [0, 0, 0];
78                self.tab_colors.tab_inactive_text = [255, 255, 255];
79                self.tab_colors.tab_active_indicator = [255, 255, 0];
80                self.tab_colors.tab_border_color = [255, 255, 255];
81                self.tab_colors.tab_border_width = 2.0;
82                self.tabs.tab_bar_height = 30.0;
83            }
84            TabStyle::Automatic => {
85                // No-op here: actual style is resolved and applied by apply_system_tab_style()
86            }
87        }
88    }
89
90    /// Load theme configuration
91    ///
92    /// An unrecognized `theme` name falls back to the default theme; the fallback is
93    /// logged so a typo in the config does not silently produce the wrong colors.
94    pub fn load_theme(&self) -> Theme {
95        match Theme::by_name(&self.theme_colors.theme) {
96            Some(theme) => theme,
97            None => {
98                log::warn!(
99                    "Unknown theme '{}', falling back to the default theme. Available themes: {}",
100                    self.theme_colors.theme,
101                    Theme::available_themes().join(", ")
102                );
103                Theme::default()
104            }
105        }
106    }
107
108    /// Apply system theme if auto_dark_mode is enabled.
109    /// Returns true if the theme was changed.
110    pub fn apply_system_theme(&mut self, is_dark: bool) -> bool {
111        if !self.theme_colors.auto_dark_mode {
112            return false;
113        }
114        let new_theme = if is_dark {
115            &self.theme_colors.dark_theme
116        } else {
117            &self.theme_colors.light_theme
118        };
119        if self.theme_colors.theme != *new_theme {
120            self.theme_colors.theme = new_theme.clone();
121            true
122        } else {
123            false
124        }
125    }
126
127    /// Apply tab style based on system theme when tab_style is Automatic.
128    /// Returns true if the style was applied.
129    pub fn apply_system_tab_style(&mut self, is_dark: bool) -> bool {
130        if self.tabs.tab_style != TabStyle::Automatic {
131            return false;
132        }
133        let target = if is_dark {
134            self.tabs.dark_tab_style
135        } else {
136            self.tabs.light_tab_style
137        };
138        // Temporarily set to concrete style, apply colors, then restore Automatic
139        self.tabs.tab_style = target;
140        self.apply_tab_style();
141        self.tabs.tab_style = TabStyle::Automatic;
142        true
143    }
144}