-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrayicon.cpp
127 lines (90 loc) · 3.03 KB
/
trayicon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
This file is part of Waver
Copyright (C) 2021 Peter Papp
Please visit https://launchpad.net/waver for details
*/
#include "trayicon.h"
TrayIcon::TrayIcon(Waver *waver, QObject *parent) : QObject(parent)
{
this->waver = waver;
std::wstring appName = QGuiApplication::instance()->applicationName().toStdWString();
const auto aumi = WinToast::configureAUMI(QGuiApplication::instance()->organizationName().toStdWString(), appName, L"", QGuiApplication::instance()->applicationVersion().toStdWString());
WinToast::instance()->setAppName(appName);
WinToast::instance()->setAppUserModelId(aumi);
if (WinToast::instance()->initialize()) {
toastTemplate = WinToastTemplate(WinToastTemplate::ImageAndText02);
toastTemplate.setImagePath(QString("%1\\waver.png").arg(QGuiApplication::applicationDirPath().append("\\images").replace("/", "\\")).toStdWString());
toastTemplate.setAudioOption(WinToastTemplate::Silent);
toastTemplate.setDuration(WinToastTemplate::Short);
toastTemplate.addAction(tr("Pause").toStdWString());
toastTemplate.addAction(tr("Play").toStdWString());
connect(waver, &Waver::notify, this, &TrayIcon::showMetadataMessage);
connect(this, &TrayIcon::pause, waver, &Waver::pauseButton);
connect(this, &TrayIcon::play, waver, &Waver::playButton);
}
}
TrayIcon::~TrayIcon()
{
WinToast::instance()->clear();
}
void TrayIcon::showMetadataMessage()
{
WinToast::instance()->clear();
Track::TrackInfo trackInfo = waver->getCurrentTrackInfo();
toastTemplate.setTextField(trackInfo.title.toStdWString(), WinToastTemplate::FirstLine);
toastTemplate.setTextField(trackInfo.artist.toStdWString(), WinToastTemplate::SecondLine);
QTimer::singleShot(250, this, &TrayIcon::showToast);
}
void TrayIcon::sendPause()
{
emit pause();
}
void TrayIcon::sendPlay()
{
emit play();
}
void TrayIcon::sendPlayPause()
{
if (waver->getCurrentTrackStatus() == Track::Playing) {
emit pause();
return;
}
emit play();
}
void TrayIcon::showToast()
{
WinToast::instance()->showToast(toastTemplate, new WinToastHandler(this));
}
WinToastHandler::WinToastHandler(TrayIcon *trayIcon)
{
this->trayIcon = trayIcon;
}
void WinToastHandler::toastActivated() const
{
toastActivated(2);
}
void WinToastHandler::toastActivated(int actionIndex) const
{
if (trayIcon == nullptr) {
return;
}
WinToast::instance()->clear();
switch (actionIndex) {
case 0:
trayIcon->sendPause();
break;
case 1:
trayIcon->sendPlay();
break;
default:
trayIcon->sendPlayPause();
}
QTimer::singleShot(250, trayIcon, &TrayIcon::showToast);
}
void WinToastHandler::toastDismissed(WinToastDismissalReason state) const
{
Q_UNUSED(state);
}
void WinToastHandler::toastFailed() const
{
}