|
| 1 | +const Main = imports.ui.main |
| 2 | +const Applet = imports.ui.applet |
| 3 | +const Settings = imports.ui.settings |
| 4 | +const SignalManager = imports.misc.signalManager |
| 5 | +const Cinnamon = imports.gi.Cinnamon |
| 6 | +const St = imports.gi.St |
| 7 | + |
| 8 | +const APPNAME = 'Maximus Tittle Buttons' |
| 9 | +const tracker = Cinnamon.WindowTracker.get_default() |
| 10 | +const UUID = 'maximus-title-buttons@hanspr' |
| 11 | + |
| 12 | +class MyApplet extends Applet.TextIconApplet { |
| 13 | + constructor(metadata, orientation, panelHeight, instanceId) { |
| 14 | + try { |
| 15 | + super(orientation, panelHeight, instanceId) |
| 16 | + this.metadata = metadata |
| 17 | + this.uuid = metadata.uuid |
| 18 | + this.orientation = orientation |
| 19 | + this.panelHeight = panelHeight |
| 20 | + this.instanceId = instanceId |
| 21 | + this.appletPath = metadata.path |
| 22 | + this.hide_applet_label(false) |
| 23 | + this.bindSettings() |
| 24 | + this.initButtons() |
| 25 | + this.connectSignals() |
| 26 | + setTimeout(() => { |
| 27 | + this.initialized = true |
| 28 | + }, 500) |
| 29 | + } catch (e) { |
| 30 | + global.logError(e) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + initButtons() { |
| 35 | + let buttons = this.buttons_style.split(":") |
| 36 | + if (this.checkButton(buttons, "maximize") || this.checkButton(buttons, "minimize") || this.checkButton(buttons, "close")) { |
| 37 | + this.loadTheme() |
| 38 | + } |
| 39 | + this.button = [] |
| 40 | + this.createButtons(this.buttons_style) |
| 41 | + this.on_panel_edit_mode_changed |
| 42 | + this._showButtons(global.display.focus_window) |
| 43 | + } |
| 44 | + |
| 45 | + bindSettings() { |
| 46 | + this.settings = new Settings.AppletSettings(this, this.uuid, this.instanceId) |
| 47 | + |
| 48 | + this.settings.bind("buttons-style", "buttons_style", this.on_settings_changed) |
| 49 | + this.settings.bind("buttons-theme", "buttonsTheme") |
| 50 | + this.settings.bind("only-maximized", "onlyMaximized", this.on_settings_changed) |
| 51 | + this.settings.bind("hide-buttons", "hideButtons", this.on_settings_changed) |
| 52 | + this.settings.bind("on-desktop-shutdown", "onDesktopShutdown", this.on_settings_changed) |
| 53 | + } |
| 54 | + |
| 55 | + connectSignals() { |
| 56 | + this.signalManager = new SignalManager.SignalManager(null) |
| 57 | + |
| 58 | + this.signalManager.connect(Main.themeManager, 'theme-set', this.loadTheme, this) |
| 59 | + this.signalManager.connect(global.settings, 'changed::panel-edit-mode', this.on_panel_edit_mode_changed, this) |
| 60 | + this.signalManager.connect(global.window_manager, 'size-change', () => { |
| 61 | + let w = global.display.focus_window |
| 62 | + if (w) { |
| 63 | + this._showButtons(w) |
| 64 | + } |
| 65 | + }, this) |
| 66 | + |
| 67 | + this.signalManager.connect(global.display, 'notify::focus-window', () => { |
| 68 | + let w = global.display.focus_window |
| 69 | + if (w) { |
| 70 | + this._showButtons(w) |
| 71 | + } |
| 72 | + }, this) |
| 73 | + this.signalManager.connect(global.screen, 'window-monitor-changed', () => { |
| 74 | + let w = global.display.focus_window |
| 75 | + if (w) { |
| 76 | + this._showButtons(w) |
| 77 | + } |
| 78 | + }, this) |
| 79 | + this.signalManager.connect(global.screen, 'monitors-changed', () => { |
| 80 | + let w = global.display.focus_window |
| 81 | + if (w == null) { |
| 82 | + this.setButtons("hide") |
| 83 | + } else { |
| 84 | + this._showButtons(w) |
| 85 | + } |
| 86 | + }, this) |
| 87 | + } |
| 88 | + |
| 89 | + on_panel_edit_mode_changed() { |
| 90 | + let reactive = !global.settings.get_boolean("panel-edit-mode") |
| 91 | + |
| 92 | + let b = this.buttons_style.split(":") |
| 93 | + for (let i = 0; i < b.length; ++i) { |
| 94 | + this.button[b[i]].reactive = reactive |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + getCssPath(theme) { |
| 99 | + let cssPath = this.appletPath + "/themes/" + theme + "/style.css" |
| 100 | + return cssPath |
| 101 | + } |
| 102 | + |
| 103 | + loadTheme() { |
| 104 | + this.actor.set_style_class_name("window-buttons") |
| 105 | + let theme = St.ThemeContext.get_for_stage(global.stage).get_theme() |
| 106 | + theme.load_stylesheet(this.getCssPath(this.buttonsTheme)) |
| 107 | + this.oldTheme = this.buttonsTheme |
| 108 | + } |
| 109 | + |
| 110 | + createButtons(buttonsStyle) { |
| 111 | + buttonsStyle = buttonsStyle.split(":") |
| 112 | + for (let i = 0; i < buttonsStyle.length; ++i) { |
| 113 | + let buttonName = buttonsStyle[i] + "Button" |
| 114 | + if (this[buttonName]) { |
| 115 | + this[buttonName]() |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + iconButton() { |
| 121 | + this.button["icon"] = new St.Button({ |
| 122 | + name: "iconButton", |
| 123 | + style_class: "window-list-item", |
| 124 | + reactive: true |
| 125 | + }) |
| 126 | + this.actor.add(this.button["icon"]) |
| 127 | + } |
| 128 | + |
| 129 | + minimizeButton() { |
| 130 | + this.button["minimize"] = new St.Button({ |
| 131 | + name: "windowButton", |
| 132 | + style_class: "minimize window-button", |
| 133 | + reactive: true |
| 134 | + }) |
| 135 | + this.actor.add(this.button["minimize"]) |
| 136 | + this.button["minimize"].connect("button-press-event", (actor, event) => { |
| 137 | + let button = event.get_button() |
| 138 | + if (button == 1) { |
| 139 | + this.minimizeWindow() |
| 140 | + return true |
| 141 | + } else if (button == 3) { |
| 142 | + this._applet_context_menu.toggle() |
| 143 | + } |
| 144 | + return true |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + minimizeWindow() { |
| 149 | + if (this.button["minimize"].opacity == 0) { |
| 150 | + return false |
| 151 | + } |
| 152 | + let activeWindow = global.display.focus_window |
| 153 | + let app = tracker.get_window_app(activeWindow) |
| 154 | + if (!app) { |
| 155 | + return |
| 156 | + } else { |
| 157 | + activeWindow.minimize() |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + maximizeButton() { |
| 162 | + this.button["maximize"] = new St.Button({ |
| 163 | + name: "windowButton", |
| 164 | + style_class: "maximize window-button", |
| 165 | + reactive: true |
| 166 | + }) |
| 167 | + this.actor.add(this.button["maximize"]) |
| 168 | + this.button["maximize"].connect("button-press-event", (actor, event) => { |
| 169 | + let button = event.get_button() |
| 170 | + if (button == 1) { |
| 171 | + this.maximizeWindow() |
| 172 | + return true |
| 173 | + } else if (button == 3) { |
| 174 | + this._applet_context_menu.toggle() |
| 175 | + } |
| 176 | + return true |
| 177 | + }) |
| 178 | + } |
| 179 | + |
| 180 | + maximizeWindow() { |
| 181 | + if (this.button["maximize"].opacity == 0) { |
| 182 | + return false |
| 183 | + } |
| 184 | + let activeWindow = global.display.focus_window |
| 185 | + if (activeWindow) { |
| 186 | + let app = tracker.get_window_app(activeWindow) |
| 187 | + if (!app) { |
| 188 | + return |
| 189 | + } else { |
| 190 | + if (activeWindow.get_maximized()) { |
| 191 | + activeWindow.unmaximize(3) |
| 192 | + } else { |
| 193 | + activeWindow.maximize(3) |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + closeButton() { |
| 201 | + this.button["close"] = new St.Button({ |
| 202 | + name: "windowButton", |
| 203 | + style_class: "close window-button", |
| 204 | + reactive: true |
| 205 | + }) |
| 206 | + this.actor.add(this.button["close"]) |
| 207 | + this.button["close"].connect("button-press-event", (actor, event) => { |
| 208 | + let button = event.get_button() |
| 209 | + if (button == 1) { |
| 210 | + this.closeWindow() |
| 211 | + return true |
| 212 | + } else if (button == 3) { |
| 213 | + this._applet_context_menu.toggle() |
| 214 | + } |
| 215 | + return true |
| 216 | + }) |
| 217 | + } |
| 218 | + |
| 219 | + closeWindow() { |
| 220 | + if (this.button["close"].opacity == 0) { |
| 221 | + return false |
| 222 | + } |
| 223 | + let activeWindow = global.display.focus_window |
| 224 | + let app = tracker.get_window_app(activeWindow) |
| 225 | + |
| 226 | + if (!app) { |
| 227 | + if (this.onDesktopShutdown == true) { |
| 228 | + this._session.ShutdownRemote() |
| 229 | + } |
| 230 | + return |
| 231 | + } else { |
| 232 | + activeWindow.delete(global.get_current_time()) |
| 233 | + } |
| 234 | + |
| 235 | + } |
| 236 | + |
| 237 | + updateWindowIcon() { |
| 238 | + let activeWindow = global.display.focus_window |
| 239 | + if (activeWindow) { |
| 240 | + let app = tracker.get_window_app(activeWindow) |
| 241 | + if (app) { |
| 242 | + let icon = tracker.get_window_app(activeWindow).create_icon_texture(20) |
| 243 | + this.button["icon"].set_child(icon) |
| 244 | + this.actor.add(this.button['icon']) |
| 245 | + } else { |
| 246 | + let icon = new St.Icon({ |
| 247 | + icon_name: "video-display", |
| 248 | + icon_type: St.IconType.SYMBOLIC, |
| 249 | + style: "icon-size:20px;" |
| 250 | + }) |
| 251 | + this.button["icon"].set_child(icon) |
| 252 | + } |
| 253 | + } else { |
| 254 | + let icon = new St.Icon({ |
| 255 | + icon_name: "video-display", |
| 256 | + icon_type: St.IconType.SYMBOLIC, |
| 257 | + style: "icon-size:20px;" |
| 258 | + }) |
| 259 | + this.button["icon"].set_child(icon) |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + _showButtons(w) { |
| 264 | + if (!w) { |
| 265 | + return |
| 266 | + } |
| 267 | + if (w.get_monitor() != this.panel.monitorIndex) { |
| 268 | + this.setButtons("hide") |
| 269 | + return |
| 270 | + } |
| 271 | + if (w.get_window_type() >= 1) { |
| 272 | + this.setButtons("hide") |
| 273 | + return |
| 274 | + } |
| 275 | + let buttons = this.buttons_style.split(":") |
| 276 | + if (this.checkButton(buttons, "icon")) { |
| 277 | + this.updateWindowIcon() |
| 278 | + } |
| 279 | + if (this.onlyMaximized == true) { |
| 280 | + this.onlyMaximize(w) |
| 281 | + } else { |
| 282 | + this.setButtons("show") |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + on_settings_changed() { |
| 287 | + this.actor.destroy_all_children() |
| 288 | + let buttons = this.buttons_style.split(":") |
| 289 | + if (this.checkButton(buttons, "maximize") || this.checkButton(buttons, "minimize") || this.checkButton(buttons, "close")) { |
| 290 | + this.loadTheme() |
| 291 | + } |
| 292 | + this.button = [] |
| 293 | + this.createButtons(this.buttons_style) |
| 294 | + this._showButtons(global.display.focus_window) |
| 295 | + } |
| 296 | + |
| 297 | + checkButton(arr, obj) { |
| 298 | + for (var i = 0; i < arr.length; i++) { |
| 299 | + if (arr[i] == obj) { |
| 300 | + return true |
| 301 | + } |
| 302 | + } |
| 303 | + return null |
| 304 | + } |
| 305 | + |
| 306 | + onlyMaximize(w) { |
| 307 | + let app = tracker.get_window_app(w) |
| 308 | + if (app && w.get_maximized()) { |
| 309 | + this.setButtons("show") |
| 310 | + } else { |
| 311 | + this.setButtons("hide") |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + setButtons(what) { |
| 316 | + let buttons = this.buttons_style.split(":") |
| 317 | + let skip = 0 |
| 318 | + if (what == "show") { |
| 319 | + skip = 255 |
| 320 | + } |
| 321 | + for (let i = 0; i < buttons.length; ++i) { |
| 322 | + if (buttons[i] == undefined || buttons[i] == "icon" || this.button[buttons[i]] == undefined || this.button[buttons[i]].opacity == skip) { |
| 323 | + continue |
| 324 | + } |
| 325 | + if (what == "show") { |
| 326 | + if (!this.hideButtons) { |
| 327 | + this.button[buttons[i]].show() |
| 328 | + } |
| 329 | + this.button[buttons[i]].opacity = 255 |
| 330 | + } else { |
| 331 | + if (!this.hideButtons) { |
| 332 | + this.button[buttons[i]].hide() |
| 333 | + } |
| 334 | + this.button[buttons[i]].opacity = 0 |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | +} |
| 339 | + |
| 340 | +function main(metadata, orientation, panelHeight, instanceId) { |
| 341 | + return new MyApplet(metadata, orientation, panelHeight, instanceId) |
| 342 | +} |
0 commit comments