Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmrjnck committed Jan 28, 2024
1 parent 818bb0e commit 9b07671
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# kwin-screen-shortcuts
KWin script for consistently ordered screen shortcuts
KWin provides shortcuts to send windows to specific screens ("Window to Screen
N") and to set focus on specific screens when "Separate screen focus" is enabled
("Switch to Screen N"). However, the ordering of the screens is not predictable
or consistent across different screen configurations.

This KWin script adds shortcuts with the same name and functionality, but with
predictable screen ordering from left to right. So no matter what your screen
configuration is, triggering "Window to Screen 0" will always send to window to
the leftmost screen.

Ultimately, I think this should be fixed in KWin, but for now a script does the
trick.
28 changes: 28 additions & 0 deletions reload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# Install the package
PKGTOOL="kpackagetool5 --type KWin/Script"
PKGNAME=screen_shortcuts
if $PKGTOOL --show $PKGNAME >/dev/null 2>&1
then
$PKGTOOL --upgrade src
else
$PKGTOOL --install src
fi

# In case it was already loaded, disable it here so that it will restart when we
# enable it
sleep 0.5
kwriteconfig5 --file kwinrc --group Plugins --key $PKGNAME false
sleep 0.5
qdbus org.kde.KWin /KWin reconfigure

# Clean any stale shortcuts
sleep 0.5
qdbus org.kde.kglobalaccel /component/kwin org.kde.kglobalaccel.Component.cleanUp > /dev/null

# Enable it
sleep 0.5
kwriteconfig5 --file kwinrc --group Plugins --key $PKGNAME true
sleep 0.5
qdbus org.kde.KWin /KWin reconfigure
69 changes: 69 additions & 0 deletions src/contents/code/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Given two screen bounds, a < b if a's left edge is left of b's. If left edges
// are equal, a < b if a's top edge is below b's.
function screenSort(a, b) {
var x = a[1].x - b[1].x
if (x != 0) {
return x;
}
var y = a[1].y - b[1].y
if (y != 0) {
return -y;
}
return 0;
}

// Convert a logical screen number (based on above sorting) into its KWin
// internal index.
function logicalToPhysicalScreen(logicalScreen) {
var screens = [];
for (var i = 0; i < workspace.numScreens; ++i) {
var screen =
workspace.clientArea(KWin.ScreenArea, i, workspace.currentDesktop);
screens.push([ i, screen ])
}
screens.sort(screenSort)
return screens[logicalScreen][0]
}

// Return the topmost client window on the given screen.
function topClientOnScreen(physicalScreen) {
const allClients = workspace.clientList();
var topClient = null;
for (var i = 0; i < allClients.length; ++i) {
var c = allClients[i];
if (c.specialWindow || c.screen != physicalScreen ||
(c.desktop != workspace.currentDesktop && c.desktop != -1)) {
continue;
}

if (!topClient || c.stackingOrder > topClient.stackingOrder) {
topClient = c;
}
}
return topClient;
}

function setupShortcuts() {
const scriptName = "screen-shortcuts";
for (let i = 0; i < 3; ++i) {
let title = `_Switch to Screen ${i}_`;
let text = `Switch to Screen ${i} (${scriptName})`;
let shortcut = "Alt+" + [ "Q", "W", "E" ][i];
registerShortcut(title, text, shortcut, function() {
var newClient = topClientOnScreen(logicalToPhysicalScreen(i));
if (newClient !== null) {
workspace.activeClient = newClient;
}
});

title = `_Window to Screen ${i}_`;
text = `Window to Screen ${i} (${scriptName})`;
shortcut = "Alt+Shift+" + [ "Q", "W", "E" ][i];
registerShortcut(title, text, shortcut, function() {
workspace.sendClientToScreen(workspace.activeClient,
logicalToPhysicalScreen(i));
});
}
}

setupShortcuts();
20 changes: 20 additions & 0 deletions src/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"KPlugin": {
"Name": "Screen Shortcuts",
"Description": "Fix KDE's screen shortcut ordering",
"Icon": "preferences-system-windows",
"Authors": [
{
"Email": "jonathan.doman@gmail.com",
"Name": "Jonathan Doman"
}
],
"Id": "screen-shortcuts",
"Version": "1.0",
"License": "Unlicense",
"Website": "https://github.com/jrmrjnck/kwin-screen-shortcuts"
},
"X-Plasma-API": "javascript",
"X-Plasma-MainScript": "code/main.js",
"KPackageStructure": "KWin/Script"
}

0 comments on commit 9b07671

Please sign in to comment.