Skip to content

Commit

Permalink
Let user change default font size via preference
Browse files Browse the repository at this point in the history
  • Loading branch information
Libretto committed Nov 24, 2023
1 parent 3eea00d commit 8ce6018
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 16 deletions.
7 changes: 5 additions & 2 deletions data/app.drey.Dialect.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
<!-- 0 = Ctrl+Enter | 1 = Enter -->
<default>0</default>
</key>
<key type="i" name="font-size">
<!-- 0 = System default font size -->
<key type="b" name="custom-default-font-size">
<default>false</default>
</key>
<key type="i" name="default-font-size">
<!-- 0 = User has never enabled custom default font size before -->
<default>0</default>
</key>

Expand Down
2 changes: 1 addition & 1 deletion data/resources/about.blp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Adw.AboutWindow about {
destroy-with-parent: true;
application-name: _("Dialect");
developer-name: _("The Dialect Authors");
copyright: _("Copyright 2020–⁠2022 The Dialect Authors");
copyright: _("Copyright 2020–⁠2023 The Dialect Authors");
comments: _("A translation app for GNOME.");
website: "https://github.com/dialect-app/dialect";
issue-url: "https://github.com/dialect-app/dialect/issues";
Expand Down
29 changes: 29 additions & 0 deletions data/resources/preferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@
</child>
</object>
</child>
<child>
<object class="AdwPreferencesGroup">
<property name="title" translatable="yes">Appearance</property>
<child>
<object class="AdwExpanderRow" id="custom_default_font_size">
<property name="title" translatable="yes">Use Custom Font Size</property>
<property name="subtitle" translatable="yes">System font size is used otherwise.</property>
<property name="show-enable-switch">True</property>
<property name="enable-expansion">False</property>
<property name="expanded">True</property>
<child>
<object class="AdwSpinRow" id="default_font_size">
<property name="title" translatable="yes">Default Font Size</property>
<property name="subtitle" translatable="yes">Changing the size via shortcuts does not overwrite this.</property>
<property name="adjustment">
<object class="GtkAdjustment">
<property name="lower">6</property>
<property name="upper">72</property>
<property name="value">0</property>
<property name="page-increment">10</property>
<property name="step-increment">5</property>
</object>
</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</template>
Expand Down
3 changes: 2 additions & 1 deletion dialect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def _on_about(self, _action, _param):
about.props.version = VERSION
about.props.developers = [
"Mufeed Ali",
"Rafael Mardojai CM http://rafaelmardojai.com"
"Rafael Mardojai CM http://rafaelmardojai.com",
"Libretto"
]

about.props.transient_for = self.window
Expand Down
34 changes: 34 additions & 0 deletions dialect/preferences.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2020-2022 Mufeed Ali
# Copyright 2020-2022 Rafael Mardojai CM
# Copyright 2023 Libretto
# SPDX-License-Identifier: GPL-3.0-or-later

import os
Expand Down Expand Up @@ -27,6 +28,8 @@ class DialectPreferencesWindow(Adw.PreferencesWindow):
translator_config = Gtk.Template.Child()
tts = Gtk.Template.Child()
tts_config = Gtk.Template.Child()
custom_default_font_size = Gtk.Template.Child()
default_font_size = Gtk.Template.Child()

def __init__(self, parent, **kwargs):
super().__init__(**kwargs)
Expand All @@ -42,6 +45,10 @@ def __init__(self, parent, **kwargs):
'selected', Gio.SettingsBindFlags.DEFAULT)
Settings.get().bind('src-auto', self.src_auto, 'active',
Gio.SettingsBindFlags.DEFAULT)
Settings.get().bind('custom-default-font-size', self.custom_default_font_size, 'enable-expansion',
Gio.SettingsBindFlags.DEFAULT)
Settings.get().bind('default-font-size', self.default_font_size, 'value',
Gio.SettingsBindFlags.DEFAULT)

self.translator_config.props.sensitive = False
self.tts_config.props.sensitive = False
Expand Down Expand Up @@ -74,6 +81,10 @@ def __init__(self, parent, **kwargs):
if os.getenv('XDG_CURRENT_DESKTOP') != 'GNOME':
self.search_provider.props.visible = False

# Connect font size signals
self.custom_default_font_size.connect("notify::enable-expansion", self._custom_default_font_size_switch)
self.default_font_size.get_adjustment().connect("value-changed", self._change_current_font_size)

@Gtk.Template.Callback()
def is_not_true(self, _widget, boolean):
""" Check if boolean is not true
Expand Down Expand Up @@ -121,3 +132,26 @@ def _provider_settings_tooltip(self, button, _pspec):
def _on_translator_loading(self, window, _value):
self.translator.props.sensitive = not window.translator_loading
self.tts.props.sensitive = not window.translator_loading

def _custom_default_font_size_switch(self, row, _value):
"""Called on self.custom_default_font_size::notify::enable-expansion signal"""
enabled = row.get_enable_expansion()
system_font_size = int(Gtk.Settings.get_default().get_property('gtk-font-name').split()[1])

if enabled:
if Settings.get().default_font_size == 0:
# User has never set custom size before
Settings.default_font_size = system_font_size
self.default_font_size.set_value(system_font_size)
self.parent.set_font_size(system_font_size)

else:
self.parent.set_font_size(Settings.get().default_font_size)
else:
self.parent.set_font_size(system_font_size)
self.custom_default_font_size.set_enable_expansion(False)

def _change_current_font_size(self, row):
"""Called on self.default_font_size.get_adjustment()::value-changed signal"""
Settings.default_font_size = row.get_value()
self.parent.set_font_size(row.get_value())
24 changes: 14 additions & 10 deletions dialect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,22 @@ def translate_accel_value(self):
return self.get_int('translate-accel')

@property
def font_size(self):
"""Return the user's preferred font size."""
value = self.get_int('font-size')
def custom_default_font_size(self):
"""Return whether the user wants a custom default font size."""
return self.get_boolean('custom-default-font-size')

if value == 0:
return int(Gtk.Settings.get_default().get_property('gtk-font-name').split()[1])
else:
return value
@custom_default_font_size.setter
def default_font_size(self, enabled):
self.set_boolean('custom-default-font-size', enabled)

@property
def default_font_size(self):
"""Return the user's preferred default font size."""
return self.get_int('default-font-size')

@font_size.setter
def font_size(self, size):
self.set_int('font-size', size)
@default_font_size.setter
def default_font_size(self, size):
self.set_int('default-font-size', size)

@property
def active_tts(self):
Expand Down
7 changes: 5 additions & 2 deletions dialect/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ def __init__(self, text, langs, **kwargs):

# Text buffers font size
self.font_css_provider = Gtk.CssProvider()
self.font_size = Settings.get().font_size
if Settings.get().custom_default_font_size:
self.font_size = Settings.get().default_font_size
else:
self.font_size = int(Gtk.Settings.get_default().get_property('gtk-font-name').split()[1])

# Setup window
self.setup_actions()
Expand Down Expand Up @@ -800,7 +803,7 @@ def ui_font_size_inc(self, _action, _param):

def ui_font_size_dec(self, _action, _param):
new_size = self.font_size - 5
if new_size >= 5:
if new_size >= 6:
self.set_font_size(new_size)

def ui_copy(self, _action, _param):
Expand Down

0 comments on commit 8ce6018

Please sign in to comment.