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 const char *cfgKeyProperty = "cfgKey"; | ||||
35 | | ||||
36 | constexpr bool invertMouseWheelDefault = false; | ||||
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 | const QString invertMouseCfgKey = "invert mouse wheel"; | ||||
38 | | ||||
39 | bool readKwinrcRollOverDesktops() { | ||||
40 | auto cfg = KSharedConfig::openConfig("kwinrc"); | ||||
41 | return KConfigGroup{cfg, "Windows"}.readEntry<bool>("RollOverDesktops", true); | ||||
42 | } | ||||
43 | } | ||||
44 | | ||||
29 | SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args) | 45 | SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args) | ||
30 | : Plasma::ContainmentActions(parent, args) | 46 | : Plasma::ContainmentActions{parent, args} | ||
47 | , m_kwinrcRollOverDesktops{readKwinrcRollOverDesktops()} | ||||
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… | |||||
31 | { | 48 | { | ||
32 | } | 49 | } | ||
33 | 50 | | |||
34 | SwitchDesktop::~SwitchDesktop() | 51 | SwitchDesktop::~SwitchDesktop() | ||
35 | { | 52 | { | ||
36 | qDeleteAll(m_actions); | 53 | qDeleteAll(m_actions); | ||
37 | } | 54 | } | ||
38 | 55 | | |||
39 | 56 | | |||
40 | QList<QAction*> SwitchDesktop::contextualActions() | 57 | QList<QAction *> SwitchDesktop::contextualActions() | ||
41 | { | 58 | { | ||
42 | QList<QAction*> list; | 59 | QList<QAction *> list; | ||
43 | 60 | | |||
44 | const int numDesktops = KWindowSystem::numberOfDesktops(); | 61 | const int numDesktops = KWindowSystem::numberOfDesktops(); | ||
45 | const int currentDesktop = KWindowSystem::currentDesktop(); | 62 | const int currentDesktop = KWindowSystem::currentDesktop(); | ||
46 | 63 | | |||
47 | //Is it either the first time or the desktop number changed? | 64 | //Is it either the first time or the desktop number changed? | ||
48 | if (m_actions.count() < numDesktops) { | 65 | if (m_actions.count() < numDesktops) { | ||
49 | for (int i = m_actions.count() + 1; i <= numDesktops; ++i) { | 66 | for (int i = m_actions.count() + 1; i <= numDesktops; ++i) { | ||
50 | QString name = KWindowSystem::desktopName(i); | 67 | QString name = KWindowSystem::desktopName(i); | ||
Show All 14 Lines | 81 | for (int i = 1; i <= numDesktops; ++i) { | |||
65 | QAction *action = m_actions.value(i); | 82 | QAction *action = m_actions.value(i); | ||
66 | action->setEnabled(i != currentDesktop); | 83 | action->setEnabled(i != currentDesktop); | ||
67 | list << action; | 84 | list << action; | ||
68 | } | 85 | } | ||
69 | 86 | | |||
70 | return list; | 87 | return list; | ||
71 | } | 88 | } | ||
72 | 89 | | |||
90 | void SwitchDesktop::restore(const KConfigGroup &config) | ||||
91 | { | ||||
92 | Option invertWheel{ | ||||
93 | invertMouseCfgKey, | ||||
94 | i18nc("plasma_containmentactions_switchdesktop", "Invert mouse wheel"), | ||||
95 | config.readEntry(invertMouseCfgKey, invertMouseWheelDefault) | ||||
96 | }; | ||||
97 | m_options[invertMouseCfgKey] = invertWheel; | ||||
98 | } | ||||
99 | | ||||
100 | QWidget *SwitchDesktop::createConfigurationInterface(QWidget *parent) | ||||
101 | { | ||||
102 | m_kwinrcRollOverDesktops = readKwinrcRollOverDesktops(); | ||||
103 | | ||||
104 | QWidget *widget = new QWidget(parent); | ||||
105 | QVBoxLayout *lay = new QVBoxLayout(); | ||||
106 | widget->setLayout(lay); | ||||
107 | widget->setWindowTitle(i18nc("plasma_containmentactions_switchdesktop", "Configure Switch Desktop")); | ||||
108 | m_settingsButtons = new QButtonGroup(widget); | ||||
109 | m_settingsButtons->setExclusive(false); | ||||
110 | | ||||
111 | for (const Option &e : m_options) { | ||||
112 | QCheckBox *item = new QCheckBox(widget); | ||||
113 | item->setText(e.description); | ||||
114 | item->setChecked(e.value); | ||||
115 | item->setProperty(cfgKeyProperty, e.cfgKey); | ||||
116 | lay->addWidget(item); | ||||
117 | m_settingsButtons->addButton(item); | ||||
118 | } | ||||
119 | | ||||
120 | return widget; | ||||
121 | } | ||||
122 | | ||||
123 | void SwitchDesktop::configurationAccepted() | ||||
124 | { | ||||
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… | |||||
125 | m_kwinrcRollOverDesktops = readKwinrcRollOverDesktops(); | ||||
126 | | ||||
127 | QList<QAbstractButton *> buttons = m_settingsButtons->buttons(); | ||||
128 | QListIterator<QAbstractButton *> it(buttons); | ||||
129 | for (QAbstractButton *b : buttons) { | ||||
130 | QVariant cfgKey = b->property(cfgKeyProperty); | ||||
131 | if (cfgKey.isValid() && m_options.contains(cfgKey.toString())) { | ||||
132 | m_options[cfgKey.toString()].value = b->isChecked(); | ||||
133 | } | ||||
134 | } | ||||
135 | } | ||||
136 | | ||||
137 | void SwitchDesktop::save(KConfigGroup &config) | ||||
138 | { | ||||
139 | for (const Option &e : m_options) { | ||||
140 | config.writeEntry(e.cfgKey, e.value); | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
73 | void SwitchDesktop::switchTo() | 144 | void SwitchDesktop::switchTo() | ||
74 | { | 145 | { | ||
75 | QAction *action = qobject_cast<QAction *>(sender()); | 146 | QAction *action = qobject_cast<QAction *>(sender()); | ||
76 | if (!action) { | 147 | if (!action) { | ||
77 | return; | 148 | return; | ||
78 | } | 149 | } | ||
79 | 150 | | |||
80 | const int desktop = action->data().toInt(); | 151 | const int desktop = action->data().toInt(); | ||
81 | KWindowSystem::setCurrentDesktop(desktop); | 152 | KWindowSystem::setCurrentDesktop(desktop); | ||
82 | } | 153 | } | ||
83 | 154 | | |||
84 | void SwitchDesktop::performNextAction() | 155 | void SwitchDesktop::switchDesktop(bool next) { | ||
85 | { | | |||
86 | const int numDesktops = KWindowSystem::numberOfDesktops(); | 156 | const int numDesktops = KWindowSystem::numberOfDesktops(); | ||
davidedmundson: if you turn this into a method you don't need the rollover/invert args | |||||
87 | const int currentDesktop = KWindowSystem::currentDesktop(); | 157 | const int currentDesktop = KWindowSystem::currentDesktop(); | ||
158 | if (m_options.contains(invertMouseCfgKey) && m_options[invertMouseCfgKey].value) { | ||||
159 | next = !next; | ||||
160 | } | ||||
161 | bool wouldRollover = (next && currentDesktop == numDesktops) || (!next && currentDesktop == 1); | ||||
162 | if (wouldRollover && !m_kwinrcRollOverDesktops) { | ||||
163 | return; | ||||
164 | } | ||||
165 | if (next) { | ||||
88 | KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); | 166 | KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); | ||
167 | } else { | ||||
168 | KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); | ||||
169 | } | ||||
170 | } | ||||
171 | | ||||
172 | void SwitchDesktop::performNextAction() | ||||
173 | { | ||||
174 | switchDesktop(true); | ||||
89 | } | 175 | } | ||
90 | 176 | | |||
91 | void SwitchDesktop::performPreviousAction() | 177 | void SwitchDesktop::performPreviousAction() | ||
92 | { | 178 | { | ||
93 | const int numDesktops = KWindowSystem::numberOfDesktops(); | 179 | switchDesktop(false); | ||
94 | const int currentDesktop = KWindowSystem::currentDesktop(); | | |||
95 | KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); | | |||
96 | } | 180 | } | ||
97 | 181 | | |||
98 | K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json") | 182 | K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json") | ||
99 | 183 | | |||
100 | #include "desktop.moc" | 184 | #include "desktop.moc" |
The rollover option is from another kde component (kwin), is there a way to get notified whenever this config changes?