1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
//! The finder column: where query panes go with `SearchPlace column`, the
//! alternative to attaching them under the pane that asked (lifted from
//! the column prototype, cut to its placement). A column with a role at
//! the right edge: the first search opens it and the other columns give it
//! its share once, each in proportion to its weight; after that no pane
//! outside it moves for a search. Its width is a share of the window, not a
//! column weight (layout.compute), so it opens and closes without rounding
//! anyone's width off. Its panes are a recency stack: the newest query pane
//! on top and open, the older ones folded to their tags. A pane opened
//! from a row goes to the column of the pane the query searches, never in
//! here. Its last pane gone, the column goes and every other is as wide as
//! before it came. The role is the column's serial.
const Finder = @This();
const std = @import("std");
const pardes = @import("pardes.zig");
const layout = @import("layout.zig");
const config = @import("config.zig");
const Pardes = pardes.Pardes;
/// The finder column's serial (layout.columnSerial); 0 while there is none.
serial: u32 = 0,
/// Its width, in thousandths of the window's: kept while it is closed, so
/// it comes back as wide as it went.
share: u16 = 300,
/// More query panes than this and the oldest closes: each folded one keeps
/// a tag row. ponytail: a fixed count; size it from the column's rows.
pub const max_results = 8;
const tag_text = "Finder Delcol";
/// The finder column's index, while it is there.
pub fn column(f: *const Finder, p: *const Pardes) ?usize {
if (f.serial == 0) return null;
return layout.columnBySerial(p, f.serial);
}
/// Its width in cells: its share, leaving every other column MINW; the
/// whole window when it is the only column.
pub fn width(f: *const Finder, p: *const Pardes) u16 {
if (p.ncol <= 1) return p.screen_w;
const others: u32 = @as(u32, config.MINW) * @as(u32, @intCast(p.ncol - 1));
const most: u32 = @as(u32, p.screen_w) -| others;
const wanted: u32 = @as(u32, p.screen_w) * f.share / 1000;
return @intCast(@min(@max(wanted, config.MINW), @max(most, @min(config.MINW, p.screen_w))));
}
/// Query pane `id` goes on top of the finder column, opened at the right
/// edge if there is none; the older ones fold. False when the window has
/// no room for the column (the caller attaches it instead).
pub fn place(f: *Finder, p: *Pardes, id: usize) bool {
const pane = p.panes[id] orelse return false;
const c = f.column(p) orelse f.open(p) orelse return false;
layout.insert(p, c, 0, id);
pane.vweight = 1;
pane.collapsed = false;
fold(p, c, id);
// The oldest goes once there are too many: the bottom one, as the top
// is always the newest.
while (p.col_n[c] > max_results) {
const oldest = p.col_panes[c][p.col_n[c] - 1];
p.removePane(oldest, null) catch break;
}
return true;
}
/// A finder column at the right edge, empty, its tag its own: the other
/// columns give it its share, each in proportion to its weight. Null when
/// the window has no room for one more column, or narrower columns would
/// leave a pane under its tag and 2 rows.
fn open(f: *Finder, p: *Pardes) ?usize {
if (p.ncol >= pardes.MAX_COLS or p.ncol == 0) return null;
if (p.screen_w < @as(u32, config.MINW) * @as(u32, @intCast(p.ncol + 1))) return null;
const before_min = layout.minima(p);
const before = p.rects;
const c = p.ncol;
p.col_n[c] = 0;
// Not used for its width (layout.compute), only kept positive for the
// code that halves and joins weights.
p.col_weight[c] = layout.column_weight_unit;
p.col_tags[c] = .{ .what = .column, .gpa = p.gpa };
p.col_tags[c].own = p.gpa.dupe(u8, tag_text) catch null;
p.col_serial[c] = 0;
p.ncol += 1;
f.serial = layout.columnSerial(p, c);
// Where nothing holds every minimum there is no finder, as Newcol is
// refused.
if (!layout.keepColumn(p, c, &before, &before_min)) {
f.serial = 0;
layout.compute(p);
return null;
}
layout.noteColumn(p, "newcol", f.serial);
return c;
}
/// Fold every pane of finder column `c` but `top` (and what is attached
/// to it, its +Preview) to its tag.
fn fold(p: *Pardes, c: usize, top: usize) void {
layout.compute(p);
for (p.col_panes[c][0..p.col_n[c]]) |k| {
const pane = p.panes[k] orelse continue;
if (k == top or layout.groupRoot(p, k) == top) {
pane.collapsed = false;
pane.vweight = 1;
} else if (!pane.collapsed) {
// As Collapse folds it: its weight keeps its height for later.
pane.vweight = @floatFromInt(@max(1, p.rects[k].h));
pane.collapsed = true;
}
}
layout.compute(p);
}
/// Query pane `id`, asked again: back on top of the stack, open, the rest
/// folded. Nothing outside the finder column moves.
pub fn raise(f: *Finder, p: *Pardes, id: usize) void {
const c = f.column(p) orelse return;
const at = layout.findPane(p, id) orelse return;
if (at.col != c) return;
if (at.idx != 0) {
// the pane and what is attached to it (its +Preview) move together
const end = layout.groupEnd(p, c, at.idx);
var moved: [pardes.MAX_PANES]usize = undefined;
const n = end - at.idx;
@memcpy(moved[0..n], p.col_panes[c][at.idx..end]);
for (moved[0..n]) |k| layout.removePane(p, k);
for (moved[0..n], 0..) |k, i| layout.insert(p, c, i, k);
}
fold(p, c, id);
}
/// The column a pane asked for from the finder goes in (a row looked at, a
/// command run there): the column of the pane its query searches, else
/// the nearest other. Null when nothing points at the finder.
pub fn away(f: *Finder, p: *Pardes, from: usize, kind: pardes.exec.NewPane) ?usize {
const fc = f.column(p) orelse return null;
const in_finder = if (layout.findPane(p, from)) |at| at.col == fc else false;
const to_last = (kind == .command or kind == .errors) and fc == p.ncol - 1;
if (!in_finder and p.exec_column != fc and !to_last) return null;
if (in_finder) {
const root = layout.groupRoot(p, from);
if (p.panes[root]) |qp| if (qp.query) |q| if (p.paneBySerial(q.owner)) |oid| if (layout.findPane(p, oid)) |at|
if (at.col != fc) return at.col;
}
if (fc > 0) return fc - 1;
if (fc + 1 < p.ncol) return fc + 1;
// The finder is the only column: a document asked from it gets one of
// its own, left of it.
return layout.insertColumn(p, fc, true);
}
/// The finder column has lost its last pane: it goes, and every other
/// column is as wide as before it came.
pub fn emptied(f: *Finder, p: *Pardes, c: usize) bool {
if (f.column(p) != c or p.col_n[c] != 0) return false;
layout.dropColumn(p, c);
f.serial = 0;
return true;
}
/// A drag of the border between the finder column and column `other`
/// beside it: the finder's share follows the edge, and `other` takes the
/// difference, every other column keeping its width.
pub fn dragWidth(f: *Finder, p: *Pardes, other: usize, finder_w: u64) void {
const fc = f.column(p) orelse return;
if (p.screen_w == 0) return;
const combined: u64 = p.col_w[fc] + p.col_w[other];
if (combined < 2 * @as(u64, config.MINW)) return;
const fw = std.math.clamp(finder_w, config.MINW, combined - config.MINW);
f.share = @intCast(@max(1, @min(999, fw * 1000 / p.screen_w)));
var rest_weight: u128 = 0;
var rest_cells: u64 = 0;
for (0..p.ncol) |k| if (k != fc and k != other) {
rest_weight += p.col_weight[k];
rest_cells += p.col_w[k];
};
if (rest_cells > 0) {
const other_cells = combined - fw;
const w = @as(u128, other_cells) * rest_weight / rest_cells;
p.col_weight[other] = @intCast(std.math.clamp(w, 1, layout.max_column_weight));
}
layout.compute(p);
}
fn typeKeys(p: *Pardes, text: []const u8) void {
for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } });
}
test "SearchPlace column: query panes stack in a finder column at the right edge, nothing else moves after it opens, and it goes with its last pane" {
const Key = pardes.Key;
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 });
defer p.deinit();
p.settings.search_place = .column;
const pane = try p.setTestFile("alpha one\nbeta\nalphabet\n");
p.newScratchBelow(0);
const other = p.active;
p.active = 0;
layout.compute(p);
const before = p.rects;
const width_before = p.col_w[0];
typeKeys(p, "/");
const fc = p.finder.column(p).?;
try std.testing.expectEqual(@as(usize, 1), fc);
const first = p.col_panes[fc][0];
try std.testing.expect(p.panes[first].?.query != null);
// the first search narrows the column once; its panes keep their rows
try std.testing.expect(p.col_w[0] < width_before);
try std.testing.expectEqual(before[0].h, p.rects[0].h);
try std.testing.expectEqual(before[other].h, p.rects[other].h);
typeKeys(p, "alpha");
p.update(.{ .key = .{ .cp = Key.enter } });
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expect(pane.places.started);
layout.compute(p);
const opened = p.rects;
// a second search, from the other pane: on top, the first folded
p.active = other;
typeKeys(p, "/");
const second = p.col_panes[fc][0];
try std.testing.expect(second != first);
try std.testing.expect(p.panes[first].?.collapsed);
layout.compute(p);
try std.testing.expectEqualDeep(opened[0], p.rects[0]);
try std.testing.expectEqualDeep(opened[other], p.rects[other]);
// Esc: the second closes, the first stays
p.update(.{ .key = .{ .cp = Key.escape } });
try std.testing.expect(p.panes[second] == null);
try std.testing.expectEqual(fc, p.finder.column(p).?);
// the first's Del: the column goes, every other as wide as before it
pardes.exec.runBuiltin(p, .Del, first, "", null);
try std.testing.expect(p.finder.column(p) == null);
layout.compute(p);
try std.testing.expectEqual(width_before, p.col_w[0]);
try std.testing.expectEqualDeep(before[0], p.rects[0]);
}
|