Skip to content

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

Prioritize matching pinned apps in launcher search

oxydizerbad4ee0f95ef
diff --git a/src/start_menu/StartMenu.zig b/src/start_menu/StartMenu.zig
index 785f17a..7915b96 100644
--- a/src/start_menu/StartMenu.zig
+++ b/src/start_menu/StartMenu.zig
@@ -127,9 +127,10 @@ pub fn createMode(server: *Server, wlr_output: *wlr.Output, mode: Mode) !*StartM
const snap = server.start_menu_catalog.retainSnapshot();
defer snap.release();
+ server.launcher_history.load(server);
+ menu.model.history = &server.launcher_history;
try menu.model.bindSnapshot(snap, false);
- server.launcher_history.load(server);
menu.buildTree();
menu.slide.retargetTo(anim.nowMs(), 1, anim.curveFor(.panel_slide));
menu.relayout();
diff --git a/src/start_menu/history.zig b/src/start_menu/history.zig
index e4ac402..b076769 100644
--- a/src/start_menu/history.zig
+++ b/src/start_menu/history.zig
@@ -53,6 +53,18 @@ pub const History = struct {
return if (h.index(app_id)) |i| h.entries[i].pinned else false;
}
+ /// Move matching pins first, retaining relevance order within both groups.
+ pub fn prioritize(h: *const History, results: []@import("search.zig").SearchResult) void {
+ var next: usize = 0;
+ for (0..results.len) |i| {
+ const result = results[i];
+ if (result.app_entry == null or !h.pinned(result.id)) continue;
+ std.mem.copyBackwards(@import("search.zig").SearchResult, results[next + 1 .. i + 1], results[next..i]);
+ results[next] = result;
+ next += 1;
+ }
+ }
+
fn remember(h: *History, app_id: []const u8) void {
if (!valid(app_id)) return;
const pin = h.pinned(app_id);
diff --git a/src/start_menu/menu_cards.zig b/src/start_menu/menu_cards.zig
index 1b1a2c4..a2619fd 100644
--- a/src/start_menu/menu_cards.zig
+++ b/src/start_menu/menu_cards.zig
@@ -126,6 +126,7 @@ pub fn onPinClick(owner: ?*anyopaque, id: usize) void {
} else {
menu.model.error_msg = null;
history.togglePin(menu.server, result.id);
+ if (menu.model.snapshot) |snap| menu.model.bindSnapshot(snap, true) catch {};
}
menu.buildTree();
menu.restoreScrollOffset(scroll);
diff --git a/src/start_menu/model.zig b/src/start_menu/model.zig
index dda3d65..419cdd2 100644
--- a/src/start_menu/model.zig
+++ b/src/start_menu/model.zig
@@ -18,6 +18,7 @@ pub const Model = struct {
error_msg: ?[]const u8 = null,
results_arena: ?*std.heap.ArenaAllocator = null,
snapshot: ?*applications.Snapshot = null,
+ history: ?*const @import("history.zig").History = null,
pub fn init(allocator: Allocator) Model {
return .{
@@ -57,8 +58,9 @@ pub const Model = struct {
pub fn bindSnapshot(self: *Model, snap: *applications.Snapshot, preserve_selection: bool) !void {
try self.updateInner(snap.entries, preserve_selection, snap.loading);
+ const retained = snap.retain();
if (self.snapshot) |old| old.release();
- self.snapshot = snap.retain();
+ self.snapshot = retained;
}
fn updateInner(self: *Model, catalog: []const AppEntry, preserve_selection: bool, loading: bool) !void {
@@ -75,6 +77,9 @@ pub const Model = struct {
}
const new_results = try search_mod.search(new_arena.allocator(), catalog, self.query.items, self.category, loading);
+ if (std.mem.trim(u8, self.query.items, " \t\r\n").len > 0) {
+ if (self.history) |history| history.prioritize(new_results);
+ }
self.freeResults();
self.results_arena = new_arena;
@@ -195,9 +200,11 @@ test "loading snapshot selects Settings not the placeholder" {
var model = Model.init(std.testing.allocator);
defer model.deinit();
const snap = try applications.Snapshot.create(std.testing.allocator);
- defer snap.release();
snap.loading = true;
try model.bindSnapshot(snap, false);
+ snap.release();
+ // Pin changes may rebind the snapshot whose only remaining owner is the model.
+ try model.bindSnapshot(model.snapshot.?, true);
try std.testing.expect(model.isLoading());
try std.testing.expectEqual(@as(usize, 0), model.countApplications());
try std.testing.expect(model.selectedResult().?.is_settings);
diff --git a/tests/start_menu.py b/tests/start_menu.py
index 938aa80..fdab52e 100644
--- a/tests/start_menu.py
+++ b/tests/start_menu.py
@@ -587,6 +587,9 @@ def run_launcher():
state = tmp / "state" / "rediwm" / "launcher"
assert "P\tlauncher-3.desktop" in state.read_text()
assert menu()["search_text"] == "Launcher App 3", "pinning lost search"
+ # A broad query selects the matching pin ahead of App 0.
+ client.key_down_up(14)
+ assert menu()["search_text"] == "Launcher App "
client.key_down_up(28)
client.wait_for("menu_closed", timeout_ms=5000)
wait_for(lambda: (tmp / "launched-3").exists(), "selected app did not launch")
@@ -649,6 +652,19 @@ def run_launcher():
client.key_down_up(57)
client.key(125, False)
client.wait_for("menu_opened", timeout_ms=5000)
+ client.key_down_up(1)
+ client.wait_for("menu_closed", timeout_ms=5000)
+ (tmp / "launched-3").unlink(missing_ok=True)
+ client.open_start_menu()
+ client.wait_for("menu_opened", timeout_ms=5000)
+ client.type_text("Launcher")
+ client.key_down_up(28)
+ client.wait_for("menu_closed", timeout_ms=5000)
+ wait_for(lambda: (tmp / "launched-3").exists(), "Start menu did not rank saved pin first")
+ client.key(125, True)
+ client.key_down_up(57)
+ client.key(125, False)
+ client.wait_for("menu_opened", timeout_ms=5000)
client.type_text("Launcher App 3")
pin = next(w for w in client.get_widget_tree("start_menu")["widgets"] if w.get("label") == "Unpin application")
box = pin["global_box"]