mattydebie/bitwarden-rofi

Focus last active i3 window

Opened this issue · 2 comments

vargn commented

I have follow focus_follows_mouse yes in my i3 config file. So when I launch the bwmenu rofi window and copy the password. The window that will be focused is where my mouse cursor happens to be. Even though I might have previously navigated windows using keybinds. This is a bit annoying as I often have to re-navigate to the window I had active when launching the bwmenu. What should I add to the bwmenu script so that it will focus the last active window and not the window where the mouse cursos is currently placed?

I can't be the only one with this issue? Please point me in the right direction :)

I wrote the following bwmenu wrapper script to deal with this:

#!/bin/bash

# store current mouse location
mouse_location="$(xdotool getmouselocation --shell)"
while IFS= read -r line; do
    declare "$line"
done <<< "$mouse_location"

# store resolution of current monitor
current_monitor_resolution="$(get_focused_monitor_resolution)"
while IFS= read -r line; do
    declare "$line"
done <<< "$current_monitor_resolution"

# mark currently focused window
i3-msg mark "bwmenu-mark"

# move mouse to upper-left of screen
warpd --move '1 1'

# call bwmenu and pass along all given parameters
bwmenu "$@"

# move mouse back to where it was before calling bwmenu
# (modulo resolution of current monitor, since warpd is relative to current monitor)
warpd --move "$((X % WIDTH)) $((Y % HEIGHT))"

# focus on window with mark
i3-msg '[con_mark="bwmenu-mark"] focus'

# remove mark
i3-msg unmark "bwmenu-mark"

It does make some assumptions though:

  • It assumes you're using the 'type all/username/password' functionality instead of copying the password. The problem with copying is that the bwmenu process only exits after the clipboard is cleared, so the script doesn't continue until that happens.
  • The cursor has to be placed somewhere unfocusable. In my case this is https://github.com/polybar/polybar/, at the top of the screen. I use https://github.com/rvaiya/warpd to place the cursor there and then place it back in the original location afterwards.

I hope this works for you or gives you an idea how to fix it for your exact usecase