3
0
mirror of https://github.com/britzl/monarch.git synced 2025-11-26 10:50:55 +01:00

Compare commits

...

5 Commits
2.2.0 ... 2.4.0

Author SHA1 Message Date
Björn Ritzl
39628b75af Merge pull request #17 from britzl/resizefix
Replay transitions when the layout changes
2018-05-26 19:43:42 +02:00
Björn Ritzl
dbf1bdea9d Replay transitions when the layout changes 2018-05-25 15:33:51 +02:00
Björn Ritzl
d45005cd90 Added documentation about monarch.debug()
Fixes #16
2018-05-24 16:34:33 +02:00
Björn Ritzl
a701f6cd92 Fixed broken tests 2018-03-26 06:33:11 +02:00
Björn Ritzl
f811c1f306 Accept both hash and string as screen id 2018-03-24 12:08:16 +01:00
4 changed files with 48 additions and 12 deletions

View File

@@ -240,3 +240,7 @@ Check if a Monarch screen exists.
**RETURN**
* ```exists``` (boolean) - True if the screen exists.
### monarch.debug()
Enable verbose logging of the internals of Monarch.

View File

@@ -32,6 +32,15 @@ function M.debug()
log = print
end
-- use a lookup table for so we don't have to do "return (type(s) == "string" and hash(s) or s)"
-- every time
local hash_lookup = {}
local function tohash(s)
hash_lookup[s] = hash_lookup[s] or (type(s) == "string" and hash(s) or s)
return hash_lookup[s]
end
local function screen_from_proxy(proxy)
for _, screen in pairs(screens) do
if screen.proxy == proxy then
@@ -51,9 +60,11 @@ end
--- Check if a screen exists in the current screen stack
-- @param id
-- @param id (string|hash)
-- @return true of the screen is in the stack
function M.in_stack(id)
assert(id, "You must provide a screen id")
id = tohash(id)
for i = 1, #stack do
if stack[i].id == id then
return true
@@ -65,9 +76,11 @@ end
--- Check if a screen is at the top of the stack
-- (primarily used for unit tests, but could have a usecase outside tests)
-- @param id
-- @param id (string|hash)
-- @return true if the screen is at the top of the stack
function M.is_top(id)
assert(id, "You must provide a screen id")
id = tohash(id)
local top = stack[#stack]
return top and top.id == id or false
end
@@ -89,6 +102,8 @@ end
-- * focus_url - URL to a script that is to be notified of focus
-- lost/gained events
function M.register(id, proxy, settings)
assert(id, "You must provide a screen id")
id = tohash(id)
assert(not screens[id], ("There is already a screen registered with id %s"):format(tostring(id)))
assert(proxy, "You must provide a collection proxy URL")
local url = msg.url(proxy)
@@ -107,6 +122,8 @@ end
-- This is done automatically by the screen.script
-- @param id Id of the screen to unregister
function M.unregister(id)
assert(id, "You must provide a screen id")
id = tohash(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
screens[id] = nil
end
@@ -290,31 +307,35 @@ end
--- Get data associated with a screen
-- @param id Id of the screen to get data for
-- @param id (string|hash) Id of the screen to get data for
-- @return Data associated with the screen
function M.data(id)
assert(id, "You must provide a screen id")
id = tohash(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
return screens[id].data
end
--- Checks to see if a screen id is registered
-- @param id 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
function M.screen_exists(id)
assert(id, "You must provide a screen id")
id = tohash(id)
return screens[id] ~= nil
end
--- Show a new screen
-- @param id Id of the screen to show
-- @param options Table with options when showing the screen (can be nil). Valid values:
-- @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:
-- * clear - Set to true if the stack should be cleared down to an existing instance of the screen
-- * reload - Set to true if screen should be reloaded if it already exists in the stack and is loaded.
-- This would be the case if doing a show() from a popup on the screen just below the popup.
-- @param data Optional data to set on the screen. Can be retrieved by the data() function
-- @param cb Optional callback to invoke when screen is shown
-- @param data (*) - Optional data to set on the screen. Can be retrieved by the data() function
-- @param cb (function) - Optional callback to invoke when screen is shown
function M.show(id, options, data, cb)
assert(id, "You must provide a screen id")
id = tohash(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
local screen = screens[id]
@@ -362,8 +383,8 @@ end
-- Go back to the previous screen in the stack
-- @param data Optional data to set for the previous screen
-- @param cb Optional callback to invoke when the previous screen is visible again
-- @param data (*) - Optional data to set for the previous screen
-- @param cb (function) - Optional callback to invoke when the previous screen is visible again
function M.back(data, cb)
local screen = table.remove(stack)
if screen then

View File

@@ -96,6 +96,8 @@ function M.create(node)
local transitions = {}
local current_transition = nil
local initial_data = {}
initial_data.pos = gui.get_position(node)
initial_data.scale = gui.get_scale(node)
@@ -114,6 +116,7 @@ function M.create(node)
local function start_transition(transition, url)
table.insert(transition.urls, url)
if not transition.in_progress then
current_transition = transition
transition.in_progress = true
transition.fn(node, initial_data, transition.easing, transition.duration, transition.delay or 0, function()
transition.in_progress = false
@@ -129,6 +132,12 @@ function M.create(node)
function instance.handle(message_id, message, sender)
if message_id == LAYOUT_CHANGED then
initial_data.pos = gui.get_position(node)
-- replay the current transition if the layout changes
-- this will ensure that things are still hidden if they
-- were transitioned out
if current_transition then
current_transition.fn(node, initial_data, current_transition.easing, 0, 0)
end
else
local transition = transitions[message_id]
if transition then

View File

@@ -1,7 +1,8 @@
local cowait = require "test.cowait"
local monarch = require "monarch.monarch"
local SCREEN1 = hash("screen1")
local SCREEN1_STR = hash("screen1")
local SCREEN1 = hash(SCREEN1_STR)
local SCREEN2 = hash("screen2")
local POPUP1 = hash("popup1")
local POPUP2 = hash("popup2")
@@ -65,12 +66,13 @@ return function()
it("should be able to tell if a screen exists", function()
assert(monarch.screen_exists(SCREEN1))
assert(monarch.screen_exists(SCREEN1_STR))
assert(not monarch.screen_exists(hash("foobar")))
end)
it("should be able to show screens and go back to previous screens", function()
monarch.show(SCREEN1)
monarch.show(SCREEN1_STR)
assert(wait_until_shown(SCREEN1), "Screen1 was never shown")
assert_stack({ SCREEN1 })