From 864908b7586073fc100151afaa5ab517af483807 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sun, 15 Dec 2024 18:58:07 -0500 Subject: [PATCH] Allow adjusting application font size * Closes #6822 * Fix fixed font not following default font's point size --- share/translations/keepassxc_en.ts | 28 +++ src/core/Config.cpp | 1 + src/core/Config.h | 1 + src/gui/Application.cpp | 30 +++- src/gui/Application.h | 1 + src/gui/ApplicationSettingsWidget.cpp | 19 +- src/gui/ApplicationSettingsWidgetGeneral.ui | 190 ++++++++++++-------- src/gui/Font.cpp | 10 +- src/gui/MainWindow.cpp | 2 + 9 files changed, 192 insertions(+), 90 deletions(-) diff --git a/share/translations/keepassxc_en.ts b/share/translations/keepassxc_en.ts index c83fefc75..6d2e5a3cb 100644 --- a/share/translations/keepassxc_en.ts +++ b/share/translations/keepassxc_en.ts @@ -243,6 +243,26 @@ Export KeePassXC Settings + + Small + + + + Normal + + + + Medium + + + + Large + + + + Custom + + ApplicationSettingsWidgetGeneral @@ -545,6 +565,14 @@ Open browser on double clicking URL field in entry view + + Font size: + + + + Font size selection + + ApplicationSettingsWidgetSecurity diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 84b4a35c2..edd8d02aa 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -117,6 +117,7 @@ static const QHash 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, {}}}, diff --git a/src/core/Config.h b/src/core/Config.h index 8131bc9af..2a5c20a0d 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -98,6 +98,7 @@ public: GUI_CheckForUpdatesIncludeBetas, GUI_ShowExpiredEntriesOnDatabaseUnlock, GUI_ShowExpiredEntriesOnDatabaseUnlockOffsetDays, + GUI_FontSizeOffset, GUI_MainWindowGeometry, GUI_MainWindowState, diff --git a/src/gui/Application.cpp b/src/gui/Application.cpp index 5916b0b81..01553ad91 100644 --- a/src/gui/Application.cpp +++ b/src/gui/Application.cpp @@ -44,6 +44,7 @@ namespace { constexpr int WaitTimeoutMSec = 150; const char BlockSizeProperty[] = "blockSize"; + int g_OriginalFontSize = 0; } // namespace Application::Application(int& argc, char** argv) @@ -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(); @@ -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) diff --git a/src/gui/Application.h b/src/gui/Application.h index 937d9d386..349c93923 100644 --- a/src/gui/Application.h +++ b/src/gui/Application.h @@ -42,6 +42,7 @@ public: ~Application() override; static void bootstrap(const QString& uiLanguage = "system"); + static void applyFontSize(); void applyTheme(); diff --git a/src/gui/ApplicationSettingsWidget.cpp b/src/gui/ApplicationSettingsWidget.cpp index ca7c5fd66..2e925c4f5 100644 --- a/src/gui/ApplicationSettingsWidget.cpp +++ b/src/gui/ApplicationSettingsWidget.cpp @@ -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))); @@ -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()); @@ -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()); diff --git a/src/gui/ApplicationSettingsWidgetGeneral.ui b/src/gui/ApplicationSettingsWidgetGeneral.ui index 25a993b23..eb69f2615 100644 --- a/src/gui/ApplicationSettingsWidgetGeneral.ui +++ b/src/gui/ApplicationSettingsWidgetGeneral.ui @@ -57,9 +57,9 @@ 0 - -419 - 573 - 1397 + 0 + 568 + 1202 @@ -493,6 +493,9 @@ false + + QComboBox::AdjustToContents + Temporary file moved into place @@ -709,22 +712,6 @@ 10 - - - - true - - - - 0 - 0 - - - - Movable toolbar - - - @@ -738,66 +725,6 @@ - - - - - 0 - 0 - - - - Qt::StrongFocus - - - Toolbar button style - - - QComboBox::AdjustToContents - - - - - - - true - - - - 0 - 0 - - - - margin-right: 5px - - - Toolbar button style: - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - toolButtonStyleComboBox - - - - - - - - 0 - 0 - - - - Qt::StrongFocus - - - Language selection - - - @@ -817,6 +744,82 @@ + + + + true + + + + 0 + 0 + + + + Movable toolbar + + + + + + + + 0 + 0 + + + + Qt::StrongFocus + + + Toolbar button style + + + QComboBox::AdjustToContents + + + + + + + + 0 + 0 + + + + Qt::StrongFocus + + + Language selection + + + QComboBox::AdjustToContents + + + + + + + true + + + + 0 + 0 + + + + Toolbar button style: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + toolButtonStyleComboBox + + + @@ -824,6 +827,32 @@ + + + + Font size: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + fontSizeComboBox + + + + + + + Qt::StrongFocus + + + Font size selection + + + QComboBox::AdjustToContents + + + @@ -912,6 +941,9 @@ Tray icon type + + QComboBox::AdjustToContents + diff --git a/src/gui/Font.cpp b/src/gui/Font.cpp index ee8034e8f..bb02b12ac 100644 --- a/src/gui/Font.cpp +++ b/src/gui/Font.cpp @@ -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; } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 20872d82a..e6e313f4f 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -1640,6 +1640,8 @@ void MainWindow::applySettingsChanges() } updateTrayIcon(); + + kpxcApp->applyFontSize(); } void MainWindow::setAllowScreenCapture(bool state)