Skip to content

Commit 479b90f

Browse files
committed
improve caret and tests
1 parent 8bfa249 commit 479b90f

File tree

3 files changed

+66
-66
lines changed

3 files changed

+66
-66
lines changed

lib/scenic/component/input/caret.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ defmodule Scenic.Component.Input.Caret do
1818
@width 2
1919
@inset_v 4
2020

21-
# carat blink speed in hertz
22-
@carat_hz 1.5
23-
@carat_ms trunc(@carat_hz * 500)
21+
# caret blink speed in hertz
22+
@caret_hz 1.5
23+
@caret_ms trunc(@caret_hz * 500)
2424

2525
# ============================================================================
2626
# setup
@@ -70,22 +70,22 @@ defmodule Scenic.Component.Input.Caret do
7070
end
7171

7272
# --------------------------------------------------------
73-
def handle_cast(:gain_focus, %{graph: graph, timer: nil} = state) do
74-
# turn on the carat
73+
def handle_cast(:start_caret, %{graph: graph, timer: nil} = state) do
74+
# turn on the caret
7575
graph =
7676
graph
7777
|> Graph.modify(:caret, &update_opts(&1, hidden: false))
7878
|> push_graph()
7979

8080
# start the timer
81-
{:ok, timer} = :timer.send_interval(@carat_ms, :blink)
81+
{:ok, timer} = :timer.send_interval(@caret_ms, :blink)
8282

8383
{:noreply, %{state | graph: graph, hidden: false, timer: timer, focused: true}}
8484
end
8585

8686
# --------------------------------------------------------
87-
def handle_cast(:lose_focus, %{graph: graph, timer: timer} = state) do
88-
# hide the carat
87+
def handle_cast(:stop_caret, %{graph: graph, timer: timer} = state) do
88+
# hide the caret
8989
graph =
9090
graph
9191
|> Graph.modify(:caret, &update_opts(&1, hidden: true))
@@ -102,10 +102,10 @@ defmodule Scenic.Component.Input.Caret do
102102

103103
# --------------------------------------------------------
104104
def handle_cast(
105-
:reset_carat,
105+
:reset_caret,
106106
%{graph: graph, timer: timer, focused: true} = state
107107
) do
108-
# show the carat
108+
# show the caret
109109
graph =
110110
graph
111111
|> Graph.modify(:caret, &update_opts(&1, hidden: false))
@@ -115,7 +115,7 @@ defmodule Scenic.Component.Input.Caret do
115115
if timer, do: :timer.cancel(timer)
116116

117117
# restart the timer
118-
{:ok, timer} = :timer.send_interval(@carat_ms, :blink)
118+
{:ok, timer} = :timer.send_interval(@caret_ms, :blink)
119119

120120
{:noreply, %{state | graph: graph, hidden: false, timer: timer}}
121121
end

lib/scenic/component/input/text_field.ex

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule Scenic.Component.Input.TextField do
8585

8686
# theme is passed in as an inherited style
8787
theme =
88-
(styles[:theme] || Theme.preset(:dark))
88+
(styles[:theme] || Theme.preset_caret(:dark))
8989
|> Theme.normalize()
9090

9191
# get the text_field specific styles
@@ -131,7 +131,7 @@ defmodule Scenic.Component.Input.TextField do
131131
t: {0, @default_font_size},
132132
id: :text
133133
)
134-
|> Caret.add_to_graph({height, theme.text}, id: :carat)
134+
|> Caret.add_to_graph({height, theme.text}, id: :caret)
135135
end,
136136
t: {@inset_x, 0}
137137
)
@@ -142,7 +142,7 @@ defmodule Scenic.Component.Input.TextField do
142142
id: :border
143143
)
144144
|> update_text(display, state)
145-
|> update_carat(display, index)
145+
|> update_caret(display, index)
146146
|> push_graph()
147147

148148
{:ok, %{state | graph: graph}}
@@ -164,12 +164,12 @@ defmodule Scenic.Component.Input.TextField do
164164

165165
# --------------------------------------------------------
166166
# current value string is empty. show the hint string
167-
# defp update_carat( graph, state ) do
168-
# x = calc_carat_x( state )
169-
# Graph.modify( graph, :carat, &update_opts(&1, t: {x,0}) )
167+
# defp update_caret( graph, state ) do
168+
# x = calc_caret_x( state )
169+
# Graph.modify( graph, :caret, &update_opts(&1, t: {x,0}) )
170170
# end
171171

172-
defp update_carat(graph, value, index) do
172+
defp update_caret(graph, value, index) do
173173
str_len = String.length(value)
174174

175175
# double check the postition
@@ -180,25 +180,25 @@ defmodule Scenic.Component.Input.TextField do
180180
true -> index
181181
end
182182

183-
# calc the carat position
183+
# calc the caret position
184184
x = index * @char_width
185185

186-
# move the carat
187-
Graph.modify(graph, :carat, &update_opts(&1, t: {x, 0}))
186+
# move the caret
187+
Graph.modify(graph, :caret, &update_opts(&1, t: {x, 0}))
188188
end
189189

190190
# --------------------------------------------------------
191191
defp capture_focus(context, %{focused: false, graph: graph, theme: theme} = state) do
192192
# capture the input
193193
ViewPort.capture_input(context, @input_capture)
194194

195-
# start animating the carat
196-
Scene.cast_to_refs(nil, :gain_focus)
195+
# start animating the caret
196+
Scene.cast_to_refs(nil, :start_caret)
197197

198-
# show the carat
198+
# show the caret
199199
graph =
200200
graph
201-
|> Graph.modify(:carat, &update_opts(&1, hidden: false))
201+
|> Graph.modify(:caret, &update_opts(&1, hidden: false))
202202
|> Graph.modify(:border, &update_opts(&1, stroke: {2, theme.focus}))
203203
|> push_graph()
204204

@@ -213,13 +213,13 @@ defmodule Scenic.Component.Input.TextField do
213213
# release the input
214214
ViewPort.release_input(context, @input_capture)
215215

216-
# stop animating the carat
217-
Scene.cast_to_refs(nil, :lose_focus)
216+
# stop animating the caret
217+
Scene.cast_to_refs(nil, :stop_caret)
218218

219-
# hide the carat
219+
# hide the caret
220220
graph =
221221
graph
222-
|> Graph.modify(:carat, &update_opts(&1, hidden: true))
222+
|> Graph.modify(:caret, &update_opts(&1, hidden: true))
223223
|> Graph.modify(:border, &update_opts(&1, stroke: {2, theme.border}))
224224
|> push_graph()
225225

@@ -306,11 +306,11 @@ defmodule Scenic.Component.Input.TextField do
306306
{index, graph}
307307

308308
i ->
309-
# reset the carat blinker
310-
Scene.cast_to_refs(nil, :reset_carat)
311-
# move the carat
309+
# reset_caret the caret blinker
310+
Scene.cast_to_refs(nil, :reset_caret)
311+
# move the caret
312312
graph =
313-
update_carat(graph, value, i)
313+
update_caret(graph, value, i)
314314
|> push_graph()
315315

316316
{i, graph}
@@ -351,13 +351,13 @@ defmodule Scenic.Component.Input.TextField do
351351
{0, graph}
352352

353353
i ->
354-
# reset the carat blinker
355-
Scene.cast_to_refs(nil, :reset_carat)
356-
# move the carat
354+
# reset_caret the caret blinker
355+
Scene.cast_to_refs(nil, :reset_caret)
356+
# move the caret
357357
i = i - 1
358358

359359
graph =
360-
update_carat(graph, value, i)
360+
update_caret(graph, value, i)
361361
|> push_graph()
362362

363363
{i, graph}
@@ -372,7 +372,7 @@ defmodule Scenic.Component.Input.TextField do
372372
_context,
373373
%{index: index, value: value, graph: graph} = state
374374
) do
375-
# the max position for the carat
375+
# the max position for the caret
376376
max_index = String.length(value)
377377

378378
# move left. clamp to 0
@@ -382,13 +382,13 @@ defmodule Scenic.Component.Input.TextField do
382382
{index, graph}
383383

384384
i ->
385-
# reset the carat blinker
386-
Scene.cast_to_refs(nil, :reset_carat)
387-
# move the carat
385+
# reset the caret blinker
386+
Scene.cast_to_refs(nil, :reset_caret_caret)
387+
# move the caret
388388
i = i + 1
389389

390390
graph =
391-
update_carat(graph, value, i)
391+
update_caret(graph, value, i)
392392
|> push_graph()
393393

394394
{i, graph}
@@ -414,11 +414,11 @@ defmodule Scenic.Component.Input.TextField do
414414
{index, graph}
415415

416416
_ ->
417-
# reset the carat blinker
418-
Scene.cast_to_refs(nil, :reset_carat)
419-
# move the carat
417+
# reset the caret blinker
418+
Scene.cast_to_refs(nil, :reset_caret)
419+
# move the caret
420420
graph =
421-
update_carat(graph, value, 0)
421+
update_caret(graph, value, 0)
422422
|> push_graph()
423423

424424
{0, graph}
@@ -437,7 +437,7 @@ defmodule Scenic.Component.Input.TextField do
437437
_context,
438438
%{index: index, value: value, graph: graph} = state
439439
) do
440-
# the max position for the carat
440+
# the max position for the caret
441441
max_index = String.length(value)
442442

443443
# move left. clamp to 0
@@ -447,11 +447,11 @@ defmodule Scenic.Component.Input.TextField do
447447
{index, graph}
448448

449449
_ ->
450-
# reset the carat blinker
451-
Scene.cast_to_refs(nil, :reset_carat)
452-
# move the carat
450+
# reset the caret blinker
451+
Scene.cast_to_refs(nil, :reset_caret)
452+
# move the caret
453453
graph =
454-
update_carat(graph, value, max_index)
454+
update_caret(graph, value, max_index)
455455
|> push_graph()
456456

457457
{max_index, graph}
@@ -477,8 +477,8 @@ defmodule Scenic.Component.Input.TextField do
477477
id: id
478478
} = state
479479
) do
480-
# reset the carat blinker
481-
Scene.cast_to_refs(nil, :reset_carat)
480+
# reset_caret the caret blinker
481+
Scene.cast_to_refs(nil, :reset_caret)
482482

483483
# delete the char to the left of the index
484484
value =
@@ -498,7 +498,7 @@ defmodule Scenic.Component.Input.TextField do
498498
graph =
499499
graph
500500
|> update_text(display, state)
501-
|> update_carat(display, index)
501+
|> update_caret(display, index)
502502
|> push_graph()
503503

504504
state =
@@ -523,8 +523,8 @@ defmodule Scenic.Component.Input.TextField do
523523
id: id
524524
} = state
525525
) do
526-
# reset the carat blinker
527-
Scene.cast_to_refs(nil, :reset_carat)
526+
# reset the caret blinker
527+
Scene.cast_to_refs(nil, :reset_caret)
528528

529529
# delete the char at the index
530530
value =
@@ -537,7 +537,7 @@ defmodule Scenic.Component.Input.TextField do
537537
# send the value changed event
538538
send_event({:value_changed, id, value})
539539

540-
# update the graph (the carat doesn't move)
540+
# update the graph (the caret doesn't move)
541541
graph =
542542
graph
543543
|> update_text(display, state)
@@ -591,8 +591,8 @@ defmodule Scenic.Component.Input.TextField do
591591
id: id
592592
} = state
593593
) do
594-
# reset the carat blinker
595-
Scene.cast_to_refs(nil, :reset_carat)
594+
# reset the caret blinker
595+
Scene.cast_to_refs(nil, :reset_caret)
596596

597597
# insert the char into the string at the index location
598598
{left, right} = String.split_at(value, index)
@@ -609,7 +609,7 @@ defmodule Scenic.Component.Input.TextField do
609609
graph =
610610
graph
611611
|> update_text(display, state)
612-
|> update_carat(display, index)
612+
|> update_caret(display, index)
613613
|> push_graph()
614614

615615
state =

test/scenic/component/input/caret_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,26 @@ defmodule Scenic.Component.Input.CaretTest do
6464
# ============================================================================
6565
# cast handlers
6666

67-
test ":gain_focus starts a timer and shows the carat" do
68-
{:noreply, state} = Caret.handle_cast(:gain_focus, @state)
67+
test ":gain_focus starts a timer and shows the caret" do
68+
{:noreply, state} = Caret.handle_cast(:start_caret, @state)
6969
assert state.graph == @graph_showing
7070
assert state.timer
7171
refute state.hidden
7272
assert state.focused
7373
end
7474

75-
test ":gain_focus stops the timer and hides the carat" do
76-
{:noreply, state} = Caret.handle_cast(:lose_focus, @state)
75+
test ":gain_focus stops the timer and hides the caret" do
76+
{:noreply, state} = Caret.handle_cast(:stop_caret, @state)
7777
assert state.graph == @graph_hidden
7878
assert state.timer == nil
7979
assert state.hidden
8080
refute state.focused
8181
end
8282

83-
test ":reset_carat resets the timer and shows the carat" do
83+
test ":reset_caret resets the timer and shows the caret" do
8484
old_timer = :timer.send_interval(1000, :blink)
8585
state = %{@state | timer: old_timer, focused: true}
86-
{:noreply, state} = Caret.handle_cast(:reset_carat, state)
86+
{:noreply, state} = Caret.handle_cast(:reset_caret, state)
8787
assert state.graph == @graph_showing
8888
assert state.timer != old_timer
8989
refute state.hidden

0 commit comments

Comments
 (0)