Changeset View
Changeset View
Standalone View
Standalone View
keyboard_layout_switching.cpp
Show All 13 Lines | |||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | GNU General Public License for more details. | 15 | GNU General Public License for more details. | ||
16 | 16 | | |||
17 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License | ||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 19 | *********************************************************************/ | ||
20 | #include "keyboard_layout_switching.h" | 20 | #include "keyboard_layout_switching.h" | ||
21 | #include "keyboard_layout.h" | 21 | #include "keyboard_layout.h" | ||
22 | #include "abstract_client.h" | ||||
23 | #include "deleted.h" | ||||
22 | #include "virtualdesktops.h" | 24 | #include "virtualdesktops.h" | ||
25 | #include "workspace.h" | ||||
23 | #include "xkb.h" | 26 | #include "xkb.h" | ||
24 | 27 | | |||
25 | namespace KWin | 28 | namespace KWin | ||
26 | { | 29 | { | ||
27 | 30 | | |||
28 | namespace KeyboardLayoutSwitching | 31 | namespace KeyboardLayoutSwitching | ||
29 | { | 32 | { | ||
30 | 33 | | |||
Show All 18 Lines | 51 | { | |||
49 | return m_xkb->currentLayout(); | 52 | return m_xkb->currentLayout(); | ||
50 | } | 53 | } | ||
51 | 54 | | |||
52 | Policy *Policy::create(Xkb *xkb, KeyboardLayout *layout, const QString &policy) | 55 | Policy *Policy::create(Xkb *xkb, KeyboardLayout *layout, const QString &policy) | ||
53 | { | 56 | { | ||
54 | if (policy.toLower() == QStringLiteral("desktop")) { | 57 | if (policy.toLower() == QStringLiteral("desktop")) { | ||
55 | return new VirtualDesktopPolicy(xkb, layout); | 58 | return new VirtualDesktopPolicy(xkb, layout); | ||
56 | } | 59 | } | ||
60 | if (policy.toLower() == QStringLiteral("window")) { | ||||
61 | return new WindowPolicy(xkb, layout); | ||||
62 | } | ||||
57 | return new GlobalPolicy(xkb, layout); | 63 | return new GlobalPolicy(xkb, layout); | ||
58 | } | 64 | } | ||
59 | 65 | | |||
60 | GlobalPolicy::GlobalPolicy(Xkb *xkb, KeyboardLayout *layout) | 66 | GlobalPolicy::GlobalPolicy(Xkb *xkb, KeyboardLayout *layout) | ||
61 | : Policy(xkb, layout) | 67 | : Policy(xkb, layout) | ||
62 | { | 68 | { | ||
63 | } | 69 | } | ||
64 | 70 | | |||
65 | GlobalPolicy::~GlobalPolicy() = default; | 71 | GlobalPolicy::~GlobalPolicy() = default; | ||
66 | 72 | | |||
67 | VirtualDesktopPolicy::VirtualDesktopPolicy(Xkb *xkb, KeyboardLayout *layout) | 73 | VirtualDesktopPolicy::VirtualDesktopPolicy(Xkb *xkb, KeyboardLayout *layout) | ||
68 | : Policy(xkb, layout) | 74 | : Policy(xkb, layout) | ||
69 | { | 75 | { | ||
70 | connect(VirtualDesktopManager::self(), &VirtualDesktopManager::currentChanged, this, &VirtualDesktopPolicy::desktopChanged); | 76 | connect(VirtualDesktopManager::self(), &VirtualDesktopManager::currentChanged, this, &VirtualDesktopPolicy::desktopChanged); | ||
71 | } | 77 | } | ||
72 | 78 | | |||
73 | VirtualDesktopPolicy::~VirtualDesktopPolicy() = default; | 79 | VirtualDesktopPolicy::~VirtualDesktopPolicy() = default; | ||
74 | 80 | | |||
75 | void VirtualDesktopPolicy::clearCache() | 81 | void VirtualDesktopPolicy::clearCache() | ||
76 | { | 82 | { | ||
77 | m_layouts.clear(); | 83 | m_layouts.clear(); | ||
78 | } | 84 | } | ||
79 | 85 | | |||
86 | namespace { | ||||
87 | template <typename T, typename U> | ||||
88 | quint32 getLayout(const T &layouts, const U &reference) | ||||
89 | { | ||||
90 | auto it = layouts.constFind(reference); | ||||
91 | if (it == layouts.constEnd()) { | ||||
92 | return 0; | ||||
93 | } else { | ||||
94 | return it.value(); | ||||
95 | } | ||||
96 | } | ||||
97 | } | ||||
98 | | ||||
80 | void VirtualDesktopPolicy::desktopChanged() | 99 | void VirtualDesktopPolicy::desktopChanged() | ||
81 | { | 100 | { | ||
82 | auto d = VirtualDesktopManager::self()->currentDesktop(); | 101 | auto d = VirtualDesktopManager::self()->currentDesktop(); | ||
83 | if (!d) { | 102 | if (!d) { | ||
84 | return; | 103 | return; | ||
85 | } | 104 | } | ||
86 | auto it = m_layouts.constFind(d); | 105 | setLayout(getLayout(m_layouts, d)); | ||
87 | if (it == m_layouts.constEnd()) { | | |||
88 | // new desktop - go to default; | | |||
89 | setLayout(0); | | |||
90 | } else { | | |||
91 | setLayout(it.value()); | | |||
92 | } | | |||
93 | } | 106 | } | ||
94 | 107 | | |||
95 | void VirtualDesktopPolicy::layoutChanged() | 108 | void VirtualDesktopPolicy::layoutChanged() | ||
96 | { | 109 | { | ||
97 | auto d = VirtualDesktopManager::self()->currentDesktop(); | 110 | auto d = VirtualDesktopManager::self()->currentDesktop(); | ||
98 | if (!d) { | 111 | if (!d) { | ||
99 | return; | 112 | return; | ||
100 | } | 113 | } | ||
Show All 9 Lines | |||||
110 | } else { | 123 | } else { | ||
111 | if (it.value() == l) { | 124 | if (it.value() == l) { | ||
112 | return; | 125 | return; | ||
113 | } | 126 | } | ||
114 | it.value() = l; | 127 | it.value() = l; | ||
115 | } | 128 | } | ||
116 | } | 129 | } | ||
117 | 130 | | |||
131 | WindowPolicy::WindowPolicy(KWin::Xkb* xkb, KWin::KeyboardLayout* layout) | ||||
132 | : Policy(xkb, layout) | ||||
133 | { | ||||
134 | connect(workspace(), &Workspace::clientActivated, this, | ||||
135 | [this] (AbstractClient *c) { | ||||
136 | if (!c) { | ||||
137 | return; | ||||
138 | } | ||||
139 | // ignore some special types | ||||
140 | if (c->isDesktop() || c->isDock()) { | ||||
141 | return; | ||||
142 | } | ||||
143 | setLayout(getLayout(m_layouts, c)); | ||||
144 | } | ||||
145 | ); | ||||
146 | } | ||||
147 | | ||||
148 | WindowPolicy::~WindowPolicy() | ||||
149 | { | ||||
150 | } | ||||
151 | | ||||
152 | void WindowPolicy::clearCache() | ||||
153 | { | ||||
154 | m_layouts.clear(); | ||||
155 | } | ||||
156 | | ||||
157 | void WindowPolicy::layoutChanged() | ||||
158 | { | ||||
159 | auto c = workspace()->activeClient(); | ||||
160 | if (!c) { | ||||
161 | return; | ||||
162 | } | ||||
163 | // ignore some special types | ||||
164 | if (c->isDesktop() || c->isDock()) { | ||||
165 | return; | ||||
166 | } | ||||
167 | | ||||
168 | auto it = m_layouts.find(c); | ||||
169 | const auto l = layout(); | ||||
170 | if (it == m_layouts.constEnd()) { | ||||
171 | m_layouts.insert(c, l); | ||||
172 | connect(c, &AbstractClient::windowClosed, this, | ||||
173 | [this, c] { | ||||
174 | m_layouts.remove(c); | ||||
175 | } | ||||
176 | ); | ||||
177 | } else { | ||||
178 | if (it.value() == l) { | ||||
179 | return; | ||||
180 | } | ||||
181 | it.value() = l; | ||||
182 | } | ||||
183 | } | ||||
184 | | ||||
118 | } | 185 | } | ||
119 | } | 186 | } |