-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtreelist.lua
609 lines (545 loc) · 17.2 KB
/
treelist.lua
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
--
-- TreeList Widget heavily based on TreeView plugin.
-- @copyright Jefferson Gonzalez
-- @license MIT
--
local core = require "core"
local common = require "core.common"
local style = require "core.style"
local Widget = require "widget"
---@class widget.treelist.item
---@field name string
---@field label string
---@field data any?
---@field expanded boolean?
---@field visible boolean?
---@field tooltip string?
---@field depth integer?
---@field childs widget.treelist.item[]?
---@field parent widget.treelist.item[]?
---@field icon string?
---@class widget.treelist : widget
---@field items widget.treelist.item[]
---@field selected_item widget.treelist.item?
---@field hovered_item widget.treelist.item?
---@field icon_font renderer.font?
---@field private count_lines integer
---@field private scroll_width integer
---@overload fun(parent:widget?):widget.treelist
local TreeList = Widget:extend()
---Constructor
---@param parent? widget
function TreeList:new(parent)
TreeList.super.new(self, parent)
self.type_name = "widget.treelist"
self.scrollable = true
self.init_size = true
self.count_lines = 0
self.scroll_width = 0
self.items = {}
self.tooltip = { x = 0, y = 0, begin = 0, alpha = 0 }
self.last_scroll_y = 0
self.item_icon_width = 0
self.item_text_spacing = 0
end
---Get the height of a single item.
---@return number h
function TreeList:get_item_height()
return style.font:get_height() + style.padding.y
end
---Add new item to to tree list
---@param item widget.treelist.item
function TreeList:add_item(item)
table.insert(self.items, item)
end
---Remove all items from the tree
function TreeList:clear()
self.items = {}
self.selected_item = nil
self.hovered_item = nil
end
---Retrieve the amount of visible items and also yield them.
---@param item widget.treelist.item
---@param x number
---@param y number
---@param w number
---@param h number
---@param depth? number
---@return integer items_count
function TreeList:get_items(item, x, y, w, h, depth)
if not depth then depth = 0 else depth = depth + 1 end
item.depth = depth
coroutine.yield(item, x, y, w, h)
local count_lines = 1
if item and item.childs and item.expanded then
for _, child in ipairs(item.childs) do
if child.visible ~= false then
count_lines = count_lines + self:get_items(
child, x, y + count_lines * h, w, h, depth
)
end
end
end
return count_lines
end
---Allows iterating the currently visible items only.
---@return fun():widget.treelist.item,number,number,number,number
function TreeList:each_item()
return coroutine.wrap(function()
local count_lines = 0
local ox, oy = self:get_content_offset()
local h = self:get_item_height()
if #self.items > 0 then
for i=1, #self.items do
count_lines = count_lines + self:get_items(
self.items[i],
ox, oy + style.padding.y + h * count_lines,
self.size.x, h
)
end
end
self.count_lines = count_lines
end)
end
---Retrieve an item by name using the query format:
---"parent_name>child_name_2>child_name_2>etc..."
---@param query string
---@param items? widget.treelist.item[]
---@param separator? string Use a different separator (default: >)
---@return widget.treelist.item?
function TreeList:query_item(query, items, separator)
local parent = items or self.items
local item = nil
separator = separator or ">"
for name in query:gmatch("([^"..separator.."]+)") do
if parent then
local found = false
for _, child in ipairs(parent) do
if name == child.name then
item = child
parent = child.childs
found = true
break
end
end
if not found then return nil end
else
return nil
end
end
return item
end
---Set the active item.
---@param selection widget.treelist.item
---@param selection_y? number
---@param center? boolean
---@param instant? boolean
function TreeList:set_selection(selection, selection_y, center, instant)
self.selected_item = selection
if selection and selection_y
and (selection_y <= 0 or selection_y >= self.size.y) then
local lh = self:get_item_height()
if not center and selection_y >= self.size.y - lh then
selection_y = selection_y - self.size.y + lh
end
if center then
selection_y = selection_y - (self.size.y - lh) / 2
end
local _, y = self:get_content_offset()
self.scroll.to.y = selection_y - y
self.scroll.to.y = common.clamp(
self.scroll.to.y, 0, self:get_scrollable_size() - self.size.y
)
if instant then
self.scroll.y = self.scroll.to.y
end
end
end
---Sets the selection to the file with the specified path.
---TODO: Not tested, idea is to allow query like selections using item names
---by introducing a get_item/set_item methods that accepts a string query,
---For the moment we leave this inherited TreeView function here.
---@param path string #Absolute path of item to select
---@param expand boolean #Expand dirs leading to the item
---@param scroll_to boolean #Scroll to make the item visible
---@param instant boolean #Don't animate the scroll
---@return table? #The selected item
function TreeList:set_selection_from_path(path, expand, scroll_to, instant)
local separator = "||"
local to_select, to_select_y
local let_it_finish, done
::restart::
for item, _,y,_,_ in self:each_item() do
if not done then
if item.childs and #item.childs > 0 then
local _, to = string.find(path, item.name..separator, 1, true)
if to and to == #item.name + #separator then
to_select, to_select_y = item, y
if expand and not item.expanded then
self:toggle_expand(true, item)
-- Because we altered the size of the TreeList
-- and because TreeList:get_scrollable_size uses self.count_lines
-- which gets updated only when TreeList:each_item finishes,
-- we can't stop here or we risk that the scroll
-- gets clamped by View:clamp_scroll_position.
let_it_finish = true
-- We need to restart the process because if TreeList:toggle_expand
-- altered the cache, TreeList:each_item risks looping indefinitely.
goto restart
end
end
else
if item.name == path then
to_select, to_select_y = item, y
done = true
if not let_it_finish then break end
end
end
end
end
if to_select then
self:set_selection(to_select, scroll_to and to_select_y, true, instant)
end
return to_select
end
---Set the icon font used to render the items icon.
---@param font renderer.font
function TreeList:set_icon_font(font)
if font:get_size() ~= style.icon_font:get_size() then
font:set_size(style.icon_font:get_size())
end
self.icon_font = font
end
---Keep the icon font size updated to match current scale.
function TreeList:on_scale_change()
if self.icon_font then
self.icon_font:set_size(style.icon_font:get_size())
end
end
function TreeList:on_mouse_moved(px, py, ...)
if TreeList.super.on_mouse_moved(self, px, py, ...) then
self.hovered_item = nil
else
return false
end
local position = self:get_position()
local item_changed, tooltip_changed
for item, x,y,w,h in self:each_item() do
if px > x and py > y and px <= (self.size.x + position.x) and py <= y + h then
item_changed = true
self.hovered_item = item
x = math.max(x, self.position.x)
if px > x and py > y and px <= x + w and py <= y + h then
tooltip_changed = true
self.tooltip.x, self.tooltip.y = px, py
self.tooltip.begin = system.get_time()
end
break
end
end
if not item_changed then self.hovered_item = nil end
if not tooltip_changed then self.tooltip.x, self.tooltip.y = nil, nil end
return true
end
---Override to listen for item click events.
---@param item widget.treelist.item
---@param button string
---@param clicks integer
function TreeList:on_item_click(item, button, x, y, clicks) end
function TreeList:on_mouse_pressed(button, x, y, clicks)
if TreeList.super.on_mouse_pressed(self, button, x, y, clicks) then
if self:scrollbar_hovering() then return true end
self:set_selection(self.hovered_item)
if self.hovered_item then
local emit_click = false
if clicks > 1 then
self:toggle_expand()
else
if self.hovered_item.childs then
local x1, x2 = self:get_chevron_position(self.hovered_item)
if x >= x1 and x <= x2 then
self:toggle_expand()
else
emit_click = true
end
else
emit_click = true
end
end
if emit_click then
self:on_item_click(self.hovered_item, button, x, y, clicks)
end
end
return true
end
return false
end
function TreeList:on_mouse_left()
TreeList.super.on_mouse_left(self)
self.hovered_item = nil
end
function TreeList:update()
if not self:is_visible() then return end
local duration = system.get_time() - self.tooltip.begin
local tooltip_delay = 0.5
if self.hovered_item and self.tooltip.x and duration > tooltip_delay then
self:move_towards(self.tooltip, "alpha", 255, 1, "treeview")
else
self.tooltip.alpha = 0
end
self.item_icon_width = style.icon_font:get_width("D")
self.item_text_spacing = style.icon_font:get_width("f") / 2
-- this will make sure hovered_item is updated
local dy = math.abs(self.last_scroll_y - self.scroll.y)
if dy > 0 then
self:on_mouse_moved(core.root_view.mouse.x, core.root_view.mouse.y, 0, 0)
self.last_scroll_y = self.scroll.y
end
TreeList.super.update(self)
end
function TreeList:get_scrollable_size()
return self.count_lines and self:get_item_height() * (self.count_lines + 1) or math.huge
end
function TreeList:get_h_scrollable_size()
local _, _, v_scroll_w = self.v_scrollbar:get_thumb_rect()
return self.scroll_width + (
self.size.x > self.scroll_width + v_scroll_w and 0 or style.padding.x
)
end
local function replace_alpha(color, alpha)
local r, g, b = table.unpack(color)
return { r, g, b, alpha }
end
function TreeList:draw_tooltip()
if not self.hovered_item or not self.hovered_item.tooltip then
return
end
local text = common.home_encode(self.hovered_item.tooltip)
local w, h = style.font:get_width(text), style.font:get_height()
local tooltip_offset = style.font:get_height()
local x, y = self.tooltip.x + tooltip_offset, self.tooltip.y + tooltip_offset
w, h = w + style.padding.x, h + style.padding.y
if x + w > core.root_view.root_node.size.x then -- check if we can span right
x = x - w -- span left instead
end
local tooltip_border = 1
local bx, by = x - tooltip_border, y - tooltip_border
local bw, bh = w + 2 * tooltip_border, h + 2 * tooltip_border
renderer.draw_rect(
bx, by, bw, bh, replace_alpha(style.text, self.tooltip.alpha)
)
renderer.draw_rect(
x, y, w, h, replace_alpha(style.background2, self.tooltip.alpha)
)
common.draw_text(
style.font,
replace_alpha(style.text, self.tooltip.alpha),
text, "center", x, y, w, h
)
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
function TreeList:get_item_icon(item, active, hovered)
local character = item.icon
local font = self.icon_font or style.icon_font
local color = style.text
if active or hovered then
color = style.accent
end
return character, font, color
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
function TreeList:get_item_text(item, active, hovered)
local text = item.label
local font = style.font
local color = style.text
if active or hovered then
color = style.accent
end
return text, font, color
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
---@param x number
---@param y number
---@param w number
---@param h number
function TreeList:draw_item_text(item, active, hovered, x, y, w, h)
local item_text, item_font, item_color = self:get_item_text(item, active, hovered)
return common.draw_text(item_font, item_color, item_text, "left", x, y, 0, h)
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
---@param x number
---@param y number
---@param w number
---@param h number
function TreeList:draw_item_icon(item, active, hovered, x, y, w, h)
local icon_char, icon_font, icon_color = self:get_item_icon(item, active, hovered)
if not icon_char then return 0 end
common.draw_text(icon_font, icon_color, icon_char, "left", x, y, 0, h)
return self.item_icon_width + self.item_text_spacing
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
---@param x number
---@param y number
---@param w number
---@param h number
function TreeList:draw_item_body(item, active, hovered, x, y, w, h)
x = x + self:draw_item_icon(item, active, hovered, x, y, w, h)
return self:draw_item_text(item, active, hovered, x, y, w, h)
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
---@param x number
---@param y number
---@param w number
---@param h number
function TreeList:draw_item_chevron(item, active, hovered, x, y, w, h)
if item.childs and #item.childs > 0 then
local chevron_icon = item.expanded and "-" or "+"
local chevron_color = hovered and style.accent or style.text
common.draw_text(style.icon_font, chevron_color, chevron_icon, "left", x, y, 0, h)
end
return style.padding.x
end
---Get an item chevron starting and ending positions.
---@return number x1
---@return number x2
function TreeList:get_chevron_position(item)
local ox = self:get_content_offset()
local x1 = ox + item.depth * style.padding.x
local x2 = x1 + style.padding.x * 2
return x1, x2
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
---@param x number
---@param y number
---@param w number
---@param h number
function TreeList:draw_item_background(item, active, hovered, x, y, w, h)
if hovered then
local hover_color = { table.unpack(style.line_highlight) }
hover_color[4] = 160
renderer.draw_rect(self.position.x, y, self.size.x, h, hover_color)
elseif active then
renderer.draw_rect(self.position.x, y, self.size.x, h, style.line_highlight)
end
end
---@param item widget.treelist.item
---@param active boolean
---@param hovered boolean
---@param x number
---@param y number
---@param w number
---@param h number
function TreeList:draw_item(item, active, hovered, x, y, w, h)
self:draw_item_background(item, active, hovered, x, y, w, h)
x = x + item.depth * style.padding.x + style.padding.x
x = x + self:draw_item_chevron(item, active, hovered, x, y, w, h)
return self:draw_item_body(item, active, hovered, x, y, w, h)
end
function TreeList:draw()
if not TreeList.super.draw(self) then return end
local position, ox = self:get_position(), self:get_content_offset()
local _y, _h, sw = position.y, self.size.y, 0
for item, x,y,w,h in self:each_item() do
if y + h >= _y and y < _y + _h then
w = self:draw_item(
item,
item == self.selected_item,
item == self.hovered_item,
x, y, w, h
) - position.x + (position.x - ox)
sw = math.max(w, sw)
end
end
self.scroll_width = sw
self:draw_scrollbar()
if
self.hovered_item and self.hovered_item.tooltip
and
self.tooltip.x and self.tooltip.alpha > 0
then
core.root_view:defer_draw(self.draw_tooltip, self)
end
end
---@param item? widget.treelist.item
---@param where integer
---@return widget.treelist.item item
---@return number x
---@return number y
---@return number w
---@return number h
function TreeList:get_item(item, where)
item = item or self.selected_item
local last_item, last_x, last_y, last_w, last_h
local stop = false
for it, x, y, w, h in self:each_item() do
if not item and where >= 0 then
return it, x, y, w, h
end
if item == it then
if where < 0 and last_item then
break
elseif where == 0 or (where < 0 and not last_item) then
return it, x, y, w, h
end
stop = true
elseif stop then
item = it
return it, x, y, w, h
end
last_item, last_x, last_y, last_w, last_h = it, x, y, w, h
end
return last_item, last_x, last_y, last_w, last_h
end
---@param item? widget.treelist.item
---@return widget.treelist.item item
---@return number x
---@return number y
---@return number w
---@return number h
function TreeList:get_next(item)
return self:get_item(item, 1)
end
---@param item? widget.treelist.item
---@return widget.treelist.item item
---@return number x
---@return number y
---@return number w
---@return number h
function TreeList:get_previous(item)
return self:get_item(item, -1)
end
function TreeList:select_next()
self.selected_item = self:get_next()
end
function TreeList:select_prev()
self.selected_item = self:get_previous()
end
---Expand or collapse the currently selected or given item.
---@param toggle? boolean
---@param item? widget.treelist.item
function TreeList:toggle_expand(toggle, item)
item = item or self.selected_item
if not item then return end
if item.childs and #item.childs > 0 then
if type(toggle) == "boolean" then
item.expanded = toggle
else
item.expanded = not item.expanded
end
end
end
return TreeList