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

Added monarch.top() and bottom()

This commit is contained in:
Björn Ritzl 2018-06-04 13:56:45 +02:00
parent 0fdfb6fd11
commit 66b1f7ca2e
3 changed files with 65 additions and 1 deletions

View File

@ -261,6 +261,26 @@ Preload a Monarch screen. This will load but not enable the screen. This is usef
* ```callback``` (function) - Optional function to call when the new screen is preloaded. * ```callback``` (function) - Optional function to call when the new screen is preloaded.
### monarch.top([offset])
Get the id of the screen at the top of the stack.
**PARAMETERS**
* ```offset``` (number) - Optional offset from the top of the stack, ie -1 to get the previous screen
**RETURN**
* ```screen_id``` (hash) - Id of the requested screen
### monarch.bottom([offset])
Get the id of the screen at the bottom of the stack.
**PARAMETERS**
* ```offset``` (number) - Optional offset from the bottom of the stack
**RETURN**
* ```screen_id``` (hash) - Id of the requested screen
### monarch.data(screen_id) ### monarch.data(screen_id)
Get the data associated with a screen (from a call to ```monarch.show()``` or ```monarch.back()```). Get the data associated with a screen (from a call to ```monarch.show()``` or ```monarch.back()```).

View File

@ -491,6 +491,25 @@ function M.get_stack()
return s return s
end end
--- Get the screen on top of the stack
-- @param offset Optional offset from the top of the stack, (eg -1 for the previous screen)
-- @return Id of the requested screen
function M.top(offset)
local screen = stack[#stack + (offset or 0)]
return screen and screen.id
end
--- Get the screen at the bottom of the stack
-- @param offset Optional offset from the bottom of the stack
-- @return Id of the requested screen
function M.bottom(offset)
local screen = stack[1 + (offset or 0)]
return screen and screen.id
end
function M.dump_stack() function M.dump_stack()
local s = "" local s = ""
for i, screen in ipairs(stack) do for i, screen in ipairs(stack) do

View File

@ -175,6 +175,31 @@ return function()
assert(wait_until_shown(SCREEN2), "Popup2 was never shown") assert(wait_until_shown(SCREEN2), "Popup2 was never shown")
assert_stack({ SCREEN1, SCREEN2 }) assert_stack({ SCREEN1, SCREEN2 })
end) end)
it("should be able to get the id of the screen at the top and bottom of the stack", function()
assert(monarch.top() == nil)
assert(monarch.bottom() == nil)
assert(monarch.top(1) == nil)
assert(monarch.bottom(-1) == nil)
monarch.show(SCREEN1)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert(monarch.top() == SCREEN1)
assert(monarch.top(0) == SCREEN1)
assert(monarch.top(1) == nil)
assert(monarch.bottom() == SCREEN1)
assert(monarch.bottom(0) == SCREEN1)
assert(monarch.bottom(-1) == nil)
monarch.show(SCREEN2)
assert(wait_until_hidden(SCREEN1), "Screen1 was never hidden")
assert(wait_until_shown(SCREEN2), "Screen2 was never shown")
assert_stack({ SCREEN1, SCREEN2 })
assert(monarch.top(0) == SCREEN2)
assert(monarch.top(-1) == SCREEN1)
assert(monarch.bottom(0) == SCREEN1)
assert(monarch.bottom(1) == SCREEN2)
end)
end) end)
end end