From 0d0b75cc77526b5afb11611f4b0f3b8413793c1b Mon Sep 17 00:00:00 2001 From: Frank Ploss Date: Sun, 3 Jan 2016 10:32:28 +0100 Subject: [PATCH] Remove extra "disconnected" menu Add a message to other menu instead. Automatic reconnection makes it superfluous to have an extra "Reconnect" button. --- ui/tor_button.js | 36 +++++-------------------- ui/tor_disconnected_menu.js | 53 ------------------------------------- ui/tor_popup_menu.js | 48 +++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 85 deletions(-) delete mode 100644 ui/tor_disconnected_menu.js diff --git a/ui/tor_button.js b/ui/tor_button.js index 8c109c6..6581f23 100644 --- a/ui/tor_button.js +++ b/ui/tor_button.js @@ -26,7 +26,6 @@ const PopupMenu = imports.ui.popupMenu; const St = imports.gi.St; const Me = imports.misc.extensionUtils.getCurrentExtension(); -const TorDisconnectedMenu = Me.imports.ui.tor_disconnected_menu.TorDisconnectedMenu; const TorPopupMenu = Me.imports.ui.tor_popup_menu.TorPopupMenu; const TorConnectedIcon = 'tor-connected'; @@ -43,8 +42,6 @@ const TorButton = new Lang.Class({ this._buildUi(); this._bindEvents(); - - this._currentState = null; }, _buildUi: function() { @@ -52,10 +49,10 @@ const TorButton = new Lang.Class({ icon_name: TorDisconnectedIcon, style_class: 'system-status-icon' }); - this.actor.add_child(this._icon); - this._showDisconnectedMenu('Haven’t tried to connect yet'); + var menu = new TorPopupMenu(this.actor, this._torControlClient, 'disconnected'); + this.setMenu(menu); }, _bindEvents: function() { @@ -67,37 +64,16 @@ const TorButton = new Lang.Class({ _onChangedConnectionState: function(source, state, reason) { switch (state) { case 'ready': - this._showConnectedMenu(); + this._icon.icon_name = TorConnectedIcon; + this.menu.setState('connected'); break; case 'closed': - this._showDisconnectedMenu(reason); + this._icon.icon_name = TorDisconnectedIcon; + this.menu.setState('disconnected', reason); break; } }, - _showConnectedMenu: function() { - if (this._menu instanceof TorPopupMenu) { - return; - } - - this._icon.icon_name = TorConnectedIcon; - this._menu = new TorPopupMenu(this.actor, this._torControlClient); - this.setMenu(this._menu); - Main.panel.menuManager.addMenu(this._menu); - }, - - _showDisconnectedMenu: function(reason) { - if (this._menu instanceof TorDisconnectedMenu) { - this._menu.setReason(reason); - return; - } - - this._icon.icon_name = TorDisconnectedIcon; - this._menu = new TorDisconnectedMenu(this.actor, this._torControlClient, reason); - this.setMenu(this._menu); - Main.panel.menuManager.addMenu(this._menu); - }, - _onSwitchedTorIdentity: function() { Main.notify('Switched to a new Tor identity!'); }, diff --git a/ui/tor_disconnected_menu.js b/ui/tor_disconnected_menu.js deleted file mode 100644 index eb6311a..0000000 --- a/ui/tor_disconnected_menu.js +++ /dev/null @@ -1,53 +0,0 @@ -// vim: set sw=4:ts=4 -/* -Copyright 2015 Frank Ploss . - -This file is part of gnome-shell-extension-tor. - -gnome-shell-extension-tor is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -gnome-shell-extension-tor is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with gnome-shell-extension-tor. If not, see ./ -*/ -'use strict'; - -const Lang = imports.lang; -const PopupMenu = imports.ui.popupMenu; -const St = imports.gi.St; - -const TorDisconnectedMenu = new Lang.Class({ - Name: 'TorDisconnectedMenu', - Extends: PopupMenu.PopupMenu, - - _init: function(actor, torControlClient, reason) { - this._torControlClient = torControlClient; - this.parent(actor, 0.25, St.Side.TOP); - - this._addActions(); - this.setReason(reason); - }, - - destroy: function() { - this.parent(arguments); - }, - - setReason: function(reason) { - this._msgLabel.set_text('No connection. ' + reason); - }, - - _addActions: function() { - var errorMessageMenuItem = new PopupMenu.PopupBaseMenuItem({reactive: false}); - errorMessageMenuItem.setSensitive(false); - this._msgLabel = new St.Label(); - errorMessageMenuItem.actor.add_actor(this._msgLabel); - this.addMenuItem(errorMessageMenuItem); - }, -}); diff --git a/ui/tor_popup_menu.js b/ui/tor_popup_menu.js index f0b648c..8727505 100644 --- a/ui/tor_popup_menu.js +++ b/ui/tor_popup_menu.js @@ -27,15 +27,59 @@ const TorPopupMenu = new Lang.Class({ Name: 'TorPopupMenu', Extends: PopupMenu.PopupMenu, - _init: function(actor, torControlClient) { + _init: function(actor, torControlClient, state) { this._torControlClient = torControlClient; this.parent(actor, 0.25, St.Side.TOP); this._addActions(); + this.setState(state); + }, + + setState: function(state, reason) { + switch (state) { + case 'connected': + this._switchTorIdentityMenuItem.setSensitive(true); + this._setMessage(null); + break; + case 'disconnected': + this._switchTorIdentityMenuItem.setSensitive(false); + this._setMessage(reason); + break; + } }, _addActions: function() { - this.addAction('Switch Tor Identity', Lang.bind(this, this._switchTorIdentity)); + this._switchTorIdentityMenuItem = + this.addAction('Switch Tor Identity', Lang.bind(this, this._switchTorIdentity)); + }, + + _setMessage: function(message) { + if (!message) { + this._removeMessageMenuItem(); + return; + } + + if (!this._messageMenuItem) { + this._addMessageMenuItem(); + } + + this._messageLabel.set_text('No connection. ' + message); + }, + + _addMessageMenuItem: function() { + this._messageMenuItem = new PopupMenu.PopupBaseMenuItem({reactive: false}); + this._messageMenuItem.setSensitive(false); + this._messageLabel = new St.Label(); + this._messageMenuItem.actor.add_actor(this._messageLabel); + this.addMenuItem(this._messageMenuItem, 0); + }, + + _removeMessageMenuItem: function() { + if (!this._messageMenuItem) + return; + + this._messageMenuItem.destroy(); + this._messageMenuItem = null; }, _switchTorIdentity: function() {