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();
}
void TrayIcon::onMouseClicked(TrayIcon::ClickType clickType)
{
QAction *action = clickAction(clickType);
if (action) {
action->trigger();
}
}
void TrayIcon::onTrayActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger: {
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);
}
onTrayActivatedByTrigger();
} break;
case QSystemTrayIcon::DoubleClick: {
if (m_trayTriggered) {
m_trayTriggered = false;
onMouseClicked(DoubleClick);
}
onTrayActivatedByDoubleClick();
} break;
case QSystemTrayIcon::MiddleClick: {
m_trayTriggered = false;
onMouseClicked(MiddleClick);
onTrayActivatedByMiddleClick();
} break;
case QSystemTrayIcon::Context: {
m_trayTriggered = false;
onMouseClicked(RightClick);
onTrayActivatedByContext();
} break;
default:
break;
@ -626,3 +602,47 @@ QAction *TrayIcon::clickActionByType(ActionType actionType) const
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:
void switchTrayMenu(bool checked);
void onMouseClicked(TrayIcon::ClickType clickType);
void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
void saveTrayFlags();
@ -97,6 +96,12 @@ private:
QAction *clickActionFromIni(ClickType clickType) const;
QAction *clickActionByType(ActionType actionType) const;
void onMouseClicked(TrayIcon::ClickType clickType);
void onTrayActivatedByTrigger();
void onTrayActivatedByDoubleClick();
void onTrayActivatedByMiddleClick();
void onTrayActivatedByContext();
private:
bool m_trayTriggered : 1;