Skip to content

Commit

Permalink
Only update default colors when needed, and update() when it's done
Browse files Browse the repository at this point in the history
 With commit 7c5d544, it became
 possible that `update_colors()` may run without being immediately
 followed by an `update()`. But `update_colors()` creates a new
 `metadata_set`, which means `attr_list` may now contain out-of-date
 metadata indices to the old cleared `metadata_set`.

 With this commit:
 * A `metadata_set` is only cleared and recreated in `update_colors()`
   if default colors have actually changed.
 * A `bool` is returned and set it to `true` from `update_colors()` if
   a color update happened.
 * An `update()` is run if we get a `true` from `update_colors()`.
 * And finally, `update_colors()` is renamed to
   `update_default_colors()`.

 Fixes #291.

Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
  • Loading branch information
MoSal authored and jackpot51 committed Aug 17, 2024
1 parent 72ffa01 commit b495419
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,31 +580,39 @@ impl Terminal {
}
}

//TODO: this is done on every set_config because the changed boolean above does not capture
// NOTE: this is done on every set_config because the changed boolean above does not capture
// WINDOW_BG changes
self.update_colors(config);
let default_colors_updated = self.update_default_colors(config);

if update_cell_size {
self.update_cell_size();
} else if update {
} else if update || default_colors_updated {
self.update();
}
}

pub fn update_colors(&mut self, config: &AppConfig) {
self.metadata_set.clear();
pub fn update_default_colors(&mut self, config: &AppConfig) -> bool {
let default_bg = convert_color(&self.colors, Color::Named(NamedColor::Background));
let default_fg = convert_color(&self.colors, Color::Named(NamedColor::Foreground));

let default_metadata = Metadata::new(default_bg, default_fg);
let (default_metadata_idx, _) = self.metadata_set.insert_full(default_metadata);
let new_default_metadata = Metadata::new(default_bg, default_fg);
let curr_metada_idx = self.default_attrs().metadata;

self.default_attrs = Attrs::new()
.family(Family::Monospace)
.weight(Weight(config.font_weight))
.stretch(config.typed_font_stretch())
.color(default_fg)
.metadata(default_metadata_idx);
let updated = new_default_metadata != self.metadata_set[curr_metada_idx];

if updated {
self.metadata_set.clear();
let (default_metadata_idx, _) = self.metadata_set.insert_full(new_default_metadata);

self.default_attrs = Attrs::new()
.family(Family::Monospace)
.weight(Weight(config.font_weight))
.stretch(config.typed_font_stretch())
.color(default_fg)
.metadata(default_metadata_idx);
}

updated
}

pub fn update_cell_size(&mut self) {
Expand Down

0 comments on commit b495419

Please sign in to comment.