Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Make circular Alt+Tab step between apps

oxydizerd284a71034d3
diff --git a/docs/wiki/Using-RediWM.md b/docs/wiki/Using-RediWM.md
index f49356d..dd630b7 100644
--- a/docs/wiki/Using-RediWM.md
+++ b/docs/wiki/Using-RediWM.md
@@ -57,9 +57,11 @@ Windows from the same app share a stack around the circle, with a badge in the
centre showing how many; stepping onto that app's stack fans it open. The
selected window's title appears in the centre. Previews fan around the hub
with their top edge at the outer rim; tall windows are cropped at the bottom.
-Up to three cards per app and eight apps per page are visible. Tab exhausts a
-stack's own windows, most recently used first, before moving to the next app;
-Shift+Tab mirrors that. Small outputs and renderers without polygon support
+Up to three cards per app and eight apps per page are visible. Tab and
+Shift+Tab move between apps, so a quick Alt+Tab always returns to the previous
+app; while the switcher is open, the key above Tab (Shift for backwards) or
+Up/Down steps through the selected app's windows, most recently used first.
+With only one app open, Tab steps through its windows. Small outputs and renderers without polygon support
use the horizontal strip. Set `switcher_style = "linear"` to use the default
horizontal strip.
diff --git a/src/Switcher.zig b/src/Switcher.zig
index d14ee34..2864c87 100644
--- a/src/Switcher.zig
+++ b/src/Switcher.zig
@@ -118,6 +118,17 @@ pub fn step(self: *Self, server: *Server, reverse: bool, trigger: Trigger) void
self.output.?.wlr_output.scheduleFrame();
}
+/// Circular style only: the key above Tab or Up/Down steps through the
+/// selected app's own windows while the switcher is held open.
+pub fn stepWindow(self: *Self, reverse: bool) void {
+ if (!self.active() or !self.circle.enabled or self.circle.closing or self.cards.items.len == 0) return;
+ const before = self.selected;
+ circular.advanceMember(self, reverse);
+ if (self.selected == before) return;
+ circular.select(self, false);
+ self.output.?.wlr_output.scheduleFrame();
+}
+
fn open(self: *Self, server: *Server) !void {
server.input.window_menu.close();
const output = server.getDefaultOutput() orelse return;
diff --git a/src/keyboard/process.zig b/src/keyboard/process.zig
index 25c8d9a..5ff791d 100644
--- a/src/keyboard/process.zig
+++ b/src/keyboard/process.zig
@@ -232,6 +232,9 @@ pub fn processKey(keyboard: *Keyboard, time_msec: u32, event_keycode: u32, state
keyboard.stopSwitcherRepeat();
if (sym == xkb.Keysym.Escape) keyboard.server.switcher.dismiss();
if (sym == xkb.Keysym.Return) keyboard.server.switcher.accept(keyboard.server);
+ // KEY_GRAVE is the key above Tab on every layout.
+ if (event_keycode == 41) keyboard.server.switcher.stepWindow(mods.shift);
+ if (sym == xkb.Keysym.Up or sym == xkb.Keysym.Down) keyboard.server.switcher.stepWindow(sym == xkb.Keysym.Up);
if (event_keycode < keyboard.switcher_consumed.len) keyboard.switcher_consumed[event_keycode] = true;
return;
}
diff --git a/src/switcher/circular.zig b/src/switcher/circular.zig
index 088d4fe..e399a76 100644
--- a/src/switcher/circular.zig
+++ b/src/switcher/circular.zig
@@ -1,6 +1,8 @@
//! Circular presentation of the switcher's frozen MRU list, grouped by app.
-//! Tab/Shift+Tab exhausts a multi-window group's own windows (`advance`,
-//! member by member in first-seen order) before moving to the next app; the
+//! Tab/Shift+Tab moves between apps (`advance`), so a quick Alt+Tab always
+//! reaches the previous app however many windows the current one has; the
+//! key above Tab and Up/Down step through the selected app's own windows
+//! (`advanceMember`). With a single app, Tab steps its windows instead. The
//! linear style keeps the flat MRU order untouched. A group's stack fans open
//! (`State.expand`) only while it holds the selection.
const std = @import("std");
@@ -173,27 +175,28 @@ fn groupFor(self: *Switcher, server: *Server, raw: []const u8, index: usize) !us
return self.circle.groups.items.len - 1;
}
-/// Replaces the flat `(selected ± 1) % n` step for the circular style: a
-/// multi-window group is exhausted member by member, in the order its
-/// windows were first seen, before the wheel moves on to the next group.
-/// Forward and backward are exact mirrors, so a full lap in either direction
-/// revisits every window exactly once, same as the flat styles.
+/// Replaces the flat `(selected ± 1) % n` step for the circular style: Tab
+/// moves to the next app's group, landing on its front window (its most
+/// recently used one, or whichever member this gesture last brought forward).
+/// The gesture opens on the focused window, so the first Tab always leaves
+/// the current app. A lone group has nowhere to go, so Tab steps its windows.
pub fn advance(self: *Switcher, reverse: bool) void {
const groups = self.circle.groups.items;
if (groups.len == 0) return;
+ if (groups.len == 1) return advanceMember(self, reverse);
const card = self.cards.items[self.selected];
- const group = groups[card.group];
- const next_member: ?usize = if (reverse)
- (if (card.member == 0) null else card.member - 1)
- else
- (if (card.member + 1 >= group.count) null else card.member + 1);
- if (next_member) |member| {
- self.selected = group.members.items[member];
- return;
- }
const next_group = if (reverse) (card.group + groups.len - 1) % groups.len else (card.group + 1) % groups.len;
- const entry: usize = if (reverse) groups[next_group].count - 1 else 0;
- self.selected = groups[next_group].members.items[entry];
+ self.selected = groups[next_group].front;
+}
+
+/// Steps to the next (or previous) window of the selected app, in MRU order,
+/// wrapping inside the group.
+pub fn advanceMember(self: *Switcher, reverse: bool) void {
+ const card = self.cards.items[self.selected];
+ const group = self.circle.groups.items[card.group];
+ if (group.count < 2) return;
+ const member = if (reverse) (card.member + group.count - 1) % group.count else (card.member + 1) % group.count;
+ self.selected = group.members.items[member];
}
pub fn select(self: *Switcher, opening: bool) void {
diff --git a/tests/switcher.py b/tests/switcher.py
index 04eb3a9..e2fa229 100644
--- a/tests/switcher.py
+++ b/tests/switcher.py
@@ -201,9 +201,10 @@ def run(scale, modifier=56, style='linear'):
def run_group_cycle(scale):
- """A multi-window app's circular sector stacks its windows; Tab exhausts
- that app's own windows (most recent first) before moving to the next
- app, Shift+Tab mirrors it, and the stack fans open while selected."""
+ """A multi-window app's circular sector stacks its windows; Tab moves
+ between apps (so a quick Alt+Tab from a multi-window app reaches the
+ previous app), the key above Tab and Up/Down step through the selected
+ app's windows, and the stack fans open while selected."""
with tempfile.TemporaryDirectory(prefix='rediwm-switcher-group-') as directory:
tmp = Path(directory)
build_client(tmp)
@@ -225,37 +226,53 @@ def run_group_cycle(scale):
wait_for(lambda: len(ipc.get_windows()) == i + 1, 'map group-cycle fixture')
by_title = {w['title']: w['id'] for w in ipc.get_windows()}
ids = [by_title[t] for t, _ in specs] # alpha1, alpha2, alpha3, alpha4, beta, gamma
+ TAB, GRAVE, SHIFT, UP, DOWN = 15, 41, 42, 103, 108
- def establish_mru():
+ def establish_mru(last=None):
# Focusing in this order leaves Gamma most recent, Beta
# next, then Alpha 4..1 (Alpha 4 the most recently used
# of the group): cards = [gamma, beta, a4, a3, a2, a1].
for wid in ids:
ipc.action('focus_window', {'id': wid})
+ if last is not None:
+ ipc.action('focus_window', {'id': last})
def focused():
return ipc.get_focused_window()['id']
- def gesture(steps, reverse=False):
- establish_mru()
+ def gesture(*keys, last=None):
+ """Hold Alt, press each key (a tuple adds Shift), release."""
+ establish_mru(last)
ipc.key(56, True)
- if reverse: ipc.key(42, True)
- for _ in range(steps): ipc.key_down_up(15)
- if reverse: ipc.key(42, False)
+ for key in keys:
+ shifted = isinstance(key, tuple)
+ if shifted: ipc.key(SHIFT, True)
+ ipc.key_down_up(key[0] if shifted else key)
+ if shifted: ipc.key(SHIFT, False)
ipc.key(56, False)
return focused()
- # Forward: leave Gamma's and Beta's singleton groups, then walk
- # the Alpha group newest-to-oldest before completing the lap.
- assert gesture(1) == ids[4], 'first Tab leaves the starting (singleton) group'
- assert gesture(2) == ids[3], 'second Tab enters the app group at its most recent window'
- assert gesture(3) == ids[2], 'third Tab stays inside the app'
- assert gesture(4) == ids[1], 'fourth Tab is still inside the app'
- assert gesture(5) == ids[0], "fifth Tab reaches the app's last window before leaving it"
- assert gesture(6) == ids[5], 'sixth Tab leaves the exhausted group and completes the lap'
- # Reverse is the exact mirror.
- assert gesture(1, reverse=True) == ids[0], 'Shift+Tab enters the previous group at its last window'
- assert gesture(4, reverse=True) == ids[3], 'reverse cycling mirrors forward order inside the group'
+ # Tab walks apps, landing on each app's most recent window.
+ assert gesture(TAB) == ids[4], 'first Tab selects the previous app'
+ assert gesture(TAB, TAB) == ids[3], "second Tab enters the stacked app at its most recent window"
+ assert gesture(TAB, TAB, TAB) == ids[5], 'third Tab completes the lap of apps'
+ assert gesture((TAB,)) == ids[3], 'Shift+Tab selects the previous app at its most recent window'
+ assert gesture((TAB,), (TAB,)) == ids[4], 'reverse cycling walks apps backwards'
+ # The reported bug: from a window of a multi-window app, a
+ # quick Alt+Tab must reach the other app, and back again.
+ assert gesture(TAB, last=ids[3]) == ids[5], 'Alt+Tab from a stacked app leaves it'
+ establish_mru(ids[3])
+ ipc.action('focus_window', {'id': ids[5]})
+ ipc.key(56, True); ipc.key_down_up(TAB); ipc.key(56, False)
+ assert focused() == ids[3], 'Alt+Tab toggles back to the stacked app'
+ # Inside the selected app: the key above Tab and Up/Down.
+ assert gesture(TAB, TAB, GRAVE) == ids[2], 'the key above Tab steps to the next window of the app'
+ assert gesture(TAB, TAB, GRAVE, GRAVE) == ids[1], 'it keeps stepping through the app'
+ assert gesture(TAB, TAB, (GRAVE,)) == ids[0], 'Shift steps backwards and wraps'
+ assert gesture(TAB, TAB, DOWN) == ids[2], 'Down steps to the next window of the app'
+ assert gesture(TAB, TAB, UP) == ids[0], 'Up steps to the previous window of the app'
+ assert gesture(GRAVE) == ids[5], 'a singleton app has no other window to step to'
+ assert gesture(TAB, TAB, GRAVE, TAB, (TAB,)) == ids[2], 'returning to an app keeps the window brought forward'
# The stack fans open only while its group holds the
# selection: a screenshot grabbed on the first frame after