-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathitemslist.lua
162 lines (137 loc) · 3.9 KB
/
itemslist.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
--
-- ItemsList Widget.
-- @copyright Jefferson Gonzalez
-- @license MIT
--
local style = require "core.style"
local Widget = require "libraries.widget"
local Button = require "libraries.widget.button"
local ListBox = require "libraries.widget.listbox"
local InputDialog = require "libraries.widget.inputdialog"
local MessageBox = require "libraries.widget.messagebox"
---@class widget.itemslist : widget
---@overload fun(parent:widget?):widget.itemslist
---@field list widget.listbox
---@field add widget.button
---@field remove widget.button
local ItemsList = Widget:extend()
---Constructor
---@param parent widget
function ItemsList:new(parent)
ItemsList.super.new(self, parent)
self.type_name = "widget.itemslist"
self.border.width = 0
self.dialog = false
self.list = ListBox(self)
local this = self
function self.list:on_mouse_pressed(button, x, y, clicks)
if not ListBox.on_mouse_pressed(self, button, x, y, clicks) then
return false
end
if clicks == 2 and not this.dialog then
this.dialog = true
local selected = this.list:get_selected()
local selvalue = this.list:get_row_text(selected)
---@type widget.inputdialog
local input = InputDialog("Edit Item", "Enter the new item value:", selvalue)
function input:on_save(value)
this:edit_item(selected, value)
end
function input:on_close()
InputDialog.on_close(self)
self:destroy()
this.dialog = false
end
input:show()
end
return true
end
self.add = Button(self, "Add")
self.add:set_icon("B")
function self.add:on_click()
if not this.dialog then
---@type widget.inputdialog
local input = InputDialog("Add Item", "Enter the new item:")
function input:on_save(value)
this:add_item(value)
end
function input:on_close()
InputDialog.on_close(self)
self:destroy()
this.dialog = false
end
input:show()
end
end
self.remove = Button(self, "Remove")
self.remove:set_icon("C")
function self.remove:on_click()
local selected = this.list:get_selected()
if selected then
this:remove_item(selected)
else
MessageBox.error("No item selected", "Please select an item to remove")
end
end
end
---Add a new item into the list.
---@param text widget.styledtext | string
---@param data any
function ItemsList:add_item(text, data)
if type(text) == "string" then
text = {text}
end
self.list:add_row(text, data)
self.list:set_visible_rows()
self:on_change()
end
---Edit an existing item on the list.
---@param idx integer
---@param text widget.styledtext | string
---@param data any
function ItemsList:edit_item(idx, text, data)
if type(text) == "string" then
text = {text}
end
self.list:set_row(idx, text)
if data then
self.list:set_row_data(idx, data)
end
self:on_change()
end
---Remove the given item from the list.
---@param idx integer
function ItemsList:remove_item(idx)
self.list:remove_row(idx)
self:on_change()
end
---Return the items from the list.
---@return table<integer, string>
function ItemsList:get_items()
local output = {}
local count = #self.list.rows
for i=1, count, 1 do
table.insert(output, self.list:get_row_text(i))
end
return output
end
function ItemsList:update()
if not ItemsList.super.update(self) then return false end
if self.size.x == 0 then
self.size.x = self.add:get_width()
+ (style.padding.x / 2) + self.remove:get_width() + (50 * SCALE)
self.size.y = self.add:get_height() + (style.padding.y * 2) + 100
end
self.list:set_position(0, 0)
self.list:set_size(
self.size.x,
self.size.y - self.add:get_height() - (style.padding.y * 2)
)
self.add:set_position(0, self.list:get_bottom() + style.padding.y)
self.remove:set_position(
self.add:get_right() + (style.padding.x / 2),
self.list:get_bottom() + style.padding.y
)
return true
end
return ItemsList