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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "view.h"
#include <QAction>
#include <QClipboard>
#include <QDebug>
#include <QGuiApplication>
#include <QPlatformSurfaceEvent>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickItem>
#include <QScreen>
#include <KAuthorized>
#include <KCrash>
#include <KIO/CommandLauncherJob>
#include <KLocalizedString>
#include <KService>
#include <KWindowEffects>
#include <KWindowSystem>
#include <KX11Extras>
#include <LayerShellQt/Window>
#include <qnamespace.h>
#include "appadaptor.h"
#include "x11windowscreenrelativepositioner.h"
KCONFIGGROUP_DECLARE_ENUM_QOBJECT(View, HistoryBehavior)
View::View(PlasmaQuick::SharedQmlEngine *engine, QWindow *)
: PlasmaQuick::PlasmaWindow()
, m_engine(engine)
, m_floating(false)
{
KCrash::initialize();
qmlRegisterUncreatableType<View>("org.kde.krunner.private.view", 1, 0, "HistoryBehavior", u"Only for enums"_s);
if (KWindowSystem::isPlatformX11()) {
m_x11Positioner = new X11WindowScreenRelativePositioner(this);
}
// used only by screen readers
setTitle(i18n("KRunner"));
m_config = KConfigGroup(KSharedConfig::openConfig(), u"General"_s);
m_stateData = KSharedConfig::openConfig(u"krunnerstaterc"_s, //
KConfig::NoGlobals,
QStandardPaths::GenericDataLocation)
->group(u"General"_s);
m_configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
connect(m_configWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group) {
const QLatin1String pluginsGrp("Plugins");
if (group.name() == QLatin1String("General")) {
loadConfig();
} else if (group.name() == pluginsGrp) {
Q_EMIT helpEnabledChanged();
}
});
connect(&m_consumer, &KActivities::Consumer::currentActivityChanged, this, &View::activityChanged);
Q_EMIT activityChanged(m_consumer.currentActivity());
loadConfig();
new AppAdaptor(this);
QDBusConnection::sessionBus().registerObject(u"/App"_s, this);
connect(m_engine, &PlasmaQuick::SharedQmlEngine::finished, this, &View::objectIncubated);
m_engine->engine()->rootContext()->setContextProperty(u"runnerWindow"_s, this);
m_engine->setSource(QUrl(u"qrc:/krunner/RunCommand.qml"_s));
m_engine->completeInitialization();
auto screenRemoved = [this](QScreen *screen) {
if (screen == this->screen()) {
setScreen(qGuiApp->primaryScreen());
hide();
}
};
connect(qGuiApp, &QGuiApplication::screenRemoved, this, screenRemoved);
connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &View::slotFocusWindowChanged);
}
View::~View()
{
}
QMargins View::margins()
{
if (m_floating) {
const QRect r = screen()->availableGeometry();
return QMargins({0, r.height() / 3, 0, 0});
} else {
return QMargins(); // Zeros
}
}
void View::objectIncubated()
{
auto item = qobject_cast<QQuickItem *>(m_engine->rootObject());
setMainItem(item);
auto updateSize = [this]() {
resize(QSize(mainItem()->implicitWidth(), mainItem()->implicitHeight()).grownBy(padding()).boundedTo(screen()->availableSize().shrunkBy(margins())));
};
connect(item, &QQuickItem::implicitHeightChanged, this, updateSize);
connect(this, &View::paddingChanged, this, updateSize);
updateSize();
}
void View::slotFocusWindowChanged()
{
if (QGuiApplication::focusWindow() && m_requestedClipboardSelection) {
displayWithClipboardContents();
}
if (!QGuiApplication::focusWindow() && !m_pinned) {
setVisible(false);
}
}
bool View::freeFloating() const
{
return m_floating;
}
void View::setFreeFloating(bool floating)
{
m_floating = floating;
if (m_floating) {
KWindowEffects::slideWindow(this, KWindowEffects::NoEdge);
setBorders(Qt::LeftEdge | Qt::TopEdge | Qt::RightEdge | Qt::BottomEdge);
} else {
KWindowEffects::slideWindow(this, KWindowEffects::TopEdge);
setBorders(Qt::LeftEdge | Qt::RightEdge | Qt::BottomEdge);
}
positionOnScreen();
}
void View::loadConfig()
{
setFreeFloating(m_config.readEntry("FreeFloating", false));
setRetainPriorSearch(m_config.readEntry("RetainPriorSearch", true));
setPinned(m_stateData.readEntry("Pinned", false));
setHistoryBehavior(m_config.readEntry("historyBehavior", m_historyBehavior));
}
void View::showEvent(QShowEvent *event)
{
if (KWindowSystem::isPlatformX11()) {
KX11Extras::setOnAllDesktops(winId(), true);
}
QQuickWindow::showEvent(event);
requestActivate();
if (KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
}
}
void View::positionOnScreen()
{
const auto screens = QGuiApplication::screens();
auto screenIt = screens.cend();
if (KWindowSystem::isPlatformWayland() && m_floating) {
auto message = QDBusMessage::createMethodCall(u"org.kde.KWin"_s, u"/KWin"_s, u"org.kde.KWin"_s, u"activeOutputName"_s);
QDBusReply<QString> reply = QDBusConnection::sessionBus().call(message);
if (reply.isValid()) {
const QString activeOutputName = reply.value();
screenIt = std::find_if(screens.cbegin(), screens.cend(), [&activeOutputName](QScreen *screen) {
return screen->name() == activeOutputName;
});
}
} else if (KWindowSystem::isPlatformX11()) {
screenIt = std::find_if(screens.cbegin(), screens.cend(), [](QScreen *screen) {
return screen->geometry().contains(QCursor::pos(screen));
});
}
QScreen *const shownOnScreen = screenIt != screens.cend() ? *screenIt : QGuiApplication::primaryScreen();
setScreen(shownOnScreen);
if (KWindowSystem::isPlatformWayland()) {
auto layerWindow = LayerShellQt::Window::get(this);
layerWindow->setAnchors(LayerShellQt::Window::AnchorTop);
layerWindow->setLayer(LayerShellQt::Window::LayerTop);
layerWindow->setScope(u"krunner"_s);
layerWindow->setKeyboardInteractivity(LayerShellQt::Window::KeyboardInteractivityOnDemand);
layerWindow->setMargins(margins());
layerWindow->setScreenConfiguration(m_floating ? LayerShellQt::Window::ScreenFromQWindow : LayerShellQt::Window::ScreenFromCompositor);
} else if (KWindowSystem::isPlatformX11()) {
m_x11Positioner->setAnchors(Qt::TopEdge);
m_x11Positioner->setMargins(margins());
if (m_floating) {
KX11Extras::setOnDesktop(winId(), KX11Extras::currentDesktop());
} else {
KX11Extras::setOnAllDesktops(winId(), true);
}
KX11Extras::setState(winId(), NET::SkipTaskbar | NET::SkipPager);
}
}
void View::toggleDisplay()
{
if (isVisible() && !QGuiApplication::focusWindow() && KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
return;
}
if (isVisible()) {
setVisible(false);
} else {
display();
}
}
void View::display()
{
positionOnScreen();
setVisible(true);
}
void View::displaySingleRunner(const QString &runnerName)
{
display();
m_engine->rootObject()->setProperty("singleRunner", runnerName);
m_engine->rootObject()->setProperty("query", QString());
}
void View::displayWithClipboardContents()
{
display();
// On Wayland we cannot retrieve the clipboard selection until we get the focus
if (QGuiApplication::focusWindow()) {
m_requestedClipboardSelection = false;
m_engine->rootObject()->setProperty("singleRunner", QString());
m_engine->rootObject()->setProperty("query", QGuiApplication::clipboard()->text(QClipboard::Selection));
} else {
m_requestedClipboardSelection = true;
}
}
void View::query(const QString &term)
{
display();
m_engine->rootObject()->setProperty("singleRunner", QString());
m_engine->rootObject()->setProperty("query", term);
}
void View::querySingleRunner(const QString &runnerName, const QString &term)
{
display();
m_engine->rootObject()->setProperty("singleRunner", runnerName);
m_engine->rootObject()->setProperty("query", term);
}
bool View::pinned() const
{
return m_pinned;
}
void View::setPinned(bool pinned)
{
if (m_pinned != pinned) {
m_pinned = pinned;
m_stateData.writeEntry("Pinned", pinned);
Q_EMIT pinnedChanged();
}
}
#include "moc_view.cpp"
|