simplify state management for menus

This commit is contained in:
Kartik K. Agaram 2023-12-04 22:09:47 -08:00
parent e93d43253d
commit 966f8a15bf
24 changed files with 48 additions and 61 deletions

View File

@ -6,13 +6,12 @@ on.mouse_press = function(x,y, mouse_button)
Button_pressed = true
return
end
Show_overflow = false
-- == settings area
if Show_settings then
if Show_menu == 'settings' then
if on_area(Settings_menu_area, x,y) then
select_settings_slider(x,y, mouse_button)
else
Show_settings = false
Show_menu = nil
-- On mobile devices, we can't depend on on.save_settings() triggering on quit.
-- So save settings every time we close the settings menu.
love.filesystem.write('config', json.encode(settings()))
@ -20,6 +19,7 @@ on.mouse_press = function(x,y, mouse_button)
return
end
-- == main area
Show_menu = nil
if Show_code then
if on_editor_scrollbar(Current_pane.editor_state, x,y) then
Current_pane.editor_state.scrollbar_drag = true
@ -42,4 +42,4 @@ on.mouse_press = function(x,y, mouse_button)
if car.mouse_press then car.mouse_press(x,y, mouse_button) end
if car.mousepressed then car.mousepressed(x,y, mouse_button) end
end
end
end

View File

@ -6,7 +6,7 @@ on.mouse_release = function(x,y, mouse_button)
return
end
-- == settings area
if Show_settings then
if Show_menu == 'settings' then
if on_area(Settings_menu_area, x,y) then
return
end
@ -37,4 +37,4 @@ on.mouse_release = function(x,y, mouse_button)
call_protected(car.mousereleased, x,y, mouse_button)
end
end
end
end

View File

@ -49,7 +49,7 @@ draw_menu = function()
if Current_pane_index < #Panes then
next_pane_button(Menu_left + Safe_width)
end
if Show_settings then
if Show_menu == 'settings' then
draw_settings_menu()
end
end
end

View File

@ -1,8 +1,7 @@
run_button = function(x, y)
styled_button('run', x,y,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
-- ## run: initialize
clear_handlers()
local buf = table.concat(map(Current_pane.editor_state.lines, function(line) return line.data end), '\n')
@ -30,4 +29,4 @@ run_button = function(x, y)
end)
local w = App.width('run')+10
return x+w+10, y
end
end

View File

@ -5,12 +5,11 @@ previous_pane_button = function()
love.graphics.polygon('fill', Menu_left+5, App.screen.height/2, Menu_left+25, App.screen.height/2-10, Menu_left+25, App.screen.height/2+10)
end,
onpress1 = function()
Show_overflow = false
Show_settings = false
Show_menu = nil
Current_pane.car = car
Current_pane_index = Current_pane_index-1
Current_pane = Panes[Current_pane_index]
car = Current_pane.car or {}
end,
})
end
end

View File

@ -5,12 +5,11 @@ next_pane_button = function(r)
love.graphics.polygon('fill', r-25, App.screen.height/2-10, r-25, App.screen.height/2+10, r-5, App.screen.height/2)
end,
onpress1 = function()
Show_overflow = false
Show_settings = false
Show_menu = nil
Current_pane.car = car
Current_pane_index = Current_pane_index+1
Current_pane = Panes[Current_pane_index]
car = Current_pane.car or {}
end,
})
end
end

View File

@ -1,10 +1,9 @@
new_pane_button = function(x, y, r)
return overflowable_button('new', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
Current_pane_index = Current_pane_index+1
table.insert(Panes, Current_pane_index, new_pane())
Current_pane = Panes[Current_pane_index]
end)
end
end

View File

@ -1,8 +1,7 @@
show_code_button = function(x, y, r)
return overflowable_button('show', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
Show_code = true
end)
end
end

View File

@ -1,8 +1,7 @@
hide_code_button = function(x, y, r)
return overflowable_button('hide', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
Show_code = false
end)
end
end

View File

@ -1,8 +1,7 @@
copy_button = function(x, y, r)
return overflowable_button('copy', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
local e = Current_pane.editor_state
local s = Text.selection(e)
if s == nil then
@ -14,4 +13,4 @@ copy_button = function(x, y, r)
end
App.set_clipboard(s)
end)
end
end

View File

@ -1,8 +1,7 @@
paste_button = function(x, y, r)
return overflowable_button('paste', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
local state = Current_pane.editor_state
-- initial state for recording undo operation
local before_line = state.cursor1.line
@ -19,4 +18,4 @@ paste_button = function(x, y, r)
state.cursor1.line)
})
end)
end
end

View File

@ -1,11 +1,12 @@
settings_button = function(x)
return right_justified_button('settings', x, Menu_top+5,
function()
Show_overflow = false
Show_settings = not Show_settings
if not Show_settings then
if Show_menu == 'settings' then
Show_menu = nil
-- On mobile devices, we can't depend on on.save_settings() triggering on quit
love.filesystem.write('config', json.encode(settings()))
else
Show_menu = 'settings'
end
end)
end
end

View File

@ -1,8 +1,7 @@
clear_pane_button = function(x, y, r)
return overflowable_button('clear', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
clear_pane()
end)
end
end

View File

@ -1,8 +1,7 @@
delete_pane_button = function(x, y, r)
return overflowable_button('delete', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
if Current_pane.example_name then
Deleted_example_panes[Current_pane.example_name] = true
end
@ -16,4 +15,4 @@ delete_pane_button = function(x, y, r)
Current_pane = Panes[Current_pane_index]
end,
--[[final button]] true)
end
end

1
0080-Show_menu Normal file
View File

@ -0,0 +1 @@
Show_menu = nil

View File

@ -1 +0,0 @@
Show_settings =false

View File

@ -1,5 +1,4 @@
update_any_sliders = function(x,y)
if not Show_settings then return end
if Selected_slider == Settings_font_slider.name then
update_font_settings(slider_value(Settings_font_slider, x))
return true
@ -16,4 +15,4 @@ update_any_sliders = function(x,y)
return true
end
end
end
end

View File

@ -1,5 +1,4 @@
select_settings_slider = function(x,y, mouse_button)
if not Show_settings then return end
local result = false
if on_slider(Settings_font_slider, x,y) then
Selected_slider = Settings_font_slider.name

View File

@ -1,10 +1,9 @@
stop_button = function(x, y)
styled_button('stop', x,y,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
clear_handlers()
end)
local w = App.width('stop')+10
return x+w+10, y
end
end

View File

@ -1,2 +0,0 @@
-- True if the overflow menu has been opened and we want to render buttons within it.
Show_overflow = false

View File

@ -1,8 +1,11 @@
overflow_button = function(x, y)
styled_button('>>', x,y,
function()
Show_settings = false
Show_overflow = not Show_overflow
if Show_menu == 'overflow' then
Show_menu = nil
else
Show_menu = 'overflow'
end
end)
return x, y+Line_height
end
end

View File

@ -1,8 +1,7 @@
save_button = function(x, y, r)
return overflowable_button('save', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
if Current_pane.filename == nil then
Show_file_dialog = true
File_dialog_callback = function(filename)
@ -14,4 +13,4 @@ save_button = function(x, y, r)
one_time_save()
end
end)
end
end

View File

@ -1,8 +1,7 @@
load_button = function(x, y, r)
return overflowable_button('load', x, y, r,
function()
Show_overflow = false
Show_settings = false
Show_menu = nil
Show_file_dialog = true
File_dialog_callback = function(filename)
if filename == '' then
@ -14,4 +13,4 @@ load_button = function(x, y, r)
one_time_load()
end
end)
end
end

View File

@ -5,7 +5,7 @@ overflowable_button = function(name, x, y, r, onpress1, final_button)
local x2, y2 = maybe_draw_overflow_button(x, y, w, r, final_button)
if Overflow_button then
-- overflow
if not Show_overflow then
if Show_menu ~= 'overflow' then
return x, y
else
y = y2