From bfd2e398c1ec6bafab52a38e3ce24fa0658205c0 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 6 Aug 2019 16:22:51 +0200 Subject: Add support for adjusting the font scale at runtime --- Makefile | 2 +- meson.build | 2 ++ terminal.ui | 1 + terminal.vala | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- weltschmerz.1 | 11 +++++++++- 5 files changed, 77 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 39463aa..1f61888 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ MANDIR ?= ${DATAROOTDIR}/man VALAC ?= valac weltschmerz: weltschmerz.vala terminal.vala config.vala resources.c - ${VALAC} --pkg posix --pkg gtk+-3.0 --pkg vte-2.91 --gresources resources.xml \ + ${VALAC} -X -lm --pkg posix --pkg gtk+-3.0 --pkg vte-2.91 --gresources resources.xml \ weltschmerz.vala terminal.vala config.vala resources.c resources.c: resources.xml terminal.ui terminal.css diff --git a/meson.build b/meson.build index a36157b..0115018 100644 --- a/meson.build +++ b/meson.build @@ -2,9 +2,11 @@ project('weltschmerz', 'vala', 'c') gnome = import('gnome') valac = meson.get_compiler('vala') +cc = meson.get_compiler('c') dependencies = [ valac.find_library('posix'), + cc.find_library('m', required: false), dependency('gtk+-3.0'), dependency('vte-2.91'), ] diff --git a/terminal.ui b/terminal.ui index 7885ecc..c82a04d 100644 --- a/terminal.ui +++ b/terminal.ui @@ -59,6 +59,7 @@ + diff --git a/terminal.vala b/terminal.vala index d0f9480..c6c2525 100644 --- a/terminal.vala +++ b/terminal.vala @@ -9,6 +9,15 @@ class Terminal : Gtk.Overlay { const uint PCRE2_JIT_PARTIAL_SOFT = 0x00000002u; const uint PCRE2_ERROR_JIT_BADOPTION = -45; + /* The following values were chosen to be powers of 1.2 that lie within the + * range of VTE's bounds for the font scale: [0.25, 4]. + * + * They allow for 7 steps above or below the default scale of 1. + */ + const double FONT_SCALE_MIN = 0.2790816472336535; // 1.2 ^ -7 + const double FONT_SCALE_MAX = 3.583180799999999; // 1.2 ^ 7 + const double FONT_SCALE_FACTOR = 1.2; + struct PaletteEntry { string name; string normal; @@ -42,6 +51,7 @@ class Terminal : Gtk.Overlay { bool has_search; string url_match; uint? infobar_timeout_id; + double scroll_delta; public Terminal(string[] args, Gtk.Container parent, Gtk.Window window) { Object(parent: parent, window: window); @@ -135,12 +145,16 @@ class Terminal : Gtk.Overlay { } } + bool match_modifiers(int state, Gdk.ModifierType modifiers) { + return (state & modifiers) == modifiers; + } + bool match_button(Gdk.EventButton event, Gdk.ModifierType modifiers, uint button) { - return (event.state & modifiers) == modifiers && event.button == button; + return match_modifiers(event.state, modifiers) && event.button == button; } bool match_key(Gdk.EventKey event, Gdk.ModifierType modifiers, uint key) { - return (event.state & modifiers) == modifiers && event.keyval == key; + return match_modifiers(event.state, modifiers) && event.keyval == key; } void infobar_show(string message, Gtk.MessageType level, uint? timeout = null) { @@ -300,6 +314,18 @@ class Terminal : Gtk.Overlay { clipboard.set_text(url_match, -1); } + void adjust_font_scale(double scale) { + vte.set_font_scale(scale.clamp(FONT_SCALE_MIN, FONT_SCALE_MAX)); + } + + void increase_font_scale() { + adjust_font_scale(vte.get_font_scale() * FONT_SCALE_FACTOR); + } + + void decrease_font_scale() { + adjust_font_scale(vte.get_font_scale() / FONT_SCALE_FACTOR); + } + [GtkCallback] void vte_copy() { if (vte.get_has_selection()) { @@ -366,6 +392,41 @@ class Terminal : Gtk.Overlay { return true; } + if (match_key(event, CONTROL_MASK, Gdk.Key.@0)) { + vte.set_font_scale(1.0); + return true; + } + + if (match_key(event, CONTROL_MASK, Gdk.Key.equal)) { + increase_font_scale(); + return true; + } + + if (match_key(event, CONTROL_MASK, Gdk.Key.minus)) { + decrease_font_scale(); + return true; + } + + return false; + } + + [GtkCallback] + bool vte_scroll(Gdk.EventScroll event) { + if (match_modifiers(event.state, CONTROL_MASK)) { + scroll_delta += event.delta_y; + + if (Math.fabs(scroll_delta) >= 1) { + if (scroll_delta < 0) { + increase_font_scale(); + } else { + decrease_font_scale(); + } + scroll_delta = 0; + } + + return true; + } + return false; } diff --git a/weltschmerz.1 b/weltschmerz.1 index 7c248c5..bb834b5 100644 --- a/weltschmerz.1 +++ b/weltschmerz.1 @@ -1,4 +1,4 @@ -.Dd January 17, 2019 +.Dd August 31, 2019 .Dt WELTSCHMERZ 1 .Os .Sh NAME @@ -26,6 +26,15 @@ The clipboard can be copied to and pasted from with and .Sy CTRL + Shift + V , respectively. +.Pp +The font scale can be adjusted by scrolling up or down whilst holding +.Sy CTRL , +or by using +.Sy CTRL + = +and +.Sy CTRL + - . +It can be reset to its default value with +.Sy CTRL + 0 . .Sh SEARCH OVERLAY The search overlay can be opened by pressing .Sy CTRL + Shift + F . -- cgit v1.2.3-2-gb3c3