Skip to content

Commit bb74090

Browse files
authored
Merge pull request #140 from boydm/update_callbacks
Update callbacks and new push model
2 parents ffdf8f1 + e4028f9 commit bb74090

29 files changed

+986
-520
lines changed

lib/scenic/cache/base.ex

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ defmodule Scenic.Cache.Base do
119119
# load the asset into the cache (run time)
120120
Scenic.Cache.Static.Texture.load(@asset_path, @asset_hash)
121121
122-
# push the graph. (run time)
123-
push_graph(@graph)
124-
125-
{:ok, @graph}
122+
{:ok, :some_state, push: @graph}
126123
end
127124
128125
end
@@ -284,10 +281,7 @@ defmodule Scenic.Cache.Base do
284281
# load the asset into the cache (run time)
285282
Scenic.Cache.File.load(@asset_path, @asset_hash)
286283
287-
# push the graph. (run time)
288-
push_graph(@graph)
289-
290-
{:ok, @graph}
284+
{:ok, :some_state, push: @graph}
291285
end
292286
293287
end

lib/scenic/cache/support/file.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ defmodule Scenic.Cache.Support.File do
7373
# load the asset into the texture cache (at run time)
7474
Scenic.Cache.Static.Texture.load(@asset_path, @asset_hash)
7575
76-
# push the graph.
77-
push_graph(@graph)
78-
79-
{:ok, @graph}
76+
{:ok, :some_state, push: @graph}
8077
end
8178
8279
end

lib/scenic/cache/support/hash.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ defmodule Scenic.Cache.Support.Hash do
4040

4141
# --------------------------------------------------------
4242
@doc false
43-
@deprecated "Cache.valid_hash_types/0 will be removed in 0.10.0"
4443
@spec valid_hash_types() :: [:ripemd160 | :sha | :sha224 | :sha256 | :sha384 | :sha512, ...]
4544
def valid_hash_types(), do: @hash_types
4645

lib/scenic/component.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ defmodule Scenic.Component do
5757
use Scenic.Component, has_children: false
5858
5959
Setting `has_children` to `false` this will do two things. First, it won't create
60-
a dynamic supervisor for this scene, which saves some resources. Second,
61-
`push_graph/1` goes through a fast pass that doesn't scan the graph for dynamic children.
60+
a dynamic supervisor for this scene, which saves some resources.
6261
6362
For example, the Button component sets `has_children` to `false`.
6463

lib/scenic/component/button.ex

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ defmodule Scenic.Component.Button do
160160
id: id
161161
}
162162

163-
push_graph(graph)
164-
165-
{:ok, state}
163+
{:ok, state, push: graph}
166164
end
167165

168166
defp do_aligned_text(graph, :center, text, fill, width, vpos) do
@@ -214,8 +212,7 @@ defmodule Scenic.Component.Button do
214212
} = state
215213
) do
216214
state = Map.put(state, :contained, true)
217-
update_color(state)
218-
{:noreply, state}
215+
{:noreply, state, push: update_color(state)}
219216
end
220217

221218
# --------------------------------------------------------
@@ -227,8 +224,7 @@ defmodule Scenic.Component.Button do
227224
} = state
228225
) do
229226
state = Map.put(state, :contained, false)
230-
update_color(state)
231-
{:noreply, state}
227+
{:noreply, state, push: update_color(state)}
232228
end
233229

234230
# --------------------------------------------------------
@@ -242,7 +238,7 @@ defmodule Scenic.Component.Button do
242238

243239
ViewPort.capture_input(context, [:cursor_button, :cursor_pos])
244240

245-
{:noreply, state}
241+
{:noreply, state, push: update_color(state)}
246242
end
247243

248244
# --------------------------------------------------------
@@ -260,7 +256,7 @@ defmodule Scenic.Component.Button do
260256
send_event({:click, id})
261257
end
262258

263-
{:noreply, state}
259+
{:noreply, state, push: update_color(state)}
264260
end
265261

266262
# --------------------------------------------------------
@@ -276,29 +272,25 @@ defmodule Scenic.Component.Button do
276272
p
277273
|> Primitive.put_style(:fill, theme.background)
278274
end)
279-
|> push_graph()
280275
end
281276

282277
defp update_color(%{graph: graph, theme: theme, pressed: false, contained: true}) do
283278
Graph.modify(graph, :btn, fn p ->
284279
p
285280
|> Primitive.put_style(:fill, theme.background)
286281
end)
287-
|> push_graph()
288282
end
289283

290284
defp update_color(%{graph: graph, theme: theme, pressed: true, contained: false}) do
291285
Graph.modify(graph, :btn, fn p ->
292286
p
293287
|> Primitive.put_style(:fill, theme.background)
294288
end)
295-
|> push_graph()
296289
end
297290

298291
defp update_color(%{graph: graph, theme: theme, pressed: true, contained: true}) do
299292
Graph.modify(graph, :btn, fn p ->
300293
Primitive.put_style(p, :fill, theme.active)
301294
end)
302-
|> push_graph()
303295
end
304296
end

lib/scenic/component/input/caret.ex

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ defmodule Scenic.Component.Input.Caret do
8282
hidden: true,
8383
id: :caret
8484
)
85-
|> push_graph()
8685

8786
state = %{
8887
graph: graph,
@@ -91,39 +90,33 @@ defmodule Scenic.Component.Input.Caret do
9190
focused: false
9291
}
9392

94-
{:ok, state}
93+
{:ok, state, push: graph}
9594
end
9695

9796
# --------------------------------------------------------
9897
@doc false
9998
def handle_cast(:start_caret, %{graph: graph, timer: nil} = state) do
10099
# turn on the caret
101-
graph =
102-
graph
103-
|> Graph.modify(:caret, &update_opts(&1, hidden: false))
104-
|> push_graph()
100+
graph = Graph.modify(graph, :caret, &update_opts(&1, hidden: false))
105101

106102
# start the timer
107103
{:ok, timer} = :timer.send_interval(@caret_ms, :blink)
108104

109-
{:noreply, %{state | graph: graph, hidden: false, timer: timer, focused: true}}
105+
{:noreply, %{state | graph: graph, hidden: false, timer: timer, focused: true}, push: graph}
110106
end
111107

112108
# --------------------------------------------------------
113109
def handle_cast(:stop_caret, %{graph: graph, timer: timer} = state) do
114110
# hide the caret
115-
graph =
116-
graph
117-
|> Graph.modify(:caret, &update_opts(&1, hidden: true))
118-
|> push_graph()
111+
graph = Graph.modify(graph, :caret, &update_opts(&1, hidden: true))
119112

120113
# stop the timer
121114
case timer do
122115
nil -> :ok
123116
timer -> :timer.cancel(timer)
124117
end
125118

126-
{:noreply, %{state | graph: graph, hidden: true, timer: nil, focused: false}}
119+
{:noreply, %{state | graph: graph, hidden: true, timer: nil, focused: false}, push: graph}
127120
end
128121

129122
# --------------------------------------------------------
@@ -132,18 +125,15 @@ defmodule Scenic.Component.Input.Caret do
132125
%{graph: graph, timer: timer, focused: true} = state
133126
) do
134127
# show the caret
135-
graph =
136-
graph
137-
|> Graph.modify(:caret, &update_opts(&1, hidden: false))
138-
|> push_graph()
128+
graph = Graph.modify(graph, :caret, &update_opts(&1, hidden: false))
139129

140130
# stop the timer
141131
if timer, do: :timer.cancel(timer)
142132

143133
# restart the timer
144134
{:ok, timer} = :timer.send_interval(@caret_ms, :blink)
145135

146-
{:noreply, %{state | graph: graph, hidden: false, timer: timer}}
136+
{:noreply, %{state | graph: graph, hidden: false, timer: timer}, push: graph}
147137
end
148138

149139
# --------------------------------------------------------
@@ -156,11 +146,8 @@ defmodule Scenic.Component.Input.Caret do
156146
# invert the hidden flag
157147
hidden = !hidden
158148

159-
graph =
160-
graph
161-
|> Graph.modify(:caret, &update_opts(&1, hidden: hidden))
162-
|> push_graph()
149+
graph = Graph.modify(graph, :caret, &update_opts(&1, hidden: hidden))
163150

164-
{:noreply, %{state | graph: graph, hidden: hidden}}
151+
{:noreply, %{state | graph: graph, hidden: hidden}, push: graph}
165152
end
166153
end

lib/scenic/component/input/checkbox.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,22 @@ defmodule Scenic.Component.Input.Checkbox do
150150
id: id
151151
}
152152

153-
push_graph(graph)
154-
155-
{:ok, state}
153+
{:ok, state, push: graph}
156154
end
157155

158156
# --------------------------------------------------------
159157
@doc false
160158
def handle_input({:cursor_enter, _uid}, _, %{pressed: true} = state) do
161159
state = Map.put(state, :contained, true)
162160
graph = update_graph(state)
163-
{:noreply, %{state | graph: graph}}
161+
{:noreply, %{state | graph: graph}, push: graph}
164162
end
165163

166164
# --------------------------------------------------------
167165
def handle_input({:cursor_exit, _uid}, _, %{pressed: true} = state) do
168166
state = Map.put(state, :contained, false)
169167
graph = update_graph(state)
170-
{:noreply, %{state | graph: graph}}
168+
{:noreply, %{state | graph: graph}, push: graph}
171169
end
172170

173171
# --------------------------------------------------------
@@ -181,7 +179,7 @@ defmodule Scenic.Component.Input.Checkbox do
181179

182180
ViewPort.capture_input(context, [:cursor_button, :cursor_pos])
183181

184-
{:noreply, %{state | graph: graph}}
182+
{:noreply, %{state | graph: graph}, push: graph}
185183
end
186184

187185
# --------------------------------------------------------
@@ -208,7 +206,7 @@ defmodule Scenic.Component.Input.Checkbox do
208206

209207
graph = update_graph(state)
210208

211-
{:noreply, %{state | graph: graph}}
209+
{:noreply, %{state | graph: graph}, push: graph}
212210
end
213211

214212
# --------------------------------------------------------
@@ -243,6 +241,5 @@ defmodule Scenic.Component.Input.Checkbox do
243241
false ->
244242
Graph.modify(graph, :chx, &Primitive.put_style(&1, :hidden, true))
245243
end
246-
|> push_graph()
247244
end
248245
end

lib/scenic/component/input/dropdown.ex

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,7 @@ defmodule Scenic.Component.Input.Dropdown do
279279
rotate_caret: rotate_caret
280280
}
281281

282-
push_graph(graph)
283-
284-
{:ok, state}
282+
{:ok, state, push: graph}
285283
end
286284

287285
# ============================================================================
@@ -312,15 +310,14 @@ defmodule Scenic.Component.Input.Dropdown do
312310
graph
313311
|> Graph.modify(@caret_id, &update_opts(&1, rotate: rotate_caret))
314312
|> Graph.modify(@dropbox_id, &update_opts(&1, hidden: false))
315-
|> push_graph()
316313

317314
state =
318315
state
319316
|> Map.put(:down, true)
320317
|> Map.put(:drop_time, :os.system_time(:milli_seconds))
321318
|> Map.put(:graph, graph)
322319

323-
{:noreply, state}
320+
{:noreply, state, push: graph}
324321
end
325322

326323
# ============================================================================
@@ -333,11 +330,9 @@ defmodule Scenic.Component.Input.Dropdown do
333330
%{down: true, items: items, graph: graph, selected_id: selected_id, theme: theme} = state
334331
) do
335332
# set the appropriate hilighting for each of the items
336-
graph =
337-
update_highlighting(graph, items, selected_id, id, theme)
338-
|> push_graph
333+
graph = update_highlighting(graph, items, selected_id, id, theme)
339334

340-
{:noreply, %{state | hover_id: id, graph: graph}}
335+
{:noreply, %{state | hover_id: id, graph: graph}, push: graph}
341336
end
342337

343338
# --------------------------------------------------------
@@ -347,11 +342,9 @@ defmodule Scenic.Component.Input.Dropdown do
347342
%{down: true, items: items, graph: graph, selected_id: selected_id, theme: theme} = state
348343
) do
349344
# set the appropriate hilighting for each of the items
350-
graph =
351-
update_highlighting(graph, items, selected_id, nil, theme)
352-
|> push_graph
345+
graph = update_highlighting(graph, items, selected_id, nil, theme)
353346

354-
{:noreply, %{state | hover_id: nil, graph: graph}}
347+
{:noreply, %{state | hover_id: nil, graph: graph}, push: graph}
355348
end
356349

357350
# --------------------------------------------------------
@@ -377,10 +370,8 @@ defmodule Scenic.Component.Input.Dropdown do
377370
# raise the dropdown
378371
|> Graph.modify(@caret_id, &update_opts(&1, rotate: @rotate_neutral))
379372
|> Graph.modify(@dropbox_id, &update_opts(&1, hidden: true))
380-
# push to the viewport
381-
|> push_graph()
382373

383-
{:noreply, %{state | down: false, graph: graph}}
374+
{:noreply, %{state | down: false, graph: graph}, push: graph}
384375
end
385376

386377
# --------------------------------------------------------
@@ -407,12 +398,11 @@ defmodule Scenic.Component.Input.Dropdown do
407398
|> update_highlighting(items, selected_id, nil, theme)
408399
|> Graph.modify(@caret_id, &update_opts(&1, rotate: @rotate_neutral))
409400
|> Graph.modify(@dropbox_id, &update_opts(&1, hidden: true))
410-
|> push_graph()
411401

412402
# release the input capture
413403
ViewPort.release_input(context, [:cursor_button, :cursor_pos])
414404

415-
{:noreply, %{state | down: false, hover_id: nil, graph: graph}}
405+
{:noreply, %{state | down: false, hover_id: nil, graph: graph}, push: graph}
416406
end
417407
end
418408

@@ -438,10 +428,8 @@ defmodule Scenic.Component.Input.Dropdown do
438428
# raise the dropdown
439429
|> Graph.modify(@caret_id, &update_opts(&1, rotate: @rotate_neutral))
440430
|> Graph.modify(@dropbox_id, &update_opts(&1, hidden: true))
441-
# push to the viewport
442-
|> push_graph()
443431

444-
{:noreply, %{state | down: false, graph: graph}}
432+
{:noreply, %{state | down: false, graph: graph}, push: graph}
445433
end
446434

447435
# --------------------------------------------------------
@@ -474,10 +462,8 @@ defmodule Scenic.Component.Input.Dropdown do
474462
# raise the dropdown
475463
|> Graph.modify(@caret_id, &update_opts(&1, rotate: @rotate_neutral))
476464
|> Graph.modify(@dropbox_id, &update_opts(&1, hidden: true))
477-
# push to the viewport
478-
|> push_graph()
479465

480-
{:noreply, %{state | down: false, graph: graph, selected_id: item_id}}
466+
{:noreply, %{state | down: false, graph: graph, selected_id: item_id}, push: graph}
481467
end
482468

483469
# ============================================================================

0 commit comments

Comments
 (0)