fix: avoid re-reading system theme from disk on every menu bar render

This commit is contained in:
James Feister 2026-06-12 13:47:00 -07:00 committed by GitHub
parent 8c042673e7
commit 18b4450c0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 15 deletions

View file

@ -290,13 +290,19 @@ impl Config {
} }
} }
pub fn color_scheme_kind(&self) -> ColorSchemeKind { pub fn color_scheme_kind(&self, system_theme: &theme::Theme) -> ColorSchemeKind {
if self.app_theme.theme().theme_type.is_dark() { match self.app_theme {
AppTheme::Dark => ColorSchemeKind::Dark,
AppTheme::Light => ColorSchemeKind::Light,
AppTheme::System => {
if system_theme.theme_type.is_dark() {
ColorSchemeKind::Dark ColorSchemeKind::Dark
} else { } else {
ColorSchemeKind::Light ColorSchemeKind::Light
} }
} }
}
}
// Get a sorted and adjusted for duplicates list of color scheme names and ids // Get a sorted and adjusted for duplicates list of color scheme names and ids
pub fn color_scheme_names( pub fn color_scheme_names(
@ -358,8 +364,11 @@ impl Config {
} }
// Get current syntax theme based on dark mode // Get current syntax theme based on dark mode
pub fn syntax_theme(&self, profile_id_opt: Option<ProfileId>) -> (String, ColorSchemeKind) { pub fn syntax_theme(
let color_scheme_kind = self.color_scheme_kind(); &self,
color_scheme_kind: ColorSchemeKind,
profile_id_opt: Option<ProfileId>,
) -> (String, ColorSchemeKind) {
let theme_name = match profile_id_opt.and_then(|profile_id| self.profiles.get(&profile_id)) let theme_name = match profile_id_opt.and_then(|profile_id| self.profiles.get(&profile_id))
{ {
Some(profile) => match color_scheme_kind { Some(profile) => match color_scheme_kind {

View file

@ -688,11 +688,12 @@ impl App {
} }
// Set config of all tabs // Set config of all tabs
let color_scheme_kind = self.config.color_scheme_kind(&theme);
for (_pane, tab_model) in self.pane_model.panes.iter() { for (_pane, tab_model) in self.pane_model.panes.iter() {
for entity in tab_model.iter() { for entity in tab_model.iter() {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) { if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let mut terminal = terminal.lock().unwrap(); let mut terminal = terminal.lock().unwrap();
terminal.set_config(&self.config, &self.themes); terminal.set_config(&self.config, color_scheme_kind, &self.themes);
} }
} }
} }
@ -708,6 +709,7 @@ impl App {
// skip writing config to fs when zoom in/ out // skip writing config to fs when zoom in/ out
// recalculate the pane due to the changes of zoom_adj value // recalculate the pane due to the changes of zoom_adj value
// but only for the active pane/tab // but only for the active pane/tab
let color_scheme_kind = self.config.color_scheme_kind(self.core.system_theme());
if let Some(tab_model) = self.pane_model.active() { if let Some(tab_model) = self.pane_model.active() {
for entity in tab_model.iter() { for entity in tab_model.iter() {
if tab_model.is_active(entity) if tab_model.is_active(entity)
@ -724,7 +726,7 @@ impl App {
} }
_ => {} _ => {}
} }
terminal.set_config(&self.config, &self.themes); terminal.set_config(&self.config, color_scheme_kind, &self.themes);
} }
} }
} }
@ -1552,10 +1554,11 @@ impl App {
self.pane_model.set_focus(pane); self.pane_model.set_focus(pane);
match &self.term_event_tx_opt { match &self.term_event_tx_opt {
Some(term_event_tx) => { Some(term_event_tx) => {
let color_scheme_kind = self.config.color_scheme_kind(self.core.system_theme());
let colors = self let colors = self
.themes .themes
.get(&self.config.syntax_theme(profile_id_opt)) .get(&self.config.syntax_theme(color_scheme_kind, profile_id_opt))
.or_else(|| match self.config.color_scheme_kind() { .or_else(|| match color_scheme_kind {
ColorSchemeKind::Dark => self ColorSchemeKind::Dark => self
.themes .themes
.get(&(config::COSMIC_THEME_DARK.to_string(), ColorSchemeKind::Dark)), .get(&(config::COSMIC_THEME_DARK.to_string(), ColorSchemeKind::Dark)),
@ -1632,7 +1635,11 @@ impl App {
tab_title_override, tab_title_override,
) { ) {
Ok(mut terminal) => { Ok(mut terminal) => {
terminal.set_config(&self.config, &self.themes); terminal.set_config(
&self.config,
color_scheme_kind,
&self.themes,
);
tab_model tab_model
.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal)); .data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} }
@ -1671,7 +1678,7 @@ impl App {
None => { None => {
log::error!( log::error!(
"failed to find terminal theme {:?}", "failed to find terminal theme {:?}",
self.config.syntax_theme(profile_id_opt) self.config.syntax_theme(color_scheme_kind, profile_id_opt)
); );
//TODO: fall back to known good theme //TODO: fall back to known good theme
} }

View file

@ -206,6 +206,8 @@ pub fn menu_bar<'a>(
//TODO: what to do if there are no profiles? //TODO: what to do if there are no profiles?
let color_scheme_kind = config.color_scheme_kind(core.system_theme());
responsive_menu_bar() responsive_menu_bar()
.item_height(ItemHeight::Dynamic(40)) .item_height(ItemHeight::Dynamic(40))
.item_width(ItemWidth::Uniform(320)) .item_width(ItemWidth::Uniform(320))
@ -267,7 +269,7 @@ pub fn menu_bar<'a>(
MenuItem::Button( MenuItem::Button(
fl!("menu-color-schemes"), fl!("menu-color-schemes"),
None, None,
Action::ColorSchemes(config.color_scheme_kind()), Action::ColorSchemes(color_scheme_kind),
), ),
MenuItem::Button( MenuItem::Button(
fl!("menu-keyboard-shortcuts"), fl!("menu-keyboard-shortcuts"),

View file

@ -613,6 +613,7 @@ impl Terminal {
pub fn set_config( pub fn set_config(
&mut self, &mut self,
config: &AppConfig, config: &AppConfig,
color_scheme_kind: ColorSchemeKind,
themes: &HashMap<(String, ColorSchemeKind), Colors>, themes: &HashMap<(String, ColorSchemeKind), Colors>,
) { ) {
let mut update_cell_size = false; let mut update_cell_size = false;
@ -655,7 +656,9 @@ impl Terminal {
update_cell_size = true; update_cell_size = true;
} }
if let Some(colors) = themes.get(&config.syntax_theme(self.profile_id_opt)) { if let Some(colors) =
themes.get(&config.syntax_theme(color_scheme_kind, self.profile_id_opt))
{
let mut changed = false; let mut changed = false;
for i in 0..color::COUNT { for i in 0..color::COUNT {
if self.colors[i] != colors[i] { if self.colors[i] != colors[i] {