Skip to content

Commit

Permalink
feat: allow adjust application font size (#2183)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
xiaoyifang and autofix-ci[bot] authored Feb 21, 2025
1 parent 6db4306 commit e8619a9
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 143 deletions.
44 changes: 28 additions & 16 deletions locale/crowdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2912,22 +2912,6 @@ the application.</source>
<source>Interface Font</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Interface language:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Article Display style:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Add-on style:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Interface Style:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Turn the UI to dark.</source>
<translation type="unfinished"></translation>
Expand Down Expand Up @@ -3494,6 +3478,34 @@ from Stardict, Babylon and GLS dictionaries</source>
<source>Save debug messages to gd_log.txt in the config folder</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Interface Font Size</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Interface language</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Article Display style</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Add-on style</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Interface Style</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Preview Font</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Goldendict-ng is a dictionary software</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ProgramTypeEditor</name>
Expand Down
14 changes: 5 additions & 9 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ Preferences::Preferences():
clearNetworkCacheOnExit( true ),
zoomFactor( 1 ),
helpZoomFactor( 1 ),
wordsZoomLevel( 0 ),
maxStringsInHistory( 500 ),
storeHistory( 1 ),
alwaysExpandOptionalParts( true ),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -344,7 +345,6 @@ struct Preferences

qreal zoomFactor;
qreal helpZoomFactor;
int wordsZoomLevel;

unsigned maxStringsInHistory;
unsigned storeHistory;
Expand Down
6 changes: 6 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
74 changes: 0 additions & 74 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] {
Expand Down Expand Up @@ -868,7 +857,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
// Update zoomers
adjustCurrentZoomFactor();
scaleArticlesByCurrentZoomFactor();
applyWordsZoomLevel();

// Update autostart info
setAutostart( cfg.preferences.autoStart );
Expand Down Expand Up @@ -1123,7 +1111,6 @@ void MainWindow::updateSearchPaneAndBar( bool searchInDock )
wordListSelChanged = false;

updateGroupList( false );
applyWordsZoomLevel();

setInputLineText( text, WildcardPolicy::WildcardsAreAlreadyEscaped, DisablePopup );
focusTranslateLine();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 )
Expand Down Expand Up @@ -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" ) {
Expand Down
7 changes: 0 additions & 7 deletions src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
32 changes: 31 additions & 1 deletion src/ui/preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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();
Expand Down Expand Up @@ -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." );
}

Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/ui/preferences.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Loading

0 comments on commit e8619a9

Please sign in to comment.