Changeset View
Standalone View
containmentactions/switchdesktop/desktop.cpp
Show All 13 Lines | |||||
14 | * You should have received a copy of the GNU Library General Public | 14 | * You should have received a copy of the GNU Library General Public | ||
15 | * License along with this program; if not, write to the | 15 | * License along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | 16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "desktop.h" | 20 | #include "desktop.h" | ||
21 | 21 | | |||
22 | #include <KSharedConfig> | ||||
23 | #include <QAbstractButton> | ||||
22 | #include <QAction> | 24 | #include <QAction> | ||
23 | #include <QWheelEvent> | 25 | #include <QButtonGroup> | ||
26 | #include <QCheckBox> | ||||
27 | #include <QVBoxLayout> | ||||
24 | 28 | | |||
25 | #include <QDebug> | 29 | #include <QDebug> | ||
26 | #include <KWindowSystem> | 30 | #include <KWindowSystem> | ||
27 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
28 | 32 | | |||
33 | namespace { | ||||
34 | constexpr bool invertMouseWheelDefault = false; | ||||
35 | constexpr const char *cfgKeyProperty = "cfgKey"; | ||||
36 | bool readKwinrcRollOverDesktops() { | ||||
totto: The rollover option is from another kde component (kwin), is there a way to get notified… | |||||
I have this coming: davidedmundson: I have this coming:
https://phabricator.kde.org/D13034
| |||||
totto: Great, will keep an eye on that. | |||||
37 | auto cfg = KSharedConfig::openConfig("kwinrc"); | ||||
38 | return KConfigGroup{cfg, "Windows"}.readEntry<bool>("RollOverDesktops", true); | ||||
39 | } | ||||
40 | } | ||||
41 | | ||||
29 | SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args) | 42 | SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args) | ||
30 | : Plasma::ContainmentActions(parent, args) | 43 | : Plasma::ContainmentActions(parent, args) | ||
44 | , m_invertMouseWheel{QSharedPointer<explicitBool>::create(explicitBool{invertMouseWheelDefault})} | ||||
The m_kwinrcRollOverDesktops variable does not work at runtime because two instances of SwitchDesktop are created: One when plasma-workspace/KDE is started, a second when the settings window is opened. The actual desktop switching is done by the former, but subsequent changes to the kwinrc/RollOverDesktops setting are only seen and set by the latter. So the desktop rollover setting remains what it was when KDE was started. Is there a way to sync these without writing another config key? Or wait for "Add mechanism to notify other clients of config changes over DBus" / https://phabricator.kde.org/D13034 ? totto: The m_kwinrcRollOverDesktops variable does not work at runtime because two instances of… | |||||
45 | , m_kwinrcRollOverDesktops(QSharedPointer<explicitBool>::create(explicitBool{readKwinrcRollOverDesktops()})) | ||||
31 | { | 46 | { | ||
32 | } | 47 | } | ||
33 | 48 | | |||
34 | SwitchDesktop::~SwitchDesktop() | 49 | SwitchDesktop::~SwitchDesktop() | ||
35 | { | 50 | { | ||
36 | qDeleteAll(m_actions); | 51 | qDeleteAll(m_actions); | ||
37 | } | 52 | } | ||
38 | 53 | | |||
Show All 26 Lines | 79 | for (int i = 1; i <= numDesktops; ++i) { | |||
65 | QAction *action = m_actions.value(i); | 80 | QAction *action = m_actions.value(i); | ||
66 | action->setEnabled(i != currentDesktop); | 81 | action->setEnabled(i != currentDesktop); | ||
67 | list << action; | 82 | list << action; | ||
68 | } | 83 | } | ||
69 | 84 | | |||
70 | return list; | 85 | return list; | ||
71 | } | 86 | } | ||
72 | 87 | | |||
88 | void SwitchDesktop::restore(const KConfigGroup &config) | ||||
89 | { | ||||
90 | Option invertWheel{ | ||||
91 | "invert_mouse_wheel", | ||||
92 | "Invert mouse wheel", | ||||
93 | m_invertMouseWheel, | ||||
94 | false | ||||
95 | }; | ||||
96 | invertWheel.valuePtr->val = config.readEntry(invertWheel.cfgKey, invertWheel.valuePtr->val); | ||||
97 | m_options[invertWheel.cfgKey] = invertWheel; | ||||
98 | | ||||
99 | Option rollOverDesktop{ | ||||
100 | "rollover_kwinrc_readonly", | ||||
101 | "Desktops wrap around (set in kwin)", | ||||
102 | m_kwinrcRollOverDesktops, | ||||
103 | true | ||||
104 | }; | ||||
105 | m_kwinrcRollOverDesktops->val = readKwinrcRollOverDesktops(); | ||||
106 | m_options[rollOverDesktop.cfgKey] = rollOverDesktop; | ||||
107 | } | ||||
108 | | ||||
109 | QWidget *SwitchDesktop::createConfigurationInterface(QWidget *parent) | ||||
110 | { | ||||
111 | m_kwinrcRollOverDesktops->val = readKwinrcRollOverDesktops(); | ||||
112 | | ||||
113 | QWidget *widget = new QWidget(parent); | ||||
114 | QVBoxLayout *lay = new QVBoxLayout(); | ||||
115 | widget->setLayout(lay); | ||||
116 | widget->setWindowTitle(i18nc("plasma_containmentactions_switchdesktop", "Configure Switch Desktop")); | ||||
117 | m_settingsButtons = new QButtonGroup(widget); | ||||
118 | m_settingsButtons->setExclusive(false); | ||||
119 | | ||||
120 | for (const Option &e : m_options) { | ||||
121 | QCheckBox *item = new QCheckBox(widget); | ||||
122 | item->setText(i18nc(e.cfgKey.toStdString().c_str(), | ||||
Unforunately, that's not how our i18n works. a script greps for i18n("some text here") to generate the english translations. davidedmundson: Unforunately, that's not how our i18n works.
a script greps for i18n("some text here") to… | |||||
123 | e.name.toStdString().c_str())); | ||||
124 | item->setChecked(e.valuePtr->val); | ||||
125 | if (e.readOnly) { | ||||
126 | item->setEnabled(false); | ||||
127 | } else { | ||||
128 | item->setProperty(cfgKeyProperty, e.cfgKey); | ||||
129 | } | ||||
130 | lay->addWidget(item); | ||||
131 | m_settingsButtons->addButton(item); | ||||
132 | } | ||||
133 | | ||||
134 | return widget; | ||||
135 | } | ||||
136 | | ||||
137 | void SwitchDesktop::configurationAccepted() | ||||
138 | { | ||||
139 | m_kwinrcRollOverDesktops->val = readKwinrcRollOverDesktops(); | ||||
140 | | ||||
141 | QList<QAbstractButton *> buttons = m_settingsButtons->buttons(); | ||||
142 | QListIterator<QAbstractButton *> it(buttons); | ||||
143 | for (QAbstractButton *b : buttons) { | ||||
144 | QVariant cfgKey = b->property(cfgKeyProperty); | ||||
145 | if (cfgKey.isValid() && m_options.contains(cfgKey.toString())) { | ||||
146 | m_options[cfgKey.toString()].valuePtr->val = b->isChecked(); | ||||
147 | } | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | void SwitchDesktop::save(KConfigGroup &config) | ||||
152 | { | ||||
153 | for (const Option &e : m_options) { | ||||
154 | if (!e.readOnly) { | ||||
155 | config.writeEntry(e.cfgKey, e.valuePtr->val); | ||||
156 | } | ||||
157 | } | ||||
158 | } | ||||
159 | | ||||
73 | void SwitchDesktop::switchTo() | 160 | void SwitchDesktop::switchTo() | ||
74 | { | 161 | { | ||
75 | QAction *action = qobject_cast<QAction *>(sender()); | 162 | QAction *action = qobject_cast<QAction *>(sender()); | ||
76 | if (!action) { | 163 | if (!action) { | ||
77 | return; | 164 | return; | ||
78 | } | 165 | } | ||
79 | 166 | | |||
80 | const int desktop = action->data().toInt(); | 167 | const int desktop = action->data().toInt(); | ||
81 | KWindowSystem::setCurrentDesktop(desktop); | 168 | KWindowSystem::setCurrentDesktop(desktop); | ||
82 | } | 169 | } | ||
83 | 170 | | |||
84 | void SwitchDesktop::performNextAction() | 171 | namespace { | ||
85 | { | 172 | void switchDesktop(bool next, bool rollover, bool invert) { | ||
davidedmundson: if you turn this into a method you don't need the rollover/invert args | |||||
86 | const int numDesktops = KWindowSystem::numberOfDesktops(); | 173 | const int numDesktops = KWindowSystem::numberOfDesktops(); | ||
87 | const int currentDesktop = KWindowSystem::currentDesktop(); | 174 | const int currentDesktop = KWindowSystem::currentDesktop(); | ||
175 | if (invert) { | ||||
176 | next = !next; | ||||
177 | } | ||||
178 | bool wouldRollover = (next && currentDesktop == numDesktops) || (!next && currentDesktop == 1); | ||||
179 | if (wouldRollover && !rollover) { | ||||
180 | return; | ||||
181 | } | ||||
182 | if (next) { | ||||
88 | KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); | 183 | KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); | ||
184 | } else { | ||||
185 | KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); | ||||
186 | } | ||||
187 | } | ||||
188 | } | ||||
189 | | ||||
190 | void SwitchDesktop::performNextAction() | ||||
191 | { | ||||
192 | switchDesktop(true, m_kwinrcRollOverDesktops, m_invertMouseWheel->val); | ||||
89 | } | 193 | } | ||
90 | 194 | | |||
91 | void SwitchDesktop::performPreviousAction() | 195 | void SwitchDesktop::performPreviousAction() | ||
92 | { | 196 | { | ||
93 | const int numDesktops = KWindowSystem::numberOfDesktops(); | 197 | switchDesktop(false, m_kwinrcRollOverDesktops, m_invertMouseWheel->val); | ||
94 | const int currentDesktop = KWindowSystem::currentDesktop(); | | |||
95 | KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); | | |||
96 | } | 198 | } | ||
97 | 199 | | |||
98 | K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json") | 200 | K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json") | ||
99 | 201 | | |||
100 | #include "desktop.moc" | 202 | #include "desktop.moc" |
The rollover option is from another kde component (kwin), is there a way to get notified whenever this config changes?