Skip to content

Commit

Permalink
ws: Hide the icon if there isn't enough space for it
Browse files Browse the repository at this point in the history
  • Loading branch information
anaximeno committed Nov 28, 2024
1 parent d3f9de8 commit 780c541
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
71 changes: 39 additions & 32 deletions files/usr/share/cinnamon/applets/[email protected]/applet.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ class SimpleButton extends WorkspaceButton {


class WindowGraph {
constructor(workspaceGraph, metaWindow, tracker, showIcon, iconSize) {
constructor(workspaceGraph, metaWindow, tracker, iconEnabled, iconSize) {
this.workspaceGraph = workspaceGraph;
this.metaWindow = metaWindow;
this.tracker = tracker;
this.showIcon = showIcon;
this.iconEnabled = iconEnabled;
this.iconSize = iconSize;
this.iconScaledSize = iconSize * global.ui_scale;
this.halvedIconScaledSize = this.iconScaledSize * 0.5;

this.drawingArea = new St.DrawingArea({
style_class: 'windows',
Expand All @@ -159,13 +161,8 @@ class WindowGraph {

this.drawingArea.connect('repaint', this.onRepaint.bind(this));

this._icon = undefined;
if (this.showIcon) {
const rect = this.intersectionRect();
const [x, y] = this.calcIconPos(rect);
this.icon.set_x(x);
this.icon.set_y(y);
}
this._icon = null;
this.positionIconOnGraph(this.intersectionRect());
}

get icon() {
Expand All @@ -175,11 +172,26 @@ class WindowGraph {
}

calcIconPos(rect) {
const x = Math.round(rect.x + rect.width / 2 - this.iconSize * global.ui_scale / 2);
const y = Math.round(rect.y + rect.height / 2 - this.iconSize * global.ui_scale / 2);
const x = Math.round(rect.x + rect.width / 2 - this.halvedIconScaledSize);
const y = Math.round(rect.y + rect.height / 2 - this.halvedIconScaledSize);
return [x, y];
}

positionIconOnGraph(rect) {
if (this.iconEnabled) {
const [x, y] = this.calcIconPos(rect);

this.icon.set_x(x);
this.icon.set_y(y);

if (rect.width < this.iconScaledSize || rect.height < this.iconScaledSize) {
this.icon.hide();
} else {
this.icon.show();
}
}
}

/**
* Intersection between the scaled window rect area
* and the workspace graph area.
Expand Down Expand Up @@ -237,11 +249,7 @@ class WindowGraph {
cr.fill();
cr.$dispose();

if (this.showIcon) {
const [x, y] = this.calcIconPos(rect);
this.icon.set_x(x);
this.icon.set_y(y);
}
this.positionIconOnGraph(rect);
}

getWinThemeColors() {
Expand Down Expand Up @@ -282,13 +290,13 @@ class WindowGraph {
}

destroy() {
if (this.showIcon && this._icon) {
if (this.iconEnabled && this._icon) {
this._icon.destroy();
this._icon = undefined;
this._icon = null;
}

this.drawingArea.destroy();
this.drawingArea = undefined;
this.drawingArea = null;
}

update(options = {}) {
Expand All @@ -297,7 +305,8 @@ class WindowGraph {

show() {
this.workspaceGraph.graphArea.add_child(this.drawingArea);
if (this.showIcon) {

if (this.iconEnabled) {
this.workspaceGraph.graphArea.add_child(this.icon);
}
}
Expand All @@ -321,7 +330,7 @@ class WorkspaceGraph extends WorkspaceButton {
this.graphArea.set_size(1, 1);
this.graphArea.connect('repaint', this.onRepaint.bind(this));

this.focusGraph = undefined;
this.focusGraph = null;
this.windowsGraphs = [];

this.height = 1;
Expand Down Expand Up @@ -390,19 +399,17 @@ class WorkspaceGraph extends WorkspaceButton {

this.graphArea.remove_all_children();

if (this.windowsGraphs) {
this.windowsGraphs.forEach(e => e.destroy());
}
if (this.windowsGraphs) this.windowsGraphs.forEach(e => e.destroy());

this.windowsGraphs = [];

const showIcon = this.applet.show_window_icons;
const iconEnabled = this.applet.show_window_icons;
const iconSize = this.applet.window_icon_size;
const tracker = Cinnamon.WindowTracker.get_default();

this.focusGraph = undefined;
this.focusGraph = null;
for (const window of windows) {
const graph = new WindowGraph(this, window, tracker, showIcon, iconSize);
const graph = new WindowGraph(this, window, tracker, iconEnabled, iconSize);

this.windowsGraphs.push(graph);

Expand All @@ -421,9 +428,9 @@ class WorkspaceGraph extends WorkspaceButton {
}

update(options = {}) {
const stateChanged = options.stateChanged;
const state = options.state;

if ((stateChanged == "position-changed" || stateChanged == "size-changed") &&
if ((state == "position-changed" || state == "size-changed") &&
this.focusGraph && this.focusGraph.metaWindow.has_focus()
) {
this.focusGraph.update(options);
Expand All @@ -440,7 +447,7 @@ class WorkspaceGraph extends WorkspaceButton {
}

destroy() {
this.focusGraph = undefined;
this.focusGraph = null;
this.windowsGraphs.forEach(e => e.destroy());
this.windowsGraphs = [];
super.destroy();
Expand Down Expand Up @@ -626,9 +633,9 @@ class CinnamonWorkspaceSwitcher extends Applet.Applet {
this._onWindowsStateChanged("focus-changed");
}

_onWindowsStateChanged(stateChanged) {
_onWindowsStateChanged(state) {
let button = this.buttons[global.workspace_manager.get_active_workspace_index()];
button.update({stateChanged: stateChanged});
button.update({ state: state });
}

on_applet_removed_from_panel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
},
"show-window-icons": {
"type": "switch",
"default": false,
"dependency": "display-type=visual",
"description": "Display window icons",
"default": false
"tooltip": "Show the window icon at the center of it's respective window graph if there's enough space for it."
},
"window-icon-size": {
"type": "spinbutton",
Expand Down

0 comments on commit 780c541

Please sign in to comment.