hyprwm / Hyprland

Hyprland is an independent, highly customizable, dynamic tiling Wayland compositor that doesn't sacrifice on its looks.
https://hyprland.org
BSD 3-Clause "New" or "Revised" License
19.51k stars 818 forks source link

How to switch focus to previous window in the same workspace? #2061

Open TTtrace opened 1 year ago

TTtrace commented 1 year ago

Discussed in https://github.com/hyprwm/Hyprland/discussions/2037

Originally posted by **TTtrace** April 13, 2023 The `focuscurrentorlast` dispatcher will move focus to the previously focused window, no matter in which workspace. I hope to use `ALT + Tab` to switch focus between two windows in the same workspace, how to achieve that? Further more, if we type `ALT + Tab ` once, focus will cycle through just two window in the workspace; if we type `ALT + Tab ` many times, focus can cycle through all the windows in the workspace. This behavior looks like in Windows/macOS. Is this possible?
tchofy commented 1 year ago

bind = ALT,Tab,cyclenext prev

And if you happen to have two or more floating windows overlapping, you might also want to add a second bind to the same keys: bind = ALT,Tab,bringactivetotop

TTtrace commented 1 year ago

bind = ALT,Tab,cyclenext prev

I have tried it. Unfortunately not what I expect.

Let me make it clearer. The Scenario is:

There are three or more windows in the same workspace. I do something in window 1, and move to window 2 for something else. Then if I want to go back to window 1 , I just ALT+Tab. After a while, I can switch back to window 2 using ALT + Tab. This way, ALT + Tab always makes me go back to previous focused window.

But cyclenext, prev will move like: window 1 -> 2 -> 3 -> 4 -> 1 ...

By the way, if windows in the workspace is in full screen mode, cyclenext, prev will act as I need, different from normal behavior.

Anyway, thank you for your help!

tchofy commented 1 year ago

Ah I see, sorry for the misunderstanding.

I mulled over this for a bit, tried binding hyprctl dispatch cyclenext || hyprctl dispatch cyclenext prev, like the wofi/rofi bind suggested in the wiki, but seems like it only works for processes that hang on. Your idea is essentially the workspace previous dispatcher (with misc:allow_workspace_cycles 1) but for cyclenext instead, so each window remembers what was focused before, like workspaces remember what was the previous workspace. Would be an interesting feature to have.

mgutz commented 1 year ago

I asked the same Q on reddit about master layout and switching between last focused window. hyprctl doesn't return enough metadata to script solutions. To get close to this in master layout, bind focuscurrentorlast and layoutmsg, swapwithmaster.

Unfortunately, there isn't enough metadata to determine if the active window is master or a slave in the layout. The solution would be something like

if is_focused_master:
    hyprctrl dispatch "focuscurrentorlast"
    hyprctl dispatch "layoutmsg swapwithmaster"
else:
   hyprctl dispatch "swapwithmaster"

In an ideal world, hyprctl would provide metadata about the current workspace layout, the order of clients within the layout and a MRU clients stack. Additionally, a dispatcher like swapwindows ADDR1 ADDR2.

I probably could take a stab at a PR, but it's been ages since I touched C++.

valerii15298 commented 8 months ago

I use these bindings:

bind = ALT,Tab,cyclenext
bind = ALT_SHIFT,Tab,cyclenext,prev

Quite useful ALT+TAB => to go forwards and ALT + SHIFT + TAB => to go backwards Similar experience to what I have with browser tabs(ctrl+tab and ctrl+shift+tab)

BR00l commented 8 months ago

any idea on how to expand this in a multimonitor setup?

aposteeltje-bwg commented 8 months ago

Hi all. I found this website, when i was searching for the exact same functionallity and bindings: https://lib.rs/crates/window_switcher WindowSwitcher

A small rust CLI tool to switch between windows in hyperland.

It can switch through all windows or only windows of same class(= application) in regular or reverse order. The script accepts 5 parameters:.

--same-class Switch between windows of same class (type)
--reverse Reverse the order of the windows
--stay-workspace Restrict cycling of windows to current workspace
--ignore-workspace Ignore workspaces and sort like one big workspace for each monitor
--ignore-monitor Ignore monitors and sort like one big monitor, workspaces must have offset of 10 for each monitor
--vertical-workspaces will treat workspaces as vertical aligned (used with --ignore-workspace)
aposteeltje-bwg commented 8 months ago

I used i3-wm for years, and it has a unique feature: auto_back_and_forth , which is applicable to workspaces. So, what it does is: Say, i'm on ws.2, running a browser. I open a terminal, which opens on ws.1 and autom. switches to ws.1., bringing the terminal on top. Now, when i invoke "Alt-Tab"., it switches back to the previously focused window, across workspaces, so it switches to ws.2 , bringing the browser on top.

When i close the terminal on ws.1 ., the exact same thing happens, and brings me back to ws.2 ., de browser focused.

I'm looking for the same functionallity for Hyprland, and the closest thing/setting i can think of is:

(since we can bind more commands to the same binding...) bind = ALT, Tab, cyclenext # cycle through applications on same workspace bind = ALT, Tab, bringactivetotop # while cycling on same workspace, bring selected to top bind = $mainMod, Tab, focuscurrentorlast # bring back focus to previous focused window across workspaces

It somehow DOES work., allthough it's not really the same.

zvictor commented 2 months ago

hyprswitch is the only thing that worked for me:

hyprswitch.conf