Skip to content

Commit bee2016

Browse files
committed
Use glib property for zoom
1 parent 19d9099 commit bee2016

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/widgets/window.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ use crate::self_action;
2121
use crate::widgets::tab::{HistoryStatus, Tab};
2222

2323
const ZOOM_CHANGE_FACTOR: f32 = 1.15;
24+
#[derive(Debug, Clone, Default)]
25+
pub(crate) struct Zoom {
26+
value: f32,
27+
provider: gtk::CssProvider,
28+
}
2429

2530
pub mod imp {
2631
use super::*;
@@ -53,7 +58,9 @@ pub mod imp {
5358
pub(crate) action_previous: RefCell<Option<gio::SimpleAction>>,
5459
pub(crate) action_next: RefCell<Option<gio::SimpleAction>>,
5560
pub(crate) style_provider: RefCell<gtk::CssProvider>,
56-
pub(crate) zoom: RefCell<(f32, gtk::CssProvider)>,
61+
62+
#[property(get = Self::zoom, set = Self::set_zoom, type = f32, member = value)]
63+
pub(crate) zoom: RefCell<Zoom>,
5764
}
5865

5966
impl Window {
@@ -86,6 +93,22 @@ pub mod imp {
8693
animation.play();
8794
self.progress_animation.replace(Some(animation));
8895
}
96+
fn zoom(&self) -> f32 {
97+
self.zoom.borrow().value
98+
}
99+
fn set_zoom(&self, v: f32) {
100+
let Zoom { value, provider } = &mut *self.zoom.borrow_mut();
101+
*value = v;
102+
provider.load_from_data(
103+
format!(
104+
"textview {{
105+
font-size: {}rem;
106+
}}",
107+
value
108+
)
109+
.as_bytes(),
110+
);
111+
}
89112
}
90113

91114
#[glib::object_subclass]
@@ -145,10 +168,10 @@ impl Window {
145168
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
146169
);
147170

148-
imp.zoom.borrow_mut().0 = 1.0;
171+
imp.zoom.borrow_mut().value = 1.0;
149172
gtk::StyleContext::add_provider_for_display(
150173
&gdk::Display::default().unwrap(),
151-
&imp.zoom.borrow().1,
174+
&imp.zoom.borrow().provider,
152175
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
153176
);
154177

@@ -486,31 +509,11 @@ impl Window {
486509
let imp = self.imp();
487510
imp.tab_view.select_previous_page();
488511
}
512+
489513
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-
);
514+
self.set_zoom(&(self.zoom() * ZOOM_CHANGE_FACTOR));
501515
}
502516
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-
);
517+
self.set_zoom(&(self.zoom() * 1.0 / ZOOM_CHANGE_FACTOR));
515518
}
516519
}

0 commit comments

Comments
 (0)