Skip to content

Commit 445a392

Browse files
committed
events: Remove events unrelated to sliders
Using events for communication between components in the same screen seem to make things racy, simplify/sequentialize by using callbacks instead. Keep events only for external events like slider inputs.
1 parent f11ece5 commit 445a392

18 files changed

+216
-222
lines changed

src/ui/components/confirm.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,19 @@ typedef struct {
3131
void* user_data;
3232
} data_t;
3333

34-
static void _dispatch_confirm(component_t* self)
34+
static void _on_confirm(void* user_data)
3535
{
36+
component_t* self = (component_t*)user_data;
3637
data_t* data = (data_t*)self->data;
3738
if (data->callback) {
3839
data->callback(true, data->user_data);
3940
data->callback = NULL;
4041
}
4142
}
4243

43-
static void _on_event(const event_t* event, component_t* component)
44+
static void _on_cancel(void* user_data)
4445
{
45-
if (event->id == EVENT_CONFIRM) {
46-
_dispatch_confirm(component);
47-
}
48-
}
49-
50-
static void _on_confirm(component_t* component)
51-
{
52-
component_t* self = component->parent;
53-
_dispatch_confirm(self);
54-
}
55-
56-
static void _on_cancel(component_t* component)
57-
{
58-
component_t* self = component->parent;
46+
component_t* self = (component_t*)user_data;
5947
data_t* data = (data_t*)self->data;
6048
if (data->callback) {
6149
data->callback(false, data->user_data);
@@ -71,7 +59,7 @@ static void _on_cancel(component_t* component)
7159
static const component_functions_t _component_functions = {
7260
.cleanup = ui_util_component_cleanup,
7361
.render = ui_util_component_render_subcomponents,
74-
.on_event = _on_event,
62+
.on_event = NULL,
7563
};
7664

7765
/********************************** Create Instance **********************************/
@@ -154,17 +142,18 @@ component_t* confirm_create(
154142
// Create buttons
155143
if (!params->accept_only) {
156144
ui_util_add_sub_component(
157-
confirm, icon_button_create(slider_position, ICON_BUTTON_CROSS, _on_cancel));
145+
confirm, icon_button_create(slider_position, ICON_BUTTON_CROSS, _on_cancel, confirm));
158146
}
159147
if (params->longtouch) {
160-
ui_util_add_sub_component(confirm, confirm_gesture_create());
148+
ui_util_add_sub_component(confirm, confirm_gesture_create(_on_confirm, confirm));
161149
} else {
162150
ui_util_add_sub_component(
163151
confirm,
164152
icon_button_create(
165153
slider_position,
166154
params->accept_is_nextarrow ? ICON_BUTTON_NEXT : ICON_BUTTON_CHECK,
167-
_on_confirm));
155+
_on_confirm,
156+
confirm));
168157
}
169158

170159
return confirm;

src/ui/components/confirm_gesture.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef struct {
3636
bool confirmed; // Confirm event occurred
3737
uint16_t active_count; // Start at an offset to allow movement on first touch
3838
uint16_t bottom_arrow_slidein; // from zero to arrow height * SCALE
39+
void (*callback)(void* user_data);
40+
void* user_data;
3941
} confirm_data_t;
4042

4143
bool confirm_gesture_is_active(component_t* component)
@@ -84,9 +86,9 @@ static void _render(component_t* component)
8486

8587
// The user confirms when the top and bottom arrows touch
8688
if (y0 + arrow_height > y1 && !data->confirmed) {
87-
event_t event;
88-
event.id = EVENT_CONFIRM;
89-
emit_event(&event);
89+
if (data->callback) {
90+
data->callback(data->user_data);
91+
}
9092
data->confirmed = true;
9193
}
9294
}
@@ -155,7 +157,7 @@ static component_functions_t _component_functions = {
155157
/**
156158
* Creates a confirm_gesture component on the top slider.
157159
*/
158-
component_t* confirm_gesture_create(void)
160+
component_t* confirm_gesture_create(void (*callback)(void*), void* user_data)
159161
{
160162
confirm_data_t* data = malloc(sizeof(confirm_data_t));
161163
if (!data) {
@@ -167,6 +169,8 @@ component_t* confirm_gesture_create(void)
167169
data->confirmed = false;
168170
data->active_count = SCALE - 1;
169171
data->bottom_arrow_slidein = 0;
172+
data->callback = callback;
173+
data->user_data = user_data;
170174

171175
component_t* confirm_gesture = malloc(sizeof(component_t));
172176
if (!confirm_gesture) {

src/ui/components/confirm_gesture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Creates a confirm_gesture component on the top slider.
2525
* @param[in] parent The parent component.
2626
*/
27-
component_t* confirm_gesture_create(void);
27+
component_t* confirm_gesture_create(void (*callback)(void* user_data), void* user_data);
2828

2929
bool confirm_gesture_is_active(component_t* component);
3030

src/ui/components/confirm_transaction.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,20 @@ static void _render(component_t* component)
5050
}
5151
}
5252

53-
static void _on_event(const event_t* event, component_t* component)
53+
static void _cancel_cb(void* user_data)
5454
{
55-
if (event->id == EVENT_CONFIRM) {
56-
data_t* data = (data_t*)component->data;
57-
if (data->callback) {
58-
data->callback(true, data->user_data);
59-
data->callback = NULL;
60-
}
61-
}
62-
}
63-
64-
static void _cancel(component_t* cancel_button)
65-
{
66-
component_t* component = cancel_button->parent;
67-
data_t* data = (data_t*)component->data;
55+
component_t* self = (component_t*)user_data;
56+
data_t* data = (data_t*)self->data;
6857
if (data->callback != NULL) {
6958
data->callback(false, data->user_data);
7059
data->callback = NULL;
7160
}
7261
}
7362

74-
static void _confirm_button_cb(component_t* confirm_button)
63+
static void _confirm_cb(void* user_data)
7564
{
76-
component_t* component = confirm_button->parent;
77-
data_t* data = (data_t*)component->data;
65+
component_t* self = (component_t*)user_data;
66+
data_t* data = (data_t*)self->data;
7867
if (data->callback) {
7968
data->callback(true, data->user_data);
8069
data->callback = NULL;
@@ -89,7 +78,7 @@ static void _confirm_button_cb(component_t* confirm_button)
8978
static const component_functions_t _component_functions = {
9079
.cleanup = ui_util_component_cleanup,
9180
.render = _render,
92-
.on_event = _on_event,
81+
.on_event = NULL,
9382
};
9483

9584
/********************************** Create Instance **********************************/
@@ -128,13 +117,14 @@ static component_t* _confirm_transaction_create(
128117
confirm->dimension.width = SCREEN_WIDTH;
129118
confirm->dimension.height = SCREEN_HEIGHT;
130119

131-
ui_util_add_sub_component(confirm, icon_button_create(top_slider, ICON_BUTTON_CROSS, _cancel));
120+
ui_util_add_sub_component(
121+
confirm, icon_button_create(top_slider, ICON_BUTTON_CROSS, _cancel_cb, confirm));
132122

133123
if (longtouch) {
134-
ui_util_add_sub_component(confirm, confirm_gesture_create());
124+
ui_util_add_sub_component(confirm, confirm_gesture_create(_confirm_cb, confirm));
135125
} else {
136126
ui_util_add_sub_component(
137-
confirm, icon_button_create(top_slider, ICON_BUTTON_NEXT, _confirm_button_cb));
127+
confirm, icon_button_create(top_slider, ICON_BUTTON_NEXT, _confirm_cb, confirm));
138128
}
139129

140130
if (data->has_address) {

src/ui/components/icon_button.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ typedef struct {
3434
bool active; // Marker is 'active', i.e., touched
3535
uint16_t active_count;
3636
icon_button_type_t type;
37-
void (*callback)(component_t* component);
37+
void (*callback)(void* user_data);
38+
void* user_data;
3839
} data_t;
3940

4041
/**
@@ -153,7 +154,7 @@ static void _on_event(const event_t* event, component_t* component)
153154
case EVENT_TOP_SHORT_TAP:
154155
case EVENT_BOTTOM_SHORT_TAP:
155156
if (data->callback) {
156-
data->callback(component);
157+
data->callback(data->user_data);
157158
}
158159
break;
159160
default:
@@ -175,7 +176,8 @@ static component_functions_t _component_functions = {
175176
component_t* icon_button_create(
176177
slider_location_t location,
177178
icon_button_type_t type,
178-
void (*callback)(component_t* component))
179+
void (*callback)(void* user_data),
180+
void* user_data)
179181
{
180182
component_t* icon_button = malloc(sizeof(component_t));
181183
if (!icon_button) {
@@ -192,6 +194,7 @@ component_t* icon_button_create(
192194
data->active_count = SCALE - 1; // Start at an offset to allow movement on first touch
193195
data->type = type;
194196
data->callback = callback;
197+
data->user_data = user_data;
195198
icon_button->data = data;
196199
icon_button->f = &_component_functions;
197200
icon_button->parent = NULL; // Gets set by ui_util_add_sub_component() if `icon_button` is

src/ui/components/icon_button.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef enum {
3232
component_t* icon_button_create(
3333
slider_location_t location,
3434
icon_button_type_t type,
35-
void (*callback)(component_t* component));
35+
void (*callback)(void* user_data),
36+
void* user_data);
3637

3738
#endif

src/ui/components/keyboard_switch.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ typedef struct {
3434
bool active; // Marker is 'active', i.e., touched
3535
// if true, the special chars keyboard mode is available.
3636
bool special_chars;
37+
void (*on_keyboard_switch_cb)(keyboard_mode_t mode, void* user_data);
38+
void* user_data;
3739
} keyboard_switch_data_t;
3840

3941
/**
@@ -90,29 +92,6 @@ static void _on_event(const event_t* event, component_t* component)
9092
keyboard_switch_data_t* ks_data = (keyboard_switch_data_t*)component->data;
9193
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
9294
switch (event->id) {
93-
case EVENT_TOGGLE_ALPHANUMERIC:
94-
switch (ks_data->mode) {
95-
case LOWER_CASE:
96-
ks_data->mode = UPPER_CASE;
97-
break;
98-
case UPPER_CASE:
99-
ks_data->mode = DIGITS;
100-
break;
101-
case DIGITS:
102-
ks_data->mode = ks_data->special_chars ? SPECIAL_CHARS : LOWER_CASE;
103-
break;
104-
case SPECIAL_CHARS:
105-
ks_data->mode = LOWER_CASE;
106-
break;
107-
default:
108-
Abort("Keyboard mode unrecognized");
109-
break;
110-
}
111-
break;
112-
113-
case EVENT_UPDATE_ALPHANUMERIC:
114-
ks_data->mode = *(const keyboard_mode_t*)event->data;
115-
break;
11695
case EVENT_TOP_CONTINUOUS_TAP:
11796
if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD &&
11897
slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
@@ -124,9 +103,24 @@ static void _on_event(const event_t* event, component_t* component)
124103
if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD &&
125104
slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
126105
ks_data->active = false;
127-
event_t e;
128-
e.id = EVENT_TOGGLE_ALPHANUMERIC;
129-
emit_event(&e);
106+
switch (ks_data->mode) {
107+
case LOWER_CASE:
108+
ks_data->mode = UPPER_CASE;
109+
break;
110+
case UPPER_CASE:
111+
ks_data->mode = DIGITS;
112+
break;
113+
case DIGITS:
114+
ks_data->mode = ks_data->special_chars ? SPECIAL_CHARS : LOWER_CASE;
115+
break;
116+
case SPECIAL_CHARS:
117+
ks_data->mode = LOWER_CASE;
118+
break;
119+
default:
120+
Abort("Keyboard mode unrecognized");
121+
break;
122+
}
123+
ks_data->on_keyboard_switch_cb(ks_data->mode, ks_data->user_data);
130124
break;
131125
}
132126
/* FALLTHROUGH */
@@ -153,7 +147,9 @@ component_t* keyboard_switch_create(
153147
slider_location_t location,
154148
bool special_chars,
155149
bool default_to_digits,
156-
component_t* parent)
150+
component_t* parent,
151+
void (*on_keyboard_switch_cb)(keyboard_mode_t mode, void* user_data),
152+
void* user_data)
157153
{
158154
component_t* keyboard_switch = malloc(sizeof(component_t));
159155
if (!keyboard_switch) {
@@ -171,6 +167,8 @@ component_t* keyboard_switch_create(
171167
ks_data->mode = default_to_digits ? DIGITS : LOWER_CASE;
172168
ks_data->active = false;
173169
ks_data->special_chars = special_chars;
170+
ks_data->on_keyboard_switch_cb = on_keyboard_switch_cb;
171+
ks_data->user_data = user_data;
174172

175173
keyboard_switch->data = ks_data;
176174
keyboard_switch->f = &_component_functions;

src/ui/components/keyboard_switch.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ component_t* keyboard_switch_create(
3333
slider_location_t location,
3434
bool special_chars,
3535
bool default_to_digits,
36-
component_t* parent);
36+
component_t* parent,
37+
void (*on_keyboard_switch_cb)(keyboard_mode_t mode, void* user_data),
38+
void* user_data);
3739

3840
/**
3941
* @return the currently selected keyboard

src/ui/components/left_arrow.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
typedef struct {
3434
uint8_t location;
3535
bool active; // Marker is 'active', i.e., touched
36+
void (*callback)(void* user_data);
37+
void* user_data;
3638
} left_arrow_data_t;
3739

3840
/**
@@ -70,9 +72,9 @@ static void _on_event(const event_t* event, component_t* component)
7072
}
7173
if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) {
7274
data->active = false;
73-
event_t e;
74-
e.id = EVENT_BACKWARD;
75-
emit_event(&e);
75+
if (data->callback) {
76+
data->callback(data->user_data);
77+
}
7678
break;
7779
}
7880
/* FALLTHROUGH */
@@ -111,7 +113,11 @@ static component_functions_t _component_functions = {
111113
* @param[in] location whether the arrow should be rendered on top or bottom (top/bottom slider)
112114
* @param[in] parent The parent component.
113115
*/
114-
component_t* left_arrow_create(slider_location_t location, component_t* parent)
116+
component_t* left_arrow_create(
117+
slider_location_t location,
118+
component_t* parent,
119+
void (*callback)(void*),
120+
void* user_data)
115121
{
116122
left_arrow_data_t* data = malloc(sizeof(left_arrow_data_t));
117123
if (!data) {
@@ -120,6 +126,8 @@ component_t* left_arrow_create(slider_location_t location, component_t* parent)
120126
memset(data, 0, sizeof(left_arrow_data_t));
121127
data->location = location;
122128
data->active = false;
129+
data->callback = callback;
130+
data->user_data = user_data;
123131

124132
component_t* left_arrow = malloc(sizeof(component_t));
125133
if (!left_arrow) {

src/ui/components/left_arrow.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
* Creates a left arrow component.
2525
* param[in] location whether the arrow should be rendered on top or bottom (UPPER/LOWER slider)
2626
*/
27-
component_t* left_arrow_create(slider_location_t location, component_t* parent);
27+
component_t* left_arrow_create(
28+
slider_location_t location,
29+
component_t* parent,
30+
void (*callback)(void*),
31+
void* user_data);
2832

2933
#endif

0 commit comments

Comments
 (0)