From 5bdc3e45405f9815dc27f16bd5c81cc0748b26e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Tue, 8 Aug 2023 22:53:00 +0200 Subject: [PATCH] Added option table to back() --- README_API.md | 7 ++++++- monarch/monarch.lua | 23 ++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README_API.md b/README_API.md index 38fcb10..e30f962 100644 --- a/README_API.md +++ b/README_API.md @@ -40,13 +40,18 @@ Clear the stack of screens completely. Any visible screen will be hidden by navi * `callback` (function) - Optional function to call when the stack has been cleared. -## monarch.back([data], [callback]) +## monarch.back([options], [data], [callback]) Go back to a previous Monarch screen. This operation will be added to the queue if Monarch is busy. **PARAMETERS** +* `options` (table) - Options when showing the new screen (see below). * `data` (table) - Optional data to associate with the screen you are going back to. Retrieve using `monarch.data()`. * `callback` (function) - Optional function to call when the previous screen is visible. +The options table can contain the following fields: + +* `sequential` (boolean) - If the `sequential` flag is set Monarch will start loading the screen only after the previous screen finished transitioning out. + ## monarch.preload(screen_id, [options], [callback]) Preload a Monarch screen. This will load but not enable the screen. This is useful for content heavy screens that you wish to be able to show without having to wait for it load. This operation will be added to the queue if Monarch is busy. diff --git a/monarch/monarch.lua b/monarch/monarch.lua index 3975619..097189b 100644 --- a/monarch/monarch.lua +++ b/monarch/monarch.lua @@ -965,21 +965,38 @@ end -- Go back to the previous screen in the stack. +-- @param options (table) - Table with options when backing out from the screen (can be nil). +-- Valid values: +-- * sequential - Set to true to wait for the current screen to hide itself out before starting the +-- back in transition even when transitioning to a different scene ID. + -- @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) +function M.back(options, data, cb) log("back() queuing action") + -- backwards compatibility with old version M.back(data, cb) + -- case when back(data, cb) + if type(data) == "function" then + cb = data + data = options + options = nil + -- case when back(data, nil) + elseif options ~= nil and data == nil and cb == nil then + data = options + options = nil + end queue_action(function(action_done) local callbacks = callback_tracker() - local back_cb = callbacks.track() local screen = table.remove(stack) if screen then log("back()", screen.id) local top = stack[#stack] -- if we go back to the same screen we need to first hide it -- and wait until it is hidden before we show it again - if top and screen.id == top.id then + local same_screen = top and top.id == screen.id + if same_screen or (options and options.sequential) then + local back_cb = callbacks.track() back_out(screen, top, WAIT_FOR_TRANSITION, function() if data then top.data = data