type out a filename to load/save

This commit is contained in:
Kartik K. Agaram 2023-11-26 08:52:34 -08:00
parent 83ebcaa5c4
commit 1b898d4a2e
14 changed files with 70 additions and 4 deletions

View File

@ -1,6 +1,6 @@
on.keychord_press = function(chord, key)
if Show_file_dialog then
Show_file_dialog = false
keychord_press_on_file_dialog(chord, key)
return
end
if chord == 'C-=' then

View File

@ -1,5 +1,7 @@
on.text_input = function(t)
if Show_code then
if Show_file_dialog then
text_input_on_file_dialog(t)
elseif Show_code then
if Current_pane.editor_state.cursor_x then
edit.text_input(Current_pane.editor_state, t)
end

View File

@ -4,6 +4,11 @@ load_button = function(x, y, r)
Directory_contents = nil -- refresh from file system
Show_file_dialog = true
File_dialog_callback = function(filename)
if filename == '' then
-- clear filename
Current_pane.filename = nil
return
end
Current_pane.filename = filename
one_time_load()
end

View File

@ -1,4 +1,5 @@
draw_file_dialog = function()
draw_file_dialog_input()
App.color(Menu_background)
love.graphics.rectangle('fill',
Menu_left+5,
@ -15,8 +16,7 @@ draw_file_dialog = function()
if x == Menu_left+10 or x+w < Safe_width-Menu_left-10 then
styled_button(filename, x,y, function()
File_dialog_callback(filename)
Show_file_dialog = false
File_dialog_callback = nil
reset_file_dialog_state()
end)
x = x+w+10
else

View File

@ -0,0 +1,7 @@
draw_file_dialog_input = function()
-- draw a suffix to ensure the cursor is visible
App.color(Normal_color)
love.graphics.print(File_dialog_input_draw_suffix, Menu_left+15, Menu_top+5)
local w = App.width(File_dialog_input_draw_suffix)
draw_cursor(Menu_left+15+w, Menu_top+5, Current_pane.editor_state.line_height)
end

7
0129-draw_cursor Normal file
View File

@ -0,0 +1,7 @@
draw_cursor = function(x, y, line_height)
-- blink every 0.5s
if math.floor(Cursor_time*2)%2 == 0 then
App.color(Cursor_color)
love.graphics.rectangle('fill', x,y, 3,line_height)
end
end

View File

@ -0,0 +1,13 @@
keychord_press_on_file_dialog = function(chord, key)
if chord == 'escape' then
reset_file_dialog_state()
elseif chord == 'return' then
File_dialog_callback(File_dialog_input_text)
reset_file_dialog_state()
elseif chord == 'backspace' then
local len = utf8.len(File_dialog_input_text)
local byte_offset = Text.offset(File_dialog_input_text, len)
File_dialog_input_text = string.sub(File_dialog_input_text, 1, byte_offset-1)
refresh_file_dialog_input_start()
end
end

View File

@ -0,0 +1,5 @@
text_input_on_file_dialog = function(t)
-- the file dialog input only supports appending
File_dialog_input_text = File_dialog_input_text..t
refresh_file_dialog_input_start()
end

View File

@ -0,0 +1 @@
File_dialog_input_text = ''

View File

@ -0,0 +1,5 @@
reset_file_dialog_state = function()
Show_file_dialog = false
File_dialog_callback = nil
File_dialog_input_text = ''
end

View File

@ -0,0 +1,4 @@
-- Cursor in the file dialog is always at end of input text.
-- Cache where we start rendering the input from
-- (for narrow devices)
File_dialog_input_start_suffix = ''

View File

@ -0,0 +1,2 @@
refresh_file_dialog_start = function()
end

View File

@ -0,0 +1,11 @@
refresh_file_dialog_input_start = function()
local len = utf8.len(File_dialog_input_text)
for start=0,len do
local s = File_dialog_input_text:sub(start)
if App.width(s) < Safe_width-Menu_left-15-15 then
File_dialog_input_draw_suffix = s
return
end
end
assert(false, "refresh_file_dialog_input_start isn't working right")
end

View File

@ -0,0 +1,4 @@
-- Cursor in the file dialog is always at end of input text.
-- Cache where we start rendering the input from
-- (for narrow devices)
File_dialog_input_draw_suffix = ''