Skip to content

Commit

Permalink
Implement ZoomIn/ZoomOut/ZoomReset with configurable step
Browse files Browse the repository at this point in the history
 Implements pop-os#14.

Signed-off-by: Mohammad AlSaleh <[email protected]>
  • Loading branch information
MoSal authored and jackpot51 committed Jan 3, 2024
1 parent 1e0ce2b commit 1c8a058
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions i18n/en/cosmic_term.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ syntax-dark = Syntax dark
syntax-light = Syntax light
default-font = Default font
default-font-size = Default font size
default-zoom-step = Default zoom step
# Context menu
copy = Copy
Expand Down
13 changes: 12 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct Config {
pub app_theme: AppTheme,
pub font_name: String,
pub font_size: u16,
pub font_size_zoom_adj: i8,
pub font_size_zoom_step_mul_100: u16,
pub show_headerbar: bool,
pub syntax_theme_dark: String,
pub syntax_theme_light: String,
Expand All @@ -42,6 +44,8 @@ impl Default for Config {
app_theme: AppTheme::System,
font_name: "Fira Mono".to_string(),
font_size: 14,
font_size_zoom_adj: 0,
font_size_zoom_step_mul_100: 100,
show_headerbar: true,
syntax_theme_dark: "COSMIC Dark".to_string(),
syntax_theme_light: "COSMIC Light".to_string(),
Expand All @@ -50,9 +54,16 @@ impl Default for Config {
}

impl Config {
fn font_size_adjusted(&self) -> f32 {
let font_size = f32::from(self.font_size).max(1.0);
let adj = f32::from(self.font_size_zoom_adj);
let adj_step = f32::from(self.font_size_zoom_step_mul_100) / 100.0;
(font_size + adj*adj_step).max(1.0)
}

// Calculate metrics from font size
pub fn metrics(&self) -> Metrics {
let font_size = self.font_size.max(1) as f32;
let font_size = self.font_size_adjusted();
let line_height = (font_size * 1.4).ceil();
Metrics::new(font_size, line_height)
}
Expand Down
79 changes: 79 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ pub enum Message {
Copy(Option<segmented_button::Entity>),
DefaultFont(usize),
DefaultFontSize(usize),
ZoomIn,
ZoomOut,
ZoomReset,
DefaultZoomStep(usize),
Paste(Option<segmented_button::Entity>),
PasteValue(Option<segmented_button::Entity>, String),
SelectAll(Option<segmented_button::Entity>),
Expand Down Expand Up @@ -179,6 +183,8 @@ pub struct App {
font_names: Vec<String>,
font_size_names: Vec<String>,
font_sizes: Vec<u16>,
zoom_step_names: Vec<String>,
zoom_steps: Vec<u16>,
theme_names: Vec<String>,
themes: HashMap<String, TermColors>,
context_page: ContextPage,
Expand Down Expand Up @@ -251,6 +257,10 @@ impl App {
.font_sizes
.iter()
.position(|font_size| font_size == &self.config.font_size);
let zoom_step_selected = self
.zoom_steps
.iter()
.position(|zoom_step| zoom_step == &self.config.font_size_zoom_step_mul_100);
widget::settings::view_column(vec![widget::settings::view_section(fl!("appearance"))
.add(
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
Expand Down Expand Up @@ -293,6 +303,13 @@ impl App {
}),
),
)
.add(
widget::settings::item::builder(fl!("default-zoom-step")).control(
widget::dropdown(&self.zoom_step_names, zoom_step_selected, |index| {
Message::DefaultZoomStep(index)
}),
),
)
.add(
widget::settings::item::builder(fl!("show-headerbar"))
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
Expand Down Expand Up @@ -366,6 +383,13 @@ impl Application for App {
font_sizes.push(font_size);
}

let mut zoom_step_names = Vec::new();
let mut zoom_steps = Vec::new();
for zoom_step in [25, 50, 75, 100, 150, 200] {
zoom_step_names.push(format!("{}px", f32::from(zoom_step) / 100.0));
zoom_steps.push(zoom_step);
}

let themes = terminal_theme::terminal_themes();
let mut theme_names: Vec<_> = themes.keys().map(|x| x.clone()).collect();
theme_names.sort();
Expand All @@ -379,6 +403,8 @@ impl Application for App {
font_names,
font_size_names,
font_sizes,
zoom_step_names,
zoom_steps,
theme_names,
themes,
context_page: ContextPage::Settings,
Expand Down Expand Up @@ -445,9 +471,32 @@ impl Application for App {
}
}
}
Message::ZoomIn => {
self.config.font_size_zoom_adj = self.config.font_size_zoom_adj.saturating_add(1);
return self.save_config();
},
Message::ZoomOut => {
self.config.font_size_zoom_adj = self.config.font_size_zoom_adj.saturating_sub(1);
return self.save_config();
},
Message::ZoomReset => {
self.config.font_size_zoom_adj = 0;
return self.save_config();
},
Message::DefaultZoomStep(index) => match self.zoom_steps.get(index) {
Some(zoom_step) => {
self.config.font_size_zoom_step_mul_100 = *zoom_step;
self.config.font_size_zoom_adj = 0; // reset zoom
return self.save_config();
}
None => {
log::warn!("failed to find zoom step with index {}", index);
}
},
Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
Some(font_size) => {
self.config.font_size = *font_size;
self.config.font_size_zoom_adj = 0; // reset zoom
return self.save_config();
}
None => {
Expand Down Expand Up @@ -779,6 +828,36 @@ impl Application for App {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Equals,
modifiers,
}) => {
if modifiers == Modifiers::CTRL {
Some(Message::ZoomIn)
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Minus,
modifiers,
}) => {
if modifiers == Modifiers::CTRL {
Some(Message::ZoomOut)
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Key0,
modifiers,
}) => {
if modifiers == Modifiers::CTRL {
Some(Message::ZoomReset)
} else {
None
}
}
_ => None,
}),
subscription::channel(
Expand Down

0 comments on commit 1c8a058

Please sign in to comment.