-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpopup_window.c
More file actions
614 lines (488 loc) · 18 KB
/
popup_window.c
File metadata and controls
614 lines (488 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*******************************************************************************
* FILENAME : popup_window.c
*
* DESCRIPTION :
* Displays a pop-up window with a PDC or image, some text, and an optional
* ActionBar. It also can auto-close after a customizable length of time.
* This could be used in any project.
*
* PUBLIC FUNCTIONS :
* PopupWindow *popup_window_create(void);
* void popup_window_destroy(PopupWindow *popup_window);
* void popup_window_push(PopupWindow *popup_window,
* bool animated);
* void popup_window_pop(PopupWindow *popup_window,
* bool animated);
* bool popup_window_get_topmost_window(PopupWindow
* *popup_window);
* void popup_window_set_auto_close_duration(PopupWindow
* *popup_window, int64_t duration);
* void popup_window_set_countdown_timer(PopupWindow
* *popup_window, CountdownTimer *countdown_timer);
* void popup_window_refresh(PopupWindow *popup_window);
* void popup_window_set_pdc(PopupWindow *popup_window,
* uint32_t resource_id, bool endless);
* int64_t popup_window_get_pdc_duration(PopupWindow
* *popup_window);
* void popup_window_set_image(PopupWindow *popup_window,
* uint32_t resource_id);
* void popup_window_set_title(PopupWindow *popup_window,
* const char *text);
* void popup_window_set_highlight_color(PopupWindow
* *popup_window, GColor color);
* void popup_window_add_action_bar(PopupWindow *popup_window);
* void popup_window_remove_action_bar(PopupWindow
* *popup_window);
* void popup_window_set_action_bar_callbacks(PopupWindow
* *popup_window, PopupWindowCallbacks callbacks);
* void popup_window_remove_action_bar_callbacks(PopupWindow
* *popup_window);
*
* AUTHOR : Eric Phillips START DATE : 07/13/15
*
*/
#include <pebble.h>
#include "popup_window.h"
#include "countdown_timer.h"
/*******************************************************************************
* CONSTANTS
*/
#define NUM_VIBE_INTERVALS 6
/*******************************************************************************
* MAIN LOCAL VARIABLES
*/
static AppTimer *s_app_timer = NULL;
/*******************************************************************************
* STRUCTURE DECLARATION
*/
/*
* the structure of a PopupWindow
*/
struct PopupWindow {
Window *window; //< main window
Layer *layer; //< drawing layer for PDC or image
TextLayer *text; //< displays title text
ActionBarLayer *action; //< optional action bar for dialogs
PopupWindowCallbacks callbacks; //< callbacks for optional ActionBar
GBitmap *snooze_icon, *stop_icon; //< icons for ActionBar
#ifndef PBL_PLATFORM_APLITE
GDrawCommandSequence *draw_sequence; //< draw command sequence
GDrawCommandFrame *draw_frame; //< current draw command
uint32_t frame_count; //< total number of frames in draw sequence
uint32_t frame_index; //< index of current frame (for endless)
bool endless; //< if PDC is endless, having no duration
#else
GBitmap *image; //< alternate image for Aplite
#endif
CountdownTimer *countdown_timer; //< timer associated with PopupWindow
int64_t set_time, close_time; //< time opened and time to close
bool action_visible; //< whether the ActionBar is visible
GColor highlight_color;
const char* title;
bool has_action_bar;
uint32_t pdc_resource;
};
/*******************************************************************************
* PRIVATE FUNCTIONS
*/
/*
* layer update proc
* PDC/image is drawn on here
*/
static void layer_update_proc(Layer *layer, GContext *ctx) {
// get DetailWindow pointer from layer data
Window* window = layer_get_window(layer);
PopupWindow *popup_window = window_get_user_data(window);
// draw current PDC frame or image
#ifndef PBL_PLATFORM_APLITE
if (popup_window->draw_sequence != NULL) {
gdraw_command_frame_draw(ctx, popup_window->draw_sequence,
popup_window->draw_frame, GPointZero);
}
#else
if (popup_window->image != NULL) {
graphics_draw_bitmap_in_rect(ctx, popup_window->image, gbitmap_get_bounds(popup_window->image));
}
#endif
}
/*
* resize the layers based on PDC or image size
* this centers the layers in the window, accounting for the potential
* ActionBarLayer
*/
static void layers_center_in_window(PopupWindow *popup_window) {
#ifdef PBL_ROUND
int16_t horiz_off = 0;
#else
int16_t horiz_off = ACTION_BAR_WIDTH;
#endif
GRect window_frame = layer_get_frame(window_get_root_layer(popup_window->window));
GRect text_frame = layer_get_frame(text_layer_get_layer(popup_window->text));
// size of the graphic (PDC or bitmap)
#ifndef PBL_PLATFORM_APLITE
GSize gfx_size = gdraw_command_sequence_get_bounds_size(popup_window->draw_sequence);
#else
GSize gfx_size = gbitmap_get_bounds(popup_window->image).size;
#endif
// keep the graphic vertically centered (its animations are authored to fit
// around this position) and place the text directly below it, clamped so it
// stays on screen on both short and tall displays
const int16_t gap = 5;
int16_t gfx_y = (window_frame.size.h - gfx_size.h) / 2;
#ifdef PBL_PLATFORM_APLITE
gfx_y -= 7; // preserve original aplite vertical offset
#endif
int16_t text_y = gfx_y + gfx_size.h + gap;
if (text_y + text_frame.size.h > window_frame.size.h) {
text_y = window_frame.size.h - text_frame.size.h;
}
GRect layer_frame = GRect(0, gfx_y, gfx_size.w, gfx_size.h);
if (popup_window->action_visible) {
layer_frame.origin.x = (window_frame.size.w - horiz_off) / 2 - gfx_size.w / 2;
} else {
layer_frame.origin.x = window_frame.size.w / 2 - gfx_size.w / 2;
}
layer_set_frame(popup_window->layer, layer_frame);
text_frame.origin.x = 0;
text_frame.origin.y = text_y;
text_frame.size.w = window_frame.size.w -
((popup_window->action_visible) ? horiz_off : 0);
layer_set_frame(text_layer_get_layer(popup_window->text), text_frame);
GRect text_bounds = text_frame;
text_bounds.origin.x = text_bounds.origin.y = 0;
layer_set_bounds(text_layer_get_layer(popup_window->text), text_bounds);
}
/*******************************************************************************
* CALLBACKS
*/
/*
* Vibe callback
*
* Timer finished vibration split into separate intervals
*/
static void app_timer_vibe_callback(void *data) {
int num_vibes_left = (int)data;
if (num_vibes_left != 0) {
// start vibration
static const uint32_t vibe_seg[] = {300, 200, 300, 200, 300};
const VibePattern pat_vibe = {
.durations = vibe_seg,
.num_segments = ARRAY_LENGTH(vibe_seg),
};
vibes_enqueue_custom_pattern(pat_vibe);
--num_vibes_left;
s_app_timer = app_timer_register(2300, app_timer_vibe_callback, (void*)num_vibes_left);
}
}
/*
* UP click handler callback
*
* snoozes the vibrating timer
*/
static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
PopupWindow *popup_window = (PopupWindow*)context;
if (popup_window->callbacks.up_click == NULL) {
return;
}
return popup_window->callbacks.up_click(popup_window->countdown_timer, context);
}
/*
* SELECT click handler callback
*
* nothing yet... here for completeness
*/
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
PopupWindow *popup_window = (PopupWindow*)context;
if (popup_window->callbacks.select_click == NULL) {
return;
}
return popup_window->callbacks.select_click(context);
}
/*
* DOWN click handler callback
*
* stops the vibrating timer
*/
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
PopupWindow *popup_window = (PopupWindow*)context;
if (popup_window->callbacks.down_click == NULL) {
return;
}
return popup_window->callbacks.down_click(context);
}
/*
* click configuration provider
*/
static void click_config_provider(void *context) {
window_set_click_context(BUTTON_ID_UP, context);
window_set_click_context(BUTTON_ID_SELECT, context);
window_set_click_context(BUTTON_ID_DOWN, context);
window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}
static void prv_window_load(Window* window){
PopupWindow *popup_window = window_get_user_data(window);
// get window parameters
popup_window->stop_icon = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_DISMISS);
popup_window->snooze_icon = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_SNOOZE);
// get window parameters
Layer *root = window_get_root_layer(popup_window->window);
GRect bounds = layer_get_frame(root);
// create layer
popup_window->layer = layer_create(bounds);
layer_set_clips(popup_window->layer, false);
layer_set_update_proc(popup_window->layer, layer_update_proc);
layer_add_child(root, popup_window->layer);
// text
#ifndef PBL_PLATFORM_APLITE
const int text_layer_origin_y = 125;
#else
const int text_layer_origin_y = 110;
#endif
popup_window->text = text_layer_create(GRect(0, text_layer_origin_y, bounds.size.w, 36));
text_layer_set_font(popup_window->text, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
text_layer_set_text_alignment(popup_window->text, GTextAlignmentCenter);
text_layer_set_background_color(popup_window->text, GColorClear);
text_layer_set_text(popup_window->text, popup_window->title);
layer_add_child(root, text_layer_get_layer(popup_window->text));
// create action bar
popup_window->action = action_bar_layer_create();
action_bar_layer_set_context(popup_window->action, popup_window);
action_bar_layer_set_click_config_provider(popup_window->action, click_config_provider);
action_bar_layer_set_icon(popup_window->action, BUTTON_ID_UP, popup_window->snooze_icon);
action_bar_layer_set_icon(popup_window->action, BUTTON_ID_DOWN, popup_window->stop_icon);
if (popup_window->action_visible) {
action_bar_layer_add_to_window(popup_window->action, popup_window->window);
}
// re-center layers
layers_center_in_window(popup_window);
window_set_background_color(popup_window->window, popup_window->highlight_color);
}
static void prv_window_unload(Window* window){
PopupWindow *popup_window = window_get_user_data(window);
action_bar_layer_destroy(popup_window->action);
text_layer_destroy(popup_window->text);
layer_destroy(popup_window->layer);
window_destroy(popup_window->window);
gbitmap_destroy(popup_window->snooze_icon);
gbitmap_destroy(popup_window->stop_icon);
#ifndef PBL_PLATFORM_APLITE
if (popup_window->draw_sequence != NULL) {
gdraw_command_sequence_destroy(popup_window->draw_sequence);
popup_window->draw_sequence = NULL;
}
#else
if (popup_window->image != NULL) {
gbitmap_destroy(popup_window->image);
popup_window->image = NULL;
}
#endif
popup_window->window = NULL;
app_timer_cancel(s_app_timer);
s_app_timer = NULL;
vibes_cancel();
}
/*******************************************************************************
* API FUNCTIONS
*/
/*
* create a new PopupWindow and return a pointer to it
* this includes creating all its children layers but
* does not push it onto the window stack
*/
PopupWindow *popup_window_create(void) {
PopupWindow *popup_window = (PopupWindow*)malloc(sizeof(PopupWindow));
// error handling
if (popup_window == NULL) {
// error handling
APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to create PopupWindow");
return NULL;
}
// zero some values
popup_window->countdown_timer = NULL;
popup_window->action_visible = false;
#ifndef PBL_PLATFORM_APLITE
popup_window->draw_sequence = NULL;
popup_window->draw_frame = NULL;
popup_window->frame_count = 0;
popup_window->frame_index = 0;
#else
popup_window->image = NULL;
#endif
return popup_window;
}
/*
* destroy a previously created PopupWindow
*/
void popup_window_destroy(PopupWindow *popup_window) {
if (popup_window != NULL) {
free(popup_window);
popup_window = NULL;
return;
}
// error handling
APP_LOG(APP_LOG_LEVEL_ERROR, "Attempted to free NULL PopupWindow");
}
/*
* push the PopupWindow's window onto the window stack
*/
void popup_window_push(PopupWindow *popup_window, bool animated) {
if (popup_window->window == NULL) {
popup_window->window = window_create();
window_set_user_data(popup_window->window, popup_window);
window_set_window_handlers(popup_window->window,
(WindowHandlers){
.load = prv_window_load,
.unload = prv_window_unload
});
}
if (popup_window->window) {
window_stack_push(popup_window->window, animated);
}
}
/*
* remove the PopupWindow's window from the window stack
*/
void popup_window_pop(PopupWindow *popup_window, bool animated) {
if (popup_window->window) {
window_stack_remove(popup_window->window, animated);
}
}
/*
* gets whether the PopupWindow's window is the topmost on the window stack
*/
bool popup_window_get_topmost_window(PopupWindow *popup_window) {
return window_stack_get_top_window() == popup_window->window;
}
/*
* set the time that the PopupWindow will automatically pop itself off the stack
* it will auto-pop when a refresh event is called after this time
*/
void popup_window_set_auto_close_duration(PopupWindow *popup_window, int64_t duration) {
popup_window->set_time = countdown_timer_get_epoch_ms();
if (duration > 0) {
popup_window->close_time = popup_window->set_time + duration;
}
else {
popup_window->close_time = 0;
}
}
/*
* sets up an app_timer callback to run multiple instances of a vibration pattern
*/
void popup_window_set_vibes() {
s_app_timer = app_timer_register(0, app_timer_vibe_callback, (void*)NUM_VIBE_INTERVALS);
}
/*
* sets the CountdownTimer associated with this PopupWindow
*/
void popup_window_set_countdown_timer(PopupWindow *popup_window, CountdownTimer *countdown_timer) {
popup_window->countdown_timer = countdown_timer;
}
/*
* refresh the PopupWindow and step any ongoing PDC animation
*/
void popup_window_refresh(PopupWindow *popup_window) {
int64_t current_time = countdown_timer_get_epoch_ms();
// check if it should auto-pop
if (popup_window->close_time != 0 && popup_window->close_time <= current_time) {
popup_window_pop(popup_window, true);
}
// step animation
#ifndef PBL_PLATFORM_APLITE
if (popup_window->endless) {
popup_window->draw_frame = gdraw_command_sequence_get_frame_by_index(
popup_window->draw_sequence, popup_window->frame_index);
} else {
popup_window->draw_frame = gdraw_command_sequence_get_frame_by_elapsed(
popup_window->draw_sequence, current_time - popup_window->set_time);
}
popup_window->frame_index++;
if (popup_window->frame_index >= popup_window->frame_count) {
popup_window->frame_index = 0;
}
#endif
// refresh
layer_mark_dirty(popup_window->layer);
}
#ifndef PBL_PLATFORM_APLITE
/*
* sets the PDC that will be displayed
* and resizes the layer containing it so that it is centered
*/
void popup_window_set_pdc(PopupWindow *popup_window, uint32_t resource_id, bool endless) {
// temp variable
GDrawCommandSequence *tmp_sequence = popup_window->draw_sequence;
// load PDC
popup_window->draw_sequence = gdraw_command_sequence_create_with_resource(resource_id);
popup_window->frame_count = gdraw_command_sequence_get_num_frames(popup_window->draw_sequence);
popup_window->frame_index = 0;
popup_window->endless = endless;
// destroy old pointer
if (tmp_sequence != NULL) {
gdraw_command_sequence_destroy(tmp_sequence);
}
}
/*
* gets the duration of the PDC currently assigned to the PopupWindow
* returns 0 if the PDC is looping
*/
int64_t popup_window_get_pdc_duration(PopupWindow *popup_window) {
return gdraw_command_sequence_get_total_duration(popup_window->draw_sequence);
}
#else
/*
* sets the image that will be displayed
* and resizes the layer containing it so that it is centered
*/
void popup_window_set_image(PopupWindow *popup_window, uint32_t resource_id) {
// check if old image and destroy
if (popup_window->image != NULL) {
gbitmap_destroy(popup_window->image);
}
// load image
popup_window->image = gbitmap_create_with_resource(resource_id);
}
#endif
/*
* sets the text to display at the bottom of the PopupWindow
*/
void popup_window_set_title(PopupWindow *popup_window, const char *text) {
popup_window->title = text;
}
/*
* set the background color of the window
*/
void popup_window_set_highlight_color(PopupWindow *popup_window, GColor color) {
popup_window->highlight_color = color;
}
/*
* adds the ActionBarLayer to the PopupWindow
* this ActionBarLayer was already created in the creation of the PopupWindow
* it is merely added and removed from the PopupWindow's window
*/
void popup_window_add_action_bar(PopupWindow *popup_window) {
popup_window->action_visible = true;
}
/*
* removes the ActionBarLayer to the PopupWindow
* this ActionBarLayer was already created in the creation of the PopupWindow
* it is merely added and removed from the PopupWindow's window
*/
void popup_window_remove_action_bar(PopupWindow *popup_window) {
popup_window->action_visible = false;
}
/*
* sets the callbacks associated with this PopupWindow
*/
void popup_window_set_action_bar_callbacks(PopupWindow *popup_window,
PopupWindowCallbacks callbacks) {
popup_window->callbacks = callbacks;
}
/*
* cleans up the callbacks associated with this PopupWindow
*/
void popup_window_remove_action_bar_callbacks(PopupWindow *popup_window) {
popup_window->callbacks = (PopupWindowCallbacks) { NULL };
}