Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ MainWindow::MainWindow(const ClipboardBrowserSharedPtr &sharedData, QWidget *par
m_menu->setObjectName("Menu");

auto act = m_trayMenu->addAction( appIcon(), tr("&Show/Hide") );
connect(act, &QAction::triggered, this, &MainWindow::toggleVisible);
connect(act, &QAction::triggered, this, &MainWindow::toggleVisibleFromTray);
m_trayMenu->setDefaultAction(act);
addTrayAction(Actions::File_Preferences);
addTrayAction(Actions::File_ToggleClipboardStoring);
Expand Down Expand Up @@ -3197,6 +3197,15 @@ bool MainWindow::toggleVisible()
return true;
}

void MainWindow::toggleVisibleFromTray()
{
if (!isMinimized() && isVisible()) {
hideWindow();
} else {
showWindow();
}
}

void MainWindow::showBrowser(const ClipboardBrowser *browser)
{
int i = 0;
Expand Down Expand Up @@ -3249,13 +3258,7 @@ void MainWindow::trayActivated(int reason)
{
toggleMenu();
} else if ( reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick ) {
// Like toggleVisible() but hide window if visible and not focused
// (this seems better behavior when using mouse).
if (!isMinimized() && isVisible())
hideWindow();
else
showWindow();

toggleVisibleFromTray();
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ class MainWindow final : public QMainWindow
/** Show/hide main window. Return true only if window is shown. */
bool toggleVisible();

/**
* Like toggleVisible() but hide window if visible and not focused, which
* seems more reasonable when using mouse.
*/
void toggleVisibleFromTray();

/** Set icon for current tab or tab group. */
void setTabIcon();

Expand Down
1 change: 1 addition & 0 deletions src/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private slots:

void traySearch();
void trayPaste();
void trayShowHideAction();

void pasteNext();

Expand Down
29 changes: 29 additions & 0 deletions src/tests/tests_tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ void Tests::trayPaste()
RUN("tab" << tab1 << "read" << "0", "NEW ");
}

void Tests::trayShowHideAction()
{
#ifdef Q_OS_MAC
SKIP("Tests seem unable to trigger Show/Hide from tray on macOS");
#endif

// Test Show/Hide action from tray menu:
// The main window should be hide even if unfocused.
RUN("visible", "true\n");
WAIT_ON_OUTPUT("visible", "true\n");
WAIT_ON_OUTPUT("focused", "true\n");

// Unfocus the main window by focusing a dialog
RUN("action('copyq:dialog()')", "");
WAIT_ON_OUTPUT("visible", "true\n");
WAIT_ON_OUTPUT("focused", "false\n");

// Trigger &Show/Hide
RUN("menu", "");
KEYS(trayMenuId << "S");
WAIT_ON_OUTPUT("visible", "false\n");
WAIT_ON_OUTPUT("focused", "false\n");

RUN("menu", "");
KEYS(trayMenuId << "S");
WAIT_ON_OUTPUT("visible", "true\n");
WAIT_ON_OUTPUT("focused", "true\n");
}

void Tests::configTrayTab()
{
const auto tab1 = testTab(1);
Expand Down
Loading