Skip to content

Commit 76d4ca2

Browse files
committed
Added monarch.clear()
1 parent 9e81b3a commit 76d4ca2

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

README_API.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Hide a screen that has been shown using the `no_stack` option. If used on a scre
3333
* `success` (boolean) - True if the process of hiding the screen was started successfully.
3434

3535

36+
## monarch.clear([callback])
37+
Clear the stack of screens completely. Any visible screen will be hidden by navigating back out from them. This operation will be added to the queue if Monarch is busy.
38+
39+
**PARAMETERS**
40+
* `callback` (function) - Optional function to call when the stack has been cleared.
41+
42+
3643
## monarch.back([data], [callback])
3744
Go back to a previous Monarch screen. This operation will be added to the queue if Monarch is busy.
3845

game.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
title = Monarch
33
version = 0.9
4-
dependencies = https://github.com/britzl/deftest/archive/2.7.0.zip
4+
dependencies#0 = https://github.com/britzl/deftest/archive/2.7.0.zip
55

66
[bootstrap]
77
main_collection = /example/advanced/advanced.collectionc

monarch/monarch.lua

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ end
826826

827827
-- Hide a screen. The screen must either be at the top of the stack or
828828
-- visible but not added to the stack (through the no_stack option)
829-
-- @param id (string|hash) - Id of the screen to show
829+
-- @param id (string|hash) - Id of the screen to .hide
830830
-- @param cb (function) - Optional callback to invoke when the screen is hidden
831831
-- @return true if successfully hiding, false if busy or for some other reason unable to hide the screen
832832
function M.hide(id, cb)
@@ -863,6 +863,42 @@ function M.hide(id, cb)
863863
end
864864

865865

866+
867+
868+
-- Clear stack completely. Any visible screens will be hidden by navigating back out
869+
-- from them.
870+
-- @param cb (function) - Optional callback to invoke when the stack has been cleared
871+
function M.clear(cb)
872+
log("clear() queuing action")
873+
874+
queue_action(function(action_done, action_error)
875+
local co
876+
co = coroutine.create(function()
877+
878+
local callbacks = callback_tracker()
879+
880+
local top = stack[#stack]
881+
while top and top.visible do
882+
stack[#stack] = nil
883+
back_out(top, screen, WAIT_FOR_TRANSITION, callbacks.track())
884+
callbacks.yield_until_done()
885+
top = stack[#stack]
886+
end
887+
888+
while stack[#stack] do
889+
table.remove(stack)
890+
end
891+
892+
callbacks.when_done(function()
893+
pcallfn(cb)
894+
pcallfn(action_done)
895+
end)
896+
end)
897+
assert(coroutine.resume(co))
898+
end)
899+
end
900+
901+
866902
-- Go back to the previous screen in the stack.
867903
-- @param data (*) - Optional data to set for the previous screen
868904
-- @param cb (function) - Optional callback to invoke when the previous screen is visible again

0 commit comments

Comments
 (0)