-
Notifications
You must be signed in to change notification settings - Fork 2
/
textbox.lua
308 lines (260 loc) · 7.81 KB
/
textbox.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
--
-- TextBox widget re-using code from pragtical's DocView.
--
local core = require "core"
local style = require "core.style"
local translate = require "core.doc.translate"
local Doc = require "core.doc"
local DocView = require "core.docview"
local View = require "core.view"
local Widget = require "widget"
---@class widget.textbox.SingleLineDoc : core.doc
---@overload fun():widget.textbox.SingleLineDoc
---@field super core.doc
local SingleLineDoc = Doc:extend()
function SingleLineDoc:insert(line, col, text)
SingleLineDoc.super.insert(self, line, col, text:gsub("\n", ""))
end
---@class widget.textbox.TextView : core.docview
---@overload fun():widget.textbox.TextView
---@field super core.docview
local TextView = DocView:extend()
function TextView:new()
TextView.super.new(self, SingleLineDoc())
self.gutter_width = 0
self.hide_lines_gutter = true
self.gutter_text_brightness = 0
self.scrollable = true
self.font = "font"
self.name = View.get_name(self)
self.size.y = 0
self.label = ""
end
function TextView:get_name()
return self.name
end
function TextView:get_scrollable_size()
return 0
end
function TextView:get_text()
return self.doc:get_text(1, 1, 1, math.huge)
end
function TextView:set_text(text, select)
self.doc:remove(1, 1, math.huge, math.huge)
self.doc:text_input(text)
if select then
self.doc:set_selection(math.huge, math.huge, 1, 1)
end
end
function TextView:get_gutter_width()
return self.gutter_width or 0
end
function TextView:get_line_height()
return math.floor(self:get_font():get_height() * 1.2)
end
function TextView:draw_line_gutter(idx, x, y)
if self.hide_lines_gutter then
return
end
TextView.super.draw_line_gutter(self, idx, x, y)
end
function TextView:draw_line_highlight()
-- no-op function to disable this functionality
end
-- Overwrite this function just to disable the core.push_clip_rect
function TextView:draw()
self:draw_background(style.background)
local _, indent_size = self.doc:get_indent_info()
self:get_font():set_tab_size(indent_size)
local minline, maxline = self:get_visible_line_range()
local lh = self:get_line_height()
local x, y = self:get_line_screen_position(minline)
for i = minline, maxline do
self:draw_line_gutter(i, self.position.x, y)
y = y + lh
end
x, y = self:get_line_screen_position(minline)
for i = minline, maxline do
self:draw_line_body(i, x, y)
y = y + lh
end
self:draw_overlay()
self:draw_scrollbar()
end
---@class widget.textbox : widget
---@overload fun(parent?:widget, text?:string, placeholder?:string):widget.textbox
---@field textview widget.textbox.TextView
---@field placeholder string
---@field placeholder_active boolean
local TextBox = Widget:extend()
function TextBox:new(parent, text, placeholder)
TextBox.super.new(self, parent)
self.type_name = "widget.textbox"
self.textview = TextView()
self.textview.name = parent.name
self.size.x = 200 + (style.padding.x * 2)
self.textview.size.x = self.size.x
self.size.y = self:get_font():get_height() + (style.padding.y * 2)
self.placeholder = placeholder or ""
self.placeholder_active = false
-- this widget is for text input
self.input_text = true
self.cursor = "ibeam"
self.active = false
self.drag_select = false
if text ~= "" then
self.textview:set_text(text, select)
else
self.placeholder_active = true
self.textview:set_text(self.placeholder)
end
local this = self
function self.textview.doc:on_text_change()
if not this.placeholder_active then
this:on_change(this:get_text())
end
end
-- more granular listening of text changing events
local doc_raw_insert = self.textview.doc.raw_insert
function self.textview.doc:raw_insert(...)
doc_raw_insert(self, ...)
this:on_text_change("insert", ...)
end
local doc_raw_remove = self.textview.doc.raw_remove
function self.textview.doc:raw_remove(...)
doc_raw_remove(self, ...)
this:on_text_change("remove", ...)
end
end
---@param width integer
function TextBox:set_size(width)
TextBox.super.set_size(
self,
width,
self:get_font():get_height() + (style.padding.y * 2)
)
self.textview.size.x = self.size.x
end
--- Get the text displayed on the textbox.
---@return string
function TextBox:get_text()
if self.placeholder_active then
return ""
end
return self.textview:get_text()
end
--- Set the text displayed on the textbox.
---@param text string
---@param select? boolean
function TextBox:set_text(text, select)
self.textview:set_text(text, select)
end
--
-- Events
--
function TextBox:on_mouse_pressed(button, x, y, clicks)
if TextBox.super.on_mouse_pressed(self, button, x, y, clicks) then
self.textview:on_mouse_pressed(button, x, y, clicks)
local line, col = self.textview:resolve_screen_position(x, y)
self.drag_select = { line = line, col = col }
self.textview.doc:set_selection(line, col, line, col)
if clicks == 2 then
local line1, col1 = translate.start_of_word(self.textview.doc, line, col)
local line2, col2 = translate.end_of_word(self.textview.doc, line1, col1)
self.textview.doc:set_selection(line2, col2, line1, col1)
elseif clicks == 3 then
self.textview.doc:set_selection(1, 1, 1, math.huge)
end
if core.active_view ~= self.textview then
self.textview:on_mouse_released(button, x, y)
end
return true
end
return false
end
function TextBox:on_mouse_released(button, x, y)
if TextBox.super.on_mouse_released(self, button, x, y) then
self.drag_select = false
self.textview:on_mouse_released(button, x, y)
return true
end
return false
end
function TextBox:on_mouse_moved(x, y, dx, dy)
if self.drag_select then
local line, col = self.textview:resolve_screen_position(x, y)
self.textview.doc:set_selection(
self.drag_select.line, self.drag_select.col, line, col
)
end
if TextBox.super.on_mouse_moved(self, x, y, dx, dy) then
if self.active or core.active_view == self.textview then
self.textview:on_mouse_moved(x, y, dx, dy)
end
return true
end
return false
end
function TextBox:activate()
self.hover_border = style.caret
if self.placeholder_active then
self.placeholder_active = false
self:set_text("")
end
self.active = true
core.request_cursor("ibeam")
end
function TextBox:deactivate()
self.hover_border = nil
self.drag_select = false
if self:get_text() == "" then
self.placeholder_active = true
self:set_text(self.placeholder)
end
self.active = false
core.request_cursor("arrow")
end
function TextBox:on_text_input(text)
TextBox.super.on_text_input(self, text)
self.textview:on_text_input(text)
end
---Event fired on any text change event.
---@param action string Can be "insert" or "remove",
---insert arguments (see Doc:raw_insert):
--- line, col, text, undo_stack, time
---remove arguments (see Doc:raw_remove):
--- line1, col1, line2, col2, undo_stack, time
---@diagnostic disable-next-line
function TextBox:on_text_change(action, ...) end
function TextBox:update()
if not TextBox.super.update(self) then return false end
if
self.drag_select
or
(self.active and self:mouse_on_top(self.mouse.x, self.mouse.y))
then
core.request_cursor("ibeam")
end
self.textview:update()
self.size.y = self:get_font():get_height() + (style.padding.y * 2)
return true
end
function TextBox:draw()
if not self:is_visible() then return false end
self.border.color = self.hover_border or style.text
TextBox.super.draw(self)
self.textview.position.x = self.position.x + (style.padding.x / 2)
self.textview.position.y = self.position.y - (style.padding.y/2.5)
self.textview.size.x = self.size.x
self.textview.size.y = self.size.y - (style.padding.y * 2)
core.push_clip_rect(
self.position.x,
self.position.y,
self.size.x,
self.size.y
)
self.textview:draw()
core.pop_clip_rect()
return true
end
return TextBox