plasma: windows runner review

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-17 22:11:52 +03:00
parent 8a8631e902
commit 669c74744e
2 changed files with 151 additions and 132 deletions

View file

@ -34,7 +34,6 @@ WindowsRunner::WindowsRunner(QObject* parent, const QVariantList& args)
m_inSession(false), m_inSession(false),
m_ready(false) m_ready(false)
{ {
Q_UNUSED(args)
setObjectName( QLatin1String("Windows" )); setObjectName( QLatin1String("Windows" ));
addSyntax(Plasma::RunnerSyntax(":q:", i18n("Finds windows whose name, window class or window role match :q:. " addSyntax(Plasma::RunnerSyntax(":q:", i18n("Finds windows whose name, window class or window role match :q:. "
@ -57,10 +56,6 @@ WindowsRunner::WindowsRunner(QObject* parent, const QVariantList& args)
connect(this, SIGNAL(teardown()), this, SLOT(matchSessionComplete())); connect(this, SIGNAL(teardown()), this, SLOT(matchSessionComplete()));
} }
WindowsRunner::~WindowsRunner()
{
}
void WindowsRunner::gatherInfo() void WindowsRunner::gatherInfo()
{ {
if (!m_inSession) { if (!m_inSession) {
@ -68,15 +63,20 @@ void WindowsRunner::gatherInfo()
} }
foreach (const WId w, KWindowSystem::windows()) { foreach (const WId w, KWindowSystem::windows()) {
KWindowInfo info = KWindowSystem::windowInfo(w, NET::WMWindowType | NET::WMDesktop | KWindowInfo info = KWindowSystem::windowInfo(
NET::WMState | NET::XAWMState | w,
NET::WMName, NET::WMWindowType | NET::WMDesktop |
NET::WM2WindowClass | NET::WM2WindowRole | NET::WM2AllowedActions); NET::WMState | NET::XAWMState |
NET::WMName,
NET::WM2WindowClass | NET::WM2WindowRole | NET::WM2AllowedActions
);
if (info.valid()) { if (info.valid()) {
// ignore NET::Tool and other special window types // ignore NET::Tool and other special window types
NET::WindowType wType = info.windowType(NET::NormalMask | NET::DesktopMask | NET::DockMask | NET::WindowType wType = info.windowType(
NET::ToolbarMask | NET::MenuMask | NET::DialogMask | NET::NormalMask | NET::DesktopMask | NET::DockMask |
NET::UtilityMask | NET::SplashMask); NET::ToolbarMask | NET::MenuMask | NET::DialogMask |
NET::UtilityMask | NET::SplashMask
);
if (wType != NET::Normal && wType != NET::Unknown && if (wType != NET::Normal && wType != NET::Unknown &&
wType != NET::Dialog && wType != NET::Utility) { wType != NET::Dialog && wType != NET::Utility) {
@ -195,7 +195,7 @@ void WindowsRunner::match(Plasma::RunnerContext& context)
QString windowClass; QString windowClass;
QString windowRole; QString windowRole;
int desktop = -1; int desktop = -1;
foreach (const QString& keyword, keywords) { foreach (const QString &keyword, keywords) {
if (keyword.endsWith('=')) { if (keyword.endsWith('=')) {
continue; continue;
} }
@ -258,7 +258,7 @@ void WindowsRunner::match(Plasma::RunnerContext& context)
} }
// blacklisted everything else: we have a match // blacklisted everything else: we have a match
if (actionSupported(info, action)){ if (actionSupported(info, action)){
matches << windowMatch(info, action); matches << windowMatch(info, action, 1.0, Plasma::QueryMatch::ExactMatch);
} }
} }
@ -316,7 +316,7 @@ void WindowsRunner::match(Plasma::RunnerContext& context)
} }
// check for matching desktops by name // check for matching desktops by name
foreach (const QString& desktopName, m_desktopNames) { foreach (const QString &desktopName, m_desktopNames) {
int desktop = m_desktopNames.indexOf(desktopName) +1; int desktop = m_desktopNames.indexOf(desktopName) +1;
if (desktopName.contains(term, Qt::CaseInsensitive)) { if (desktopName.contains(term, Qt::CaseInsensitive)) {
// desktop name matches - offer switch to // desktop name matches - offer switch to
@ -342,7 +342,7 @@ void WindowsRunner::match(Plasma::RunnerContext& context)
} }
} }
void WindowsRunner::run(const Plasma::RunnerContext& context, const Plasma::QueryMatch& match) void WindowsRunner::run(const Plasma::RunnerContext& context, const Plasma::QueryMatch &match)
{ {
Q_UNUSED(context) Q_UNUSED(context)
// check if it's a desktop // check if it's a desktop
@ -356,57 +356,63 @@ void WindowsRunner::run(const Plasma::RunnerContext& context, const Plasma::Quer
WId w = WId(parts[1].toULong()); WId w = WId(parts[1].toULong());
KWindowInfo info = m_windows[w]; KWindowInfo info = m_windows[w];
switch (action) { switch (action) {
case ActivateAction: case ActivateAction: {
KWindowSystem::forceActiveWindow(w); KWindowSystem::forceActiveWindow(w);
break; break;
case CloseAction:
{
NETRootInfo ri(QX11Info::display(), NET::CloseWindow);
ri.closeWindowRequest(w);
break;
} }
case MinimizeAction: case CloseAction: {
if (info.isMinimized()) { NETRootInfo ri(QX11Info::display(), NET::CloseWindow);
KWindowSystem::unminimizeWindow(w); ri.closeWindowRequest(w);
} else { break;
KWindowSystem::minimizeWindow(w);
} }
break; case MinimizeAction: {
case MaximizeAction: if (info.isMinimized()) {
if (info.hasState(NET::Max)) { KWindowSystem::unminimizeWindow(w);
KWindowSystem::clearState(w, NET::Max); } else {
} else { KWindowSystem::minimizeWindow(w);
KWindowSystem::setState(w, NET::Max); }
break;
} }
break; case MaximizeAction: {
case FullscreenAction: if (info.hasState(NET::Max)) {
if (info.hasState(NET::FullScreen)) { KWindowSystem::clearState(w, NET::Max);
KWindowSystem::clearState(w, NET::FullScreen); } else {
} else { KWindowSystem::setState(w, NET::Max);
KWindowSystem::setState(w, NET::FullScreen); }
break;
} }
break; case FullscreenAction: {
case ShadeAction: if (info.hasState(NET::FullScreen)) {
if (info.hasState(NET::Shaded)) { KWindowSystem::clearState(w, NET::FullScreen);
KWindowSystem::clearState(w, NET::Shaded); } else {
} else { KWindowSystem::setState(w, NET::FullScreen);
KWindowSystem::setState(w, NET::Shaded); }
break;
} }
break; case ShadeAction: {
case KeepAboveAction: if (info.hasState(NET::Shaded)) {
if (info.hasState(NET::KeepAbove)) { KWindowSystem::clearState(w, NET::Shaded);
KWindowSystem::clearState(w, NET::KeepAbove); } else {
} else { KWindowSystem::setState(w, NET::Shaded);
KWindowSystem::setState(w, NET::KeepAbove); }
break;
} }
break; case KeepAboveAction: {
case KeepBelowAction: if (info.hasState(NET::KeepAbove)) {
if (info.hasState(NET::KeepBelow)) { KWindowSystem::clearState(w, NET::KeepAbove);
KWindowSystem::clearState(w, NET::KeepBelow); } else {
} else { KWindowSystem::setState(w, NET::KeepAbove);
KWindowSystem::setState(w, NET::KeepBelow); }
break;
}
case KeepBelowAction: {
if (info.hasState(NET::KeepBelow)) {
KWindowSystem::clearState(w, NET::KeepBelow);
} else {
KWindowSystem::setState(w, NET::KeepBelow);
}
break;
} }
break;
} }
} }
@ -433,7 +439,7 @@ Plasma::QueryMatch WindowsRunner::windowMatch(const KWindowInfo& info, WindowAct
{ {
Plasma::QueryMatch match(this); Plasma::QueryMatch match(this);
match.setType(type); match.setType(type);
match.setData(QString(QString::number((int)action) + "_" + QString::number(info.win()))); match.setData(QString::number((int)action) + QLatin1String("_") + QString::number(info.win()));
match.setIcon(m_icons[info.win()]); match.setIcon(m_icons[info.win()]);
match.setText(info.name()); match.setText(info.name());
QString desktopName; QString desktopName;
@ -447,55 +453,70 @@ Plasma::QueryMatch WindowsRunner::windowMatch(const KWindowInfo& info, WindowAct
desktopName = KWindowSystem::desktopName(desktop); desktopName = KWindowSystem::desktopName(desktop);
} }
switch (action) { switch (action) {
case CloseAction: case CloseAction: {
match.setSubtext(i18n("Close running window on %1", desktopName)); match.setSubtext(i18n("Close running window on %1", desktopName));
break; break;
case MinimizeAction: }
match.setSubtext(i18n("(Un)minimize running window on %1", desktopName)); case MinimizeAction: {
break; match.setSubtext(i18n("(Un)minimize running window on %1", desktopName));
case MaximizeAction: break;
match.setSubtext(i18n("Maximize/restore running window on %1", desktopName)); }
break; case MaximizeAction: {
case FullscreenAction: match.setSubtext(i18n("Maximize/restore running window on %1", desktopName));
match.setSubtext(i18n("Toggle fullscreen for running window on %1", desktopName)); break;
break; }
case ShadeAction: case FullscreenAction: {
match.setSubtext(i18n("(Un)shade running window on %1", desktopName)); match.setSubtext(i18n("Toggle fullscreen for running window on %1", desktopName));
break; break;
case KeepAboveAction: }
match.setSubtext(i18n("Toggle keep above for running window on %1", desktopName)); case ShadeAction: {
break; match.setSubtext(i18n("(Un)shade running window on %1", desktopName));
case KeepBelowAction: break;
match.setSubtext(i18n("Toggle keep below running window on %1", desktopName)); }
break; case KeepAboveAction: {
case ActivateAction: match.setSubtext(i18n("Toggle keep above for running window on %1", desktopName));
default: break;
match.setSubtext(i18n("Activate running window on %1", desktopName)); }
break; case KeepBelowAction: {
match.setSubtext(i18n("Toggle keep below running window on %1", desktopName));
break;
}
case ActivateAction:
default: {
match.setSubtext(i18n("Activate running window on %1", desktopName));
break;
}
} }
match.setRelevance(relevance); match.setRelevance(relevance);
return match; return match;
} }
bool WindowsRunner::actionSupported(const KWindowInfo& info, WindowAction action) bool WindowsRunner::actionSupported(const KWindowInfo &info, WindowAction action)
{ {
switch (action) { switch (action) {
case CloseAction: case CloseAction: {
return info.actionSupported(NET::ActionClose); return info.actionSupported(NET::ActionClose);
case MinimizeAction: }
return info.actionSupported(NET::ActionMinimize); case MinimizeAction: {
case MaximizeAction: return info.actionSupported(NET::ActionMinimize);
return info.actionSupported(NET::ActionMax); }
case ShadeAction: case MaximizeAction: {
return info.actionSupported(NET::ActionShade); return info.actionSupported(NET::ActionMax);
case FullscreenAction: }
return info.actionSupported(NET::ActionFullScreen); case ShadeAction: {
case KeepAboveAction: return info.actionSupported(NET::ActionShade);
case KeepBelowAction: }
case ActivateAction: case FullscreenAction: {
default: return info.actionSupported(NET::ActionFullScreen);
return true; }
case KeepAboveAction:
case KeepBelowAction:
case ActivateAction:
default: {
return true;
}
} }
Q_UNREACHABLE();
} }
#include "moc_windowsrunner.cpp" #include "moc_windowsrunner.cpp"

View file

@ -26,41 +26,39 @@ class KWindowInfo;
class WindowsRunner : public Plasma::AbstractRunner class WindowsRunner : public Plasma::AbstractRunner
{ {
Q_OBJECT Q_OBJECT
public:
WindowsRunner(QObject* parent, const QVariantList &args);
public: void match(Plasma::RunnerContext &context) final;
WindowsRunner(QObject* parent, const QVariantList &args); void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) final;
~WindowsRunner();
virtual void match(Plasma::RunnerContext& context); private Q_SLOTS:
virtual void run(const Plasma::RunnerContext& context, const Plasma::QueryMatch& match); void prepareForMatchSession();
void matchSessionComplete();
void gatherInfo();
private Q_SLOTS: private:
void prepareForMatchSession(); enum WindowAction {
void matchSessionComplete(); ActivateAction,
void gatherInfo(); CloseAction,
MinimizeAction,
MaximizeAction,
FullscreenAction,
ShadeAction,
KeepAboveAction,
KeepBelowAction
};
Plasma::QueryMatch desktopMatch(int desktop, qreal relevance = 1.0);
Plasma::QueryMatch windowMatch(const KWindowInfo &info, WindowAction action, qreal relevance,
Plasma::QueryMatch::Type type);
bool actionSupported(const KWindowInfo& info, WindowAction action);
private: QHash<WId, KWindowInfo> m_windows;
enum WindowAction { QHash<WId, QIcon> m_icons;
ActivateAction, QStringList m_desktopNames;
CloseAction,
MinimizeAction,
MaximizeAction,
FullscreenAction,
ShadeAction,
KeepAboveAction,
KeepBelowAction
};
Plasma::QueryMatch desktopMatch(int desktop, qreal relevance = 1.0);
Plasma::QueryMatch windowMatch(const KWindowInfo& info, WindowAction action, qreal relevance = 1.0,
Plasma::QueryMatch::Type type = Plasma::QueryMatch::ExactMatch);
bool actionSupported(const KWindowInfo& info, WindowAction action);
QHash<WId, KWindowInfo> m_windows; bool m_inSession;
QHash<WId, QIcon> m_icons; bool m_ready;
QStringList m_desktopNames;
bool m_inSession : 1;
bool m_ready : 1;
}; };
K_EXPORT_PLASMA_RUNNER(windows, WindowsRunner) K_EXPORT_PLASMA_RUNNER(windows, WindowsRunner)