Skip to content

Commit e495d1c

Browse files
committed
Add zoom with keyboard shortcuts
1 parent 0d5e27d commit e495d1c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ fn main() {
9595
application.set_accels_for_action("win.new-tab", &["<Ctrl>t"]);
9696
application.set_accels_for_action("win.close-tab", &["<Ctrl>w"]);
9797
application.set_accels_for_action("win.focus-url-bar", &["F6"]);
98+
application.set_accels_for_action("win.zoom-in", &["<Ctrl>plus"]);
99+
application.set_accels_for_action("win.zoom-out", &["<Ctrl>minus"]);
98100
// Sadly Tab doesn't work as an accelerator in gtk...
99101
application.set_accels_for_action("win.focus-next-tab", &["<Ctrl>Tab"]);
100102
application.set_accels_for_action("win.focus-previous-tab", &["<Ctrl><Shift>Tab"]);

src/widgets/window.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use crate::config;
2020
use crate::self_action;
2121
use crate::widgets::tab::{HistoryStatus, Tab};
2222

23+
const ZOOM_CHANGE_FACTOR: f32 = 1.15;
24+
2325
pub mod imp {
2426
use super::*;
2527
#[derive(Debug, Default, Properties, CompositeTemplate)]
@@ -51,6 +53,7 @@ pub mod imp {
5153
pub(crate) action_previous: RefCell<Option<gio::SimpleAction>>,
5254
pub(crate) action_next: RefCell<Option<gio::SimpleAction>>,
5355
pub(crate) style_provider: RefCell<gtk::CssProvider>,
56+
pub(crate) zoom: RefCell<(f32, gtk::CssProvider)>,
5457
}
5558

5659
impl Window {
@@ -141,6 +144,14 @@ impl Window {
141144
&*imp.style_provider.borrow(),
142145
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
143146
);
147+
148+
imp.zoom.borrow_mut().0 = 1.0;
149+
gtk::StyleContext::add_provider_for_display(
150+
&gdk::Display::default().unwrap(),
151+
&imp.zoom.borrow().1,
152+
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
153+
);
154+
144155
this.squeezer_changed();
145156
this.setup_actions_signals();
146157
this.open_in_new_tab(bookmarks_url().as_str());
@@ -164,6 +175,8 @@ impl Window {
164175
self_action!(self, "focus-tab-previous", focus_tab_previous);
165176
self_action!(self, "focus-tab-next", focus_tab_next);
166177
self_action!(self, "donate", donate);
178+
self_action!(self, "zoom-in", zoom_in);
179+
self_action!(self, "zoom-out", zoom_out);
167180

168181
let act_open_page = gio::SimpleAction::new("open-omni", Some(glib::VariantTy::STRING));
169182
act_open_page.connect_activate(
@@ -473,4 +486,31 @@ impl Window {
473486
let imp = self.imp();
474487
imp.tab_view.select_previous_page();
475488
}
489+
fn zoom_in(&self) {
490+
let imp = self.imp();
491+
let (factor, css) = &mut *imp.zoom.borrow_mut();
492+
*factor *= ZOOM_CHANGE_FACTOR;
493+
css.load_from_data(
494+
format!(
495+
"textview {{
496+
font-size: {factor}rem;
497+
}}"
498+
)
499+
.as_bytes(),
500+
);
501+
}
502+
fn zoom_out(&self) {
503+
let imp = self.imp();
504+
let (factor, css) = &mut *imp.zoom.borrow_mut();
505+
*factor /= ZOOM_CHANGE_FACTOR;
506+
css.load_from_data(
507+
format!(
508+
"textview {{
509+
font-size: {}rem;
510+
}}",
511+
factor
512+
)
513+
.as_bytes(),
514+
);
515+
}
476516
}

0 commit comments

Comments
 (0)