https://bugs.kde.org/show_bug.cgi?id=318422

Stephan Badragan <sbadra...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sbadra...@gmail.com

--- Comment #3 from Stephan Badragan <sbadra...@gmail.com> ---
Hi, first, greatly appreciate KDE, and the work people have put in to give us
kwin scripting! Just think it could be improved to be a killer feature of KDE.

Ran into this problem recently when switching over to linux KDE from macOS
where one can write lua scripts to control the window management through
hammerspoon. What I wanted was to write a script that would allow me to "toggle
an app". Similar to the window shortcut functionality, but de-activate the
current app if already active on shortcut press.

Shortcuts don't get re-created when calling registerShortcut(), and there is no
unregisterShortcut()

Some of the other related issues I ran into and could be improved to get
experience closer to hammerspoon levels:
1. proper script reloading. I eventually found this script, but experience
could be greatly improved:
https://discuss.kde.org/t/how-to-properly-reload-a-kwin-script/4260/2
2. it's very hard to clean up garbage shortcuts created during development of
the script (maybe if we had unregisterShortcut() it would not be an issue)
3. workspace.<something>.connect(<fn>) will keep adding new callbacks as you
create new versions of the script and they never get cleaned up. Those should
be cleaned up when the script is reloaded / removed.

In the end I was able to do what I wanted, but it was more painful then it
should be. Here's my script just for reference:
```js
let currentActiveWinId = null
let lastActiveWinId = null
workspace.windowActivated.connect(function(win) {
    lastActiveWinId = currentActiveWinId
    currentActiveWinId = win.internalId
})

function toggleApp(name)
{
    const win = workspace.stackingOrder.find(function(w) {
        return w.resourceClass === name || w.resourceName === name
    })

    if (!win) {
        // launch app in future
        return
    }

    if (workspace.activeWindow === win) {
        console.info('activating last active thing', lastActiveWinId)
        const lastWin = workspace.stackingOrder.find(function(w) {
            return w.internalId === lastActiveWinId
        })
        if (lastWin) {
            workspace.activeWindow = lastWin
        }
    } else {
        workspace.activeWindow = win
    }
}

function getToggleApp2(name) {
    return function() {
        toggleApp(name)
    }
}

function registerToggleAppShortcut(name, key) {
    const prefix = 'Meta+Ctrl+Alt+Shift+'
    const shortcut = prefix + key
    const title = 'toggle app ' + name
    const callback = function() { toggleApp(name) }
    registerShortcut(title, title, shortcut, callback)
}

registerToggleAppShortcut('dolphin', 'p')
registerToggleAppShortcut('firefox', 'r')
registerToggleAppShortcut('wezterm-gui', 'e')
registerToggleAppShortcut('vivaldi-stable', 'd')
registerToggleAppShortcut('obsidian', 'f')
registerToggleAppShortcut('thunderbird', 'o')
```

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to