TrayIcon: Simplify onTrayActivated()

This commit is contained in:
Nodir Temirkhodjaev 2022-09-24 19:39:11 +03:00
parent ea94865f62
commit fc51a44eb0
2 changed files with 54 additions and 29 deletions

View File

@ -196,44 +196,20 @@ WindowManager *TrayIcon::windowManager() const
return ctrl()->windowManager(); return ctrl()->windowManager();
} }
void TrayIcon::onMouseClicked(TrayIcon::ClickType clickType)
{
QAction *action = clickAction(clickType);
if (action) {
action->trigger();
}
}
void TrayIcon::onTrayActivated(QSystemTrayIcon::ActivationReason reason) void TrayIcon::onTrayActivated(QSystemTrayIcon::ActivationReason reason)
{ {
switch (reason) { switch (reason) {
case QSystemTrayIcon::Trigger: { case QSystemTrayIcon::Trigger: {
if (clickAction(DoubleClick)) { onTrayActivatedByTrigger();
m_trayTriggered = true;
QTimer::singleShot(QApplication::doubleClickInterval(), this, [&] {
if (m_trayTriggered) {
m_trayTriggered = false;
onMouseClicked(SingleClick);
}
});
} else {
m_trayTriggered = false;
onMouseClicked(SingleClick);
}
} break; } break;
case QSystemTrayIcon::DoubleClick: { case QSystemTrayIcon::DoubleClick: {
if (m_trayTriggered) { onTrayActivatedByDoubleClick();
m_trayTriggered = false;
onMouseClicked(DoubleClick);
}
} break; } break;
case QSystemTrayIcon::MiddleClick: { case QSystemTrayIcon::MiddleClick: {
m_trayTriggered = false; onTrayActivatedByMiddleClick();
onMouseClicked(MiddleClick);
} break; } break;
case QSystemTrayIcon::Context: { case QSystemTrayIcon::Context: {
m_trayTriggered = false; onTrayActivatedByContext();
onMouseClicked(RightClick);
} break; } break;
default: default:
break; break;
@ -626,3 +602,47 @@ QAction *TrayIcon::clickActionByType(ActionType actionType) const
return nullptr; return nullptr;
} }
} }
void TrayIcon::onMouseClicked(TrayIcon::ClickType clickType)
{
QAction *action = clickAction(clickType);
if (action) {
action->trigger();
}
}
void TrayIcon::onTrayActivatedByTrigger()
{
if (clickAction(DoubleClick)) {
m_trayTriggered = true;
QTimer::singleShot(QApplication::doubleClickInterval(), this, [&] {
if (m_trayTriggered) {
m_trayTriggered = false;
onMouseClicked(SingleClick);
}
});
} else {
m_trayTriggered = false;
onMouseClicked(SingleClick);
}
}
void TrayIcon::onTrayActivatedByDoubleClick()
{
if (m_trayTriggered) {
m_trayTriggered = false;
onMouseClicked(DoubleClick);
}
}
void TrayIcon::onTrayActivatedByMiddleClick()
{
m_trayTriggered = false;
onMouseClicked(MiddleClick);
}
void TrayIcon::onTrayActivatedByContext()
{
m_trayTriggered = false;
onMouseClicked(RightClick);
}

View File

@ -65,7 +65,6 @@ public slots:
protected slots: protected slots:
void switchTrayMenu(bool checked); void switchTrayMenu(bool checked);
void onMouseClicked(TrayIcon::ClickType clickType);
void onTrayActivated(QSystemTrayIcon::ActivationReason reason); void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
void saveTrayFlags(); void saveTrayFlags();
@ -97,6 +96,12 @@ private:
QAction *clickActionFromIni(ClickType clickType) const; QAction *clickActionFromIni(ClickType clickType) const;
QAction *clickActionByType(ActionType actionType) const; QAction *clickActionByType(ActionType actionType) const;
void onMouseClicked(TrayIcon::ClickType clickType);
void onTrayActivatedByTrigger();
void onTrayActivatedByDoubleClick();
void onTrayActivatedByMiddleClick();
void onTrayActivatedByContext();
private: private:
bool m_trayTriggered : 1; bool m_trayTriggered : 1;