Refactored, implemented a proper Javascript class structure, as well as code cleanup and logging infrastructure.

This commit is contained in:
Xavier Moffett 2022-06-22 06:23:42 -04:00
parent 005e8fd8a8
commit 756fa448c7
3 changed files with 169 additions and 168 deletions

View file

@ -15,119 +15,147 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
const Window = imports.ui.windowManager; const UUID = "window-controls-applet@sapphirus.org";
const PanelManager = imports.ui.panel;
const St = imports.gi.St; const St = imports.gi.St;
const Lang = imports.lang; const Lang = imports.lang;
const Applet = imports.ui.applet; const Applet = imports.ui.applet;
const Cinnamon = imports.gi.Cinnamon; const Cinnamon = imports.gi.Cinnamon;
const Settings = imports.ui.settings; const Settings = imports.ui.settings;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main; const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Meta = imports.gi.Meta; const Meta = imports.gi.Meta;
const Util = imports.misc.util;
const GLib = imports.gi.GLib;
const Gdk = imports.gi.Gdk;
const GnomeSession = imports.misc.gnomeSession;
const WindowTracker = imports.gi.Cinnamon.WindowTracker; const WindowTracker = imports.gi.Cinnamon.WindowTracker;
const wt = WindowTracker.get_default(); const _WindowTracker = WindowTracker.get_default();
const wpm = global.display.get_workspace_manager(); const WorkspaceManager = global.display.get_workspace_manager();
const INFO = 0;
const VERBOSE = 1;
const ERROR = 2;
const ALL = 3;
function WindowButtonApplet(orientation,metadata, panelHeight, instance_id) { class Logger {
this._init(orientation,metadata, panelHeight, instance_id); constructor(omittanceLevel, loggerName) {
this.omittanceLevel = omittanceLevel;
this.loggerName = loggerName;
} }
WindowButtonApplet.prototype = { log(lvl, log) {
if(this.omittanceLevel < lvl)
return;
switch(lvl) {
case ERROR:
global.logError("["+this.loggerName+"]: " + log);
break;
__proto__: Applet.Applet.prototype, default:
global.log("["+this.loggerName+"] " + log);
break;
}
}
_init: function(orientation,metadata, panelHeight, instance_id) { setLoggingLevel(level) {
Applet.Applet.prototype._init.call(this, orientation, panelHeight, instance_id); this.omittanceLevel = level;
this.instance_id=instance_id; }
}
class WindowControlApplet extends Applet.Applet {
constructor(metadata, orientation, panelHeight, instance_id) {
super(orientation, panelHeight, instance_id);
this.logger = new Logger(INFO, UUID);
this.appletPath=metadata.path; this.appletPath=metadata.path;
this.instance_id = instance_id;
try { try {
this.initialize_settings(); this._initialize_settings();
this.initialize_events(); this.logger.log(VERBOSE, "Intialised Settings!");
this.initialize_buttons(); this._initialize_events();
this.on_panel_edit_mode_changed(); this.logger.log(VERBOSE, "Intialised Events!");
this.toggleButtons(!this.onlyMaximized); this._initialize_buttons();
global.log("[WTDEBUG] Complete!"); this.logger.log(VERBOSE, "Intialised Buttons!");
this._on_panel_edit_mode_changed();
this._toggleButtons(!this.onlyMaximized);
this.logger.log(INFO, "Initialisation Complete!");
} catch (e) { } catch (e) {
global.logError(e); this.logger.log(ERROR, e);
}
} }
},
initialize_events: function() {
let tracker = Cinnamon.WindowTracker.get_default(); _initialize_events() {
Main.themeManager.connect("theme-set", Lang.bind(this, this.loadTheme)); Main.themeManager.connect("theme-set", Lang.bind(this, this._loadTheme));
tracker.connect('notify::focus-app', Lang.bind(this, this._windowChange)); WindowTracker.get_default().connect('notify::focus-app', Lang.bind(this, this._windowChange));
global.settings.connect('changed::panel-edit-mode', Lang.bind(this, this.on_panel_edit_mode_changed)); global.settings.connect('changed::panel-edit-mode', Lang.bind(this, this._on_panel_edit_mode_changed));
global.window_manager.connect('size-changed', Lang.bind(this, this._windowChange)); global.window_manager.connect('size-changed', Lang.bind(this, this._windowChange));
global.window_manager.connect('minimize', Lang.bind(this, this._windowChange)); global.window_manager.connect('minimize', Lang.bind(this, this._windowChange));
global.window_manager.connect('destroy', Lang.bind(this, this._windowChange)); global.window_manager.connect('destroy', Lang.bind(this, this._windowChange));
global.display.connect('window-entered-monitor', Lang.bind(this, this._windowChange)); global.display.connect('window-entered-monitor', Lang.bind(this, this._windowChange));
//global.display.connect('window-left-monitor', Lang.bind(this, this._windowChange));
global.display.connect('showing-desktop-changed', Lang.bind(this, this._windowChange)); global.display.connect('showing-desktop-changed', Lang.bind(this, this._windowChange));
}
_initialize_buttons() {
}, let buttons=this.button_layout.split(':');
initialize_buttons: function() { if(this._checkButton(buttons,'maximize') || this._checkButton(buttons,'minimize') || this._checkButton(buttons,'close')){
let buttons=this.buttons_style.split(':'); this._loadTheme();
if(this.checkButton(buttons,'maximize') || this.checkButton(buttons,'minimize') || this.checkButton(buttons,'close')){
this.loadTheme();
} }
this.button = []; this.button = [];
this.createButtons(this.buttons_style); this._createButtons(this.button_layout);
}
}, _initialize_settings() {
initialize_settings: function() { this.settings = new Settings.AppletSettings(this, UUID, this.instance_id);
this.settings = new Settings.AppletSettings(this, "window-buttons-fixed@sapphirus.org", this.instance_id); this.settings.bindProperty(Settings.BindingDirection.IN,"button-layout","button_layout",this._bind_settings,null);
this.settings.bindProperty(Settings.BindingDirection.IN,"buttons-style","buttons_style",this.bind_settings,null); this.settings.bindProperty(Settings.BindingDirection.IN,"button-theme","button_theme", this._bind_settings,null);
this.settings.bindProperty(Settings.BindingDirection.IN,"buttons-theme","buttons_theme", this.bind_settings,null); this.settings.bindProperty(Settings.BindingDirection.IN,"only-maximized", "onlyMaximized", this._bind_settings,null);
this.settings.bindProperty(Settings.BindingDirection.IN,"only-maximized", "onlyMaximized", this.bind_settings,null); this.settings.bindProperty(Settings.BindingDirection.IN,"verbose-log", "verbose_logging", this._set_logging,null);
this.settings.bindProperty(Settings.BindingDirection.IN,"on-desktop-shutdown", "onDesktopShutdown", this.bind_settings,null); this._set_logging();
}, }
bind_settings: function() {
_set_logging() {
let loggingLevel = (this.verbose_logging ? ALL : INFO);
this.logger.log(VERBOSE, "Set logging level to "+loggingLevel);
this.logger.setLoggingLevel(loggingLevel);
}
_bind_settings() {
this.actor.destroy_all_children(); this.actor.destroy_all_children();
this.initialize_buttons(); this._initialize_buttons();
}, }
on_panel_edit_mode_changed: function() {
_on_panel_edit_mode_changed() {
let reactive = !global.settings.get_boolean('panel-edit-mode'); let reactive = !global.settings.get_boolean('panel-edit-mode');
let b=this.buttons_style.split(':'); let b=this.button_layout.split(':');
for (let i=0; i < b.length; ++i ){ for (let i=0; i < b.length; ++i ){
this.button[b[i]].reactive=reactive; this.button[b[i]].reactive=reactive;
} }
}, }
getCssPath: function(theme) {
_getCssPath(theme) {
let cssPath = this.appletPath + '/themes/'+theme+'/style.css'; let cssPath = this.appletPath + '/themes/'+theme+'/style.css';
return cssPath; return cssPath;
}
}, _loadTheme(){
loadTheme: function(){
this.actor.set_style_class_name("window-buttons"); this.actor.set_style_class_name("window-buttons");
let theme = St.ThemeContext.get_for_stage(global.stage).get_theme(); let theme = St.ThemeContext.get_for_stage(global.stage).get_theme();
let theme_path = this.getCssPath(this.buttons_theme); let theme_path = this._getCssPath(this.button_theme);
theme.load_stylesheet(theme_path); theme.load_stylesheet(theme_path);
}, }
createButtons: function(buttonsStyle) {
buttonsStyle=buttonsStyle.split(':'); _createButtons(buttonLayout) {
for (let i=0; i < buttonsStyle.length; ++i ){ buttonLayout=buttonLayout.split(':');
let buttonName=buttonsStyle[i]+"Button"; for (let i=0; i < buttonLayout.length; ++i ){
let buttonName="_"+buttonLayout[i]+"Button";
this[buttonName](); this[buttonName]();
} }
}, }
minimizeButton:function () {
_minimizeButton() {
this.button['minimize'] = new St.Button({ name: 'windowButton', style_class: 'minimize window-button', reactive: true }); this.button['minimize'] = new St.Button({ name: 'windowButton', style_class: 'minimize window-button', reactive: true });
this.actor.add(this.button['minimize']); this.actor.add(this.button['minimize']);
this.button['minimize'].connect('button-press-event', Lang.bind(this,function(actor,event){ this.button['minimize'].connect('button-press-event', Lang.bind(this,function(actor,event){
let button = event.get_button(); let button = event.get_button();
if (button == 1) { if (button == 1) {
this.minimizeWindow(); this._minimizeWindow();
return true; return true;
} else if(button == 3) { } else if(button == 3) {
this._applet_context_menu.toggle(); this._applet_context_menu.toggle();
@ -135,24 +163,24 @@ WindowButtonApplet.prototype = {
} }
return false; return false;
})); }));
}, }
minimizeWindow: function() { _minimizeWindow() {
let activeWindow = this.getActiveWindow(); let activeWindow = this._getActiveWindow();
let app = wt.get_window_app(activeWindow); let app = _WindowTracker.get_window_app(activeWindow);
if(!activeWindow || !app) if(!activeWindow || !app)
return; return;
activeWindow.minimize(); activeWindow.minimize();
}, }
maximizeButton: function () { _maximizeButton() {
this.button['maximize'] = new St.Button({ name: 'windowButton'+this.instance_id, style_class: 'maximize window-button', reactive: true }); this.button['maximize'] = new St.Button({ name: 'windowButton'+this.instance_id, style_class: 'maximize window-button', reactive: true });
this.actor.add(this.button['maximize']); this.actor.add(this.button['maximize']);
this.button['maximize'].connect('button-press-event', Lang.bind(this,function(actor,event){ this.button['maximize'].connect('button-press-event', Lang.bind(this,function(actor,event){
let button = event.get_button(); let button = event.get_button();
if (button == 1) { if (button == 1) {
this.maximizeWindow(); this._maximizeWindow();
return true; return true;
} else if(button == 3){ } else if(button == 3){
this._applet_context_menu.toggle(); this._applet_context_menu.toggle();
@ -160,11 +188,11 @@ WindowButtonApplet.prototype = {
} }
return false; return false;
})); }));
}, }
maximizeWindow: function() { _maximizeWindow() {
let activeWindow = this.getActiveWindow(); let activeWindow = this._getActiveWindow();
let app = wt.get_window_app(activeWindow); let app = _WindowTracker.get_window_app(activeWindow);
if(!activeWindow || !app) if(!activeWindow || !app)
return; return;
@ -173,14 +201,15 @@ WindowButtonApplet.prototype = {
} else { } else {
activeWindow.maximize(3); activeWindow.maximize(3);
} }
}, }
closeButton: function () {
_closeButton() {
this.button['close'] = new St.Button({ name: 'windowButton', style_class: 'close window-button', reactive: true }); this.button['close'] = new St.Button({ name: 'windowButton', style_class: 'close window-button', reactive: true });
this.actor.add(this.button['close']); this.actor.add(this.button['close']);
this.button['close'].connect('button-press-event', Lang.bind(this,function(actor,event){ this.button['close'].connect('button-press-event', Lang.bind(this,function(actor,event){
let button = event.get_button(); let button = event.get_button();
if (button == 1) { if (button == 1) {
this.closeWindow(); this._closeWindow();
return true; return true;
} else if(button == 3){ } else if(button == 3){
this._applet_context_menu.toggle(); this._applet_context_menu.toggle();
@ -188,18 +217,19 @@ WindowButtonApplet.prototype = {
} }
return false; return false;
})); }));
}, }
closeWindow: function() {
let activeWindow = this.getActiveWindow(); _closeWindow() {
let app = wt.get_window_app(activeWindow); let activeWindow = this._getActiveWindow();
let app = _WindowTracker.get_window_app(activeWindow);
if(!activeWindow || !app) if(!activeWindow || !app)
return; return;
this.logger.log(INFO, "Closed!");
activeWindow.delete(global.get_current_time()); activeWindow.delete(global.get_current_time());
}, }
getActiveWindow: function() { _getActiveWindow() {
let workspace = wpm.get_active_workspace(); let workspace = WorkspaceManager.get_active_workspace();
let windows = workspace.list_windows(); let windows = workspace.list_windows();
for (var i = 0; i < windows.length; i++) { for (var i = 0; i < windows.length; i++) {
let w = windows[i]; let w = windows[i];
@ -210,50 +240,33 @@ WindowButtonApplet.prototype = {
return w; return w;
} }
return false; return false;
},
_windowChange: function(destroy=false) {
if(!this.onlyMaximized)
return;
let workspace = wpm.get_active_workspace();
let windows = workspace.list_windows();
for (var i = 0; i < windows.length; i++) {
let w = windows[i];
let thisMonitor = (w.get_monitor() == this.panel.monitorIndex);
if(!thisMonitor)
continue;
let toggle = ((w.get_maximized() === Meta.MaximizeFlags.BOTH) && (w.get_window_type() != Meta.WindowType.DESKTOP) && !w.minimized && thisMonitor);
this.toggleButtons(toggle);
if(toggle) break;
} }
/* let w = global.display.focus_window; _windowChange(destroy) {
let windowType = (w.get_window_type() == Meta.WindowType.DESKTOP); if(!this.onlyMaximized)
let thisMonitor = (w.get_monitor() == this.panel.monitorIndex); return;
let toggle = ((w.get_maximized() === Meta.MaximizeFlags.BOTH) !w.minimized && thisMonitor && !windowType); if(!this._getActiveWindow()) this._toggleButtons(false); else this._toggleButtons(true);
this.toggleButtons(toggle); */
}, }
_destroy: function() {
this.toggleButtons(false); _checkButton(arr, obj) {
},
checkButton: function(arr, obj) {
for(var i=0; i<arr.length; i++) { for(var i=0; i<arr.length; i++) {
if (arr[i] == obj){ if (arr[i] == obj){
return true; return true;
} }
} }
return null; return null;
}, }
toggleButtons: function(toggle) {
let buttons=this.buttons_style.split(':'); _toggleButtons(toggle) {
let buttons=this.button_layout.split(':');
for(var i=0; i < buttons.length; i++) { for(var i=0; i < buttons.length; i++) {
if(toggle) { this.button[buttons[i]].show(); } else { this.button[buttons[i]].hide(); } if(toggle) { this.button[buttons[i]].show(); } else { this.button[buttons[i]].hide(); }
} }
},
} }
}
function main(metadata, orientation, panelHeight, instance_id) { function main(metadata, orientation, panelHeight, instance_id) {
//appletPath = metadata.path; return new WindowControlApplet(metadata, orientation, panelHeight, instance_id);
let myApplet = new WindowButtonApplet(orientation,metadata, panelHeight, instance_id);
return myApplet;
} }

View file

@ -1,9 +1,11 @@
{ {
"uuid": "window-buttons-fixed@sapphirus.org", "uuid": "window-controls-applet@sapphirus.org",
"author": "Sapphirus",
"max-instances": "15", "max-instances": "15",
"dangerous": false, "dangerous": false,
"description": "You can control windows with this applet", "description": "An applet which provides window controls. Supports multi-display and multi-panel configurations.",
"name": "Window Buttons", "name": "Window Controls Applet",
"icon": "applications-other", "icon": "applications-other",
"last-edited": "1395875196" "version": "1.1.2",
"last-edited": 1655893302
} }

View file

@ -1,35 +1,21 @@
{ "head" : { { "head" : {
"type" : "header", "type" : "header",
"description" : "Window Buttons Settings" "description" : "Window Controls Applet Configuration"
}, "buttons-style": { }, "button-layout": {
"type": "entry", "type": "entry",
"description": "Window Buttons Style (default: title:icon:minimize:maximize:close)", "description": "Button Layout (default: minimize:maximize:close)",
"default": "minimize:maximize:close" "default": "minimize:maximize:close"
}, "buttons-theme" : { }, "button-theme" : {
"type": "combobox", "type": "entry",
"default" : "default", "default" : "default",
"description" : "Buttons Theme (Needs Restart Cinnamon)", "description" : "Button Theme (Restart the Window Manager with Ctrl+Alt+Esc to apply changes. Only required to be set on first instance.)"
"options" : {
"Default" : "default",
"Ambiance" : "Ambiance",
"Ambiance-Blue" : "Ambiance-Blue",
"Mac-OS-X" : "Mac-OS-X",
"Mint-X" : "Mint-X",
"Radiance" : "Radiance",
"UniMetro" : "UniMetro",
"UniMetro-Black" : "UniMetro-Black",
"Zukitwo" : "Zukitwo",
"Zukitwo-Dark" : "Zukitwo-Dark",
"Vimix" : "Vimix"
},
"tooltip" : "Please select a theme for window buttons"
}, "on-desktop-shutdown" : {
"type" : "checkbox",
"default" : false,
"description": "On desktop show shutdown menu when click close button"
}, "only-maximized" : { }, "only-maximized" : {
"type" : "checkbox", "type" : "checkbox",
"default" : false, "default" : false,
"description": "Show applet (active buttons-title-icon on this instance) only active window maximized" "description": "Show window controls only when an active window maximized"
}, "verbose-log": {
"type": "checkbox",
"default" : false,
"description": "Toggle logging visibility to all. (Only for debugging purposes)"
} }
} }