Cleanup and refactor
This commit is contained in:
parent
7e103a775a
commit
03190ae2d8
1 changed files with 120 additions and 144 deletions
|
@ -1,10 +1,9 @@
|
||||||
/* Window Controls Applet for the Cinnamon Desktop Environment
|
/* Window Controls Applet for the Cinnamon Desktop Environment
|
||||||
* Copyright (C) 2017-2022 Xavier (sapphirus@azorium.net)
|
* Copyright (C) 2017-2023 Xavier (sapphirus@azorium.net)
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, with only version 3 of the License.
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
@ -25,6 +24,7 @@ const Settings = imports.ui.settings;
|
||||||
const Main = imports.ui.main;
|
const Main = imports.ui.main;
|
||||||
const Meta = imports.gi.Meta;
|
const Meta = imports.gi.Meta;
|
||||||
const WindowTracker = imports.gi.Cinnamon.WindowTracker;
|
const WindowTracker = imports.gi.Cinnamon.WindowTracker;
|
||||||
|
const GLib = imports.gi.GLib;
|
||||||
|
|
||||||
const _WindowTracker = WindowTracker.get_default();
|
const _WindowTracker = WindowTracker.get_default();
|
||||||
const WorkspaceManager = global.display.get_workspace_manager();
|
const WorkspaceManager = global.display.get_workspace_manager();
|
||||||
|
@ -34,8 +34,11 @@ const VERBOSE = 1;
|
||||||
const ERROR = 2;
|
const ERROR = 2;
|
||||||
const ALL = 3;
|
const ALL = 3;
|
||||||
|
|
||||||
class Logger {
|
const MAXIMIZE = 'maximize';
|
||||||
|
const MINIMIZE = 'minimize';
|
||||||
|
const CLOSE = 'close';
|
||||||
|
|
||||||
|
class Logger {
|
||||||
constructor(omittanceLevel, loggerName) {
|
constructor(omittanceLevel, loggerName) {
|
||||||
this.omittanceLevel = omittanceLevel;
|
this.omittanceLevel = omittanceLevel;
|
||||||
this.loggerName = loggerName;
|
this.loggerName = loggerName;
|
||||||
|
@ -55,23 +58,73 @@ class Logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoggingLevel(level) {
|
getOmittance() {
|
||||||
|
return this.omittanceLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
setOmittance(level) {
|
||||||
this.omittanceLevel = level;
|
this.omittanceLevel = level;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ButtonEvent {
|
||||||
|
constructor(wca, buttonType) {
|
||||||
|
this.wca = wca;
|
||||||
|
this.logger = wca.logger;
|
||||||
|
this.buttonType = buttonType;
|
||||||
|
}
|
||||||
|
|
||||||
|
eventHandler(actor, event) {
|
||||||
|
let button = event.get_button();
|
||||||
|
this.wca.logger.log(VERBOSE, "Button event triggered for "+button+" "+this.buttonType);
|
||||||
|
if (button == 1) {
|
||||||
|
return this._buttonPress();
|
||||||
|
} else if(button == 3) {
|
||||||
|
this._applet_context_menu.toggle();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_buttonPress() {
|
||||||
|
let activeWindow = this.wca.getActiveWindow();
|
||||||
|
let app = _WindowTracker.get_window_app(activeWindow);
|
||||||
|
if(!activeWindow || !app)
|
||||||
|
return false;
|
||||||
|
switch(this.buttonType) {
|
||||||
|
case MINIMIZE:
|
||||||
|
activeWindow.minimize();
|
||||||
|
this.loggger.log(VERBOSE, "Minimised Window: "+ activeWindow.get_title());
|
||||||
|
return true;
|
||||||
|
case MAXIMIZE:
|
||||||
|
if (activeWindow.get_maximized()) activeWindow.unmaximize(3); else activeWindow.maximize(3);
|
||||||
|
this.logger.log(VERBOSE, "Maximize Window toggle: "+ activeWindow.get_title());
|
||||||
|
return true;
|
||||||
|
case CLOSE:
|
||||||
|
activeWindow.delete(global.get_current_time());
|
||||||
|
this.logger.log(VERBOSE, "Closed Window: "+activeWindow.get_title());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class WindowControlApplet extends Applet.Applet {
|
class WindowControlApplet extends Applet.Applet {
|
||||||
constructor(metadata, orientation, panelHeight, instance_id) {
|
constructor(metadata, orientation, panelHeight, instanceId) {
|
||||||
super(orientation, panelHeight, instance_id);
|
super(orientation, panelHeight, instanceId);
|
||||||
this.logger = new Logger(INFO, UUID);
|
this.logger = new Logger(INFO, UUID);
|
||||||
this.appletPath=metadata.path;
|
this.appletPath=metadata.path;
|
||||||
this.instance_id = instance_id;
|
this.instanceId = instanceId;
|
||||||
|
this.monitorId = this.panel.monitorIndex;
|
||||||
|
this.themePath = "";
|
||||||
try {
|
try {
|
||||||
this._initialize_settings();
|
this._initialize_settings();
|
||||||
this.logger.log(VERBOSE, "Intialised Settings!");
|
this.logger.log(VERBOSE, "Intialised Settings!");
|
||||||
this._initialize_events();
|
this._initialize_events();
|
||||||
this.logger.log(VERBOSE, "Intialised Events!");
|
this.logger.log(VERBOSE, "Intialised Events!");
|
||||||
|
this._loadTheme();
|
||||||
this._initialize_buttons();
|
this._initialize_buttons();
|
||||||
this.logger.log(VERBOSE, "Intialised Buttons!");
|
this.logger.log(VERBOSE, "Intialised Buttons!");
|
||||||
this._on_panel_edit_mode_changed();
|
this._on_panel_edit_mode_changed();
|
||||||
|
@ -93,180 +146,103 @@ class WindowControlApplet extends Applet.Applet {
|
||||||
global.display.connect('showing-desktop-changed', Lang.bind(this, this._windowChange));
|
global.display.connect('showing-desktop-changed', Lang.bind(this, this._windowChange));
|
||||||
}
|
}
|
||||||
_initialize_buttons() {
|
_initialize_buttons() {
|
||||||
let buttons=this.button_layout.split(':');
|
|
||||||
if(this._checkButton(buttons,'maximize') || this._checkButton(buttons,'minimize') || this._checkButton(buttons,'close')){
|
|
||||||
this._loadTheme();
|
|
||||||
}
|
|
||||||
this.button = [];
|
this.button = [];
|
||||||
this._createButtons(this.button_layout);
|
let buttons=this.button_layout.split(':');
|
||||||
|
for (let i = 0; i < buttons.length; i++) {
|
||||||
|
if(!this._checkButton(buttons[i]))
|
||||||
|
continue;
|
||||||
|
this._createButton(buttons[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_initialize_settings() {
|
_initialize_settings() {
|
||||||
this.settings = new Settings.AppletSettings(this, UUID, this.instance_id);
|
this.settings = new Settings.AppletSettings(this, UUID, this.instanceId);
|
||||||
this.settings.bindProperty(Settings.BindingDirection.IN,"button-layout","button_layout",this._bind_settings,null);
|
this.settings.bindProperty(Settings.BindingDirection.IN,"button-layout","button_layout",this._bind_settings,null);
|
||||||
this.settings.bindProperty(Settings.BindingDirection.IN,"button-theme","button_theme", this._bind_settings,null);
|
this.settings.bindProperty(Settings.BindingDirection.IN,"button-theme","button_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,"verbose-log", "verbose_logging", this._bind_logging,null);
|
||||||
this._set_logging();
|
this._bind_logging();
|
||||||
}
|
}
|
||||||
|
|
||||||
_set_logging() {
|
_bind_logging() {
|
||||||
let loggingLevel = (this.verbose_logging ? ALL : INFO);
|
let omittance = (this.verbose_logging ? ALL : INFO);
|
||||||
this.logger.log(VERBOSE, "Set logging level to "+loggingLevel);
|
this.logger.setOmittance(omittance);
|
||||||
this.logger.setLoggingLevel(loggingLevel);
|
this.logger.log(VERBOSE, "Debugging enabled.");
|
||||||
}
|
}
|
||||||
_bind_settings() {
|
_bind_settings() {
|
||||||
this.actor.destroy_all_children();
|
this.actor.destroy_all_children();
|
||||||
this._initialize_buttons();
|
this._initialize_buttons();
|
||||||
|
this._windowChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
_on_panel_edit_mode_changed() {
|
_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.button_layout.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(theme) {
|
_getCssPath(theme) {
|
||||||
let cssPath = this.appletPath + '/themes/'+theme+'/style.css';
|
return this.appletPath + '/themes/'+theme+'/style.css';
|
||||||
return cssPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_loadTheme(){
|
_loadTheme(){
|
||||||
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.button_theme);
|
let themePath = this._getCssPath(this.button_theme);
|
||||||
theme.load_stylesheet(theme_path);
|
if(!GLib.file_test(themePath, GLib.FileTest.EXISTS)) {
|
||||||
|
this.logger.log(INFO, "Theme at "+themePath+" does not exist.");
|
||||||
|
themePath = this._getCssPath("default");
|
||||||
|
}
|
||||||
|
theme.load_stylesheet(themePath);
|
||||||
|
this.themePath = themePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
_createButtons(buttonLayout) {
|
_createButton(type) {
|
||||||
buttonLayout=buttonLayout.split(':');
|
this.button[type] = new St.Button({ name: 'windowButton', style_class: type + ' window-button', reactive: true });
|
||||||
for (let i=0; i < buttonLayout.length; ++i ){
|
this.actor.add(this.button[type]);
|
||||||
let buttonName="_"+buttonLayout[i]+"Button";
|
let buttonEvent = new ButtonEvent(this, type);
|
||||||
this[buttonName]();
|
this.button[type].connect('button-press-event', Lang.bind(buttonEvent, buttonEvent.eventHandler));
|
||||||
|
this.logger.log(VERBOSE, "Created "+type+" button!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
getActiveWindow() {
|
||||||
|
this.logger.log(VERBOSE, "Event signal triggered");
|
||||||
_minimizeButton() {
|
|
||||||
this.button['minimize'] = new St.Button({ name: 'windowButton', style_class: 'minimize window-button', reactive: true });
|
|
||||||
this.actor.add(this.button['minimize']);
|
|
||||||
this.button['minimize'].connect('button-press-event', Lang.bind(this,function(actor,event){
|
|
||||||
let button = event.get_button();
|
|
||||||
if (button == 1) {
|
|
||||||
this._minimizeWindow();
|
|
||||||
return true;
|
|
||||||
} else if(button == 3) {
|
|
||||||
this._applet_context_menu.toggle();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
_minimizeWindow() {
|
|
||||||
let activeWindow = this._getActiveWindow();
|
|
||||||
let app = _WindowTracker.get_window_app(activeWindow);
|
|
||||||
if(!activeWindow || !app)
|
|
||||||
return;
|
|
||||||
activeWindow.minimize();
|
|
||||||
}
|
|
||||||
|
|
||||||
_maximizeButton() {
|
|
||||||
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.button['maximize'].connect('button-press-event', Lang.bind(this,function(actor,event){
|
|
||||||
let button = event.get_button();
|
|
||||||
if (button == 1) {
|
|
||||||
this._maximizeWindow();
|
|
||||||
return true;
|
|
||||||
} else if(button == 3){
|
|
||||||
this._applet_context_menu.toggle();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
_maximizeWindow() {
|
|
||||||
let activeWindow = this._getActiveWindow();
|
|
||||||
let app = _WindowTracker.get_window_app(activeWindow);
|
|
||||||
if(!activeWindow || !app)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (activeWindow.get_maximized()) {
|
|
||||||
activeWindow.unmaximize(3);
|
|
||||||
} else {
|
|
||||||
activeWindow.maximize(3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_closeButton() {
|
|
||||||
this.button['close'] = new St.Button({ name: 'windowButton', style_class: 'close window-button', reactive: true });
|
|
||||||
this.actor.add(this.button['close']);
|
|
||||||
this.button['close'].connect('button-press-event', Lang.bind(this,function(actor,event){
|
|
||||||
let button = event.get_button();
|
|
||||||
if (button == 1) {
|
|
||||||
this._closeWindow();
|
|
||||||
return true;
|
|
||||||
} else if(button == 3){
|
|
||||||
this._applet_context_menu.toggle();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
_closeWindow() {
|
|
||||||
let activeWindow = this._getActiveWindow();
|
|
||||||
let app = _WindowTracker.get_window_app(activeWindow);
|
|
||||||
if(!activeWindow || !app)
|
|
||||||
return;
|
|
||||||
this.logger.log(INFO, "Closed!");
|
|
||||||
activeWindow.delete(global.get_current_time());
|
|
||||||
}
|
|
||||||
|
|
||||||
_getActiveWindow() {
|
|
||||||
let workspace = WorkspaceManager.get_active_workspace();
|
let workspace = WorkspaceManager.get_active_workspace();
|
||||||
let windows = workspace.list_windows();
|
let windows = workspace.get_display().sort_windows_by_stacking(workspace.list_windows());
|
||||||
for (var i = 0; i < windows.length; i++) {
|
for (var i = windows.length - 1; i >= 0; i--) {
|
||||||
let w = windows[i];
|
let w = windows[i];
|
||||||
let thisMonitor = (w.get_monitor() == this.panel.monitorIndex);
|
if(((!(w.get_maximized() === Meta.MaximizeFlags.BOTH)
|
||||||
if(!thisMonitor)
|
|| (w.get_window_type() == Meta.WindowType.DESKTOP)
|
||||||
|
|| w.minimized) && this.onlyMaximized)
|
||||||
|
|| w.get_monitor() != this.monitorId)
|
||||||
continue;
|
continue;
|
||||||
if(((w.get_maximized() === Meta.MaximizeFlags.BOTH) && (w.get_window_type() != Meta.WindowType.DESKTOP) && !w.minimized && thisMonitor))
|
|
||||||
return w;
|
return w;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
this.logger.log(VERBOSE, "Active window not found.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_windowChange(destroy) {
|
_windowChange() {
|
||||||
if(!this.onlyMaximized)
|
if(!this.onlyMaximized)
|
||||||
return;
|
return;
|
||||||
if(!this._getActiveWindow()) this._toggleButtons(false); else this._toggleButtons(true);
|
this._toggleButtons(this.getActiveWindow() != false);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
_checkButton(arr, obj) {
|
|
||||||
for(var i=0; i<arr.length; i++) {
|
|
||||||
if (arr[i] == obj){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_toggleButtons(toggle) {
|
_toggleButtons(toggle) {
|
||||||
let buttons=this.button_layout.split(':');
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_checkButton(obj) {
|
||||||
|
return obj == MAXIMIZE || obj == MINIMIZE || obj == CLOSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function main(metadata, orientation, panelHeight, instance_id) {
|
function main(metadata, orientation, panelHeight, instanceId) {
|
||||||
return new WindowControlApplet(metadata, orientation, panelHeight, instance_id);
|
return new WindowControlApplet(metadata, orientation, panelHeight, instanceId);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue