| 1 |
// SPDX-FileCopyrightText: 2011 Vamsi Krishna Brahmajosyula <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2011 Giovanni Campagna <[email protected]> |
| 3 |
// SPDX-FileCopyrightText: 2013 Florian Müllner <[email protected]> |
| 4 |
// SPDX-FileCopyrightText: 2016 Rémy Lefevre <[email protected]> |
| 5 |
// |
| 6 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 7 |
|
| 8 |
import Clutter from 'gi://Clutter'; |
| 9 |
import GObject from 'gi://GObject'; |
| 10 |
import St from 'gi://St'; |
| 11 |
|
| 12 |
import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js'; |
| 13 |
|
| 14 |
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; |
| 15 |
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js'; |
| 16 |
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js'; |
| 17 |
|
| 18 |
import {PlacesManager} from './placeDisplay.js'; |
| 19 |
|
| 20 |
const N_ = x => x; |
| 21 |
|
| 22 |
class PlaceMenuItem extends PopupMenu.PopupImageMenuItem { |
| 23 |
static { |
| 24 |
GObject.registerClass(this); |
| 25 |
} |
| 26 |
|
| 27 |
constructor(info) { |
| 28 |
super(info.name, info.icon, { |
| 29 |
style_class: 'place-menu-item', |
| 30 |
}); |
| 31 |
this._info = info; |
| 32 |
|
| 33 |
if (info.isRemovable()) { |
| 34 |
this._ejectIcon = new St.Icon({ |
| 35 |
icon_name: 'media-eject-symbolic', |
| 36 |
style_class: 'popup-menu-icon', |
| 37 |
}); |
| 38 |
this._ejectButton = new St.Button({ |
| 39 |
child: this._ejectIcon, |
| 40 |
style_class: 'button', |
| 41 |
}); |
| 42 |
this._ejectButton.connect('clicked', info.eject.bind(info)); |
| 43 |
this.add_child(this._ejectButton); |
| 44 |
} |
| 45 |
|
| 46 |
info.connectObject('changed', |
| 47 |
this._propertiesChanged.bind(this), this); |
| 48 |
} |
| 49 |
|
| 50 |
activate(event) { |
| 51 |
this._info.launch(event.get_time()); |
| 52 |
|
| 53 |
super.activate(event); |
| 54 |
} |
| 55 |
|
| 56 |
_propertiesChanged(info) { |
| 57 |
this.setIcon(info.icon); |
| 58 |
this.label.text = info.name; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
const SECTIONS = [ |
| 63 |
'special', |
| 64 |
'devices', |
| 65 |
'bookmarks', |
| 66 |
'network', |
| 67 |
]; |
| 68 |
|
| 69 |
class PlacesMenu extends PanelMenu.Button { |
| 70 |
static { |
| 71 |
GObject.registerClass(this); |
| 72 |
} |
| 73 |
|
| 74 |
constructor() { |
| 75 |
super(0.5, _('Places')); |
| 76 |
|
| 77 |
const label = new St.Label({ |
| 78 |
text: _('Places'), |
| 79 |
y_expand: true, |
| 80 |
y_align: Clutter.ActorAlign.CENTER, |
| 81 |
}); |
| 82 |
this.add_child(label); |
| 83 |
|
| 84 |
this.placesManager = new PlacesManager(); |
| 85 |
|
| 86 |
this._sections = { }; |
| 87 |
|
| 88 |
for (let i = 0; i < SECTIONS.length; i++) { |
| 89 |
const id = SECTIONS[i]; |
| 90 |
this._sections[id] = new PopupMenu.PopupMenuSection(); |
| 91 |
this.placesManager.connectObject( |
| 92 |
`${id}-updated`, () => this._redisplay(id), |
| 93 |
this); |
| 94 |
|
| 95 |
this._create(id); |
| 96 |
this.menu.addMenuItem(this._sections[id]); |
| 97 |
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
_onDestroy() { |
| 102 |
this.placesManager.disconnectObject(this); |
| 103 |
this.placesManager.destroy(); |
| 104 |
|
| 105 |
super._onDestroy(); |
| 106 |
} |
| 107 |
|
| 108 |
_redisplay(id) { |
| 109 |
this._sections[id].removeAll(); |
| 110 |
this._create(id); |
| 111 |
} |
| 112 |
|
| 113 |
_create(id) { |
| 114 |
const places = this.placesManager.get(id); |
| 115 |
|
| 116 |
for (let i = 0; i < places.length; i++) |
| 117 |
this._sections[id].addMenuItem(new PlaceMenuItem(places[i])); |
| 118 |
|
| 119 |
this._sections[id].actor.visible = places.length > 0; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
export default class PlacesMenuExtension extends Extension { |
| 124 |
enable() { |
| 125 |
this._indicator = new PlacesMenu(); |
| 126 |
|
| 127 |
let pos = Main.sessionMode.panel.left.length; |
| 128 |
if ('apps-menu' in Main.panel.statusArea) |
| 129 |
pos++; |
| 130 |
Main.panel.addToStatusArea('places-menu', this._indicator, pos, 'left'); |
| 131 |
} |
| 132 |
|
| 133 |
disable() { |
| 134 |
this._indicator.destroy(); |
| 135 |
this._indicator = null; |
| 136 |
} |
| 137 |
} |