From 6732f4630db20764420a8bfd0bab50706d73847f Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Mon, 5 Jun 2023 07:30:14 -0400 Subject: [PATCH] feat: expose linux's selection clipboard delay timer to user * In certain programs like Chrome, the selection clipboard will update even the user have not released mouse button. --- src/config.cc | 8 ++++++++ src/config.hh | 1 + src/ui/preferences.cc | 3 +++ src/ui/preferences.ui | 31 ++++++++++++++++++++++++++++++- src/ui/scanpopup.cc | 8 ++++++-- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/config.cc b/src/config.cc index 2372704e..9a6e80d8 100644 --- a/src/config.cc +++ b/src/config.cc @@ -245,6 +245,7 @@ Preferences::Preferences(): trackClipboardScan ( true ), trackSelectionScan ( true ), showScanFlag( false ), + selectionChangeDelayTimer( 500 ), #endif pronounceOnLoadMain( false ), pronounceOnLoadPopup( false ), @@ -931,6 +932,8 @@ Class load() c.preferences.trackClipboardScan= ( preferences.namedItem( "trackClipboardScan" ).toElement().text() == "1" ); c.preferences.trackSelectionScan= ( preferences.namedItem( "trackSelectionScan" ).toElement().text() == "1" ); c.preferences.showScanFlag= ( preferences.namedItem( "showScanFlag" ).toElement().text() == "1" ); + c.preferences.selectionChangeDelayTimer = + preferences.namedItem( "selectionChangeDelayTimer" ).toElement().text().toInt(); #endif c.preferences.pronounceOnLoadMain = ( preferences.namedItem( "pronounceOnLoadMain" ).toElement().text() == "1" ); @@ -1842,6 +1845,11 @@ void save( Class const & c ) opt = dd.createElement( "showScanFlag" ); opt.appendChild( dd.createTextNode( c.preferences.showScanFlag? "1":"0" ) ); preferences.appendChild( opt ); + + opt = dd.createElement( "selectionChangeDelayTimer" ); + opt.appendChild( dd.createTextNode( QString::number( c.preferences.selectionChangeDelayTimer ) ) ); + preferences.appendChild( opt ); + #endif opt = dd.createElement( "pronounceOnLoadMain" ); diff --git a/src/config.hh b/src/config.hh index cd6de6c5..0d017902 100644 --- a/src/config.hh +++ b/src/config.hh @@ -296,6 +296,7 @@ struct Preferences bool trackClipboardScan; bool trackSelectionScan; bool showScanFlag; + int selectionChangeDelayTimer; #endif // Whether the word should be pronounced on page load, in main window/popup diff --git a/src/ui/preferences.cc b/src/ui/preferences.cc index fc1af42c..131eb2b6 100644 --- a/src/ui/preferences.cc +++ b/src/ui/preferences.cc @@ -228,11 +228,13 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): ui.enableX11SelectionTrack->setChecked(p.trackSelectionScan); ui.enableClipboardTrack ->setChecked(p.trackClipboardScan); ui.showScanFlag->setChecked(p.showScanFlag); + ui.delayTimer->setValue( p.selectionChangeDelayTimer ); #else ui.enableX11SelectionTrack->hide(); ui.enableClipboardTrack->hide(); ui.showScanFlag->hide(); ui.ignoreOwnClipboardChanges->hide(); + ui.delayTimer->hide(); #endif // Sound @@ -409,6 +411,7 @@ Config::Preferences Preferences::getPreferences() p.trackSelectionScan = ui.enableX11SelectionTrack ->isChecked(); p.trackClipboardScan = ui.enableClipboardTrack ->isChecked(); p.showScanFlag= ui.showScanFlag->isChecked(); + p.selectionChangeDelayTimer = ui.delayTimer->value(); #endif p.storeHistory = ui.storeHistory->isChecked(); diff --git a/src/ui/preferences.ui b/src/ui/preferences.ui index e3c9c83f..c7309c5a 100644 --- a/src/ui/preferences.ui +++ b/src/ui/preferences.ui @@ -7,7 +7,7 @@ 0 0 787 - 629 + 730 @@ -620,6 +620,35 @@ in the pressed state when the word selection changes. + + + + + + + Delay time + + + + + + + ms + + + 0 + + + 10000 + + + 50 + + + + + + diff --git a/src/ui/scanpopup.cc b/src/ui/scanpopup.cc index 062fb228..8b5c73a4 100644 --- a/src/ui/scanpopup.cc +++ b/src/ui/scanpopup.cc @@ -285,9 +285,10 @@ ScanPopup::ScanPopup( QWidget * parent, translateWordFromSelection(); }); - // Use delay show to prevent multiple popups while selection in progress + // Use delay show to prevent popup from showing up while selection is still in progress + // Only certain software has this problem (e.g. Chrome) selectionDelayTimer.setSingleShot( true ); - selectionDelayTimer.setInterval( 800 ); + selectionDelayTimer.setInterval( cfg.preferences.selectionChangeDelayTimer ); connect( &selectionDelayTimer, &QTimer::timeout, this, &ScanPopup::translateWordFromSelection ); #endif @@ -319,6 +320,9 @@ void ScanPopup::refresh() { connect( ui.groupList, &GroupComboBox::currentIndexChanged, this, &ScanPopup::currentGroupChanged ); +#ifdef HAVE_X11 + selectionDelayTimer.setInterval( cfg.preferences.selectionChangeDelayTimer ); +#endif }