File: windowMenu.js

package info (click to toggle)
cinnamon 6.4.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,304 kB
  • sloc: javascript: 54,298; ansic: 51,499; python: 21,971; xml: 2,803; sh: 96; makefile: 27; perl: 13
file content (445 lines) | stat: -rw-r--r-- 16,741 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*
/* exported WindowMenuManager */

const { Clutter, GLib, Meta, St, Gtk } = imports.gi;

const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const CheckBox = imports.ui.checkBox;
const RadioButton = imports.ui.radioButton;
const SignalManager = imports.misc.signalManager;

var BaseMnemonicMenuItem = class BaseMnemonicMenuItem extends PopupMenu.PopupBaseMenuItem {
    _init (label, params) {
        super._init.call(this, params);

        this.mnemonicInit(label);
    }

    mnemonicInit(label) {
        this.origLabel = label;
        this.plain = null;
        this.mnemonized = null;
        this.showing_mnemonic = false;
    }

    _setLabels() {
        let markup = null;
        let plain = null;
        let mnemonic = undefined;
        let title_pieces = this.origLabel.split("_");

        if (title_pieces.length === 2) {
            this.mnemonic = title_pieces[1][0].toLowerCase();
            this.mnemonized = `${title_pieces[0]}<u>${title_pieces[1][0]}</u>${title_pieces[1].substring(1)}`;
            this.plain = `${title_pieces[0]}${title_pieces[1]}`; 
        } else {
            this.plain = this.origLabel;
            this.mnemonized = this.origLabel;
        }

        this.label.clutter_text.set_markup(this.plain);
    }

    toggleMnemonic() {
        if (!this.showing_mnemonic) {
            this.label.clutter_text.set_markup(this.mnemonized);
            this.showing_mnemonic = true;
        } else {
            this.label.clutter_text.set_markup(this.plain);
            this.showing_mnemonic = false;
        }

        this.label.clutter_text.queue_relayout();
    }
}

var MnemonicLeftOrnamentedMenuItem = class MnemonicLeftOrnamentedMenuItem extends BaseMnemonicMenuItem {
    _init (label, params) {
        super._init.call(this, label, params);

        this._ornament = new St.Bin();
        this._icon = new St.Icon({ style_class: 'popup-menu-icon', icon_type: St.IconType.SYMBOLIC });

        this._ornament.child = this._icon;
        this._ornament.child._delegate = this._ornament;
        this.addActor(this._ornament, {span: 1});

        this.label = new St.Label();
        this.addActor(this.label);
        this.actor.label_actor = this.label;

        this._setLabels();
    }

    setIcon(icon_name) {
        this._ornament.child.destroy()
        this._icon = new St.Icon({ style_class: 'popup-menu-icon', icon_name: icon_name, icon_type: St.IconType.SYMBOLIC });
        this._ornament.child = this._icon;
    }

    setOrnament(ornamentType, state) {
        switch (ornamentType) {
        case PopupMenu.OrnamentType.CHECK:
            if ((this._ornament.child)&&(!(this._ornament.child._delegate instanceof CheckBox.CheckButton))) {
                this._ornament.child.destroy();
                this._ornament.child = null;
            }
            if (!this._ornament.child) {
                let switchOrn = new CheckBox.CheckButton(state);
                this._ornament.child = switchOrn.actor;
                switchOrn.actor.reactive = false;
            } else {
                this._ornament.child._delegate.setToggleState(state);
            }
            this._icon = null;
            break;
        case PopupMenu.OrnamentType.DOT:
            if ((this._ornament.child)&&(!(this._ornament.child._delegate instanceof RadioButton.RadioBox))) {
                this._ornament.child.destroy();
                this._ornament.child = null;
            }
            if (!this._ornament.child) {
                let radioOrn = new RadioButton.RadioBox(state);
                this._ornament.child = radioOrn.actor;
                radioOrn.actor.reactive = false;
            } else {
                this._ornament.child._delegate.setToggleState(state);
            }
            this._icon = null;
            break;
        }
    }
}

var MnemonicSubMenuMenuItem = class MnemonicSubMenuMenuItem extends PopupMenu.PopupSubMenuMenuItem {
    _init (label, params) {
        super._init.call(this, label);
        BaseMnemonicMenuItem.prototype.mnemonicInit.call(this, label);

        // to help align with left side ornaments
        let filler = new St.Icon({ style_class: 'popup-menu-icon', icon_type: St.IconType.SYMBOLIC });
        this.addActor(filler, { span: 1, position: 0 });

        this._setLabels();
    }
}
MnemonicSubMenuMenuItem.prototype.toggleMnemonic = BaseMnemonicMenuItem.prototype.toggleMnemonic;
MnemonicSubMenuMenuItem.prototype._setLabels = BaseMnemonicMenuItem.prototype._setLabels;

var WindowMenu = class extends PopupMenu.PopupMenu {
    constructor(window, sourceActor) {
        super(sourceActor, 0, St.Side.TOP);

        this.actor.add_style_class_name('window-menu');

        Main.uiGroup.add_actor(this.actor);
        this.actor.hide();

        this.actor.connect('key-press-event', this._windowMenuKeypress.bind(this));

        this._items = [];
        this._buildMenu(window);
    }

    _windowMenuKeypress(actor, event) {
        if (this._onKeyPressEvent(actor, event) === Clutter.EVENT_STOP) {
            return Clutter.EVENT_STOP;
        }

        if (event.get_key_symbol() === Clutter.KEY_Alt_R || event.get_key_symbol() === Clutter.KEY_Alt_L) {
            let items = this._getMenuItems();

            for (let item of this._items) {
                if (item.mnemonic !== undefined) {
                    item.toggleMnemonic();
                }
            }
            return Clutter.EVENT_STOP;
        }

        let items = this._getMenuItems();
        for (let item of this._items) {
            if (!item.sensitive) {
                continue;
            }

            if (item.mnemonic != undefined && item.mnemonic[0] === String.fromCharCode(event.get_key_symbol()).toLowerCase()) {
                item.activate(event);
                return Clutter.EVENT_STOP;
            }
        }

        return Clutter.EVENT_PROPAGATE;
    }

    addAction(to_menu, title, callback) {
        let menuItem = new MnemonicLeftOrnamentedMenuItem(title);
        to_menu.addMenuItem(menuItem);

        menuItem.connect('activate', (o, event) => {
            callback(event);
        });

        this._items.push(menuItem);

        return menuItem;
    }

    _buildMenu(window) {
        let type = window.get_window_type();

        let item;

        // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
        // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
        item = this.addAction(this, _("Mi_nimize"), () => {
            window.minimize();
        });
        item.setIcon("window-minimize-symbolic");

        if (!window.can_minimize())
            item.setSensitive(false);

        if (window.get_maximized()) {
            // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
            // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
            item = this.addAction(this, _("Unma_ximize"), () => {
                window.unmaximize(Meta.MaximizeFlags.BOTH);
            });
        } else {
            // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
            // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
            item = this.addAction(this, _("Ma_ximize"), () => {
                window.maximize(Meta.MaximizeFlags.BOTH);
            });
            item.setIcon("window-maximize-symbolic");
        }
        if (!window.can_maximize())
            item.setSensitive(false);

        // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
        // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
        item = this.addAction(this, _("_Move"), event => {
            this._grabAction(window, Meta.GrabOp.KEYBOARD_MOVING, event.get_time());
        });
        if (!window.allows_move())
            item.setSensitive(false);

        // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
        // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
        item = this.addAction(this, _("_Resize"), event => {
            this._grabAction(window, Meta.GrabOp.KEYBOARD_RESIZING_UNKNOWN, event.get_time());
        });
        if (!window.allows_resize())
            item.setSensitive(false);

        if (!window.titlebar_is_onscreen() && type != Meta.WindowType.DOCK && type != Meta.WindowType.DESKTOP) {
            // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
            // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
            this.addAction(this, _("Move Titlebar Onscreen"), () => {
                window.shove_titlebar_onscreen();
            });
        }

        this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());

        // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
        // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
        item = this.addAction(this, _("Always on _Top"), () => {
            if (window.is_above())
                window.unmake_above();
            else
                window.make_above();
        });
        if (window.is_above())
            item.setOrnament(PopupMenu.OrnamentType.CHECK, true);
        else
            item.setOrnament(PopupMenu.OrnamentType.CHECK, false);
        if (type == Meta.WindowType.DOCK ||
            type == Meta.WindowType.DESKTOP ||
            type == Meta.WindowType.SPLASHSCREEN)
            item.setSensitive(false);

        if (global.workspace_manager.get_n_workspaces() > 1 &&
            (!Meta.prefs_get_workspaces_only_on_primary() ||
             window.is_on_primary_monitor())) {
            let isSticky = window.is_on_all_workspaces();

            // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
            // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
            this.sticky_action = this.addAction(this, _("_Always on Visible Workspace"), () => {
                window.stick();
            });
            // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
            // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
            this.unsticky_action = this.addAction(this, _("_Only on This Workspace"), () => {
                window.unstick();
            });
            this.sticky_action.setOrnament(PopupMenu.OrnamentType.DOT, isSticky);
            this.unsticky_action.setOrnament(PopupMenu.OrnamentType.DOT, !isSticky);

            if (window.is_always_on_all_workspaces()) {
                this.sticky_action.setSensitive(false);
                this.unsticky_action.setSensitive(false);
            }

            // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
            // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
            let ws_sub = new MnemonicSubMenuMenuItem(_("Move to Another _Workspace"));
            this.addMenuItem(ws_sub);
            this._items.push(ws_sub);

            let curr_index = window.get_workspace().index();
            let used_nums = {};
            let name = null;

            for (let i = 0; i < global.workspace_manager.get_n_workspaces(); i++) {
                if (used_nums[i + 1]) {
                    continue;
                }

                let name = Main.workspace_names[i];

                if (name === undefined || name === '') {
                    name = Main.getWorkspaceName(i);
                }

                let end = name.substring(name.length - 2).replace("_", "");
                let number = parseInt(end);

                if (!isNaN(number) && used_nums[number] == undefined) {
                    if (number == 10) {
                        name = name.replace("10", "1_0");
                        used_nums[10] = true;
                    }
                    else
                    if (number < 10) {
                        name = name.replace(number.toString(), "_" + number.toString());
                        used_nums[number] = true;
                    }
                }
                else
                {
                    name = `${name} (_${i + 1})`
                    used_nums[i + 1] = true;
                }

                item = this.addAction(ws_sub.menu, name, () => window.change_workspace(global.workspace_manager.get_workspace_by_index(i)))

                if (i == curr_index)
                    item.setSensitive(false);
            }
        }

        this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());

        // Translators: If a language-specific mnemonic doesn't make sense, add one after the label itself:
        // i.e. <translated>(_n). See https://github.com/linuxmint/muffin/blob/b9a6f3fe43e/po .po files
        item = this.addAction(this, _("_Close"), event => {
            window.delete(event.get_time());
        });
        item.setIcon("window-close-symbolic");
        if (!window.can_close())
            item.setSensitive(false);
    }

    _grabAction(window, grabOp, time) {
        if (global.display.get_grab_op() == Meta.GrabOp.NONE) {
            window.begin_grab_op(grabOp, true, time);
            return;
        }

        let waitId = 0;
        let id = global.display.connect('grab-op-end', display => {
            display.disconnect(id);
            GLib.source_remove(waitId);

            window.begin_grab_op(grabOp, true, time);
        });

        waitId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 100, () => {
            global.display.disconnect(id);
            return GLib.SOURCE_REMOVE;
        });
    }
};

var WindowMenuManager = class {
    constructor() {
        this._manager = null;
        this._wmsignals = null;

        this._sourceActor = new St.Widget({ reactive: true, visible: false });
        this._sourceActor.connect('button-press-event', () => {
            this._manager._activeMenu.toggle();
        });
        Main.uiGroup.add_actor(this._sourceActor);
        this.dummyCursor = new St.Widget({ width: 0, height: 0, opacity: 0 });
        Main.uiGroup.add_actor(this.dummyCursor);
        this.actor = this.dummyCursor;

        this.current_menu = null;
        this.current_window = null;
    }

    showWindowMenuForWindow(window, type, rect) {
        if (type != Meta.WindowMenuType.WM)
            throw new Error('Unsupported window menu type');

        if (window.window_type === Meta.WindowType.DESKTOP) {
            return;
        }

        this.destroyMenu();
        this._manager = new PopupMenu.PopupMenuManager(this);
        this._wmsignals = new SignalManager.SignalManager(null);

        let menu = new WindowMenu(window, this._sourceActor);

        this._manager.addMenu(menu);

        this._wmsignals.connect(menu, 'activate', () => {
            window.check_alive(global.get_current_time());
        });
        this._wmsignals.connect(menu, 'menu-animated-closed', () => {
            this.destroyMenu();
        });

        this._wmsignals.connect(window, 'unmanaged', () => {
            this.destroyMenu();
        });

        this._sourceActor.set_size(Math.max(1, rect.width), Math.max(1, rect.height));
        this._sourceActor.set_position(rect.x, rect.y);
        this._sourceActor.show();

        let [minWidth, minHeight, natWidth, natHeight] = menu.actor.get_preferred_size();

        menu.shiftToPosition((rect.x + natWidth / 2) + 5); // +5 for appearances

        menu.open();
        menu.actor.navigate_focus(null, Gtk.DirectionType.TAB_FORWARD, false);
        this._wmsignals.connect(menu, 'open-state-changed', () => {
            this.destroyMenu();
        });

        this.current_menu = menu;
        this.current_window = window;
    }

    destroyMenu() {
        if (this._wmsignals != null) {
            this._wmsignals.disconnectAllSignals();
            this._wmsignals = null;

            this._sourceActor.hide();

            if (this.current_menu) {
                this.current_menu.close(false)
                this._manager.destroy()
                this._manager = null
            }
        }
    }
};