Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,8 @@
"wallpaper": "Wallpaper",
"weather": "Weather",
"widget-list": "Widget List",
"widgets": "Widgets"
"widgets": "Widgets",
"window-switcher": "Window Switcher"
},
"sections": {
"accessibility": "Accessibility",
Expand Down Expand Up @@ -3059,6 +3060,10 @@
"description": "Copy the current wallpaper and colors to Noctalia Greeter (requires administrator approval)",
"label": "Noctalia Greeter"
},
"window-switcher-mru": {
"description": "Order windows in the switcher by most recently used rather than workspace and screen layout",
"label": "Use MRU Order"
},
"telemetry": {
"description": "Sends a small anonymous startup ping with version, OS, compositor, monitor resolutions, RAM, and UI scale",
"label": "Anonymous Startup Ping"
Expand Down
3 changes: 3 additions & 0 deletions docs/user/configuration/shell.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ command = "swaylock -f"
[shell.mpris]
blacklist = [] # optional list of players to hide from media widgets/control-center

[shell.window_switcher]
mru = false # order windows by most recently used (Alt+Tab toggle) instead of workspace layout

[shell.privacy]
mic_filter_regex = "" # ignore matching microphone application names
cam_filter_regex = "" # ignore matching camera process names
Expand Down
4 changes: 4 additions & 0 deletions example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ prefix = "win"
[shell.mpris]
blacklist = [] # ignore MPRIS players by bus/identity/desktop entry token


[shell.window_switcher]
mru = false # order windows by most recently used (Alt+Tab toggle) instead of workspace layout

# ── Wallpaper ─────────────────────────────────────────────────────────────────

[wallpaper]
Expand Down
7 changes: 7 additions & 0 deletions src/config/config_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,12 @@ struct ShellConfig {
bool operator==(const PrivacyConfig&) const = default;
};

struct WindowSwitcherConfig {
bool mru = false;

bool operator==(const WindowSwitcherConfig&) const = default;
};

float cornerRadiusScale = 1.0F;
bool buttonBorders = true;
bool inputBorders = true;
Expand Down Expand Up @@ -1109,6 +1115,7 @@ struct ShellConfig {
ShadowConfig shadow;
PanelConfig panel;
LauncherConfig launcher;
WindowSwitcherConfig windowSwitcher;
KeyboardLayoutConfig keyboardLayout;
ScreenCornersConfig screenCorners;
MprisConfig mpris;
Expand Down
8 changes: 8 additions & 0 deletions src/config/schema/config_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,13 @@ namespace noctalia::config::schema {
return s;
}

const Schema<ShellConfig::WindowSwitcherConfig>& shellWindowSwitcherSchema() {
static const Schema<ShellConfig::WindowSwitcherConfig> s = {
field(&ShellConfig::WindowSwitcherConfig::mru, "mru"),
};
return s;
}

const Schema<ShellConfig::ScreenCornersConfig>& shellScreenCornersSchema() {
static const Schema<ShellConfig::ScreenCornersConfig> s = {
field(&ShellConfig::ScreenCornersConfig::enabled, "enabled"),
Expand Down Expand Up @@ -1562,6 +1569,7 @@ namespace noctalia::config::schema {
subTable(&ShellConfig::panel, "panel", shellPanelSchema()),
subTable(&ShellConfig::launcher, "launcher", shellLauncherSchema()),
subTable(&ShellConfig::keyboardLayout, "keyboard_layout", shellKeyboardLayoutSchema()),
subTable(&ShellConfig::windowSwitcher, "window_switcher", shellWindowSwitcherSchema()),
subTable(&ShellConfig::screenCorners, "screen_corners", shellScreenCornersSchema()),
subTable(&ShellConfig::mpris, "mpris", shellMprisSchema()),
subTable(&ShellConfig::screenshot, "screenshot", shellScreenshotSchema()),
Expand Down
5 changes: 5 additions & 0 deletions src/shell/settings/settings_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,11 @@ namespace settings {
tr("settings.schema.shell.screenshot-pipe-to-command.description"), {"shell", "screenshot", "pipe_to_command"},
ToggleSetting{cfg.shell.screenshot.pipeToCommand}, "screenshot capture pipe command stdin"
));
entries.push_back(makeEntry(
SettingsSection::Shell, "window-switcher", tr("settings.schema.shell.window-switcher-mru.label"),
tr("settings.schema.shell.window-switcher-mru.description"), {"shell", "window_switcher", "mru"},
ToggleSetting{cfg.shell.windowSwitcher.mru}, "window switcher alt tab mru most recently used"
));
{
auto e = makeEntry(
SettingsSection::Shell, "screenshot", tr("settings.schema.shell.screenshot-pipe-command.label"),
Expand Down
103 changes: 92 additions & 11 deletions src/shell/switcher/window_switcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ namespace {
return {};
}

[[nodiscard]] std::string currentFocusedWindowKey(const CompositorPlatform& platform) {
if (const auto focusedId = platform.focusedCompositorWindowId(); focusedId.has_value() && !focusedId->empty()) {
return canonicalWindowId(*focusedId);
}
if (const auto active = platform.activeToplevel(); active.has_value() && active->handle != nullptr) {
return "handle:" + std::to_string(reinterpret_cast<std::uintptr_t>(active->handle));
}
return {};
}

[[nodiscard]] std::uintptr_t resolveCloseHandle(
const CompositorPlatform& platform, std::string_view windowId, std::string_view appId, std::string_view title
) {
Expand Down Expand Up @@ -274,6 +284,8 @@ namespace {
std::int32_t sortX = 0;
std::int32_t sortY = 0;
std::uint64_t toplevelOrder = 0;
// Initialize to max so non-MRU items gracefully fall to the back
std::size_t mruIndex = std::numeric_limits<std::size_t>::max();
};

[[nodiscard]] WindowSwitcherEntry makeEntryFromAssignment(
Expand Down Expand Up @@ -329,7 +341,8 @@ namespace {

void buildWindowEntries(
const CompositorPlatform& platform, IconResolver& iconResolver, int iconSize,
std::vector<WindowSwitcherEntry>& out, const std::optional<std::string>& focusedId
std::vector<WindowSwitcherEntry>& out, const std::optional<std::string>& focusedId, bool useMru = false,
const std::deque<std::string>& mruKeys = {}
) {
std::unordered_map<std::string, WorkspaceWindowAssignment> assignmentById;
assignmentById.reserve(32);
Expand All @@ -351,11 +364,26 @@ namespace {
std::vector<WindowSwitcherCandidate> candidates;
candidates.reserve(assignmentById.size() + liveToplevelById.size());

std::unordered_map<std::string_view, std::size_t> mruRanks;
if (useMru) {
mruRanks.reserve(mruKeys.size());
for (std::size_t i = 0; i < mruKeys.size(); ++i) {
mruRanks.try_emplace(mruKeys[i], i);
}
}

auto addCandidate = [&](WindowSwitcherCandidate candidate, const std::string& key) {
if (key.empty() || seenKeys.contains(key)) {
return;
}
seenKeys.insert(key);

if (useMru) {
const std::string idKey = identityKeyForEntry(candidate.entry);
if (auto it = mruRanks.find(idKey); it != mruRanks.end()) {
candidate.mruIndex = it->second;
}
}
candidates.push_back(std::move(candidate));
};

Expand Down Expand Up @@ -391,7 +419,23 @@ namespace {
addCandidate(std::move(candidate), key);
}

std::optional<std::string> focusedKey;
if (focusedId.has_value()) {
focusedKey = canonicalWindowId(*focusedId);
if (focusedKey->empty()) {
focusedKey = *focusedId;
}
}
if (!focusedKey.has_value()) {
if (const auto active = platform.activeToplevel(); active.has_value() && active->handle != nullptr) {
focusedKey = "handle:" + std::to_string(reinterpret_cast<std::uintptr_t>(active->handle));
}
}

std::ranges::stable_sort(candidates, [](const WindowSwitcherCandidate& a, const WindowSwitcherCandidate& b) {
if (a.mruIndex != b.mruIndex) {
return a.mruIndex < b.mruIndex;
}
if (a.workspaceKey != b.workspaceKey) {
return a.workspaceKey < b.workspaceKey;
}
Expand All @@ -412,14 +456,6 @@ namespace {
out.clear();
out.reserve(candidates.size());

std::optional<std::string> focusedKey;
if (focusedId.has_value()) {
focusedKey = canonicalWindowId(*focusedId);
if (focusedKey->empty()) {
focusedKey = *focusedId;
}
}

if (focusedKey.has_value()) {
for (auto it = candidates.begin(); it != candidates.end(); ++it) {
const std::string key = identityKeyForEntry(it->entry);
Expand Down Expand Up @@ -577,8 +613,33 @@ void WindowSwitcher::onOutputChange() {
}
}

void WindowSwitcher::recordFocusedWindow() {
if (m_platform == nullptr) {
return;
}
const bool useMru = (m_config != nullptr) ? m_config->config().shell.windowSwitcher.mru : false;
if (useMru) {
promoteMruKey(currentFocusedWindowKey(*m_platform));
}
}

void WindowSwitcher::promoteMruKey(const std::string& key) {
if (key.empty()) {
return;
}
auto it = std::ranges::find(m_mruKeys, key);
if (it != m_mruKeys.end()) {
if (it != m_mruKeys.begin()) {
std::rotate(m_mruKeys.begin(), it, it + 1);
}
} else {
m_mruKeys.insert(m_mruKeys.begin(), key);
}
}

void WindowSwitcher::onToplevelChange() {
if (!m_active) {
recordFocusedWindow();
return;
}
const std::size_t previousCount = m_windows.size();
Expand All @@ -595,6 +656,9 @@ void WindowSwitcher::show(wl_output* output) {
}

const bool wasActive = m_active;
if (!wasActive) {
recordFocusedWindow();
}
refreshWindows();

m_output = output;
Expand Down Expand Up @@ -640,9 +704,25 @@ void WindowSwitcher::refreshWindows() {
}
}

const bool useMru = (m_config != nullptr) ? m_config->config().shell.windowSwitcher.mru : false;

IconResolver iconResolver;
const int iconSize = 96;
buildWindowEntries(*m_platform, iconResolver, iconSize, m_windows, m_platform->focusedCompositorWindowId());
buildWindowEntries(
*m_platform, iconResolver, iconSize, m_windows, m_platform->focusedCompositorWindowId(), useMru, m_mruKeys
);

if (useMru) {
std::unordered_set<std::string> currentKeys;
currentKeys.reserve(m_windows.size());
for (const auto& entry : m_windows) {
const std::string k = identityKeyForEntry(entry);
if (!k.empty()) {
currentKeys.insert(k);
}
}
std::erase_if(m_mruKeys, [&](const std::string& k) { return !currentKeys.contains(k); });
}

if (selectedKey.has_value()) {
for (std::size_t i = 0; i < m_windows.size(); ++i) {
Expand Down Expand Up @@ -704,10 +784,11 @@ void WindowSwitcher::activateSelected() {
if (m_platform == nullptr || m_windows.empty() || m_selectedIndex >= m_windows.size()) {
return;
}
const WindowSwitcherEntry entry = m_windows[m_selectedIndex];
promoteMruKey(identityKeyForEntry(entry));
// Hyprland ignores zwlr_foreign_toplevel_handle_v1.activate while an exclusive
// keyboard layer-shell surface is mapped (hyprwm/Hyprland#4829). Snapshot the
// selection, tear the overlay down, then activate on the next loop tick.
const WindowSwitcherEntry entry = m_windows[m_selectedIndex];
CompositorPlatform* platform = m_platform;
hide();
DeferredCall::callLater([platform, entry]() { activateWindowSwitcherEntry(*platform, entry); });
Expand Down
4 changes: 4 additions & 0 deletions src/shell/switcher/window_switcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "wayland/wayland_seat.h"

#include <cstddef>
#include <deque>
#include <memory>
#include <optional>
#include <string>
Expand Down Expand Up @@ -55,6 +56,8 @@ class WindowSwitcher {
void buildScene(Instance& instance, std::uint32_t width, std::uint32_t height);
void positionGrid(Instance& instance, float screenW, float screenH);
void syncGridSelection();
void recordFocusedWindow();
void promoteMruKey(const std::string& key);

WaylandConnection* m_wayland = nullptr;
RenderContext* m_renderContext = nullptr;
Expand All @@ -64,6 +67,7 @@ class WindowSwitcher {

Instance* m_instance = nullptr;
std::vector<WindowSwitcherEntry> m_windows;
std::deque<std::string> m_mruKeys;
std::size_t m_selectedIndex = 0;
std::size_t m_gridColumns = 5;
wl_output* m_output = nullptr;
Expand Down