3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 10:27:49 +02:00

Update stack earlier when showing new screen

Fixes #76
This commit is contained in:
Björn Ritzl 2021-06-27 00:34:32 +02:00
parent 20cf731fdb
commit 4927d6e766
2 changed files with 39 additions and 32 deletions

View File

@ -185,7 +185,7 @@ function M.is_visible(id)
assert(id, "You must provide a screen id") assert(id, "You must provide a screen id")
id = tohash(id) id = tohash(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id))) assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
return screens[id].loaded return screens[id].visible
end end
@ -570,6 +570,7 @@ local function show_out(screen, next_screen, wait_for_transition, cb)
local current_is_popup = screen.popup local current_is_popup = screen.popup
if (not next_is_popup and not current_is_popup) or (current_is_popup) then if (not next_is_popup and not current_is_popup) or (current_is_popup) then
transition(screen, M.TRANSITION.SHOW_OUT, { next_screen = next_screen.id }, wait_for_transition) transition(screen, M.TRANSITION.SHOW_OUT, { next_screen = next_screen.id }, wait_for_transition)
screen.visible = false
unload(screen) unload(screen)
elseif next_is_popup then elseif next_is_popup then
change_timestep(screen) change_timestep(screen)
@ -590,9 +591,15 @@ local function show_in(screen, previous_screen, reload, add_to_stack, wait_for_t
log("show_in() reloading", screen.id) log("show_in() reloading", screen.id)
unload(screen, reload) unload(screen, reload)
end end
if add_to_stack then
stack[#stack + 1] = screen
end
local ok, err = load(screen) local ok, err = load(screen)
if not ok then if not ok then
log("show_in()", err) log("show_in()", err)
if add_to_stack then
stack[#stack] = nil
end
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
notify_transition_listeners(M.SCREEN_TRANSITION_FAILED, { screen = screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_FAILED, { screen = screen.id })
return return
@ -600,11 +607,9 @@ local function show_in(screen, previous_screen, reload, add_to_stack, wait_for_t
-- wait until screen has had a chance to render -- wait until screen has had a chance to render
cowait(0) cowait(0)
cowait(0) cowait(0)
if add_to_stack then
stack[#stack + 1] = screen
end
reset_timestep(screen) reset_timestep(screen)
transition(screen, M.TRANSITION.SHOW_IN, { previous_screen = previous_screen and previous_screen.id }, wait_for_transition) transition(screen, M.TRANSITION.SHOW_IN, { previous_screen = previous_screen and previous_screen.id }, wait_for_transition)
screen.visible = true
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
@ -633,6 +638,7 @@ local function back_in(screen, previous_screen, wait_for_transition, cb)
if previous_screen and not previous_screen.popup then if previous_screen and not previous_screen.popup then
transition(screen, M.TRANSITION.BACK_IN, { previous_screen = previous_screen.id }, wait_for_transition) transition(screen, M.TRANSITION.BACK_IN, { previous_screen = previous_screen.id }, wait_for_transition)
end end
screen.visible = true
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
@ -653,6 +659,7 @@ local function back_out(screen, next_screen, wait_for_transition, cb)
reset_timestep(next_screen) reset_timestep(next_screen)
end end
transition(screen, M.TRANSITION.BACK_OUT, { next_screen = next_screen and next_screen.id }, wait_for_transition) transition(screen, M.TRANSITION.BACK_OUT, { next_screen = next_screen and next_screen.id }, wait_for_transition)
screen.visible = false
unload(screen) unload(screen)
active_transition_count = active_transition_count - 1 active_transition_count = active_transition_count - 1
notify_transition_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen and next_screen.id }) notify_transition_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen and next_screen.id })

View File

@ -39,7 +39,7 @@ return function()
local screens_instances = {} local screens_instances = {}
local function is_shown(screen_id) local function is_visible(screen_id)
return monarch.is_visible(screen_id) return monarch.is_visible(screen_id)
end end
@ -65,9 +65,6 @@ return function()
local function wait_until_visible(screen_id) local function wait_until_visible(screen_id)
return wait_timeout(is_visible, screen_id) return wait_timeout(is_visible, screen_id)
end end
local function wait_until_shown(screen_id)
return wait_timeout(is_shown, screen_id)
end
local function wait_until_hidden(screen_id) local function wait_until_hidden(screen_id)
return wait_timeout(is_hidden, screen_id) return wait_timeout(is_hidden, screen_id)
end end
@ -145,23 +142,23 @@ return function()
assert(not monarch.is_visible(SCREEN1)) assert(not monarch.is_visible(SCREEN1))
monarch.show(SCREEN1) monarch.show(SCREEN1)
assert(wait_until_stack({ SCREEN1 })) assert(wait_until_stack({ SCREEN1 }))
assert(monarch.is_visible(SCREEN1)) assert(wait_until_visible(SCREEN1))
monarch.show(SCREEN2) monarch.show(SCREEN2)
assert(wait_until_stack({ SCREEN1, SCREEN2 })) assert(wait_until_stack({ SCREEN1, SCREEN2 }))
assert(not monarch.is_visible(SCREEN1)) assert(wait_until_hidden(SCREEN1))
assert(monarch.is_visible(SCREEN2)) assert(wait_until_visible(SCREEN2))
monarch.show(POPUP1) monarch.show(POPUP1)
assert(wait_until_stack({ SCREEN1, SCREEN2, POPUP1 })) assert(wait_until_stack({ SCREEN1, SCREEN2, POPUP1 }))
assert(not monarch.is_visible(SCREEN1)) assert(wait_until_hidden(SCREEN1))
assert(monarch.is_visible(SCREEN2)) assert(wait_until_visible(SCREEN2))
assert(monarch.is_visible(POPUP1)) assert(wait_until_visible(POPUP1))
end) end)
it("should be able to show a screen without adding it to the stack", function() it("should be able to show a screen without adding it to the stack", function()
monarch.show(BACKGROUND, { no_stack = true }) monarch.show(BACKGROUND, { no_stack = true })
assert(wait_until_shown(BACKGROUND), "Background was never shown") assert(wait_until_visible(BACKGROUND), "Background was never shown")
assert(wait_until_stack({ })) assert(wait_until_stack({ }))
monarch.show(SCREEN1) monarch.show(SCREEN1)
@ -175,7 +172,7 @@ return function()
monarch.show(BACKGROUND, { no_stack = true }) monarch.show(BACKGROUND, { no_stack = true })
assert(wait_until_not_busy()) assert(wait_until_not_busy())
assert(wait_until_shown(BACKGROUND)) assert(wait_until_visible(BACKGROUND))
assert(wait_until_stack({ SCREEN1 })) assert(wait_until_stack({ SCREEN1 }))
monarch.show(SCREEN2) monarch.show(SCREEN2)
@ -184,13 +181,13 @@ return function()
monarch.back() monarch.back()
assert(wait_until_not_busy()) assert(wait_until_not_busy())
assert(wait_until_shown(SCREEN1)) assert(wait_until_visible(SCREEN1))
assert(wait_until_stack({ SCREEN1 })) assert(wait_until_stack({ SCREEN1 }))
end) end)
it("should be able to hide a screen not added to the stack", function() it("should be able to hide a screen not added to the stack", function()
monarch.show(BACKGROUND, { no_stack = true }) monarch.show(BACKGROUND, { no_stack = true })
assert(wait_until_shown(BACKGROUND), "Background was never shown") assert(wait_until_visible(BACKGROUND), "Background was never shown")
assert_stack({ }) assert_stack({ })
monarch.hide(BACKGROUND) monarch.hide(BACKGROUND)
@ -213,18 +210,18 @@ return function()
it("should be able to pass data to a screen when showing it or going back to it", function() it("should be able to pass data to a screen when showing it or going back to it", function()
local data1 = { foo = "bar" } local data1 = { foo = "bar" }
monarch.show(SCREEN1, nil, data1) monarch.show(SCREEN1, nil, data1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown") assert(wait_until_visible(SCREEN1), "Screen1 was never shown")
local data2 = { boo = "car" } local data2 = { boo = "car" }
monarch.show(SCREEN2, nil, data2) monarch.show(SCREEN2, nil, data2)
assert(wait_until_shown(SCREEN2), "Screen2 was never shown") assert(wait_until_visible(SCREEN2), "Screen2 was never shown")
assert(monarch.data(SCREEN1) == data1, "Expected data on screen1 doesn't match actual data") assert(monarch.data(SCREEN1) == data1, "Expected data on screen1 doesn't match actual data")
assert(monarch.data(SCREEN2) == data2, "Expected data on screen2 doesn't match actual data") assert(monarch.data(SCREEN2) == data2, "Expected data on screen2 doesn't match actual data")
local data_back = { going = "back" } local data_back = { going = "back" }
monarch.back(data_back) monarch.back(data_back)
assert(wait_until_shown(SCREEN1)) assert(wait_until_visible(SCREEN1))
assert(monarch.data(SCREEN1) == data_back, "Expected data on screen1 doesn't match actual data") assert(monarch.data(SCREEN1) == data_back, "Expected data on screen1 doesn't match actual data")
end) end)
@ -368,7 +365,7 @@ return function()
it("should be busy while transition is running", function() it("should be busy while transition is running", function()
monarch.show(TRANSITION1) monarch.show(TRANSITION1)
assert(monarch.is_busy()) assert(monarch.is_busy())
assert(wait_until_shown(TRANSITION1), "Transition1 was never shown") assert(wait_until_visible(TRANSITION1), "Transition1 was never shown")
assert(wait_until_not_busy()) assert(wait_until_not_busy())
end) end)
@ -386,7 +383,7 @@ return function()
-- previously a call to preload() while also showing a screen would -- previously a call to preload() while also showing a screen would
-- lock up monarch. See issue #32 -- lock up monarch. See issue #32
monarch.preload(TRANSITION1) monarch.preload(TRANSITION1)
assert(wait_until_shown(TRANSITION1), "Transition1 was never shown") assert(wait_until_visible(TRANSITION1), "Transition1 was never shown")
end) end)
it("should be able to notify listeners of navigation events", function() it("should be able to notify listeners of navigation events", function()
@ -438,12 +435,12 @@ return function()
it("should be able to show a screen even while it is preloading", function() it("should be able to show a screen even while it is preloading", function()
monarch.show(SCREEN_PRELOAD, nil, { count = 1 }) monarch.show(SCREEN_PRELOAD, nil, { count = 1 })
assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") assert(wait_until_visible(SCREEN_PRELOAD), "Screen_preload was never shown")
end) end)
it("should be able to preload a screen and always keep it loaded", function() it("should be able to preload a screen and always keep it loaded", function()
monarch.show(SCREEN_PRELOAD) monarch.show(SCREEN_PRELOAD)
assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") assert(wait_until_visible(SCREEN_PRELOAD), "Screen_preload was never shown")
monarch.back() monarch.back()
assert(wait_until_hidden(SCREEN_PRELOAD), "Screen_preload was never hidden") assert(wait_until_hidden(SCREEN_PRELOAD), "Screen_preload was never hidden")
assert(monarch.is_preloaded(SCREEN_PRELOAD)) assert(monarch.is_preloaded(SCREEN_PRELOAD))
@ -451,12 +448,12 @@ return function()
it("should be able to reload a preloaded screen", function() it("should be able to reload a preloaded screen", function()
monarch.show(SCREEN_PRELOAD, nil, { count = 1 }) monarch.show(SCREEN_PRELOAD, nil, { count = 1 })
assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") assert(wait_until_visible(SCREEN_PRELOAD), "Screen_preload was never shown")
-- first time the screen gets loaded it will increment the count -- first time the screen gets loaded it will increment the count
assert(monarch.data(SCREEN_PRELOAD).count == 2) assert(monarch.data(SCREEN_PRELOAD).count == 2)
monarch.show(SCREEN_PRELOAD, { clear = true, reload = true }, { count = 1 }) monarch.show(SCREEN_PRELOAD, { clear = true, reload = true }, { count = 1 })
assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") assert(wait_until_visible(SCREEN_PRELOAD), "Screen_preload was never shown")
-- second time the screen gets shown it will be reloaded and increment the count -- second time the screen gets shown it will be reloaded and increment the count
assert(monarch.data(SCREEN_PRELOAD).count == 2) assert(monarch.data(SCREEN_PRELOAD).count == 2)
end) end)
@ -470,9 +467,11 @@ return function()
assert(wait_until_stack({ SCREEN1 })) assert(wait_until_stack({ SCREEN1 }))
monarch.show(FOCUS1) monarch.show(FOCUS1)
assert(wait_until_stack({ SCREEN1, FOCUS1 })) assert(wait_until_stack({ SCREEN1, FOCUS1 }))
assert(wait_until_visible(FOCUS1))
assert(_G.focus1_gained) assert(_G.focus1_gained)
monarch.show(SCREEN1) monarch.show(SCREEN1)
assert(wait_until_stack({ SCREEN1, FOCUS1, SCREEN1 })) assert(wait_until_stack({ SCREEN1, FOCUS1, SCREEN1 }))
assert(wait_until_hidden(FOCUS1))
assert(_G.focus1_lost) assert(_G.focus1_lost)
end) end)
@ -483,14 +482,14 @@ return function()
-- proxy screen -- proxy screen
monarch.show(SCREEN1) monarch.show(SCREEN1)
wait_until_shown(SCREEN1) wait_until_visible(SCREEN1)
assert(monarch.post(SCREEN1, "foobar"), "Expected monarch.post() to return true") assert(monarch.post(SCREEN1, "foobar"), "Expected monarch.post() to return true")
cowait(0.1) cowait(0.1)
assert(_G.screen1_foobar, "Screen1 never received a message") assert(_G.screen1_foobar, "Screen1 never received a message")
-- factory screen -- factory screen
monarch.show(SCREEN2) monarch.show(SCREEN2)
wait_until_shown(SCREEN2) wait_until_visible(SCREEN2)
assert(monarch.post(SCREEN2, "foobar"), "Expected monarch.post() to return true") assert(monarch.post(SCREEN2, "foobar"), "Expected monarch.post() to return true")
cowait(0.1) cowait(0.1)
assert(_G.screen2_foobar, "Screen2 never received a message") assert(_G.screen2_foobar, "Screen2 never received a message")
@ -503,7 +502,7 @@ return function()
-- proxy screen -- proxy screen
monarch.show(SCREEN1) monarch.show(SCREEN1)
wait_until_shown(SCREEN1) wait_until_visible(SCREEN1)
assert(monarch.post(SCREEN1, "foobar", { foo = "bar" }), "Expected monarch.post() to return true") assert(monarch.post(SCREEN1, "foobar", { foo = "bar" }), "Expected monarch.post() to return true")
cowait(0.1) cowait(0.1)
assert(_G.screen1_foobar, "Screen1 never received a message") assert(_G.screen1_foobar, "Screen1 never received a message")
@ -511,7 +510,7 @@ return function()
-- factory screen -- factory screen
monarch.show(SCREEN2) monarch.show(SCREEN2)
wait_until_shown(SCREEN2) wait_until_visible(SCREEN2)
assert(monarch.post(SCREEN2, "foobar", { foo = "bar" }), "Expected monarch.post() to return true") assert(monarch.post(SCREEN2, "foobar", { foo = "bar" }), "Expected monarch.post() to return true")
cowait(0.1) cowait(0.1)
assert(_G.screen2_foobar, "Screen2 never received a message") assert(_G.screen2_foobar, "Screen2 never received a message")
@ -525,6 +524,7 @@ return function()
monarch.show(SCREEN1) monarch.show(SCREEN1)
monarch.show(SCREEN2) monarch.show(SCREEN2)
assert(wait_until_stack({ SCREEN1, SCREEN2 })) assert(wait_until_stack({ SCREEN1, SCREEN2 }))
assert(wait_until_hidden(SCREEN1))
local ok, err = monarch.post(SCREEN1, "foobar") local ok, err = monarch.post(SCREEN1, "foobar")
assert(not ok and err, "Expected monarch.post() to return false plus an error message") assert(not ok and err, "Expected monarch.post() to return false plus an error message")
cowait(0.1) cowait(0.1)
@ -534,7 +534,7 @@ return function()
it("should not be able to post messages to proxy screens without a receiver url", function() it("should not be able to post messages to proxy screens without a receiver url", function()
monarch.show(POPUP1) monarch.show(POPUP1)
wait_until_shown(POPUP1) wait_until_visible(POPUP1)
local ok, err = monarch.post(POPUP1, "foobar") local ok, err = monarch.post(POPUP1, "foobar")
assert(not ok and err, "Expected monarch.post() to return false plus an error message") assert(not ok and err, "Expected monarch.post() to return false plus an error message")
end) end)