-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathsurface_mouse.zig
More file actions
336 lines (293 loc) · 9.81 KB
/
surface_mouse.zig
File metadata and controls
336 lines (293 loc) · 9.81 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/// SurfaceMouse represents mouse helper functionality for the core surface.
///
/// It's currently small in scope; its purpose is to isolate mouse logic that
/// has gotten a bit complex (e.g. pointer shape handling for key events), but
/// the intention is to grow it later so that we can better test said logic).
const SurfaceMouse = @This();
const std = @import("std");
const builtin = @import("builtin");
const input = @import("input.zig");
const terminal = @import("terminal/main.zig");
const MouseShape = terminal.MouseShape;
/// For processing key events; the key that was physically pressed on the
/// keyboard.
physical_key: input.Key,
/// The mouse event tracking mode, if any.
mouse_event: terminal.MouseEvent,
/// The current terminal's mouse shape.
mouse_shape: MouseShape,
/// The last mods state when the last mouse button (whatever it was) was
/// pressed or release.
mods: input.Mods,
/// True if the mouse position is currently over a link.
over_link: bool,
/// True if the mouse pointer is currently hidden.
hidden: bool,
/// Translates key state to mouse shape (cursor) state, based on a state
/// machine.
///
/// There are 4 current states:
///
/// * text: starting state, displays a text bar.
/// * default: default state when in a mouse tracking mode. (e.g. vim, etc).
/// Displays an arrow pointer.
/// * pointer: default state when over a link. Displays a pointing finger.
/// * crosshair: any above state can transition to this when the rectangle
/// select keys are pressed (ctrl/super+alt).
///
/// Additionally, default can transition back to text if one of the shift keys
/// are pressed during mouse tracking mode.
///
/// Any secondary state transitions back to its default state when the
/// appropriate keys are released.
///
/// null is returned when the mouse shape does not need changing.
pub fn keyToMouseShape(self: SurfaceMouse) ?MouseShape {
// Filter for appropriate key events
if (!eligibleMouseShapeKeyEvent(self.physical_key)) return null;
// Exceptions: link hover or hidden state overrides any other shape
// processing and does not change state.
//
// TODO: As we unravel mouse state, we can fix this to be more explicit.
if (self.over_link or self.hidden) {
return null;
}
// Set our current default state
var current_shape_state: MouseShape = undefined;
if (self.mouse_event != .none) {
// In mouse tracking mode, should be default (arrow pointer)
current_shape_state = .default;
} else {
// Default terminal mode, should be text (text bar)
current_shape_state = .text;
}
// Transition table.
//
// TODO: This could be updated eventually to be a true transition table if
// we move to a full stateful mouse surface, e.g. very specific inputs
// transitioning state based on previous state, versus flags like "is the
// mouse over a link", etc.
switch (current_shape_state) {
.default => {
if (isMouseModeOverrideState(self.mods) and isRectangleSelectState(self.mods)) {
// Crosshair (rectangle select), only set if we are also
// overriding (e.g. shift+ctrl+alt)
return .crosshair;
} else if (isMouseModeOverrideState(self.mods)) {
// Normal override state
return .text;
} else {
return .default;
}
},
.text => {
if (isRectangleSelectState(self.mods)) {
// Crosshair (rectangle select)
return .crosshair;
} else {
return .text;
}
},
// Fall back on default state
else => unreachable,
}
}
fn eligibleMouseShapeKeyEvent(physical_key: input.Key) bool {
return physical_key.ctrlOrSuper() or
physical_key.leftOrRightShift() or
physical_key.leftOrRightAlt();
}
fn isMouseModeOverrideState(mods: input.Mods) bool {
return mods.shift;
}
/// Returns true if our modifiers put us in a state where dragging
/// should cause a rectangle select.
pub fn isRectangleSelectState(mods: input.Mods) bool {
return if (comptime builtin.target.os.tag.isDarwin())
mods.alt
else
mods.ctrlOrSuper() and mods.alt;
}
test "keyToMouseShape" {
const testing = std.testing;
{
// No specific key pressed
const m: SurfaceMouse = .{
.physical_key = .unidentified,
.mouse_event = .none,
.mouse_shape = .progress,
.mods = .{},
.over_link = false,
.hidden = false,
};
const got = m.keyToMouseShape();
try testing.expect(got == null);
}
{
// Over a link. NOTE: This tests that we don't touch the inbound state,
// not necessarily if we're over a link.
const m: SurfaceMouse = .{
.physical_key = .shift_left,
.mouse_event = .none,
.mouse_shape = .progress,
.mods = .{},
.over_link = true,
.hidden = false,
};
const got = m.keyToMouseShape();
try testing.expect(got == null);
}
{
// Mouse is currently hidden
const m: SurfaceMouse = .{
.physical_key = .shift_left,
.mouse_event = .none,
.mouse_shape = .progress,
.mods = .{},
.over_link = true,
.hidden = true,
};
const got = m.keyToMouseShape();
try testing.expect(got == null);
}
{
// default, no mods (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .shift_left,
.mouse_event = .x10,
.mouse_shape = .default,
.mods = .{},
.over_link = false,
.hidden = false,
};
const want: MouseShape = .default;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// default -> crosshair (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .alt_left,
.mouse_event = .x10,
.mouse_shape = .default,
.mods = .{ .ctrl = true, .super = true, .alt = true, .shift = true },
.over_link = false,
.hidden = false,
};
const want: MouseShape = .crosshair;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// default -> text (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .shift_left,
.mouse_event = .x10,
.mouse_shape = .default,
.mods = .{ .shift = true },
.over_link = false,
.hidden = false,
};
const want: MouseShape = .text;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// crosshair -> text (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .alt_left,
.mouse_event = .x10,
.mouse_shape = .crosshair,
.mods = .{ .shift = true },
.over_link = false,
.hidden = false,
};
const want: MouseShape = .text;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// crosshair -> default (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .alt_left,
.mouse_event = .x10,
.mouse_shape = .crosshair,
.mods = .{},
.over_link = false,
.hidden = false,
};
const want: MouseShape = .default;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// text -> crosshair (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .alt_left,
.mouse_event = .x10,
.mouse_shape = .text,
.mods = .{ .ctrl = true, .super = true, .alt = true, .shift = true },
.over_link = false,
.hidden = false,
};
const want: MouseShape = .crosshair;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// text -> default (mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .shift_left,
.mouse_event = .x10,
.mouse_shape = .text,
.mods = .{},
.over_link = false,
.hidden = false,
};
const want: MouseShape = .default;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// text, no mods (no mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .shift_left,
.mouse_event = .none,
.mouse_shape = .text,
.mods = .{},
.over_link = false,
.hidden = false,
};
const want: MouseShape = .text;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// text -> crosshair (no mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .alt_left,
.mouse_event = .none,
.mouse_shape = .text,
.mods = .{ .ctrl = true, .super = true, .alt = true },
.over_link = false,
.hidden = false,
};
const want: MouseShape = .crosshair;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
{
// crosshair -> text (no mouse tracking)
const m: SurfaceMouse = .{
.physical_key = .alt_left,
.mouse_event = .none,
.mouse_shape = .crosshair,
.mods = .{},
.over_link = false,
.hidden = false,
};
const want: MouseShape = .text;
const got = m.keyToMouseShape();
try testing.expect(want == got);
}
}