summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartosz Jaroszewski <b.jarosze@gmail.com>2020-05-17 12:47:34 +0200
committerBartosz Jaroszewski <b.jarosze@gmail.com>2020-05-17 12:47:34 +0200
commit3e173911f5aeeaa4572bc4d972cd4673a64f2fa0 (patch)
treefe6da235176d0413341f6ffdbf24cc6497513aed
parent3d8b17ef4864f27c4f7b86c6d9ae55746283b7e7 (diff)
add ui bluetooth menu item
-rw-r--r--extension.js29
-rw-r--r--ui.js70
2 files changed, 57 insertions, 42 deletions
diff --git a/extension.js b/extension.js
index 111f3c4..1432dad 100644
--- a/extension.js
+++ b/extension.js
@@ -50,39 +50,20 @@ class BluetoothDevice {
get item() {
if (!this._item)
- this._buildMenuItem();
+ this._item = new UiExtension.PopupBluetoothDeviceMenuItem(this);
return this._item;
}
- _buildMenuItem() {
- this._item = new UiExtension.PopupSwitchWithButtonMenuItem(
- this.name,
- this.isConnected,
- this.isConnected && 'view-refresh'
- );
- this._item.isDeviceSwitcher = true;
- this._item.connect('toggled', (item, state) => {
- if (state)
- this._connect();
- else
- this._disconnect();
- });
-
- this._item.connect('clicked', () => {
- this._reconnect()
- });
- }
-
- _disconnect() {
+ disconnect() {
this._call_cmd(`bluetoothctl -- disconnect ${this.mac}`)
}
- _connect() {
+ connect() {
this._call_cmd(`bluetoothctl -- connect ${this.mac}`)
}
- _reconnect() {
+ reconnect() {
this._call_cmd(`bluetoothctl -- disconnect ${this.mac} && bluetoothctl -- connect ${this.mac}`)
}
@@ -221,7 +202,7 @@ class BluetoothQuickConnect {
_removeDevicesFromMenu() {
this._menu._getMenuItems().forEach((item) => {
- if (item.isDeviceSwitcher) {
+ if (item.isBluetoothDeviceSwitcher) {
item.destroy();
}
});
diff --git a/ui.js b/ui.js
index 7d78576..be134de 100644
--- a/ui.js
+++ b/ui.js
@@ -19,28 +19,31 @@ const {Atk, Clutter, Gio, GObject, Graphene, Shell, St} = imports.gi;
const Tweener = imports.ui.tweener;
const PopupMenu = imports.ui.popupMenu;
-var PopupSwitchWithButtonMenuItem = GObject.registerClass(
- {Signals: {'clicked': {}}},
+var PopupBluetoothDeviceMenuItem = GObject.registerClass(
class PopupSwitchWithButtonMenuItem extends PopupMenu.PopupSwitchMenuItem {
- _init(text, active, icon, params) {
- super._init(text, active, params);
+ _init(bluetoothDevice, params) {
+ super._init(bluetoothDevice.name, bluetoothDevice.isConnected, params);
+
+ this._bluetoothDevice = bluetoothDevice
+ this.isBluetoothDeviceSwitcher = true;
+ this.isEmitActivatedEnabled = true;
this.label.x_expand = true;
this._statusBin.x_expand = false;
- this.isEmitActivatedEnabled = true;
- if (icon) {
- this.insert_child_at_index(
- this.create_button(icon),
- this.get_n_children() - 1
- );
- }
+ this._refreshButton = this._buildRefreshButton();
+ this._pendingLabel = this._buildPendingLabel();
+ this._connectToggledEvent();
+
+ this.insert_child_at_index(this._refreshButton, this.get_n_children() - 1);
+ this.add_child(this._pendingLabel);
}
- create_button(iconName) {
+ _buildRefreshButton() {
let icon = new St.Icon({
- icon_name: iconName,
+ icon_name: 'view-refresh',
style_class: 'popup-menu-icon',
+ opacity: 155
});
let button = new St.Button({
@@ -51,8 +54,7 @@ var PopupSwitchWithButtonMenuItem = GObject.registerClass(
button.connect("enter-event", (widget) => {
Tweener.addTween(
widget.child, {
- scale_x: 1.1,
- scale_y: 1.1,
+ opacity: 255,
time: 0.05,
transition: 'linear'
}
@@ -62,8 +64,7 @@ var PopupSwitchWithButtonMenuItem = GObject.registerClass(
button.connect("leave-event", (widget) => {
Tweener.addTween(
widget.child, {
- scale_x: 1,
- scale_y: 1,
+ opacity: 155,
time: 0.05,
transition: 'linear'
}
@@ -71,14 +72,35 @@ var PopupSwitchWithButtonMenuItem = GObject.registerClass(
});
button.connect('clicked', () => {
- this.emit('clicked');
+ this._pending();
+ this._bluetoothDevice.reconnect();
+
if (this.isEmitActivatedEnabled)
this.emit('activate', Clutter.get_current_event());
});
+ if (!this._bluetoothDevice.isConnected)
+ button.hide();
+
return button;
}
+ _buildPendingLabel() {
+ let label = new St.Label({ text: _('Wait') });
+ label.hide();
+
+ return label;
+ }
+
+ _connectToggledEvent() {
+ this.connect('toggled', (item, state) => {
+ if (state)
+ this._bluetoothDevice.connect();
+ else
+ this._bluetoothDevice.disconnect();
+ });
+ }
+
activate(event) {
if (this._switch.mapped)
this.toggle();
@@ -92,5 +114,17 @@ var PopupSwitchWithButtonMenuItem = GObject.registerClass(
if (this.isEmitActivatedEnabled)
this.emit('activate', event);
}
+
+ toggle() {
+ super.toggle();
+ this._pending();
+ }
+
+ _pending() {
+ this._refreshButton.hide();
+ this._switch.hide();
+ this._pendingLabel.show();
+ this.reactive = false;
+ }
}
);