Skip to content

Commit

Permalink
add rect visualization when layout entries has the format x--y--width…
Browse files Browse the repository at this point in the history
…--height (values range from 0 to 1)
  • Loading branch information
Leleat committed Jan 5, 2021
1 parent 0fb8913 commit 66c29d3
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 40 deletions.
19 changes: 11 additions & 8 deletions tiling-assistant@leleat-on-github/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Funcs = Me.imports.funcs;

let appDash = null;
let tilePreview = null;
let settings = null;
var settings = null;

// 2 entry points:
// 1. tiled with keyboard shortcut (set with this extension) => onMyTilingShortcutPressed()
Expand Down Expand Up @@ -755,16 +755,18 @@ function onWindowMoving(window, grabStartPos, currTileGroup, freeScreenRects) {
// called when a window is tiled (via tileWindow()).
// decides wether the Dash should be opened. If yes, the dash will be opened.
function onWindowTiled(tiledWindow) {
if (appDash.shown || !settings.get_boolean("enable-dash"))
if (appDash.shown)
return;

let openWindows = Funcs.getOpenWindows();
let currTileGroup = Funcs.getTopTileGroup(openWindows, false);
let freeScreenRects = Funcs.getFreeScreenRects(currTileGroup);

// setup tileGroup to raise tiled windows as a group
Funcs.updateTileGroup(currTileGroup);

if (!settings.get_boolean("enable-dash"))
return;

// remove the tiled windows from openWindows to populate the Dash
currTileGroup.forEach(w => {
let idx = openWindows.indexOf(w);
Expand All @@ -778,7 +780,13 @@ function onWindowTiled(tiledWindow) {

if (openWindows.length == 0)
return;

// filter the openWindows array, so that no duplicate apps are shown
let openApps = [];
openWindows.forEach(w => openApps.push(winTracker.get_window_app(w)));
openWindows = openWindows.filter((w, pos) => openApps.indexOf(winTracker.get_window_app(w)) == pos);

let freeScreenRects = Funcs.getFreeScreenRects(currTileGroup);
if (!freeScreenRects.length)
return;

Expand All @@ -796,11 +804,6 @@ function onWindowTiled(tiledWindow) {
return;
}

// filter the openWindows array, so that no duplicate apps are shown
let openApps = [];
openWindows.forEach(w => openApps.push(winTracker.get_window_app(w)));
openWindows = openWindows.filter((w, pos) => openApps.indexOf(winTracker.get_window_app(w)) == pos);

if (freeScreenSpace.width < 200 || freeScreenSpace.height < 200)
return;

Expand Down
4 changes: 2 additions & 2 deletions tiling-assistant@leleat-on-github/funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,13 @@ function restoreWindowSize(window, restoreFullPos = false) {
if (restoreFullPos) {
// user_op as false to restore window while keeping it fully in screen in case DND-tiling dragged it offscreen
window.move_resize_frame(false, oldRect.x, oldRect.y, oldRect.width, oldRect.height);

} else { // scale while keeping the top at the same relative y pos (for DNDing)
let currWindowFrame = window.get_frame_rect();
let [mouseX] = global.get_pointer();
let relativeMouseX = (mouseX - currWindowFrame.x) / currWindowFrame.width;
let newPosX = mouseX - oldRect.width * relativeMouseX;

// user_op with true to properly restore big windows via DND so they can go partly offscreen
window.move_frame(true, newPosX, currWindowFrame.y); // Wayland workaround for DND/restore position
window.move_resize_frame(true, newPosX, currWindowFrame.y, oldRect.width, oldRect.height);
Expand Down
116 changes: 98 additions & 18 deletions tiling-assistant@leleat-on-github/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function buildPrefsWidget () {
const MyPrefsWidget = new GObject.Class({
Name : "MyTilingPrefsWidget",
GTypeName : "MyTilingPrefsWidget",
Extends : Gtk.ScrolledWindow, // or ScrolledWindow if this gets too big
Extends : Gtk.ScrolledWindow,

_init : function (params) {
let gschema = Gio.SettingsSchemaSource.new_from_directory(
Expand Down Expand Up @@ -64,14 +64,43 @@ const MyPrefsWidget = new GObject.Class({
for (let i = 0; i <= 9; i++) {
let drawArea = this.builder.get_object("DrawArea" + i);
drawArea.connect("draw", (widget, cr) => {
let rects = [{x: 0, y: 0, w: .5, h: 1}, {x: .5, y: 0, w: .25, h: .5}, {x: .75, y: 0, w: .25, h: .5}, {x: .5, y: .5, w: .5, h: .5}];
this.drawLayoutRects(widget, cr, rects)
let rects = this.getLayoutRects(i);
let layoutIsValid = this.isLayoutValid(rects);

if (layoutIsValid) {
this.drawLayoutRects(widget, cr, rects);

} else { // TODO error message

}
});
}

this.builder.get_object("reloadLayoutsButton").connect("clicked", () => {
for (let i = 0; i <= 9; i++) {
let drawArea = this.builder.get_object("DrawArea" + i);
drawArea.queue_draw();
}
});

this.setupTranslations();
},

// manually add the keys to the arrays in this function
getBindProperty: function(key) {
let ints = ["icon-size", "icon-margin", "window-gaps"];
let bools = ["enable-dash", "show-label", "use-anim"];

if (ints.includes(key))
return "value"; // spinbox.value

else if (bools.includes(key))
return "active"; // switch.active

else
return null;
},

// taken from Overview-Improved by human.experience
// https://extensions.gnome.org/extension/2802/overview-improved/
makeShortcutEdit: function(settingKey) {
Expand Down Expand Up @@ -111,38 +140,89 @@ const MyPrefsWidget = new GObject.Class({
updateShortcutRow(this.settings.get_strv(settingKey)[0]);
},

// manually add the keys to the arrays in this function
getBindProperty: function(key) {
let ints = ["icon-size", "icon-margin", "window-gaps"];
let bools = ["enable-dash", "show-label", "use-anim"];
// format should be: x--y--width--height where the variables range from 0.0 to 1.0
// for ex.: 0 -- 0 -- .25 -- 0.75
// return null, if wrong format
getLayoutRects: function(layoutIndex) {
let rects = [];
let rectProps = ["x", "y", "width", "height"];

if (ints.includes(key))
return "value"; // spinbox.value
let layoutListBox = this.builder.get_object("LayoutListbox" + layoutIndex)
for (let i = 0; i < 8; i++) {
let r = {};

else if (bools.includes(key))
return "active"; // switch.active
let entry = layoutListBox.get_row_at_index(i).get_child().get_children()[1];
if (!entry.get_text_length())
continue;

else
return null;
let text = entry.get_text();
let splits = text.split("--");
if (splits.length != 4)
return null;

for (let j = 0; j < 4; j++) {
let propValue = parseFloat(splits[j].trim());
if (Number.isNaN(propValue))
return null;

r[rectProps[j]] = propValue;
}

rects.push(r);
}

return rects;
},

drawLayoutRects(layoutWidget, cr, rects) {
isLayoutValid(rects) {
// wrong format for rects in Gtk.Entrys
// or no text in entries
if (!rects || !rects.length)
return false;

// calculate the surface area of an overlap
let rectsOverlap = function(r1, r2) {
return Math.max(0, Math.min(r1.x + r1.width, r2.x + r2.width) - Math.max(r1.x, r2.x)) * Math.max(0, Math.min(r1.y + r1.height, r2.y + r2.height) - Math.max(r1.y, r2.y));
}

for (let i = 0, len = rects.length; i < len; i++) {
let r = rects[i];

// rects is/reaches outside of screen (i. e. > 1)
if (r.x < 0 || r.y < 0 || r.width < 0 || r.height < 0 || r.x + r.width > 1 || r.y + r.height > 1)
return false;

for (let j = i + 1; j < len; j++) {
if (rectsOverlap(r, rects[j]))
return false;
}
}

return true;
},

drawLayoutRects: function(layoutWidget, cr, rects) {
//rects = [{x: 0, y: 0, width: .5, height: .5}]
let color = new Gdk.RGBA();
let width = layoutWidget.get_allocated_width();
let height = layoutWidget.get_allocated_height();

cr.setLineWidth(1.0);

rects.forEach(r => {
// 1px outline for rect in transparent white
color.parse("rgba(255, 255, 255, .2)");
Gdk.cairo_set_source_rgba(cr, color);

// 5 px gaps between rects
cr.moveTo(r.x * width + 5, r.y * height + 5);
cr.lineTo((r.x + r.w) * width - 5, r.y * height + 5);
cr.lineTo((r.x + r.w) * width - 5, (r.y + r.h) * height - 5);
cr.lineTo(r.x * width + 5, (r.y + r.h) * height - 5);
cr.lineTo((r.x + r.width) * width - 5, r.y * height + 5);
cr.lineTo((r.x + r.width) * width - 5, (r.y + r.height) * height - 5);
cr.lineTo(r.x * width + 5, (r.y + r.height) * height - 5);
cr.lineTo(r.x * width + 5, r.y * height + 5);
cr.strokePreserve();

// fill rect in transparent black
color.parse("rgba(0, 0, 0, .15)");
Gdk.cairo_set_source_rgba(cr, color);
cr.fill();
Expand Down
29 changes: 18 additions & 11 deletions tiling-assistant@leleat-on-github/prefs.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox0">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -2436,7 +2436,7 @@
</packing>
</child>
<child>
<object class="GtkFrame">
<object class="GtkFrame" id="FR">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">10</property>
Expand Down Expand Up @@ -2524,7 +2524,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -2925,7 +2925,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -3300,6 +3300,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image4</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -3326,7 +3327,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -3701,6 +3702,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image5</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -3727,7 +3729,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox4">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -4102,6 +4104,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image6</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -4128,7 +4131,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox5">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -4503,6 +4506,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image7</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -4529,7 +4533,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox6">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -4904,6 +4908,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image8</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -4930,7 +4935,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox7">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -5305,6 +5310,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image9</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -5331,7 +5337,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox8">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down Expand Up @@ -5706,6 +5712,7 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-bottom">15</property>
<property name="image">image10</property>
<property name="image-position">right</property>
<property name="always-show-image">True</property>
Expand All @@ -5732,7 +5739,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox">
<object class="GtkListBox" id="LayoutListbox9">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
Expand Down
2 changes: 1 addition & 1 deletion tiling-assistant@leleat-on-github/stylesheet.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.my-open-windows-dash {
background-color: rgb(22, 22, 22);
border-radius: 15px;
border: 2px solid rgba(65, 66, 68, 0.85);
border: 1px solid rgba(65, 66, 68, 0.85);
}

.tiling-window-focused {
Expand Down

0 comments on commit 66c29d3

Please sign in to comment.