tudurom / windowchef

Window Chef is a stacking window manager that cooks windows with orders from the Waitron
https://tudorr.ro/software/windowchef/
ISC License
204 stars 14 forks source link

feature request: focus last window #26

Closed dialuplama closed 7 years ago

dialuplama commented 7 years ago

How about to add waitron command to focus previously focused window.

tudurom commented 7 years ago

Why would you use it? I'm pretty curious.

I've been thinking of adding a config key for focusing the previously focused window after the currently focused window is closed.

dialuplama commented 7 years ago

Why would you use it? I'm pretty curious.

Well, most of the windows managers and even terminal multiplexers allows you to quick switch between the current and previously focused window. It's really efficiently in my opinion.

I've been thinking of adding a config key for focusing the previously focused window after the currently focused window is closed.

Yes! This is also what I wanted to ask. Because when you close some window and don't use sloppy focus you need to do extra move to set focus. It's annoying sometimes.

tudurom commented 7 years ago

what should happen if the previously focused window is destroyed? should the previously focused window be set to the second previously focused window?

dialuplama commented 7 years ago

Yes, and if second previously focused window is destroyed it should focus third and so on.

tudurom commented 7 years ago

well, because that would be pretty complicated because windowchef stores the focused window in a variable and that would be replaced by a stack, it would be simpler to write a script using wew from wmutils that listens to window creation and focus events and keeps a stack in a text file. I'm still thinking if I should really implement this as a feature or not.

n1kolas commented 7 years ago

Just an idea: maybe this can use the functions introduced by the cardinal focus feature. When the window gets closed the window closest to it gets focused. I'm not sure if there's that much benefit in having the focus specifically on the last focused window, which would make implementation a bit more difficult - as you would have to keep history.

dialuplama commented 7 years ago

it would be simpler to write a script using wew from wmutils that listens to window creation and focus events and keeps a stack in a text file.

So here is script. Maybe it will be useful for someone. It stores focused windows in file and focus previously focused window after currently focused window is closed or unmapped (and it will be ignore to focus unmapped windows so it will be work with groups)

#! /bin/sh

wlist="/tmp/.wlist"
[ -e $wlist ] && rm $wlist

wew -m 2097152 | while IFS=: read ev wid; do
    case $ev in
        # Focus in event. Write wid to the window list
        9) 
            echo $wid >> $wlist
            ;;
        # Destroy event. Delete wid from the window list
        17)
            grep -v $wid $wlist > $wlist.tmp
            mv $wlist.tmp $wlist
            ;;
        # Unmap event. Focus last wid from the window list. Ignore unmapped windows.
        18)
            for id in $(tac $wlist); do
                wattr m $id && [ "$wid" != "$id" ] && waitron window_focus $id && break
            done
            ;;
    esac
done

In additional to focus last focused window, you should bind following script to some hotkey. For example alt + grave. Also it will be not ignore unmapped windows. But it can be changed.

#! /bin/sh

wlist="/tmp/.wlist"

for id in $(tac $wlist); do
    [ "$(pfw)" != "$id" ] && mapw -m $id && waitron window_focus $id && break
done
tudurom commented 7 years ago

Kinda off-topic: should I make a directory inside the repo with example scripts?

dialuplama commented 7 years ago

Kinda off-topic: should I make a directory inside the repo with example scripts?

It makes sense

zphixon commented 7 years ago

would be simpler to write a script using wew from wmutils that listens to window creation and focus events and keeps a stack

Here you go. It seems to work pretty well so far, I haven't had any glaring issues in the ten minutes or so that I've been using it.

I would've used bash but I'm not great at it.

Edit: I just saw @dialuplama's solution. I'd probably use that over this, but we'll see where it takes me.

Edit2: There are a few edge cases that I didn't consider. Definitely go with the shell script, my version needs some work...

allora commented 7 years ago

Just put in a PR for this https://github.com/tudurom/windowchef/pull/29

EDIT: Also this change would allow a "toggle last focused" to work. I didn't expose it in the PR, but I don't see it being much work to add. Additionally, it would also be possible to wrap the "toggle last focused on unmapped or window destroy/close" in a config var. I am just lazy and didn't... :P

allora commented 7 years ago

@n1kolas Its certainly possible for some of the distance checks used by the cardinal focuser to pick the nearest window to the closed one.. Not sure if that's really useful.

dialuplama commented 7 years ago

@allora Could you please also add something like waitron window_focus_last command. The original topic was about it

allora commented 7 years ago

@dialuplama I'll do this in a different PR.

tudurom commented 7 years ago

@dialuplama @allora no need for a PR, I implemented the command by myself.

Thank you guys for this quality issue.