-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathReorderWidget.zig
402 lines (332 loc) · 13.5 KB
/
ReorderWidget.zig
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const std = @import("std");
const dvui = @import("../dvui.zig");
const Options = dvui.Options;
const Rect = dvui.Rect;
const RectScale = dvui.RectScale;
const Size = dvui.Size;
const Widget = dvui.Widget;
const WidgetData = dvui.WidgetData;
const ReorderWidget = @This();
wd: WidgetData = undefined,
id_reorderable: ?usize = null, // matches Reorderable.reorder_id
drag_point: ?dvui.Point = null,
drag_ending: bool = false,
reorderable_size: Size = .{},
found_slot: bool = false,
pub fn init(src: std.builtin.SourceLocation, opts: Options) ReorderWidget {
var self = ReorderWidget{};
const defaults = Options{ .name = "Reorder" };
self.wd = WidgetData.init(src, .{}, defaults.override(opts));
self.id_reorderable = dvui.dataGet(null, self.wd.id, "_id_reorderable", usize) orelse null;
self.drag_point = dvui.dataGet(null, self.wd.id, "_drag_point", dvui.Point) orelse null;
self.reorderable_size = dvui.dataGet(null, self.wd.id, "_reorderable_size", dvui.Size) orelse dvui.Size{};
return self;
}
pub fn install(self: *ReorderWidget) !void {
try self.wd.register();
try self.wd.borderAndBackground(.{});
dvui.parentSet(self.widget());
}
pub fn needFinalSlot(self: *ReorderWidget) bool {
return self.drag_point != null and !self.found_slot;
}
pub fn finalSlot(self: *ReorderWidget) !bool {
if (self.needFinalSlot()) {
var r = try self.reorderable(@src(), .{ .last_slot = true }, .{});
defer r.deinit();
if (r.insertBefore()) {
return true;
}
}
return false;
}
pub fn widget(self: *ReorderWidget) Widget {
return Widget.init(self, data, rectFor, screenRectScale, minSizeForChild, processEvent);
}
pub fn data(self: *ReorderWidget) *WidgetData {
return &self.wd;
}
pub fn rectFor(self: *ReorderWidget, id: u32, min_size: Size, e: Options.Expand, g: Options.Gravity) Rect {
_ = id;
return dvui.placeIn(self.wd.contentRect().justSize(), min_size, e, g);
}
pub fn screenRectScale(self: *ReorderWidget, rect: Rect) RectScale {
return self.wd.contentRectScale().rectToRectScale(rect);
}
pub fn minSizeForChild(self: *ReorderWidget, s: Size) void {
self.wd.minSizeMax(self.wd.options.padSize(s));
}
pub fn matchEvent(self: *ReorderWidget, e: *dvui.Event) bool {
return dvui.eventMatchSimple(e, self.data());
}
pub fn processEvents(self: *ReorderWidget) void {
const evts = dvui.events();
for (evts) |*e| {
if (!self.matchEvent(e))
continue;
self.processEvent(e, false);
}
}
pub fn processEvent(self: *ReorderWidget, e: *dvui.Event, bubbling: bool) void {
_ = bubbling;
if (dvui.captured(self.wd.id)) {
switch (e.evt) {
.mouse => |me| {
if ((me.action == .press or me.action == .release) and me.button.pointer()) {
self.drag_ending = true;
dvui.captureMouse(null);
dvui.dragEnd();
dvui.refresh(null, @src(), self.wd.id);
} else if (me.action == .motion) {
self.drag_point = me.p;
var scrolldrag = dvui.Event{ .evt = .{ .scroll_drag = .{
.mouse_pt = me.p,
.screen_rect = self.wd.rectScale().r,
.capture_id = self.wd.id,
} } };
self.wd.parent.processEvent(&scrolldrag, true);
}
},
else => {},
}
}
if (e.bubbleable()) {
self.wd.parent.processEvent(e, true);
}
}
pub fn deinit(self: *ReorderWidget) void {
if (self.drag_ending) {
self.id_reorderable = null;
self.drag_point = null;
}
if (self.id_reorderable) |idr| {
dvui.dataSet(null, self.wd.id, "_id_reorderable", idr);
} else {
dvui.dataRemove(null, self.wd.id, "_id_reorderable");
}
if (self.drag_point) |dp| {
dvui.dataSet(null, self.wd.id, "_drag_point", dp);
} else {
dvui.dataRemove(null, self.wd.id, "_drag_point");
}
dvui.dataSet(null, self.wd.id, "_reorderable_size", self.reorderable_size);
self.wd.minSizeSetAndRefresh();
self.wd.minSizeReportToParent();
dvui.parentReset(self.wd.id, self.wd.parent);
}
pub fn dragStart(self: *ReorderWidget, reorder_id: usize, p: dvui.Point) void {
self.id_reorderable = reorder_id;
self.drag_point = p;
self.found_slot = true;
dvui.captureMouse(self.wd.id);
}
pub const draggableInitOptions = struct {
tvg_bytes: ?[]const u8 = null,
top_left: ?dvui.Point = null,
reorderable: ?*Reorderable = null,
};
pub fn draggable(src: std.builtin.SourceLocation, init_opts: draggableInitOptions, opts: dvui.Options) !?dvui.Point {
var iw = try dvui.IconWidget.init(src, "reorder_drag_icon", init_opts.tvg_bytes orelse dvui.entypo.menu, opts);
try iw.install();
var ret: ?dvui.Point = null;
loop: for (dvui.events()) |*e| {
if (!iw.matchEvent(e))
continue;
switch (e.evt) {
.mouse => |me| {
if (me.action == .press and me.button.pointer()) {
e.handled = true;
dvui.captureMouse(iw.wd.id);
const reo_top_left: ?dvui.Point = if (init_opts.reorderable) |reo| reo.wd.rectScale().r.topLeft() else null;
const top_left: ?dvui.Point = init_opts.top_left orelse reo_top_left;
dvui.dragPreStart(me.p, .{ .offset = (top_left orelse iw.wd.rectScale().r.topLeft()).diff(me.p) });
} else if (me.action == .motion) {
if (dvui.captured(iw.wd.id)) {
e.handled = true;
if (dvui.dragging(me.p)) |_| {
ret = me.p;
if (init_opts.reorderable) |reo| {
reo.reorder.dragStart(reo.wd.id, me.p); // reorder grabs capture
}
break :loop;
}
}
}
},
else => {},
}
}
try iw.draw();
iw.deinit();
return ret;
}
pub fn reorderable(self: *ReorderWidget, src: std.builtin.SourceLocation, init_opts: Reorderable.InitOptions, opts: Options) !*Reorderable {
const ret = try dvui.currentWindow().arena().create(Reorderable);
ret.* = Reorderable.init(src, self, init_opts, opts);
try ret.install();
return ret;
}
pub const Reorderable = struct {
pub const InitOptions = struct {
// set to true for a reorderable that represents a final empty slot in
// the list shown during dragging
last_slot: bool = false,
// if null, uses widget id
// if non-null, must be unique among reorderables in a single reorder
reorder_id: ?usize = null,
// if false, caller responsible for drawing something when targetRectScale() returns true
draw_target: bool = true,
// if false, caller responsible for calling reinstall() when targetRectScale() returns true
reinstall: bool = true,
};
wd: WidgetData = undefined,
reorder: *ReorderWidget = undefined,
init_options: InitOptions = undefined,
options: Options = undefined,
installed: bool = false,
floating_widget: ?dvui.FloatingWidget = null,
target_rs: ?dvui.RectScale = null,
pub fn init(src: std.builtin.SourceLocation, reorder: *ReorderWidget, init_opts: InitOptions, opts: Options) Reorderable {
var self = Reorderable{};
self.reorder = reorder;
const defaults = Options{ .name = "Reorderable" };
self.init_options = init_opts;
self.options = defaults.override(opts);
self.wd = WidgetData.init(src, .{}, self.options.override(.{ .rect = .{} }));
return self;
}
// can call this after init before install
pub fn floating(self: *Reorderable) bool {
// if drag_point is non-null, id_reorderable is non-null
if (self.reorder.drag_point != null and self.reorder.id_reorderable.? == (self.init_options.reorder_id orelse self.wd.id)) {
return true;
}
return false;
}
pub fn install(self: *Reorderable) !void {
self.installed = true;
if (self.reorder.drag_point) |dp| {
const topleft = dp.plus(dvui.dragOffset());
if (self.reorder.id_reorderable.? == (self.init_options.reorder_id orelse self.wd.id)) {
// we are being dragged - put in floating widget
try self.wd.register();
dvui.parentSet(self.widget());
self.floating_widget = dvui.FloatingWidget.init(@src(), .{ .rect = Rect.fromPoint(topleft.scale(1 / dvui.windowNaturalScale())), .min_size_content = self.reorder.reorderable_size });
try self.floating_widget.?.install();
} else {
if (self.init_options.last_slot) {
self.wd = WidgetData.init(self.wd.src, .{}, self.options.override(.{ .min_size_content = self.reorder.reorderable_size }));
} else {
self.wd = WidgetData.init(self.wd.src, .{}, self.options);
}
const rs = self.wd.rectScale();
const dragRect = Rect.fromPoint(topleft).toSize(self.reorder.reorderable_size.scale(rs.s));
if (!self.reorder.found_slot and !rs.r.intersect(dragRect).empty()) {
// user is dragging a reorderable over this rect
self.target_rs = rs;
self.reorder.found_slot = true;
if (self.init_options.draw_target) {
try dvui.pathAddRect(rs.r, .{});
try dvui.pathFillConvex(dvui.themeGet().color_accent);
}
if (self.init_options.reinstall and !self.init_options.last_slot) {
try self.reinstall();
}
}
if (self.target_rs == null or self.init_options.last_slot) {
try self.wd.register();
dvui.parentSet(self.widget());
}
}
} else {
self.wd = WidgetData.init(self.wd.src, .{}, self.options);
self.reorder.reorderable_size = self.wd.rect.size();
try self.wd.register();
dvui.parentSet(self.widget());
}
}
pub fn targetRectScale(self: *Reorderable) ?dvui.RectScale {
return self.target_rs;
}
pub fn removed(self: *Reorderable) bool {
// if drag_ending is true, id_reorderable is non-null
if (self.reorder.drag_ending and self.reorder.id_reorderable.? == (self.init_options.reorder_id orelse self.wd.id)) {
return true;
}
return false;
}
// must be called after install()
pub fn insertBefore(self: *Reorderable) bool {
if (!self.installed) {
dvui.log.err("Reorderable.insertBefore() must be called after install()", .{});
std.debug.assert(false);
}
if (self.reorder.drag_ending and self.target_rs != null) {
return true;
}
return false;
}
pub fn reinstall(self: *Reorderable) !void {
// send our target rect to the parent for sizing
self.wd.minSizeMax(self.wd.rect.size());
self.wd.minSizeReportToParent();
// reinstall ourselves getting the next rect from parent
self.wd = WidgetData.init(self.wd.src, .{}, self.options);
try self.wd.register();
dvui.parentSet(self.widget());
}
pub fn widget(self: *Reorderable) Widget {
return Widget.init(self, Reorderable.data, Reorderable.rectFor, Reorderable.screenRectScale, Reorderable.minSizeForChild, Reorderable.processEvent);
}
pub fn data(self: *Reorderable) *WidgetData {
return &self.wd;
}
pub fn rectFor(self: *Reorderable, id: u32, min_size: Size, e: Options.Expand, g: Options.Gravity) Rect {
_ = id;
return dvui.placeIn(self.wd.contentRect().justSize(), min_size, e, g);
}
pub fn screenRectScale(self: *Reorderable, rect: Rect) RectScale {
return self.wd.contentRectScale().rectToRectScale(rect);
}
pub fn minSizeForChild(self: *Reorderable, s: Size) void {
self.wd.minSizeMax(self.wd.options.padSize(s));
}
pub fn processEvent(self: *Reorderable, e: *dvui.Event, bubbling: bool) void {
_ = bubbling;
if (e.bubbleable()) {
self.wd.parent.processEvent(e, true);
}
}
pub fn deinit(self: *Reorderable) void {
if (self.floating_widget) |*fw| {
self.wd.minSizeMax(fw.wd.min_size);
fw.deinit();
}
self.wd.minSizeSetAndRefresh();
self.wd.minSizeReportToParent();
dvui.parentReset(self.wd.id, self.wd.parent);
}
};
pub fn reorderSlice(comptime T: type, slice: []T, removed_idx: ?usize, insert_before_idx: ?usize) bool {
if (removed_idx) |ri| {
if (insert_before_idx) |ibi| {
// save this index
const removed = slice[ri];
if (ri < ibi) {
// moving down, shift others up
for (ri..ibi - 1) |i| {
slice[i] = slice[i + 1];
}
slice[ibi - 1] = removed;
} else {
// moving up, shift others down
for (ibi..ri, 0..) |_, i| {
slice[ri - i] = slice[ri - i - 1];
}
slice[ibi] = removed;
}
return true;
}
}
return false;
}