-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfontslist.lua
223 lines (190 loc) · 5.64 KB
/
fontslist.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
--
-- FontsList 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 FontDialog = require "libraries.widget.fontdialog"
local MessageBox = require "libraries.widget.messagebox"
---@class widget.fontslist.font : widget
---@field name string
---@field path string
---@class widget.fontslist : widget
---@overload fun(parent?:widget):widget.fontslist
---@field list widget.listbox
---@field add widget.button
---@field remove widget.button
---@field up widget.button
---@field down widget.button
---@field options renderer.fontoptions
---@field private dialog boolean
local FontsList = Widget:extend()
---Constructor
---@param parent widget
function FontsList:new(parent)
FontsList.super.new(self, parent)
self.type_name = "widget.fontslist"
self.border.width = 0
self.dialog = false
self.options = {}
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 fontdata = this.list:get_row_data(selected)
---@type widget.inputdialog
local font = FontDialog(fontdata, this:get_options())
function font:on_save(fontdata, options)
this:set_options(options)
this:edit_font(selected, fontdata)
end
function font:on_close()
FontDialog.on_close(self)
self:destroy()
this.dialog = false
end
font:show()
end
return true
end
self.add = Button(self, "Add")
self.add:set_icon("B")
function self.add:on_click()
if #this.list.rows > 9 then
MessageBox.error("Max Fonts Reached", "Only a maximum of ten fonts can be added.")
return
end
if not this.dialog then
this.dialog = true
---@type widget.inputdialog
local font = FontDialog(nil, this:get_options())
function font:on_save(fontdata, options)
this:set_options(options)
this:add_font(fontdata)
end
function font:on_close()
FontDialog.on_close(self)
self:destroy()
this.dialog = false
end
font: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
if #this.list.rows > 1 then
this:remove_font(selected)
else
MessageBox.error("Font required", "A minimum of one font is needed")
end
else
MessageBox.error("No font selected", "Please select a font to remove")
end
end
self.up = Button(self, "")
self.up:set_icon("<")
self.up:set_tooltip("increase font priority")
function self.up:on_click()
local selected = this.list:get_selected()
if selected then
this.list:move_row_up(selected)
this:on_change()
else
MessageBox.error("No font selected", "Please select a font to move")
end
end
self.down = Button(self, "")
self.down:set_icon(">")
self.down:set_tooltip("decrease font priority")
function self.down:on_click()
local selected = this.list:get_selected()
if selected then
this.list:move_row_down(selected)
this:on_change()
else
MessageBox.error("No font selected", "Please select a font to move")
end
end
end
---Add a new font into the list.
---@param font widget.fontslist.font
function FontsList:add_font(font)
self.list:add_row({font.name}, font)
self.list:set_visible_rows()
self:on_change()
end
---Edit an existing font on the list.
---@param idx integer
---@param font widget.fontslist.font
function FontsList:edit_font(idx, font)
self.list:set_row(idx, {font.name})
self.list:set_row_data(idx, font)
self:on_change()
end
---Remove the given font from the list.
---@param idx integer
function FontsList:remove_font(idx)
self.list:remove_row(idx)
self:on_change()
end
---Return the fonts from the list.
---@return table<integer, string>
function FontsList:get_fonts()
local output = {}
local count = #self.list.rows
for i=1, count, 1 do
table.insert(output, self.list:get_row_data(i))
end
return output
end
---Set the global options for the fonts group
---@param options renderer.fontoptions
function FontsList:set_options(options)
self.options = options
end
---Get the global options for the font group
---@return renderer.fontoptions
function FontsList:get_options()
return self.options
end
function FontsList:update()
if not FontsList.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()
+ (style.padding.x / 2) + self.up:get_width()
+ (style.padding.x / 2) + self.down: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
)
self.up:set_position(
self.remove:get_right() + (style.padding.x / 2),
self.list:get_bottom() + style.padding.y
)
self.down:set_position(
self.up:get_right() + (style.padding.x / 2),
self.list:get_bottom() + style.padding.y
)
return true
end
return FontsList