Move zoom_adj to App so it doesn't persist between runs

Addresses comments from #22.

Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
This commit is contained in:
Mohammad AlSaleh 2024-01-03 20:06:55 +03:00 • committed by Jeremy Soller
parent ef1e03e566
commit ff5c46cd19
3 changed files with 15 additions and 15 deletions

View file

@ -31,7 +31,6 @@ pub struct Config {
pub app_theme: AppTheme, pub app_theme: AppTheme,
pub font_name: String, pub font_name: String,
pub font_size: u16, pub font_size: u16,
pub font_size_zoom_adj: i8,
pub font_size_zoom_step_mul_100: u16, pub font_size_zoom_step_mul_100: u16,
pub show_headerbar: bool, pub show_headerbar: bool,
pub syntax_theme_dark: String, pub syntax_theme_dark: String,
@ -44,7 +43,6 @@ impl Default for Config {
app_theme: AppTheme::System, app_theme: AppTheme::System,
font_name: "Fira Mono".to_string(), font_name: "Fira Mono".to_string(),
font_size: 14, font_size: 14,
font_size_zoom_adj: 0,
font_size_zoom_step_mul_100: 100, font_size_zoom_step_mul_100: 100,
show_headerbar: true, show_headerbar: true,
syntax_theme_dark: "COSMIC Dark".to_string(), syntax_theme_dark: "COSMIC Dark".to_string(),
@ -54,16 +52,16 @@ impl Default for Config {
} }
impl Config { impl Config {
fn font_size_adjusted(&self) -> f32 { fn font_size_adjusted(&self, zoom_adj: i8) -> f32 {
let font_size = f32::from(self.font_size).max(1.0); let font_size = f32::from(self.font_size).max(1.0);
let adj = f32::from(self.font_size_zoom_adj); let adj = f32::from(zoom_adj);
let adj_step = f32::from(self.font_size_zoom_step_mul_100) / 100.0; let adj_step = f32::from(self.font_size_zoom_step_mul_100) / 100.0;
(font_size + adj*adj_step).max(1.0) (font_size + adj*adj_step).max(1.0)
} }
// Calculate metrics from font size // Calculate metrics from font size
pub fn metrics(&self) -> Metrics { pub fn metrics(&self, zoom_adj: i8) -> Metrics {
let font_size = self.font_size_adjusted(); let font_size = self.font_size_adjusted(zoom_adj);
let line_height = (font_size * 1.4).ceil(); let line_height = (font_size * 1.4).ceil();
Metrics::new(font_size, line_height) Metrics::new(font_size, line_height)
} }

View file

@ -183,6 +183,7 @@ pub struct App {
font_names: Vec<String>, font_names: Vec<String>,
font_size_names: Vec<String>, font_size_names: Vec<String>,
font_sizes: Vec<u16>, font_sizes: Vec<u16>,
zoom_adj: i8,
zoom_step_names: Vec<String>, zoom_step_names: Vec<String>,
zoom_steps: Vec<u16>, zoom_steps: Vec<u16>,
theme_names: Vec<String>, theme_names: Vec<String>,
@ -199,7 +200,7 @@ impl App {
for entity in entities { for entity in entities {
if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) { if let Some(terminal) = self.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, &self.themes, self.zoom_adj);
} }
} }
@ -403,6 +404,7 @@ impl Application for App {
font_names, font_names,
font_size_names, font_size_names,
font_sizes, font_sizes,
zoom_adj: 0,
zoom_step_names, zoom_step_names,
zoom_steps, zoom_steps,
theme_names, theme_names,
@ -472,21 +474,21 @@ impl Application for App {
} }
} }
Message::ZoomIn => { Message::ZoomIn => {
self.config.font_size_zoom_adj = self.config.font_size_zoom_adj.saturating_add(1); self.zoom_adj = self.zoom_adj.saturating_add(1);
return self.save_config(); return self.save_config();
}, },
Message::ZoomOut => { Message::ZoomOut => {
self.config.font_size_zoom_adj = self.config.font_size_zoom_adj.saturating_sub(1); self.zoom_adj = self.zoom_adj.saturating_sub(1);
return self.save_config(); return self.save_config();
}, },
Message::ZoomReset => { Message::ZoomReset => {
self.config.font_size_zoom_adj = 0; self.zoom_adj = 0;
return self.save_config(); return self.save_config();
}, },
Message::DefaultZoomStep(index) => match self.zoom_steps.get(index) { Message::DefaultZoomStep(index) => match self.zoom_steps.get(index) {
Some(zoom_step) => { Some(zoom_step) => {
self.config.font_size_zoom_step_mul_100 = *zoom_step; self.config.font_size_zoom_step_mul_100 = *zoom_step;
self.config.font_size_zoom_adj = 0; // reset zoom self.zoom_adj = 0; // reset zoom
return self.save_config(); return self.save_config();
} }
None => { None => {
@ -496,7 +498,7 @@ impl Application for App {
Message::DefaultFontSize(index) => match self.font_sizes.get(index) { Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
Some(font_size) => { Some(font_size) => {
self.config.font_size = *font_size; self.config.font_size = *font_size;
self.config.font_size_zoom_adj = 0; // reset zoom self.zoom_adj = 0; // reset zoom
return self.save_config(); return self.save_config();
} }
None => { None => {
@ -609,7 +611,7 @@ impl Application for App {
self.term_config.clone(), self.term_config.clone(),
colors.clone(), colors.clone(),
); );
terminal.set_config(&self.config, &self.themes); terminal.set_config(&self.config, &self.themes, self.zoom_adj);
self.tab_model self.tab_model
.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal)); .data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} }

View file

@ -316,11 +316,11 @@ impl Terminal {
self.update(); self.update();
} }
pub fn set_config(&mut self, config: &crate::Config, themes: &HashMap<String, Colors>) { pub fn set_config(&mut self, config: &crate::Config, themes: &HashMap<String, Colors>, zoom_adj: i8) {
let mut update_cell_size = false; let mut update_cell_size = false;
let mut update = false; let mut update = false;
let metrics = config.metrics(); let metrics = config.metrics(zoom_adj);
if metrics != self.buffer.metrics() { if metrics != self.buffer.metrics() {
{ {
let mut font_system = font_system().write().unwrap(); let mut font_system = font_system().write().unwrap();