@@ -8,13 +8,25 @@ local CONTEXT = hash("monarch_context")
8
8
local PROXY_LOADED = hash (" proxy_loaded" )
9
9
local PROXY_UNLOADED = hash (" proxy_unloaded" )
10
10
11
- M .TRANSITION_DONE = hash (" transition_done" )
11
+ local RELEASE_INPUT_FOCUS = hash (" release_input_focus" )
12
+ local ACQUIRE_INPUT_FOCUS = hash (" acquire_input_focus" )
13
+ local ASYNC_LOAD = hash (" async_load" )
14
+ local UNLOAD = hash (" unload" )
15
+ local ENABLE = hash (" enable" )
16
+
17
+ M .TRANSITION = {}
18
+ M .TRANSITION .DONE = hash (" transition_done" )
19
+ M .TRANSITION .SHOW_IN = hash (" transition_show_in" )
20
+ M .TRANSITION .SHOW_OUT = hash (" transition_show_out" )
21
+ M .TRANSITION .BACK_IN = hash (" transition_back_in" )
22
+ M .TRANSITION .BACK_OUT = hash (" transition_back_out" )
23
+
12
24
M .FOCUS_GAINED = hash (" monarch_focus_gained" )
13
25
M .FOCUS_LOST = hash (" monarch_focus_lost" )
14
26
15
27
16
28
local function screen_from_proxy (proxy )
17
- for _ ,screen in pairs (screens ) do
29
+ for _ , screen in pairs (screens ) do
18
30
if screen .proxy == proxy then
19
31
return screen
20
32
end
23
35
24
36
local function screen_from_script ()
25
37
local url = msg .url ()
26
- for _ ,screen in pairs (screens ) do
38
+ for _ , screen in pairs (screens ) do
27
39
if screen .script == url then
28
40
return screen
29
41
end
30
42
end
31
43
end
32
44
33
45
local function in_stack (id )
34
- for i = 1 , # stack do
46
+ for i = 1 , # stack do
35
47
if stack [i ].id == id then
36
48
return true
37
49
end
@@ -60,18 +72,18 @@ local function show_out(screen, next_screen, cb)
60
72
local co
61
73
co = coroutine.create (function ()
62
74
screen .co = co
63
- msg .post (screen .script , " release_input_focus " )
75
+ msg .post (screen .script , RELEASE_INPUT_FOCUS )
64
76
msg .post (screen .script , CONTEXT )
65
77
coroutine.yield ()
66
78
if not next_screen .popup then
67
- msg .post (screen .transition_url , " transition_show_out " )
79
+ msg .post (screen .transition_url , M . TRANSITION . SHOW_OUT )
68
80
coroutine.yield ()
69
- msg .post (screen .proxy , " unload " )
81
+ msg .post (screen .proxy , UNLOAD )
70
82
coroutine.yield ()
71
83
screen .loaded = false
72
84
end
73
85
if screen .focus_url then
74
- msg .post (screen .focus_url , M .FOCUS_LOST )
86
+ msg .post (screen .focus_url , M .FOCUS_LOST , { id = next_screen . id } )
75
87
end
76
88
screen .co = nil
77
89
if cb then cb () end
@@ -86,25 +98,25 @@ local function show_in(screen, reload, cb)
86
98
msg .post (screen .script , CONTEXT )
87
99
coroutine.yield ()
88
100
if reload and screen .loaded then
89
- msg .post (screen .proxy , " unload " )
101
+ msg .post (screen .proxy , UNLOAD )
90
102
coroutine.yield ()
91
103
screen .loaded = false
92
104
end
93
105
-- the screen could be loaded if the previous screen was a popup
94
106
-- and the popup asked to show this screen again
95
107
-- in that case we shouldn't attempt to load it again
96
108
if not screen .loaded then
97
- msg .post (screen .proxy , " async_load " )
109
+ msg .post (screen .proxy , ASYNC_LOAD )
98
110
coroutine.yield ()
99
- msg .post (screen .proxy , " enable " )
111
+ msg .post (screen .proxy , ENABLE )
100
112
screen .loaded = true
101
113
end
102
114
stack [# stack + 1 ] = screen
103
- msg .post (screen .transition_url , " transition_show_in " )
115
+ msg .post (screen .transition_url , M . TRANSITION . SHOW_IN )
104
116
coroutine.yield ()
105
- msg .post (screen .script , " acquire_input_focus " )
117
+ msg .post (screen .script , ACQUIRE_INPUT_FOCUS )
106
118
if screen .focus_url then
107
- msg .post (screen .focus_url , M .FOCUS_GAINED )
119
+ msg .post (screen .focus_url , M .FOCUS_GAINED , { id = screen . id } )
108
120
end
109
121
screen .co = nil
110
122
if cb then cb () end
@@ -119,16 +131,16 @@ local function back_in(screen, previous_screen, cb)
119
131
msg .post (screen .script , CONTEXT )
120
132
coroutine.yield ()
121
133
if not previous_screen .popup then
122
- msg .post (screen .proxy , " async_load " )
134
+ msg .post (screen .proxy , ASYNC_LOAD )
123
135
coroutine.yield ()
124
- msg .post (screen .proxy , " enable " )
136
+ msg .post (screen .proxy , ENABLE )
125
137
screen .loaded = true
126
- msg .post (screen .transition_url , " transition_back_in " )
138
+ msg .post (screen .transition_url , M . TRANSITION . BACK_IN )
127
139
coroutine.yield ()
128
140
end
129
- msg .post (screen .script , " acquire_input_focus " )
141
+ msg .post (screen .script , ACQUIRE_INPUT_FOCUS )
130
142
if screen .focus_url then
131
- msg .post (screen .focus_url , M .FOCUS_GAINED )
143
+ msg .post (screen .focus_url , M .FOCUS_GAINED , { id = previous_screen . id } )
132
144
end
133
145
screen .co = nil
134
146
if cb then cb () end
@@ -140,16 +152,16 @@ local function back_out(screen, cb)
140
152
local co
141
153
co = coroutine.create (function ()
142
154
screen .co = co
143
- msg .post (screen .script , " release_input_focus " )
155
+ msg .post (screen .script , RELEASE_INPUT_FOCUS )
144
156
msg .post (screen .script , CONTEXT )
145
157
coroutine.yield ()
146
- msg .post (screen .transition_url , " transition_back_out " )
158
+ msg .post (screen .transition_url , M . TRANSITION . BACK_OUT )
147
159
coroutine.yield ()
148
- msg .post (screen .proxy , " unload " )
160
+ msg .post (screen .proxy , UNLOAD )
149
161
coroutine.yield ()
150
162
screen .loaded = false
151
163
if screen .focus_url then
152
- msg .post (screen .focus_url , M .FOCUS_LOST )
164
+ msg .post (screen .focus_url , M .FOCUS_LOST , { id = screen . id } )
153
165
end
154
166
screen .co = nil
155
167
if cb then cb () end
@@ -254,7 +266,7 @@ function M.on_message(message_id, message, sender)
254
266
local screen = screen_from_script ()
255
267
assert (screen , " Unable to find screen for current script url" )
256
268
coroutine.resume (screen .co )
257
- elseif message_id == M .TRANSITION_DONE then
269
+ elseif message_id == M .TRANSITION . DONE then
258
270
local screen = screen_from_script ()
259
271
assert (screen , " Unable to find screen for current script url" )
260
272
coroutine.resume (screen .co )
263
275
264
276
function M .dump_stack ()
265
277
local s = " "
266
- for i ,screen in ipairs (stack ) do
278
+ for i , screen in ipairs (stack ) do
267
279
s = s .. (" %d = %s\n " ):format (i , tostring (screen .id ))
268
280
end
269
281
return s
0 commit comments