diff options
author | wurstsalat <mailtrash@posteo.de> | 2023-10-27 23:20:22 +0300 |
---|---|---|
committer | wurstsalat <mailtrash@posteo.de> | 2023-10-29 13:20:17 +0300 |
commit | 9ba49134bea777f1de403cc886320e4c442236fd (patch) | |
tree | dae59d471d4a892cf79e3228948c23d54cc24fc1 | |
parent | d6b85870903d00c1f84f8d879b25e9b197b1f0c6 (diff) |
feat: Allow adjusting user interface font size via hotkeys
Primary - plus/keypad plus = increase font size
Primary - minus/keypad minus = decrease font size
Primary - 0/keypad 0 = reset font size
Fixes #11343
-rw-r--r-- | gajim/common/setting_values.py | 12 | ||||
-rw-r--r-- | gajim/common/settings.py | 14 | ||||
-rw-r--r-- | gajim/data/gui/shortcuts_window.ui | 28 | ||||
-rw-r--r-- | gajim/data/other/shortcuts.json | 3 | ||||
-rw-r--r-- | gajim/gtk/const.py | 3 | ||||
-rw-r--r-- | gajim/gtk/css_config.py | 30 | ||||
-rw-r--r-- | gajim/gtk/main.py | 31 | ||||
-rw-r--r-- | gajim/gtk/preferences.py | 11 |
8 files changed, 114 insertions, 18 deletions
diff --git a/gajim/common/setting_values.py b/gajim/common/setting_values.py index d4ff0022f..1bba3453c 100644 --- a/gajim/common/setting_values.py +++ b/gajim/common/setting_values.py @@ -12,6 +12,7 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see <http://www.gnu.org/licenses/>. +from typing import Any from typing import Literal from typing import TypedDict @@ -125,6 +126,10 @@ IntSettings = Literal[ 'preview_size', ] +FloatSettings = Literal[ + 'app_font_size', +] + StringSettings = Literal[ 'action_on_close', 'additional_uri_schemes', @@ -159,10 +164,10 @@ StringSettings = Literal[ 'video_size', ] -AllSettings = Literal[BoolSettings, IntSettings, StringSettings] -AllSettingsT = str | int | bool | list[str] +AllSettings = Literal[BoolSettings, IntSettings, FloatSettings, StringSettings] +AllSettingsT = str | int | float | bool | list[str] -APP_SETTINGS = { +APP_SETTINGS: dict[str, str | int | float | bool | list[Any]] = { 'additional_uri_schemes': '', 'allow_open_file_uris': False, 'always_english_wikipedia': False, @@ -277,6 +282,7 @@ APP_SETTINGS = { 'use_speller': True, 'use_stun_server': False, 'use_urgency_hint': True, + 'app_font_size': 1.0, 'video_framerate': '', 'video_input_device': 'autovideosrc', 'video_see_self': True, diff --git a/gajim/common/settings.py b/gajim/common/settings.py index d4324d9f5..063860184 100644 --- a/gajim/common/settings.py +++ b/gajim/common/settings.py @@ -52,6 +52,7 @@ from gajim.common.setting_values import BoolContactSettings from gajim.common.setting_values import BoolGroupChatSettings from gajim.common.setting_values import BoolSettings from gajim.common.setting_values import DEFAULT_SOUNDEVENT_SETTINGS +from gajim.common.setting_values import FloatSettings from gajim.common.setting_values import HAS_ACCOUNT_DEFAULT from gajim.common.setting_values import HAS_APP_DEFAULT from gajim.common.setting_values import INITAL_WORKSPACE @@ -239,6 +240,9 @@ class Settings: if IS_PORTABLE: APP_SETTINGS['use_keyring'] = False + if sys.platform == 'win32': + APP_SETTINGS['app_font_size'] = 1.125 + def _load_app_overrides(self) -> None: if not OVERRIDES_PATH.exists(): return @@ -628,6 +632,10 @@ class Settings: def get_app_setting(self, setting: IntSettings) -> int: ... + @overload + def get_app_setting(self, setting: FloatSettings) -> float: + ... + def get_app_setting(self, setting: str) -> AllSettingsT: if setting not in APP_SETTINGS: raise ValueError(f'Invalid app setting: {setting}') @@ -659,6 +667,12 @@ class Settings: @overload def set_app_setting(self, + setting: FloatSettings, + value: float | None) -> None: + ... + + @overload + def set_app_setting(self, setting: Literal['workspace_order'], value: list[str]) -> None: ... diff --git a/gajim/data/gui/shortcuts_window.ui b/gajim/data/gui/shortcuts_window.ui index 1c24e5be6..b032bcc19 100644 --- a/gajim/data/gui/shortcuts_window.ui +++ b/gajim/data/gui/shortcuts_window.ui @@ -65,6 +65,34 @@ <child> <object class="GtkShortcutsGroup"> <property name="visible">1</property> + <property name="title" translatable="yes">User Interface</property> + <child> + <object class="GtkShortcutsShortcut"> + <property name="visible">1</property> + <property name="accelerator"><primary>plus</property> + <property name="title" translatable="yes">Increase User Interface Font Size</property> + </object> + </child> + <child> + <object class="GtkShortcutsShortcut"> + <property name="visible">1</property> + <property name="accelerator"><primary>minus</property> + <property name="title" translatable="yes">Decrease User Interface Font Size</property> + </object> + </child> + <child> + <object class="GtkShortcutsShortcut"> + <property name="visible">1</property> + <property name="accelerator"><primary>0</property> + <property name="title" translatable="yes">Reset User Interface Font Size</property> + </object> + </child> + </object> + </child> + + <child> + <object class="GtkShortcutsGroup"> + <property name="visible">1</property> <property name="title" translatable="yes">Message composition</property> <child> <object class="GtkShortcutsShortcut"> diff --git a/gajim/data/other/shortcuts.json b/gajim/data/other/shortcuts.json index 27e14db7b..4f26e891f 100644 --- a/gajim/data/other/shortcuts.json +++ b/gajim/data/other/shortcuts.json @@ -38,6 +38,9 @@ "win.switch-workspace-7": ["<Primary>7", "<Primary>KP_7"], "win.switch-workspace-8": ["<Primary>8", "<Primary>KP_8"], "win.switch-workspace-9": ["<Primary>9", "<Primary>KP_9"], + "win.increase-app-font-size": ["<Primary>plus", "<Primary>KP_Add"], + "win.decrease-app-font-size": ["<Primary>minus", "<Primary>KP_Subtract"], + "win.reset-app-font-size": ["<Primary>0", "<Primary>KP_0"], "win.toggle-chat-list": ["<Primary>R"], "win.scroll-view-up": ["<Shift>Page_Up"], "win.scroll-view-down": ["<Shift>Page_Down"] diff --git a/gajim/gtk/const.py b/gajim/gtk/const.py index 107a910df..085eb449e 100644 --- a/gajim/gtk/const.py +++ b/gajim/gtk/const.py @@ -270,6 +270,9 @@ MAIN_WIN_ACTIONS = [ ('switch-workspace-7', None, True), ('switch-workspace-8', None, True), ('switch-workspace-9', None, True), + ('increase-app-font-size', None, True), + ('decrease-app-font-size', None, True), + ('reset-app-font-size', None, True), ('toggle-chat-list', None, True), ('add-workspace', 's', True), ('edit-workspace', 's', True), diff --git a/gajim/gtk/css_config.py b/gajim/gtk/css_config.py index a683510ae..4f246250c 100644 --- a/gajim/gtk/css_config.py +++ b/gajim/gtk/css_config.py @@ -108,6 +108,13 @@ class CSSConfig: self._dynamic_provider, CSSPriority.APPLICATION) + # Font size provider for GUI font size + self._app_font_size_provider = Gtk.CssProvider() + Gtk.StyleContext.add_provider_for_screen( + screen, + self._app_font_size_provider, + CSSPriority.PRE_APPLICATION) + # Cache of recently requested values self._cache: dict[str, str | Pango.FontDescription | None] = {} @@ -128,8 +135,7 @@ class CSSConfig: self._provider, CSSPriority.USER_THEME) - if sys.platform == 'win32': - self._apply_windows_font_size() + self.apply_app_font_size() @property def prefer_dark(self) -> bool: @@ -197,20 +203,14 @@ class CSSConfig: except Exception: log.exception('Error loading application css') - @staticmethod - def _apply_windows_font_size() -> None: - css = ''' - * { - font-size: 1.125rem; - } + def apply_app_font_size(self) -> None: + app_font_size = app.settings.get('app_font_size') + css = f''' + * {{ + font-size: {app_font_size}rem; + }} ''' - provider = Gtk.CssProvider() - provider.load_from_data(bytes(css.encode('utf-8'))) - screen = Gdk.Screen.get_default() - assert screen is not None - Gtk.StyleContext.add_provider_for_screen(screen, - provider, - CSSPriority.PRE_APPLICATION) + self._app_font_size_provider.load_from_data(bytes(css.encode('utf-8'))) @staticmethod def _pango_to_css_weight(number: int) -> int: diff --git a/gajim/gtk/main.py b/gajim/gtk/main.py index 21b33eedd..7e9d7991a 100644 --- a/gajim/gtk/main.py +++ b/gajim/gtk/main.py @@ -416,6 +416,9 @@ class MainWindow(Gtk.ApplicationWindow, EventHelper): ('switch-workspace-7', self._on_action), ('switch-workspace-8', self._on_action), ('switch-workspace-9', self._on_action), + ('increase-app-font-size', self._on_app_font_size_action), + ('decrease-app-font-size', self._on_app_font_size_action), + ('reset-app-font-size', self._on_app_font_size_action), ('toggle-chat-list', self._on_action), ('preview-download', self._on_preview_action), ('preview-open', self._on_preview_action), @@ -505,6 +508,34 @@ class MainWindow(Gtk.ApplicationWindow, EventHelper): return None + def _on_app_font_size_action( + self, + action: Gio.SimpleAction, + _param: GLib.Variant + ) -> None: + + action_name = action.get_name() + if action_name == 'reset-app-font-size': + app.settings.set_app_setting('app_font_size', None) + app.css_config.apply_app_font_size() + return + + app_font_size = app.settings.get('app_font_size') + new_app_font_size = app_font_size + if action_name == 'increase-app-font-size': + new_app_font_size = app_font_size + 0.125 + elif action_name == 'decrease-app-font-size': + new_app_font_size = app_font_size - 0.125 + + # Clamp font size + new_app_font_size = max(min(1.5, new_app_font_size), 1.0) + + if new_app_font_size == app_font_size: + return + + app.settings.set_app_setting('app_font_size', new_app_font_size) + app.css_config.apply_app_font_size() + def _on_preview_action(self, action: Gio.SimpleAction, param: GLib.Variant diff --git a/gajim/gtk/preferences.py b/gajim/gtk/preferences.py index 6264ecb56..86b86bbd5 100644 --- a/gajim/gtk/preferences.py +++ b/gajim/gtk/preferences.py @@ -643,6 +643,13 @@ class Themes(PreferenceBox): } settings = [ + Setting(SettingKind.SPIN, + _('User Interface Font Size'), + SettingType.CONFIG, + 'app_font_size', + props={'range_': (1.0, 1.5, 0.125)}, + callback=self._on_app_font_size_changed), + Setting(SettingKind.POPOVER, _('Dark Theme'), SettingType.CONFIG, @@ -664,6 +671,10 @@ class Themes(PreferenceBox): PreferenceBox.__init__(self, settings) @staticmethod + def _on_app_font_size_changed(_value: float, *args: Any) -> None: + app.css_config.apply_app_font_size() + + @staticmethod def _get_theme_items() -> list[str]: theme_items = ['default'] theme_items.extend(app.css_config.themes) |