-
Notifications
You must be signed in to change notification settings - Fork 29
/
launchercontroller.cpp
176 lines (144 loc) · 4.61 KB
/
launchercontroller.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "launchercontroller.h"
#include <QDir>
#include <QTimer>
#include <QSettings>
#include <QStandardPaths>
#include <DGuiApplicationHelper>
#include <QCommandLineParser>
#include <launcher1adaptor.h>
DGUI_USE_NAMESPACE
LauncherController::LauncherController(QObject *parent)
: QObject(parent)
, optShow(QStringList{"s", "show"}, tr("Show launcher (hidden by default)"))
, optToggle(QStringList{"t", "toggle"}, tr("Toggle launcher visibility"))
, m_timer(new QTimer(this))
, m_launcher1Adaptor(new Launcher1Adaptor(this))
, m_visible(false)
{
// TODO: settings should be managed in somewhere else.
const QString settingBasePath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
const QString settingPath(QDir(settingBasePath).absoluteFilePath("settings.ini"));
QSettings settings(settingPath, QSettings::NativeFormat);
m_currentFrame = settings.value("current_frame", "WindowedFrame").toString();
// Interval set to 500=>1000ms for issue https://github.com/linuxdeepin/developer-center/issues/8137
m_timer->setInterval(1000);
m_timer->setSingleShot(true);
connect(m_timer, &QTimer::timeout, this, [this] {
if (m_pendingHide) {
m_pendingHide = false;
setVisible(false);
}
});
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::newProcessInstance,
this, [this](qint64 pid, const QStringList & args) {
Q_UNUSED(pid)
QCommandLineParser parser;
parser.addOption(optShow);
parser.addOption(optToggle);
parser.parse(args);
if (parser.isSet(optShow)) {
setVisible(true);
} else if (parser.isSet(optToggle)) {
setVisible(!visible());
}
});
// for dbus adapter signals.
connect(this, &LauncherController::visibleChanged, this, [this](bool isVisible){
if (isVisible) {
emit Shown();
} else {
emit Closed();
}
emit VisibleChanged(isVisible);
});
}
void LauncherController::Exit()
{
qApp->quit();
}
void LauncherController::Hide()
{
setVisible(false);
}
void LauncherController::Show()
{
setVisible(true);
}
void LauncherController::ShowByMode(qlonglong in0)
{
Q_UNUSED(in0)
// the original launcher implementation did nothing while calling this dbus API
// I guess we can deprecate this API.
}
void LauncherController::Toggle()
{
if (m_timer->isActive()) {
qDebug() << "hit";
m_pendingHide = false;
m_timer->stop();
return;
}
setVisible(!visible());
}
LauncherController::~LauncherController()
{
}
bool LauncherController::visible() const
{
return m_visible;
}
void LauncherController::setVisible(bool visible)
{
if (visible == m_visible) return;
m_visible = visible;
emit visibleChanged(m_visible);
}
bool LauncherController::isFullScreenFrame() const
{
return m_currentFrame == QStringLiteral("FullscreenFrame");
}
QString LauncherController::currentFrame() const
{
return m_currentFrame;
}
void LauncherController::setCurrentFrame(const QString &frame)
{
if (m_currentFrame == frame) return;
const QString settingBasePath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
const QString settingPath(QDir(settingBasePath).absoluteFilePath("settings.ini"));
QSettings settings(settingPath, QSettings::NativeFormat);
settings.setValue("current_frame", frame);
m_currentFrame = frame;
qDebug() << "set current frame:" << m_currentFrame;
m_pendingHide = false;
m_timer->start();
emit currentFrameChanged();
}
// We need to hide the launcher when it lost focus, but clicking the launcher icon on the taskbar/dock will also trigger
// `Toggle()`, which will show the launcher even if it just get hid caused by losting focus. Thus, we added a timer to
// mark it as we just hide it, and check if the timer is running while calling `Toggle()`. This function will do nothing
// if it's already hidden (`Toggle()` get triggered before `hideWithTimer()` get called).
void LauncherController::hideWithTimer()
{
if (visible()) {
if (m_timer->isActive()) {
m_pendingHide = true;
return;
}
qDebug() << "hide with timer";
setVisible(false);
}
}
void LauncherController::cancelHide()
{
m_pendingHide = false;
}
QFont LauncherController::adjustFontWeight(const QFont &f, QFont::Weight weight)
{
QFont font(f);
font.setWeight(weight);
return font;
}