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

Changed how monarch busy state is tracked

This commit is contained in:
Björn Ritzl 2018-06-16 22:27:03 +02:00
parent 01196ad350
commit 091fab0c7f

View File

@ -27,8 +27,9 @@ local screens = {}
-- the current stack of screens -- the current stack of screens
local stack = {} local stack = {}
-- true while busy showing/hiding something -- the number of active transitions
local busy = false -- monarch is considered busy while there are active transitions
local active_transition_count = 0
local function log(...) end local function log(...) end
@ -230,7 +231,7 @@ local function show_out(screen, next_screen, cb)
log("show_out()", screen.id) log("show_out()", screen.id)
local co local co
co = coroutine.create(function() co = coroutine.create(function()
busy = true active_transition_count = active_transition_count + 1
screen.co = co screen.co = co
change_context(screen) change_context(screen)
release_input(screen) release_input(screen)
@ -244,7 +245,7 @@ local function show_out(screen, next_screen, cb)
unload(screen) unload(screen)
end end
screen.co = nil screen.co = nil
busy = false active_transition_count = active_transition_count - 1
if cb then cb() end if cb then cb() end
end) end)
coroutine.resume(co) coroutine.resume(co)
@ -254,7 +255,7 @@ local function show_in(screen, previous_screen, reload, cb)
log("show_in()", screen.id) log("show_in()", screen.id)
local co local co
co = coroutine.create(function() co = coroutine.create(function()
busy = true active_transition_count = active_transition_count + 1
screen.co = co screen.co = co
change_context(screen) change_context(screen)
if reload and screen.loaded then if reload and screen.loaded then
@ -279,7 +280,7 @@ local function show_in(screen, previous_screen, reload, cb)
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
screen.co = nil screen.co = nil
busy = false active_transition_count = active_transition_count - 1
if cb then cb() end if cb then cb() end
end) end)
coroutine.resume(co) coroutine.resume(co)
@ -289,7 +290,7 @@ local function back_in(screen, previous_screen, cb)
log("back_in()", screen.id) log("back_in()", screen.id)
local co local co
co = coroutine.create(function() co = coroutine.create(function()
busy = true active_transition_count = active_transition_count + 1
screen.co = co screen.co = co
change_context(screen) change_context(screen)
if screen.preloaded then if screen.preloaded then
@ -307,7 +308,7 @@ local function back_in(screen, previous_screen, cb)
acquire_input(screen) acquire_input(screen)
focus_gained(screen, previous_screen) focus_gained(screen, previous_screen)
screen.co = nil screen.co = nil
busy = false active_transition_count = active_transition_count - 1
if cb then cb() end if cb then cb() end
end) end)
coroutine.resume(co) coroutine.resume(co)
@ -317,7 +318,7 @@ local function back_out(screen, next_screen, cb)
log("back_out()", screen.id) log("back_out()", screen.id)
local co local co
co = coroutine.create(function() co = coroutine.create(function()
busy = true active_transition_count = active_transition_count + 1
screen.co = co screen.co = co
change_context(screen) change_context(screen)
release_input(screen) release_input(screen)
@ -325,7 +326,7 @@ local function back_out(screen, next_screen, cb)
transition(screen, M.TRANSITION.BACK_OUT, { next_screen = next_screen and next_screen.id }) transition(screen, M.TRANSITION.BACK_OUT, { next_screen = next_screen and next_screen.id })
unload(screen) unload(screen)
screen.co = nil screen.co = nil
busy = false active_transition_count = active_transition_count - 1
if cb then cb() end if cb then cb() end
end) end)
coroutine.resume(co) coroutine.resume(co)
@ -342,6 +343,7 @@ function M.data(id)
return screens[id].data return screens[id].data
end end
--- Checks to see if a screen id is registered --- Checks to see if a screen id is registered
-- @param id (string|hash) Id of the screen to check if is registered -- @param id (string|hash) Id of the screen to check if is registered
-- @return True or False if the screen id is registered or not -- @return True or False if the screen id is registered or not
@ -351,6 +353,14 @@ function M.screen_exists(id)
return screens[id] ~= nil return screens[id] ~= nil
end end
--- Check if Monarch is busy hiding and or showing a screen
-- @return true if busy
function M.is_busy()
return active_transition_count > 0
end
--- Show a new screen --- Show a new screen
-- @param id (string|hash) - Id of the screen to show -- @param id (string|hash) - Id of the screen to show
-- @param options (table) - Table with options when showing the screen (can be nil). Valid values: -- @param options (table) - Table with options when showing the screen (can be nil). Valid values:
@ -362,7 +372,7 @@ end
-- @return success True if screen is successfully shown, false if busy performing another operation -- @return success True if screen is successfully shown, false if busy performing another operation
function M.show(id, options, data, cb) function M.show(id, options, data, cb)
assert(id, "You must provide a screen id") assert(id, "You must provide a screen id")
if busy then if M.is_busy() then
return false return false
end end
@ -420,7 +430,7 @@ end
-- @param cb (function) - Optional callback to invoke when the previous screen is visible again -- @param cb (function) - Optional callback to invoke when the previous screen is visible again
-- @return true if successfully going back, false if busy performing another operation -- @return true if successfully going back, false if busy performing another operation
function M.back(data, cb) function M.back(data, cb)
if busy then if M.is_busy() then
return false return false
end end