Skip to content

Commit b4f54bd

Browse files
karlbjsparber
authored andcommitted
Use TextTag scaling for zoom instead of CSS
The CSS was global, causing zomming across mutiple windows and additional weirdness due to cascading when multiple windows add CSS. Solution: Don't use CSS for zooming, but a single TextTag wrapped around the whole content and set it's scaling to affect the text size. Closes #178
1 parent 49f3072 commit b4f54bd

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

reflection-app/src/document_view.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::cell::{Cell, RefCell};
2323
use reflection_doc::document::{Document, DocumentId};
2424

2525
use adw::{prelude::*, subclass::prelude::*};
26-
use gtk::{gdk, gio::prelude::ApplicationExtManual, glib, glib::clone};
26+
use gtk::{gdk, glib, glib::clone};
2727

2828
use crate::{
2929
ConnectionPopover, ReflectionApplication, ReflectionTextBuffer, TextView,
@@ -48,7 +48,7 @@ mod imp {
4848
pub share_code_label: TemplateChild<gtk::Label>,
4949
#[template_child]
5050
pub copy_code_button: TemplateChild<gtk::Button>,
51-
pub css_provider: gtk::CssProvider,
51+
pub zoom_tag: gtk::TextTag,
5252
pub font_size: Cell<f64>,
5353
#[property(get, set = Self::set_font_scale, default = 0.0)]
5454
pub font_scale: Cell<f64>,
@@ -134,19 +134,22 @@ mod imp {
134134
let buffer = ReflectionTextBuffer::new();
135135
self.text_view.set_buffer(Some(&buffer));
136136

137+
buffer.tag_table().add(&self.zoom_tag);
138+
buffer.connect_changed(clone!(
139+
#[weak(rename_to = this)]
140+
self,
141+
move |buffer| {
142+
this.apply_zoom_tag(buffer.upcast_ref());
143+
}
144+
));
145+
137146
let size = ReflectionApplication::default()
138147
.system_settings()
139148
.monospace_font_name()
140149
.map_or(BASE_TEXT_FONT_SIZE, |font| {
141150
font.size() as f64 / gtk::pango::SCALE as f64
142151
});
143152
self.font_size.set(size);
144-
self.obj().set_font_scale(0.0);
145-
gtk::style_context_add_provider_for_display(
146-
&gtk::Widget::display(self.obj().upcast_ref()),
147-
&self.css_provider,
148-
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
149-
);
150153

151154
let scroll_controller =
152155
gtk::EventControllerScroll::new(gtk::EventControllerScrollFlags::VERTICAL);
@@ -218,13 +221,18 @@ mod imp {
218221
self.font_scale.set(value);
219222

220223
let size = (font_size + self.obj().font_scale()).max(1.0);
221-
self.zoom_level.set(size / font_size);
224+
let scale = size / font_size;
225+
self.zoom_level.set(scale);
222226
self.obj().notify_zoom_level();
223-
self.css_provider
224-
.load_from_string(&format!(".sourceview {{ font-size: {size}pt; }}"));
227+
self.zoom_tag.set_scale(scale);
228+
self.apply_zoom_tag(&self.text_view.buffer());
225229
self.obj().action_set_enabled("window.zoom-out", size > 1.0);
226230
}
227231

232+
fn apply_zoom_tag(&self, buffer: &gtk::TextBuffer) {
233+
buffer.apply_tag(&self.zoom_tag, &buffer.start_iter(), &buffer.end_iter());
234+
}
235+
228236
fn set_document(&self, document: Option<Document>) {
229237
if let Some(ref document) = document {
230238
let document_id = Self::format_document_id(&document.id());

0 commit comments

Comments
 (0)