@@ -7,6 +7,7 @@ const Util = imports.misc.util;
7
7
const Gio = imports . gi . Gio ;
8
8
const Cairo = imports . cairo ;
9
9
const Signals = imports . signals ;
10
+ const KeyboardManager = imports . ui . keyboardManager ;
10
11
11
12
const PANEL_EDIT_MODE_KEY = "panel-edit-mode" ;
12
13
@@ -112,120 +113,18 @@ class LayoutMenuItem extends PopupMenu.PopupBaseMenuItem {
112
113
}
113
114
}
114
115
115
- class KbdLayoutController {
116
- constructor ( ) {
117
- this . _bus_watch_id = 0 ;
118
- this . _layouts = [ ] ;
119
- this . _current_layout_idx = 0 ;
120
- }
121
-
122
- applet_added ( ) {
123
- this . _xappController = new XApp . KbdLayoutController ( ) ;
124
- this . _xappController . connect ( 'layout-changed' , ( ) => this . _on_layout_changed ( ) ) ;
125
- this . _xappController . connect ( 'config-changed' , ( ) => this . _on_config_changed ( ) ) ;
126
-
127
- if ( this . _bus_watch_id === 0 ) {
128
- const on_ibus = ( is_active ) => {
129
- this . _ibus_active = is_active ;
130
- this . emit ( "layout-changed" ) ;
131
- this . emit ( "config-changed" ) ;
132
- } ;
133
- this . _bus_watch_id = Gio . DBus . session . watch_name (
134
- "org.fcitx.Fcitx" , Gio . BusNameWatcherFlags . NONE ,
135
- ( ) => on_ibus ( true ) , ( ) => on_ibus ( false )
136
- ) ;
137
- }
138
-
139
- this . _on_config_changed ( ) ;
140
- }
141
-
142
- applet_removed ( ) {
143
- if ( this . _bus_watch_id > 0 ) {
144
- Gio . DBus . session . unwatch_name ( this . _bus_watch_id ) ;
145
- this . _bus_watch_id = 0 ;
146
- }
147
- }
148
-
149
- _retrieve_current_layout_idx ( ) {
150
- let idx = this . _xappController . get_enabled ( ) ?
151
- this . _xappController . get_current_group ( ) : 0 ;
152
- if ( idx >= this . get_layouts_count ( ) ) {
153
- this . set_current_layout_idx ( 0 ) ;
154
- } else {
155
- this . _current_layout_idx = idx ;
156
- }
157
- }
158
-
159
- _on_layout_changed ( ) {
160
- this . _retrieve_current_layout_idx ( ) ;
161
- this . emit ( "layout-changed" ) ;
162
- }
163
-
164
- _on_config_changed ( ) {
165
- if ( this . _xappController . get_enabled ( ) ) {
166
- const layouts = [ ] ;
167
-
168
- const groups = this . _xappController . get_all_names ( ) ;
169
- for ( let i = 0 ; i < groups . length ; i ++ ) {
170
- layouts . push ( {
171
- display_name : groups [ i ] ,
172
- flag_name : this . _xappController . get_icon_name_for_group ( i ) ,
173
- layout_dupe_id : this . _xappController . get_flag_id_for_group ( i ) ,
174
- short_name : this . _xappController . get_short_group_label_for_group ( i ) ,
175
- variant_name : this . _xappController . get_variant_label_for_group ( i ) ,
176
- } ) ;
177
- }
178
- this . _layouts = layouts ;
179
- this . _retrieve_current_layout_idx ( ) ;
180
- } else {
181
- this . _layouts = [ ] ;
182
- this . _current_layout_idx = 0 ;
183
- }
184
-
185
- this . emit ( "config-changed" ) ;
186
- }
187
-
188
- have_multiple_layouts ( ) {
189
- return ( this . get_layouts_count ( ) > 1 ) || false ;
190
- }
191
-
192
- get_layouts_count ( ) {
193
- return this . _layouts ?. length || 0 ;
194
- }
195
-
196
- get_current_layout_idx ( ) {
197
- return this . _current_layout_idx || 0 ;
198
- }
199
-
200
- set_current_layout_idx ( idx ) {
201
- if ( this . _xappController . get_enabled ( ) ) {
202
- this . _xappController . set_current_group ( idx ) ;
203
- this . _current_layout_idx = idx ;
204
- }
205
- }
206
-
207
- get_layout_data ( idx ) {
208
- const layouts = this . _layouts || [ ] ;
209
- return layouts [ idx ] || { } ;
210
- }
211
-
212
- is_ibus_active ( ) {
213
- return ! ! this . _ibus_active ;
214
- }
215
- }
216
- Signals . addSignalMethods ( KbdLayoutController . prototype ) ;
217
-
218
116
class CinnamonKeyboardApplet extends Applet . TextIconApplet {
219
117
constructor ( metadata , orientation , panel_height , instance_id ) {
220
118
super ( orientation , panel_height , instance_id ) ;
221
119
222
120
this . setAllowedLayout ( Applet . AllowedLayout . BOTH ) ;
223
121
224
- this . _layoutItems = [ ] ;
225
- this . _layoutIcons = [ ] ;
122
+ this . _layoutItems = new Map ( ) ;
123
+ this . _layoutIcons = new Map ( ) ;
226
124
227
125
this . _maxSeenWidth = this . _maxSeenHeight = 0 ;
228
126
this . show_flags = this . use_upper = this . use_variants = false ;
127
+ this . _bus_watch_id = 0
229
128
230
129
try {
231
130
this . metadata = metadata ;
@@ -242,28 +141,38 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
242
141
const _syncConfig = ( ) => this . _syncConfig ( ) ;
243
142
244
143
this . desktop_settings . connect ( "changed::keyboard-layout-show-flags" , _syncConfig ) ;
245
- this . desktop_settings . connect ( "changed::keyboard-layout-use-upper" , _syncConfig ) ;
246
- this . desktop_settings . connect ( "changed::keyboard-layout-prefer-variant-names" , _syncConfig ) ;
247
144
global . settings . connect ( 'changed::' + PANEL_EDIT_MODE_KEY , ( ) => this . _onPanelEditModeChanged ( ) ) ;
248
145
249
146
this . menu . addMenuItem ( new PopupMenu . PopupSeparatorMenuItem ( ) ) ;
250
147
this . menu . addAction ( _ ( "Show Keyboard Layout" ) , ( ) => {
251
148
Main . overview . hide ( ) ;
252
- Util . spawn ( [ 'gkbd-keyboard-display' , '-g' , String ( this . _controller . get_current_layout_idx ( ) + 1 ) ] ) ;
149
+ global . log
150
+ Util . spawn ( [ 'gkbd-keyboard-display' , '-g' , String ( this . _manager . currentSource . index + 1 ) ] ) ;
253
151
} ) ;
254
152
this . menu . addAction ( _ ( "Show Character Table" ) , ( ) => {
255
153
Main . overview . hide ( ) ;
256
154
Util . spawn ( [ 'gucharmap' ] ) ;
257
155
} ) ;
258
156
this . menu . addSettingsAction ( _ ( "Keyboard Settings" ) , 'keyboard' ) ;
259
157
260
- this . _controller = new KbdLayoutController ( ) ;
158
+ this . _manager = KeyboardManager . getInputSourceManager ( ) ;
159
+ this . _manager . connect ( "sources-changed" , this . _onSourcesChanged . bind ( this ) ) ;
160
+ this . _manager . connect ( "current-source-changed" , this . _onCurrentSourceChanged . bind ( this ) ) ;
161
+ this . _syncConfig ( ) ;
261
162
}
262
163
catch ( e ) {
263
164
global . logError ( e ) ;
264
165
}
265
166
}
266
167
168
+ _onCurrentSourceChanged ( ) {
169
+ this . _syncGroup ( ) ;
170
+ }
171
+
172
+ _onSourcesChanged ( ) {
173
+ this . _syncConfig ( ) ;
174
+ }
175
+
267
176
_onPanelEditModeChanged ( ) {
268
177
if ( global . settings . get_boolean ( PANEL_EDIT_MODE_KEY ) ) {
269
178
if ( ! this . actor . visible ) {
@@ -279,10 +188,17 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
279
188
if ( global . settings . get_boolean ( PANEL_EDIT_MODE_KEY ) ) {
280
189
this . _onPanelEditModeChanged ( ) ;
281
190
}
282
- this . _controller . connect ( 'layout-changed' , ( ) => this . _syncGroup ( ) ) ;
283
- this . _controller . connect ( 'config-changed' , ( ) => this . _syncConfig ( ) ) ;
284
191
this . connect ( 'orientation-changed' , ( ) => this . on_orientation_changed ( ) ) ;
285
- this . _controller . applet_added ( ) ;
192
+
193
+ if ( this . _bus_watch_id === 0 ) {
194
+ const on_ibus = ( is_active ) => {
195
+ this . actor . visible = ! is_active ;
196
+ } ;
197
+ this . _bus_watch_id = Gio . DBus . session . watch_name (
198
+ "org.fcitx.Fcitx" , Gio . BusNameWatcherFlags . NONE ,
199
+ ( ) => on_ibus ( true ) , ( ) => on_ibus ( false )
200
+ ) ;
201
+ }
286
202
}
287
203
288
204
on_orientation_changed ( ) {
@@ -293,10 +209,14 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
293
209
_onButtonPressEvent ( actor , event ) {
294
210
// Cycle to the next layout
295
211
if ( event . get_button ( ) === 2 ) {
296
- const selected_group = this . _controller . get_current_layout_idx ( ) ;
297
- const new_group = ( selected_group + 1 ) % this . _layoutItems . length ;
298
- this . _controller . set_current_layout_idx ( new_group ) ;
212
+ let new_index = this . _manager . currentSource . index + 1 ;
213
+ if ( new_index == this . _manager . numInputSources ) {
214
+ new_index = 0 ;
215
+ }
216
+
217
+ this . _manager . activateInputSourceIndex ( new_index ) ;
299
218
}
219
+
300
220
return Applet . Applet . prototype . _onButtonPressEvent . call ( this , actor , event ) ;
301
221
}
302
222
@@ -306,34 +226,33 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
306
226
307
227
_setLayoutItems ( items ) {
308
228
this . _selectedLayout = null ;
309
- this . _layoutItems . forEach ( item => item . destroy ( ) ) ;
310
- this . _layoutItems = items || [ ] ;
229
+
230
+ this . _layoutItems . forEach ( ( v , k , m ) => v . destroy ( ) ) ;
231
+ this . _layoutItems = items || new Map ( ) ;
311
232
}
312
233
313
234
_setLayoutIcons ( icons ) {
314
- this . _layoutIcons = icons || [ ] ;
235
+ this . _layoutIcons = icons || new Map ( ) ;
315
236
}
316
237
317
- _createIcon ( layoutIndex , actorClass ) {
318
- const layoutInfo = this . _controller . get_layout_data ( layoutIndex ) ;
319
-
238
+ _createIcon ( source , actorClass ) {
320
239
let isFlagIcon = false ;
321
240
let iconActor = null ;
322
241
let iconInstance = null ;
323
- let name = layoutInfo . flag_name ;
242
+ let name = source . flagName ;
324
243
325
244
if ( this . show_flags ) {
326
245
const file = Gio . file_new_for_path ( getFlagFileName ( name ) ) ;
327
246
if ( file . query_exists ( null ) ) {
328
- iconInstance = new EmblemedIcon ( file . get_path ( ) , layoutInfo . layout_dupe_id , actorClass ) ;
247
+ iconInstance = new EmblemedIcon ( file . get_path ( ) , source . dupeId , actorClass ) ;
329
248
iconActor = iconInstance . actor ;
330
249
isFlagIcon = true ;
331
250
}
332
251
}
333
252
334
253
if ( ! isFlagIcon ) {
335
- name = this . use_variants ? layoutInfo . variant_name : layoutInfo . short_name ;
336
- name = this . use_upper ? name . toUpperCase ( ) : name ;
254
+
255
+ name = source . shortName ;
337
256
iconActor = new St . Label ( { text : name } ) ;
338
257
}
339
258
@@ -347,43 +266,36 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
347
266
_syncConfig ( ) {
348
267
this . _maxSeenWidth = this . _maxSeenHeight = 0 ;
349
268
350
- if ( ! this . _controller . have_multiple_layouts ( ) ) {
269
+ if ( ! this . _manager . multipleSources ) {
351
270
this . _setLayoutItems ( [ ] ) ;
352
271
this . menu . close ( ) ;
353
272
this . actor . hide ( ) ;
354
273
return ;
355
274
}
356
275
357
- const layoutItems = [ ] ;
358
- const layoutIcons = [ ] ;
276
+ const layoutItems = new Map ( ) ;
277
+ const layoutIcons = new Map ( ) ;
359
278
360
279
this . show_flags = this . desktop_settings . get_boolean ( "keyboard-layout-show-flags" ) ;
361
- this . use_upper = this . desktop_settings . get_boolean ( "keyboard-layout-use-upper" ) ;
362
- this . use_variants = this . desktop_settings . get_boolean ( "keyboard-layout-prefer-variant-names" ) ;
363
280
364
281
if ( this . show_flags ) {
365
282
this . _maxSeenWidth = this . _maxSeenHeight = 0 ;
366
283
}
367
284
368
285
this . actor . show ( ) ;
369
286
370
- const layoutsCount = this . _controller . get_layouts_count ( ) ;
371
-
372
- for ( let i = 0 ; i < layoutsCount ; i ++ ) {
373
- const idx = i ;
374
-
375
- const layoutInfo = this . _controller . get_layout_data ( idx ) ;
287
+ for ( const sourceId of Object . keys ( this . _manager . inputSources ) ) {
288
+ const source = this . _manager . inputSources [ sourceId ] ;
376
289
377
- const popupIconInfo = this . _createIcon ( idx , POPUP_MENU_ICON_STYLE_CLASS ) ;
290
+ const popupIconInfo = this . _createIcon ( source , POPUP_MENU_ICON_STYLE_CLASS ) ;
378
291
const menuItem = new LayoutMenuItem (
379
- ( ) => this . _controller . set_current_layout_idx ( idx ) ,
380
- popupIconInfo . iconActor , layoutInfo . display_name
292
+ ( ) => source . activate ( ) ,
293
+ popupIconInfo . iconActor , source . displayName
381
294
) ;
382
- layoutItems . push ( menuItem ) ;
383
-
384
- this . menu . addMenuItem ( menuItem , idx ) ;
385
- const appletIconInfo = this . _createIcon ( idx , APPLET_ICON_STYLE_CLASS ) ;
386
- layoutIcons . push ( appletIconInfo ) ;
295
+ layoutItems . set ( source , menuItem ) ;
296
+ this . menu . addMenuItem ( menuItem ) ;
297
+ const appletIconInfo = this . _createIcon ( source , APPLET_ICON_STYLE_CLASS ) ;
298
+ layoutIcons . set ( source , appletIconInfo ) ;
387
299
}
388
300
389
301
this . _setLayoutItems ( layoutItems ) ;
@@ -393,9 +305,9 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
393
305
}
394
306
395
307
_syncGroup ( ) {
396
- const selected = this . _controller . get_current_layout_idx ( ) ;
308
+ const selected = this . _manager . currentSource ;
397
309
398
- if ( ! this . _layoutItems . length ) {
310
+ if ( ! this . _manager . multipleSources ) {
399
311
this . actor . hide ( ) ;
400
312
return ;
401
313
}
@@ -405,20 +317,17 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
405
317
this . _selectedLayout = null ;
406
318
}
407
319
408
- const item = this . _layoutItems [ selected ] ;
320
+ const item = this . _layoutItems . get ( selected ) ;
409
321
item . setShowDot ( true ) ;
410
322
411
323
this . _selectedLayout = item ;
412
-
413
- const layoutInfo = this . _controller . get_layout_data ( selected ) ;
414
-
415
- this . set_applet_tooltip ( layoutInfo . display_name ) ;
324
+ this . set_applet_tooltip ( selected . displayName ) ;
416
325
417
326
const _applet_label_box = this . _applet_label . get_parent ( ) ;
418
327
this . _setMargin ( _applet_label_box , 0 , 0 ) ;
419
328
this . _setMargin ( this . _applet_icon_box , 0 , 0 ) ;
420
329
421
- const { name, iconActor, iconInstance, isFlagIcon} = this . _layoutIcons [ selected ] ;
330
+ const { name, iconActor, iconInstance, isFlagIcon} = this . _layoutIcons . get ( selected ) ;
422
331
if ( isFlagIcon ) {
423
332
this . _applet_icon = iconInstance ;
424
333
this . _applet_icon_box . set_child ( iconActor ) ;
@@ -448,12 +357,15 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
448
357
if ( isFlagIcon ) {
449
358
this . _setStyle ( ) ;
450
359
}
451
-
452
- this . im_running ? this . actor . hide ( ) : this . actor . show ( ) ;
453
360
}
454
361
455
362
on_applet_removed_from_panel ( ) {
456
- this . _controller . applet_removed ( ) ;
363
+ if ( this . _bus_watch_id > 0 ) {
364
+ Gio . DBus . session . unwatch_name ( this . _bus_watch_id ) ;
365
+ this . _bus_watch_id = 0 ;
366
+ }
367
+
368
+ // TODO disconnect ISM signals.
457
369
458
370
Main . systrayManager . unregisterTrayIconReplacement ( this . metadata . uuid ) ;
459
371
}
0 commit comments