From e8619a991bf5ffd96e731da7e82d3e6e5f6fd817 Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Fri, 21 Feb 2025 21:12:58 +0800 Subject: [PATCH] feat: allow adjust application font size (#2183) * opt: add font size adjustment * opt:remove words zoom * opt: add font preview * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * opt: add font preview * [autofix.ci] apply automated fixes * Apply suggestions from code review --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- locale/crowdin.ts | 44 +++++++++++++-------- src/config.cc | 14 +++---- src/config.hh | 2 +- src/main.cc | 6 +++ src/ui/mainwindow.cc | 74 ------------------------------------ src/ui/mainwindow.hh | 7 ---- src/ui/preferences.cc | 32 +++++++++++++++- src/ui/preferences.hh | 2 + src/ui/preferences.ui | 60 +++++++++++++++++++++++++---- src/ui/scanpopup.cc | 22 ----------- src/ui/scanpopup.hh | 1 - website/docs/ui_shortcuts.md | 5 +-- 12 files changed, 126 insertions(+), 143 deletions(-) diff --git a/locale/crowdin.ts b/locale/crowdin.ts index b8197aa9e..ae4bca81b 100644 --- a/locale/crowdin.ts +++ b/locale/crowdin.ts @@ -2912,22 +2912,6 @@ the application. Interface Font - - Interface language: - - - - Article Display style: - - - - Add-on style: - - - - Interface Style: - - Turn the UI to dark. @@ -3494,6 +3478,34 @@ from Stardict, Babylon and GLS dictionaries Save debug messages to gd_log.txt in the config folder + + Interface Font Size + + + + Interface language + + + + Article Display style + + + + Add-on style + + + + Interface Style + + + + Preview Font + + + + Goldendict-ng is a dictionary software + + ProgramTypeEditor diff --git a/src/config.cc b/src/config.cc index 0c8df6dcf..dbcf3012a 100644 --- a/src/config.cc +++ b/src/config.cc @@ -187,7 +187,6 @@ Preferences::Preferences(): clearNetworkCacheOnExit( true ), zoomFactor( 1 ), helpZoomFactor( 1 ), - wordsZoomLevel( 0 ), maxStringsInHistory( 500 ), storeHistory( 1 ), alwaysExpandOptionalParts( true ), @@ -829,6 +828,7 @@ Class load() c.preferences.interfaceLanguage = preferences.namedItem( "interfaceLanguage" ).toElement().text(); c.preferences.displayStyle = preferences.namedItem( "displayStyle" ).toElement().text(); c.preferences.interfaceFont = preferences.namedItem( "interfaceFont" ).toElement().text(); + c.preferences.interfaceFontSize = preferences.namedItem( "interfaceFontSize" ).toElement().text().toInt(); #if !defined( Q_OS_WIN ) c.preferences.interfaceStyle = preferences.namedItem( "interfaceStyle" ).toElement().text(); #endif @@ -890,10 +890,6 @@ Class load() c.preferences.helpZoomFactor = preferences.namedItem( "helpZoomFactor" ).toElement().text().toDouble(); } - if ( !preferences.namedItem( "wordsZoomLevel" ).isNull() ) { - c.preferences.wordsZoomLevel = preferences.namedItem( "wordsZoomLevel" ).toElement().text().toInt(); - } - applyBoolOption( c.preferences.enableMainWindowHotkey, preferences.namedItem( "enableMainWindowHotkey" ) ); if ( !preferences.namedItem( "mainWindowHotkey" ).isNull() ) { c.preferences.mainWindowHotkey = @@ -1738,6 +1734,10 @@ void save( Class const & c ) opt.appendChild( dd.createTextNode( c.preferences.interfaceFont ) ); preferences.appendChild( opt ); + opt = dd.createElement( "interfaceFontSize" ); + opt.appendChild( dd.createTextNode( QString::number( c.preferences.interfaceFontSize ) ) ); + preferences.appendChild( opt ); + opt = dd.createElement( "customFonts" ); auto customFont = c.preferences.customFonts.toElement( dd ); preferences.appendChild( customFont ); @@ -1820,10 +1820,6 @@ void save( Class const & c ) opt.appendChild( dd.createTextNode( QString::number( c.preferences.helpZoomFactor ) ) ); preferences.appendChild( opt ); - opt = dd.createElement( "wordsZoomLevel" ); - opt.appendChild( dd.createTextNode( QString::number( c.preferences.wordsZoomLevel ) ) ); - preferences.appendChild( opt ); - opt = dd.createElement( "enableMainWindowHotkey" ); opt.appendChild( dd.createTextNode( c.preferences.enableMainWindowHotkey ? "1" : "0" ) ); preferences.appendChild( opt ); diff --git a/src/config.hh b/src/config.hh index 9ea0a1f2d..e47bfead3 100644 --- a/src/config.hh +++ b/src/config.hh @@ -274,6 +274,7 @@ struct Preferences { QString interfaceLanguage; // Empty value corresponds to system default QString interfaceFont; //Empty as default value. + int interfaceFontSize; CustomFonts customFonts; bool newTabsOpenAfterCurrentOne; @@ -344,7 +345,6 @@ struct Preferences qreal zoomFactor; qreal helpZoomFactor; - int wordsZoomLevel; unsigned maxStringsInHistory; unsigned storeHistory; diff --git a/src/main.cc b/src/main.cc index 8235f5e72..d7f4608a7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -459,6 +459,12 @@ int main( int argc, char ** argv ) QApplication::setFont( font ); } + //system font size + if ( cfg.preferences.interfaceFontSize > 0 ) { + font.setPointSize( cfg.preferences.interfaceFontSize ); + QApplication::setFont( font ); + } + // Update locale if the user's choice disagrees with the system QLocale locale = QLocale::system(); if ( !cfg.preferences.interfaceLanguage.isEmpty() && locale.name() != cfg.preferences.interfaceLanguage ) { diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index f3c1af94e..5d6606e94 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -392,17 +392,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ): ui.menuZoom->addSeparator(); - wordsZoomIn = new QShortcut( this ); - wordsZoomIn->setKey( QKeySequence( "Alt+=" ) ); - wordsZoomOut = new QShortcut( this ); - wordsZoomOut->setKey( QKeySequence( "Alt+-" ) ); - wordsZoomBase = new QShortcut( this ); - wordsZoomBase->setKey( QKeySequence( "Alt+0" ) ); - - connect( wordsZoomIn, &QShortcut::activated, this, &MainWindow::doWordsZoomIn ); - connect( wordsZoomOut, &QShortcut::activated, this, &MainWindow::doWordsZoomOut ); - connect( wordsZoomBase, &QShortcut::activated, this, &MainWindow::doWordsZoomBase ); - // tray icon #ifndef Q_OS_MACOS // macOS uses the dock menu instead of the tray icon connect( trayIconMenu.addAction( tr( "Show &Main Window" ) ), &QAction::triggered, this, [ this ] { @@ -868,7 +857,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ): // Update zoomers adjustCurrentZoomFactor(); scaleArticlesByCurrentZoomFactor(); - applyWordsZoomLevel(); // Update autostart info setAutostart( cfg.preferences.autoStart ); @@ -1123,7 +1111,6 @@ void MainWindow::updateSearchPaneAndBar( bool searchInDock ) wordListSelChanged = false; updateGroupList( false ); - applyWordsZoomLevel(); setInputLineText( text, WildcardPolicy::WildcardsAreAlreadyEscaped, DisablePopup ); focusTranslateLine(); @@ -2277,7 +2264,6 @@ void MainWindow::editPreferences() // These parameters are not set in dialog p.zoomFactor = cfg.preferences.zoomFactor; p.helpZoomFactor = cfg.preferences.helpZoomFactor; - p.wordsZoomLevel = cfg.preferences.wordsZoomLevel; p.hideMenubar = cfg.preferences.hideMenubar; p.searchInDock = cfg.preferences.searchInDock; p.alwaysOnTop = cfg.preferences.alwaysOnTop; @@ -3139,10 +3125,6 @@ int MainWindow::getIconSize() void MainWindow::iconSizeActionTriggered( QAction * /*action*/ ) { - //reset word zoom - cfg.preferences.wordsZoomLevel = 0; - wordsZoomBase->setEnabled( false ); - bool useLargeIcons = useLargeIconsInToolbarsAction.isChecked(); int extent = getIconSize(); if ( useLargeIcons ) { @@ -3161,15 +3143,6 @@ void MainWindow::iconSizeActionTriggered( QAction * /*action*/ ) updateDictionaryBar(); scanPopup->setDictionaryIconSize(); - - //adjust the font size as well - auto font = translateBox->translateLine()->font(); - font.setWeight( QFont::Normal ); - //arbitrary value to make it look good - font.setPixelSize( extent * 0.8 ); - // translateBox->completerWidget()->setFont( font ); - //only set the font in toolbar - translateBox->translateLine()->setFont( font ); } void MainWindow::toggleMenuBarTriggered( bool announce ) @@ -3624,53 +3597,6 @@ void MainWindow::scaleArticlesByCurrentZoomFactor() scanPopup->applyZoomFactor(); } -void MainWindow::doWordsZoomIn() -{ - cfg.preferences.wordsZoomLevel = cfg.preferences.wordsZoomLevel + 2; - - applyWordsZoomLevel(); -} - -void MainWindow::doWordsZoomOut() -{ - cfg.preferences.wordsZoomLevel = cfg.preferences.wordsZoomLevel - 2; - - applyWordsZoomLevel(); -} - -void MainWindow::doWordsZoomBase() -{ - cfg.preferences.wordsZoomLevel = 0; - - applyWordsZoomLevel(); -} - -void MainWindow::applyWordsZoomLevel() -{ - QFont font = translateBox->translateLine()->font(); - - int ps = getIconSize(); - - ps += cfg.preferences.wordsZoomLevel; - if ( ps < 12 ) { - ps = 12; - } - - font.setPixelSize( ps * 0.8 ); - font.setWeight( QFont::Normal ); - translateBox->translateLine()->setFont( font ); - // translateBox->completerWidget()->setFont( font ); - wordsZoomBase->setEnabled( cfg.preferences.wordsZoomLevel != 0 ); - - if ( !cfg.preferences.searchInDock ) { - // Invalidating navToolbar's layout displays translateBoxWidget w/o the need to press the toolbar - // extension button when Words Zoom level decreases enough for translateBoxWidget to fit in the toolbar. - navToolbar->layout()->invalidate(); - } - - scanPopup->applyWordsZoomLevel(); -} - void MainWindow::messageFromAnotherInstanceReceived( QString const & message ) { if ( message == "bringToFront" ) { diff --git a/src/ui/mainwindow.hh b/src/ui/mainwindow.hh index 7fea70ca3..4c805138a 100644 --- a/src/ui/mainwindow.hh +++ b/src/ui/mainwindow.hh @@ -119,7 +119,6 @@ private: QAction *navBack, *navForward, *navPronounce, *enableScanningAction; QAction * beforeOptionsSeparator; QAction *zoomIn, *zoomOut, *zoomBase; - QShortcut *wordsZoomIn, *wordsZoomOut, *wordsZoomBase; QAction *addToFavorites, *beforeAddToFavoritesSeparator; QMenu trayIconMenu; QMenu * tabMenu; @@ -334,12 +333,6 @@ private slots: void scaleArticlesByCurrentZoomFactor(); - void doWordsZoomIn(); - void doWordsZoomOut(); - void doWordsZoomBase(); - - void applyWordsZoomLevel(); - /// If editDictionaryGroup is specified, the dialog positions on that group /// initially. void editDictionaries( unsigned editDictionaryGroup = GroupId::NoGroupId ); diff --git a/src/ui/preferences.cc b/src/ui/preferences.cc index dbf106a46..6021bdf73 100644 --- a/src/ui/preferences.cc +++ b/src/ui/preferences.cc @@ -44,6 +44,15 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): } ); connect( ui.buttonBox, &QDialogButtonBox::helpRequested, &helpAction, &QAction::trigger ); + connect( ui.systemFont, &QFontComboBox::currentTextChanged, this, [ this ]( const QString & font ) { + previewInterfaceFont( font, ui.interfaceFontSize->value() ); + } ); + + connect( ui.interfaceFontSize, &QSpinBox::valueChanged, this, [ this ]( int size ) { + previewInterfaceFont( ui.systemFont->currentText(), size ); + } ); + previewInterfaceFont( ui.systemFont->currentText(), ui.interfaceFontSize->value() ); + addAction( &helpAction ); // Load values into form @@ -91,9 +100,17 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): ui.systemFont->setCurrentText( p.interfaceFont ); } + if ( p.interfaceFontSize > 0 ) { + ui.interfaceFontSize->setValue( p.interfaceFontSize ); + } + else { + ui.interfaceFontSize->setValue( QApplication::font().pointSize() ); + } + prevWebFontFamily = p.customFonts; prevSysFont = p.interfaceFont; + prevFontSize = p.interfaceFontSize; if ( !p.customFonts.standard.isEmpty() ) { ui.font_standard->setCurrentText( p.customFonts.standard ); @@ -400,6 +417,13 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): ui.parallelThreads->setMaximum( QThread::idealThreadCount() ); ui.parallelThreads->setValue( p.fts.parallelThreads ); } +void Preferences::previewInterfaceFont( QString family, int size ) +{ + QFont f = QApplication::font(); + f.setFamily( family ); + f.setPointSize( size ); + this->ui.previewFont->setFont( f ); +} void Preferences::buildDisabledTypes( QString & disabledTypes, bool is_checked, QString name ) { @@ -418,6 +442,7 @@ Config::Preferences Preferences::getPreferences() p.interfaceLanguage = ui.interfaceLanguage->itemData( ui.interfaceLanguage->currentIndex() ).toString(); p.interfaceFont = ui.systemFont->currentText(); + p.interfaceFontSize = ui.interfaceFontSize->value(); Config::CustomFonts c; c.standard = ui.font_standard->currentText(); @@ -592,7 +617,7 @@ void Preferences::on_buttonBox_accepted() } #endif - if ( ui.systemFont->currentText() != prevSysFont ) { + if ( ui.systemFont->currentText() != prevSysFont || ui.interfaceFontSize->value() != prevFontSize ) { promptText += tr( "Restart to apply the interface font change." ); } @@ -612,6 +637,11 @@ void Preferences::on_buttonBox_accepted() c.customFonts.monospace ); } + if ( ui.interfaceFontSize->value() != prevFontSize ) { + auto font = QApplication::font(); + font.setPointSize( ui.interfaceFontSize->value() ); + QApplication::setFont( font ); + } //change interface font. if ( ui.systemFont->currentText() != prevSysFont ) { auto font = QApplication::font(); diff --git a/src/ui/preferences.hh b/src/ui/preferences.hh index af6ac28f8..b1bb3bab4 100644 --- a/src/ui/preferences.hh +++ b/src/ui/preferences.hh @@ -19,9 +19,11 @@ class Preferences: public QDialog Config::CustomFonts prevWebFontFamily; QString prevSysFont; + int prevFontSize; Config::Class & cfg; QAction helpAction; + void previewInterfaceFont( QString family, int size ); public: diff --git a/src/ui/preferences.ui b/src/ui/preferences.ui index e894c2ff1..b0fc5ed4b 100644 --- a/src/ui/preferences.ui +++ b/src/ui/preferences.ui @@ -298,12 +298,33 @@ the application. + + + + + + Interface Font Size + + + + + + + 8 + + + 32 + + + + + - Interface language: + Interface language @@ -333,7 +354,7 @@ the application. - Article Display style: + Article Display style @@ -360,7 +381,7 @@ the application. - Add-on style: + Add-on style @@ -387,7 +408,7 @@ the application. - Interface Style: + Interface Style @@ -438,6 +459,29 @@ the application. + + + + + + Preview Font + + + + + + Goldendict-ng is a dictionary software + + + true + + + + + + + + @@ -2012,8 +2056,8 @@ from Stardict, Babylon and GLS dictionaries accept() - 248 - 254 + 257 + 720 157 @@ -2028,8 +2072,8 @@ from Stardict, Babylon and GLS dictionaries reject() - 316 - 260 + 325 + 720 286 diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index 3be34fddf..112232ae2 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -292,7 +292,6 @@ ScanPopup::ScanPopup( QWidget * parent, #endif applyZoomFactor(); - applyWordsZoomLevel(); } void ScanPopup::onActionTriggered() @@ -410,25 +409,6 @@ void ScanPopup::applyZoomFactor() const definition->setZoomFactor( cfg.preferences.zoomFactor ); } -void ScanPopup::applyWordsZoomLevel() -{ - QFont font = ui.translateBox->translateLine()->font(); - - int ps = dictionaryBar.iconSize().height(); - - if ( cfg.preferences.wordsZoomLevel != 0 ) { - ps += cfg.preferences.wordsZoomLevel; - if ( ps < 12 ) { - ps = 12; - } - font.setPixelSize( ps * 0.8 ); - } - ui.translateBox->completerWidget()->setFont( font ); - // ui.translateBox->translateLine()->setFont( font ); - - ui.outerFrame->layout()->activate(); -} - Qt::WindowFlags ScanPopup::unpinnedWindowFlags() const { return defaultUnpinnedWindowFlags; @@ -1178,8 +1158,6 @@ void ScanPopup::setDictionaryIconSize() else if ( cfg.usingToolbarsIconSize == Config::ToolbarsIconSize::Large ) { dictionaryBar.setDictionaryIconSize( DictionaryBar::IconSize::Large ); } - - applyWordsZoomLevel(); } diff --git a/src/ui/scanpopup.hh b/src/ui/scanpopup.hh index a14d9f7a9..b5df0ab8c 100644 --- a/src/ui/scanpopup.hh +++ b/src/ui/scanpopup.hh @@ -45,7 +45,6 @@ public: /// Applies current zoom factor to the popup's view. Should be called when /// it's changed. void applyZoomFactor() const; - void applyWordsZoomLevel(); /// Translate the word void translateWord( QString const & word ); diff --git a/website/docs/ui_shortcuts.md b/website/docs/ui_shortcuts.md index 3d0a4aa36..c6d738585 100644 --- a/website/docs/ui_shortcuts.md +++ b/website/docs/ui_shortcuts.md @@ -1,10 +1,7 @@ # Shortcuts | Shortcut | Action | -|-----------------------|----------------------------------------------------------------------------------------------------------------------------------| -| Alt++ | Increase font size for matches list | -| Alt+– | Decrease font size for matches list | -| Alt+0 | Restore default font size for matches list | +|-----------------------|----------------------------------------------------------------------------------------------------------------------------------| | Alt+Left, Backspace | (In main and popup windows) history navigation: show previous founded results | | Alt+Right | (In main and popup windows) history navigation: show next founded results | | Alt+Down | Jump to article from next dictionary |