Skip to content

Commit 683e924

Browse files
committed
Also limit text_width and total_width
1 parent c3716e1 commit 683e924

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

gooey/internal/input.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,24 @@ function INPUT.set_text(input, text)
8585
input.empty = #text == 0 and #marked_text == 0
8686

8787
-- measure it
88-
input.text_width = get_text_width(input.node, text)
89-
input.marked_text_width = get_text_width(input.node, marked_text)
90-
input.total_width = input.text_width + input.marked_text_width
88+
local text_width = get_text_width(input.node, text)
89+
local marked_text_width = get_text_width(input.node, marked_text)
9190

9291
-- prevent text from overflowing the input field
93-
local visible_text_width = input.total_width
94-
local visible_text = text .. marked_text
92+
-- this will get increasingly expensive the longer the text
93+
-- future improvement is to make a best guess and then either add or
94+
-- remove characters depending on if the text is too short or long
9595
local field_width = gui.get_size(input.node).x * gui.get_scale(input.node).x
96-
while visible_text_width > field_width do
97-
visible_text = visible_text:sub(2)
98-
visible_text_width = get_text_width(input.node, visible_text)
96+
while (text_width + marked_text_width) > field_width do
97+
text = text:sub(2)
98+
text_width = get_text_width(input.node, text)
9999
end
100-
101-
gui.set_text(input.node, visible_text)
100+
101+
input.text_width = text_width
102+
input.marked_text_width = marked_text_width
103+
input.total_width = text_width + marked_text_width
104+
105+
gui.set_text(input.node, text .. marked_text)
102106
end
103107
end
104108
function INPUT.set_long_pressed_time(input, time)

0 commit comments

Comments
 (0)