Skip to content

Commit

Permalink
try adding settings to configure sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidosomething committed Apr 6, 2021
1 parent 1fd4d7e commit cb7e7ce
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 19 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@

KWin script to resize and move windows. Like Spectacle/Rectangle on mac.


## Installation

Available in the [KDE store](https://store.kde.org/p/1492899/)

## Configuration

The sizes can be changed by using the settings button in `System Settings
> KWin Scripts`
![settings screenshot](meta/settings.png)

To enable this menu you may need to link the script metadata to kservices5:

```sh
mkdir -p ~/.local/share/kservices5/
ln -s ~/.local/share/kwin/scripts/yanjing/metadata.desktop ~/.local/share/kservices5/yanjing.desktop
```

Why? See this [issue](https://github.com/faho/kwin-tiling/issues/79#issuecomment-311465357)

Restarting plasma afterwards in recommended.

## Commands

- Yanjing LEFT - `no default`
Expand Down
41 changes: 33 additions & 8 deletions contents/code/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,38 @@ Yanjing.States = {
ERROR: 'ERROR',
};

// Percent of screen to fill
Yanjing.Sizes = [
75,
100 / 3 * 2,
50,
100 / 3,
25,
];
/**
* @param {string} sizesStringList comma separated
* @return {number[]} No zero/falsies
*/
Yanjing.sanitizeSizes = function (sizesStringList) {
return (sizesStringList
.split(',')
.map(function ensureFloat(v) {
return parseFloat(v); // also serves to trim()
})
.filter(Boolean) // remove 0 and NaN
);
}

var DEFAULT_SIZES = '75,66.6666,50,33.3333,25';

var configSizes = [];
var configSizesString = '';
try {
var configSizesString = readConfig('sizes', '').toString();
configSizes = Yanjing.sanitizeSizes(configSizesString);
} catch (err) {
print(err);
}

if (configSizes.length > 0) {
Yanjing.Sizes = configSizes;
print('Using custom sizes', configSizesString);
} else {
Yanjing.Sizes = Yanjing.sanitizeSizes(DEFAULT_SIZES);
print('Using DEFAULT_SIZES', DEFAULT_SIZES);
}

Yanjing.Dirs = {
Left: 'Left',
Expand Down Expand Up @@ -123,6 +147,7 @@ Yanjing.cycle = function (client, dir) {
rect.width = nextWidth;
client.geometry = rect;

// Move again after cycle to fix reposition due to resize
var after = Yanjing.AfterCycle[dir];
return after && after(client);
};
Expand Down
14 changes: 13 additions & 1 deletion contents/code/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// ===========================================================================

global.print = jest.fn()//.mockImplementation((str) => console.error(str));

global.readConfig = jest.fn().mockImplementation(function (key, defaults) {
return defaults;
});

global.registerShortcut = jest.fn();

global.KWin = {
Expand Down Expand Up @@ -48,6 +53,13 @@ it('calls registerShortcut in main', () => {
expect(global.registerShortcut).toHaveBeenCalledTimes(6);
});

describe('sanitizeSizes', () => {
it('returns array of filtered floats', () => {
expect(Yanjing.sanitizeSizes('0, 5, 10, x, 111.111, 0, 222.333'))
.toEqual([5, 10, 111.111, 222.333]);
});
});

describe('sizeToWidth', () => {
it(`should return px value against work area width`, () => {
expect(Yanjing.sizeToWidth(33.333)).toBeCloseTo(615.99384);
Expand All @@ -59,7 +71,7 @@ describe('sizeToWidth', () => {
describe('widthToSizeIndex', () => {
it(`should return size index relative to ${global.workspace.workspaceWidth}`, () => {
expect(Yanjing.widthToSizeIndex(640))
.toBe(Yanjing.Sizes.findIndex((s) => s === 100/3));
.toBe(Yanjing.Sizes.findIndex((s) => s === 33.3333));
expect(Yanjing.widthToSizeIndex(HALF_SCREEN))
.toBe(Yanjing.Sizes.findIndex((s) => s === 50));
});
Expand Down
12 changes: 12 additions & 0 deletions contents/config/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
<kcfgfile name=""/>
<group name="General">
<entry name="sizes" type="StringList">
<default>75,66.6666,50,33.3333,25</default>
</entry>
</group>
</kcfg>
51 changes: 51 additions & 0 deletions contents/ui/config.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>KWin::YanjingConfigForm</class>
<widget class="QWidget" name="KWin::YanjingConfigForm">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>480</width>
<height>320</height>
</rect>
</property>
<property name="windowTitle">
<string>Yanjing</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="1" column="0">
<layout class="QHBoxLayout" name="layout1">
<item>
<widget class="QLabel" name="labelSizes">
<property name="text">
<string>Sizes, float, comma sep</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="kcfg_sizes">
<property name="toolTip">
<string>Comma separated window size percentages, e.g. "66.6666, 50, 25"</string>
</property>
<property name="statusTip">
<string>Window width percentages</string>
</property>
<property name="whatsThis">
<string>Window width percentages</string>
</property>
<property name="text">
<string>75,66.6666,50,33.3333,25</string>
</property>
<property name="placeholderText">
<string>e.g. 75,66.6666,50,33.3333,25</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources />
<connections />
</ui>
Binary file added meta/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions metadata.desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Comment=Window size and movement
Icon=preferences-system-windows-script-test
Type=Service

X-KDE-ServiceTypes=KWin/Script
X-KDE-ServiceTypes=KWin/Script,KCModule
X-Plasma-API=javascript
X-Plasma-MainScript=code/main.js

X-KDE-PluginInfo-EnabledByDefault=true

X-KDE-PluginInfo-Name=yanjing
X-KDE-PluginInfo-Version=4.1.1
Expand All @@ -16,8 +20,7 @@ X-KDE-PluginInfo-Website=https://github.com/davidosomething/yanjing
[email protected]
X-KDE-PluginInfo-License=MIT
X-KDE-PluginInfo-EnabledByDefault=true
X-Plasma-API=javascript
X-Plasma-MainScript=code/main.js
X-KDE-Library=kwin/effects/configs/kcm_kwin4_genericscripted
X-KDE-PluginKeyword=yanjing
X-KDE-ParentComponents=yanjing
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"scripts": {
"test": "jest",
"debugger": "qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.showInteractiveKWinConsole",
"restartplasma": "kquitapp5 plasmashell; kstart5 plasmashell",
"kwin:is_installed": "plasmapkg2 --type kwinscript --show yanjing",
"kwin:install": "plasmapkg2 --type kwinscript --install .",
"kwin:upgrade": "plasmapkg2 --type kwinscript --upgrade .",
Expand Down
13 changes: 8 additions & 5 deletions scripts/metadata.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Comment={{ description }}
Icon=preferences-system-windows-script-test
Type=Service

X-KDE-ServiceTypes=KWin/Script
X-KDE-ServiceTypes=KWin/Script,KCModule
X-Plasma-API=javascript
X-Plasma-MainScript={{ main }}

X-KDE-PluginInfo-EnabledByDefault=true

X-KDE-PluginInfo-Name={{ name }}
X-KDE-PluginInfo-Version={{ version }}
Expand All @@ -16,7 +20,6 @@ X-KDE-PluginInfo-Website={{ homepage }}
X-KDE-PluginInfo-Email={{ author.email }}
X-KDE-PluginInfo-License={{ license }}

X-KDE-PluginInfo-EnabledByDefault=true

X-Plasma-API=javascript
X-Plasma-MainScript={{ main }}
X-KDE-Library=kwin/effects/configs/kcm_kwin4_genericscripted
X-KDE-PluginKeyword=yanjing
X-KDE-ParentComponents=yanjing

0 comments on commit cb7e7ce

Please sign in to comment.