Skip to content

Commit

Permalink
Allow adjusting application font size
Browse files Browse the repository at this point in the history
* Closes #6822
* Fix fixed font not following default font's point size
  • Loading branch information
droidmonkey committed Dec 15, 2024
1 parent f2ed4e3 commit 864908b
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 55 deletions.
28 changes: 28 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@
<source>Export KeePassXC Settings</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Small</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Normal</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Medium</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Large</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Custom</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ApplicationSettingsWidgetGeneral</name>
Expand Down Expand Up @@ -545,6 +565,14 @@
<source>Open browser on double clicking URL field in entry view</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Font size:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Font size selection</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ApplicationSettingsWidgetSecurity</name>
Expand Down
1 change: 1 addition & 0 deletions src/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static const QHash<Config::ConfigKey, ConfigDirective> configStrings = {
{Config::GUI_CheckForUpdatesIncludeBetas, {QS("GUI/CheckForUpdatesIncludeBetas"), Roaming, false}},
{Config::GUI_ShowExpiredEntriesOnDatabaseUnlock, {QS("GUI/ShowExpiredEntriesOnDatabaseUnlock"), Roaming, true}},
{Config::GUI_ShowExpiredEntriesOnDatabaseUnlockOffsetDays, {QS("GUI/ShowExpiredEntriesOnDatabaseUnlockOffsetDays"), Roaming, 3}},
{Config::GUI_FontSizeOffset, {QS("GUI/FontSizeOffset"), Local, 0}},

{Config::GUI_MainWindowGeometry, {QS("GUI/MainWindowGeometry"), Local, {}}},
{Config::GUI_MainWindowState, {QS("GUI/MainWindowState"), Local, {}}},
Expand Down
1 change: 1 addition & 0 deletions src/core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Config : public QObject
GUI_CheckForUpdatesIncludeBetas,
GUI_ShowExpiredEntriesOnDatabaseUnlock,
GUI_ShowExpiredEntriesOnDatabaseUnlockOffsetDays,
GUI_FontSizeOffset,

GUI_MainWindowGeometry,
GUI_MainWindowState,
Expand Down
30 changes: 24 additions & 6 deletions src/gui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace
{
constexpr int WaitTimeoutMSec = 150;
const char BlockSizeProperty[] = "blockSize";
int g_OriginalFontSize = 0;
} // namespace

Application::Application(int& argc, char** argv)
Expand Down Expand Up @@ -151,12 +152,7 @@ void Application::bootstrap(const QString& uiLanguage)
{
Bootstrap::bootstrap(uiLanguage);

#ifdef Q_OS_WIN
// Qt on Windows uses "MS Shell Dlg 2" as the default font for many widgets, which resolves
// to Tahoma 8pt, whereas the correct font would be "Segoe UI" 9pt.
// Apparently, some widgets are already using the correct font. Thanks, MuseScore for this neat fix!
QApplication::setFont(QApplication::font("QMessageBox"));
#endif
applyFontSize();

osUtils->registerNativeEventFilter();
MessageBox::initializeButtonDefs();
Expand Down Expand Up @@ -205,6 +201,28 @@ void Application::applyTheme()
}
}

void Application::applyFontSize()
{
auto font = QApplication::font();

// Store the original font size on first call
if (g_OriginalFontSize <= 0) {
#ifdef Q_OS_WIN
// Qt on Windows uses "MS Shell Dlg 2" as the default font for many widgets, which resolves
// to Tahoma 8pt, whereas the correct font would be "Segoe UI" 9pt.
// Apparently, some widgets are already using the correct font. Thanks, MuseScore for this neat fix!
font = QApplication::font("QMessageBox");
#endif
g_OriginalFontSize = font.pointSize();
}

// Adjust application wide default font size
auto newSize = g_OriginalFontSize + qBound(-2, config()->get(Config::GUI_FontSizeOffset).toInt(), 4);
font.setPointSize(newSize);
QApplication::setFont(font);
QApplication::setFont(font, "QWidget");
}

bool Application::event(QEvent* event)
{
// Handle Apple QFileOpenEvent from finder (double click on .kdbx file)
Expand Down
1 change: 1 addition & 0 deletions src/gui/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Application : public QApplication
~Application() override;

static void bootstrap(const QString& uiLanguage = "system");
static void applyFontSize();

void applyTheme();

Expand Down
19 changes: 18 additions & 1 deletion src/gui/ApplicationSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ ApplicationSettingsWidget::ApplicationSettingsWidget(QWidget* parent)
m_generalUi->toolButtonStyleComboBox->installEventFilter(mouseWheelFilter);
m_generalUi->languageComboBox->installEventFilter(mouseWheelFilter);
m_generalUi->trayIconAppearance->installEventFilter(mouseWheelFilter);
m_generalUi->fontSizeComboBox->installEventFilter(mouseWheelFilter);

#ifdef WITH_XC_UPDATECHECK
connect(m_generalUi->checkForUpdatesOnStartupCheckBox, SIGNAL(toggled(bool)), SLOT(checkUpdatesToggled(bool)));
Expand Down Expand Up @@ -265,10 +266,25 @@ void ApplicationSettingsWidget::loadSettings()
m_generalUi->toolButtonStyleComboBox->addItem(tr("Follow style"), Qt::ToolButtonFollowStyle);
int toolButtonStyleIndex =
m_generalUi->toolButtonStyleComboBox->findData(config()->get(Config::GUI_ToolButtonStyle));
if (toolButtonStyleIndex > 0) {
if (toolButtonStyleIndex >= 0) {
m_generalUi->toolButtonStyleComboBox->setCurrentIndex(toolButtonStyleIndex);
}

m_generalUi->fontSizeComboBox->clear();
m_generalUi->fontSizeComboBox->addItem(tr("Small"), -1);
m_generalUi->fontSizeComboBox->addItem(tr("Normal"), 0);
m_generalUi->fontSizeComboBox->addItem(tr("Medium"), 1);
m_generalUi->fontSizeComboBox->addItem(tr("Large"), 2);

int fontSizeIndex = m_generalUi->fontSizeComboBox->findData(config()->get(Config::GUI_FontSizeOffset));
if (fontSizeIndex >= 0) {
m_generalUi->fontSizeComboBox->setCurrentIndex(fontSizeIndex);
} else {
// Custom value entered into config file, add it to the list and select it
m_generalUi->fontSizeComboBox->addItem(tr("Custom"), config()->get(Config::GUI_FontSizeOffset).toInt());
m_generalUi->fontSizeComboBox->setCurrentIndex(m_generalUi->fontSizeComboBox->count() - 1);
}

m_generalUi->systrayShowCheckBox->setChecked(config()->get(Config::GUI_ShowTrayIcon).toBool());
systrayToggled(m_generalUi->systrayShowCheckBox->isChecked());
m_generalUi->systrayMinimizeToTrayCheckBox->setChecked(config()->get(Config::GUI_MinimizeToTray).toBool());
Expand Down Expand Up @@ -412,6 +428,7 @@ void ApplicationSettingsWidget::saveSettings()
config()->set(Config::GUI_ColorPasswords, m_generalUi->colorPasswordsCheckBox->isChecked());

config()->set(Config::GUI_ToolButtonStyle, m_generalUi->toolButtonStyleComboBox->currentData().toString());
config()->set(Config::GUI_FontSizeOffset, m_generalUi->fontSizeComboBox->currentData().toInt());

config()->set(Config::GUI_ShowTrayIcon, m_generalUi->systrayShowCheckBox->isChecked());
config()->set(Config::GUI_TrayIconAppearance, m_generalUi->trayIconAppearance->currentData().toString());
Expand Down
120 changes: 76 additions & 44 deletions src/gui/ApplicationSettingsWidgetGeneral.ui
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-419</y>
<width>573</width>
<height>1397</height>
<y>0</y>
<width>568</width>
<height>1202</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
Expand Down Expand Up @@ -493,6 +493,9 @@
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
<item>
<property name="text">
<string>Temporary file moved into place</string>
Expand Down Expand Up @@ -709,22 +712,6 @@
<property name="horizontalSpacing">
<number>10</number>
</property>
<item row="1" column="2">
<widget class="QCheckBox" name="toolbarMovableCheckBox">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Movable toolbar</string>
</property>
</widget>
</item>
<item row="0" column="3">
<spacer name="horizontalSpacer_5">
<property name="orientation">
Expand All @@ -738,47 +725,57 @@
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="toolButtonStyleComboBox">
<item row="0" column="0">
<widget class="QLabel" name="languageLabel_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
<property name="text">
<string>Language:</string>
</property>
<property name="accessibleName">
<string>Toolbar button style</string>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
<property name="buddy">
<cstring>languageComboBox</cstring>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="toolButtonStyleLabel">
<item row="1" column="2">
<widget class="QCheckBox" name="toolbarMovableCheckBox">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">margin-right: 5px</string>
</property>
<property name="text">
<string>Toolbar button style:</string>
<string>Movable toolbar</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="toolButtonStyleComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="buddy">
<cstring>toolButtonStyleComboBox</cstring>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="accessibleName">
<string>Toolbar button style</string>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item>
Expand All @@ -796,24 +793,30 @@
<property name="accessibleName">
<string>Language selection</string>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="languageLabel_2">
<item row="1" column="0">
<widget class="QLabel" name="toolButtonStyleLabel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Language:</string>
<string>Toolbar button style:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>languageComboBox</cstring>
<cstring>toolButtonStyleComboBox</cstring>
</property>
</widget>
</item>
Expand All @@ -824,6 +827,32 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="fontSizeLabel">
<property name="text">
<string>Font size:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>fontSizeComboBox</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="fontSizeComboBox">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="accessibleName">
<string>Font size selection</string>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -912,6 +941,9 @@
<property name="accessibleName">
<string>Tray icon type</string>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item>
<item row="0" column="1">
Expand Down
10 changes: 6 additions & 4 deletions src/gui/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ QFont Font::defaultFont()
QFont Font::fixedFont()
{
auto fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
fixedFont.setPointSize(defaultFont().pointSize());

#ifdef Q_OS_WIN
// try to use Consolas on Windows, because the default Courier New has too many similar characters
auto consolasFont = QFontDatabase().font("Consolas", fixedFont.styleName(), fixedFont.pointSize());
auto consolasFont = QFontDatabase().font("Consolas", fixedFont.styleName(), defaultFont().pointSize());
if (consolasFont.family().contains("consolas", Qt::CaseInsensitive)) {
fixedFont = consolasFont;
// Bump up the font size by one point to match the default font
fixedFont.setPointSize(fixedFont.pointSize() + 1);
// Bump up the font size by one point to better match the default font on Windows
fixedFont.setPointSize(defaultFont().pointSize() + 1);
}
#endif
#ifdef Q_OS_MACOS
// Qt doesn't choose a monospace font correctly on macOS
fixedFont = QFontDatabase().font("Menlo", fixedFont.styleName(), qApp->font().pointSize());
fixedFont = QFontDatabase().font("Menlo", fixedFont.styleName(), defaultFont().pointSize());
fixedFont.setPointSize(defaultFont().pointSize());
#endif
return fixedFont;
}
2 changes: 2 additions & 0 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,8 @@ void MainWindow::applySettingsChanges()
}

updateTrayIcon();

kpxcApp->applyFontSize();
}

void MainWindow::setAllowScreenCapture(bool state)
Expand Down

0 comments on commit 864908b

Please sign in to comment.