-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathwindow.c
More file actions
569 lines (474 loc) · 18.8 KB
/
window.c
File metadata and controls
569 lines (474 loc) · 18.8 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
/* SPDX-FileCopyrightText: 2024 Google LLC */
/* SPDX-License-Identifier: Apache-2.0 */
#include "window_private.h"
#include "applib/app_logging.h"
#include "applib/graphics/graphics.h"
#include "applib/ui/app_window_click_glue.h"
#include "applib/ui/app_window_stack.h"
#include "applib/ui/click.h"
#include "applib/ui/layer.h"
#include "applib/ui/layer_private.h"
#include "applib/ui/window_manager.h"
#include "applib/ui/window_stack.h"
#include "applib/applib_malloc.auto.h"
#include "applib/legacy2/ui/status_bar_legacy2.h"
#include "kernel/ui/kernel_ui.h"
#include "kernel/ui/modals/modal_manager.h"
#include "process_management/process_manager.h"
#include "process_state/app_state/app_state.h"
#include "shell/system_theme.h"
#include "system/logging.h"
#include "system/passert.h"
#include "syscall/syscall.h"
#include "status_bar_layer.h"
#include <string.h>
typedef enum {
WindowHandlerOffsetLoad = offsetof(WindowHandlers, load),
WindowHandlerOffsetAppear = offsetof(WindowHandlers, appear),
WindowHandlerOffsetDisappear = offsetof(WindowHandlers, disappear),
WindowHandlerOffsetUnload = offsetof(WindowHandlers, unload),
} WindowHandlerOffset;
void window_do_layer_update_proc(Layer *layer, GContext* ctx) {
Window *window = layer_get_window(layer);
const GColor bg_color = window->background_color;
if (!gcolor_is_transparent(bg_color)) {
GDrawState prev_state = graphics_context_get_drawing_state(ctx);
graphics_context_set_fill_color(ctx, bg_color);
graphics_fill_rect(ctx, &layer->bounds);
graphics_context_set_drawing_state(ctx, prev_state);
}
}
//! helper struct to move displacement logic out of window_render()
typedef struct {
GPoint drawing_box_origin;
GRect clip_box;
} DrawingStateOrigins;
static void prv_adjust_drawing_state_for_legacy2_apps(DrawingStateOrigins *saved_state,
GContext *ctx, Window *window) {
GDrawState * const draw_state = &ctx->draw_state;
*saved_state = (DrawingStateOrigins){
.drawing_box_origin = draw_state->drawing_box.origin,
.clip_box = draw_state->clip_box,
};
const int16_t full_screen_displacement = window->is_fullscreen ? 0 : STATUS_BAR_HEIGHT;
draw_state->drawing_box.origin.y += full_screen_displacement;
draw_state->clip_box.origin.y += full_screen_displacement;
WindowStack *stack = window->parent_window_stack;
if (window_transition_context_has_legacy_window_to(stack, window)) {
// for 2.x apps, we cannot animate the window frame during a transition but need to use this
// externalized state
const GPoint displacement = stack->transition_context.window_to_displacement;
gpoint_add_eq(&draw_state->drawing_box.origin, displacement);
gpoint_add_eq(&draw_state->clip_box.origin, displacement);
}
// clip_box must respect screen boundaries
grect_clip(&draw_state->clip_box, &saved_state->clip_box);
}
static void prv_restore_drawing_state(DrawingStateOrigins *saved_state, GContext *ctx) {
ctx->draw_state.drawing_box.origin = saved_state->drawing_box_origin;
ctx->draw_state.clip_box = saved_state->clip_box;
}
void prv_render_legacy2_system_status_bar(GContext *ctx, Window *window) {
if (!window->is_fullscreen) {
// adjust clipping rectangle so that rendering doesn't happen outside of the window
// this prevents instant colors changes when going from one window to another
GRect saved_clip_box = ctx->draw_state.clip_box;
grect_clip(&ctx->draw_state.clip_box, &window->layer.frame);
StatusBarLayerConfig config = {
.foreground_color = system_theme_get_fg_color(),
.background_color = system_theme_get_bg_color(),
.mode = StatusBarLayerModeClock,
};
GRect frame = window->layer.frame;
// window.frame.origin.y is 0 already (for 2.x compatibility reasons)
// see prv_adjust_drawing_state_for_legacy2_apps()
// so all we need to alter is the height of the frame
frame.size.h = STATUS_BAR_HEIGHT;
if (window_stack_is_animating_with_fixed_status_bar(window->parent_window_stack)) {
frame.origin.x = 0;
}
status_bar_layer_render(ctx, &frame, &config);
ctx->draw_state.clip_box = saved_clip_box;
}
}
void window_render(Window *window, GContext *ctx) {
PBL_ASSERTN(window);
if (window->on_screen == false) {
window->is_render_scheduled = false;
return;
}
// workaround for 3rd-party apps
// if a window is configured as non-fullscreen, it's frame needs to start at .origin={0,0}
// to compensate for cases where clients configure a layer hierarchy with
// my_layer = layer_create(window.root_layer.frame) // ! wrong, should be .bounds
// Of course on the screen, it still needs to start at {0, 16}. We adjust for that by
// moving the GContext's draw_state before we traverse the layer hierarchy to render it.
// Also see window_calc_frame()
DrawingStateOrigins saved_state;
prv_adjust_drawing_state_for_legacy2_apps(&saved_state, ctx, window);
layer_render_tree(&window->layer, ctx);
prv_restore_drawing_state(&saved_state, ctx);
prv_render_legacy2_system_status_bar(ctx, window);
window->is_render_scheduled = false;
}
void window_call_handler(Window *window, WindowHandlerOffset handler_offset) {
if (window == NULL) {
return;
}
WindowHandler handler = *(WindowHandler*)(((uint8_t*)&window->window_handlers) + handler_offset);
if (handler) {
handler(window);
}
}
void window_schedule_render(Window *window) {
window->is_render_scheduled = true;
}
GRect window_calc_frame(bool fullscreen) {
GContext *ctx = graphics_context_get_current_context();
GRect result = (GRect) {
.origin = { 0, 0 },
.size = graphics_context_get_framebuffer_size(ctx)
};
result.size.h -= fullscreen ? 0 : STATUS_BAR_HEIGHT;
return result;
}
// FIXME: there is a problem in this function:
// This function initializes the root layer to be the screen size. So, on a
// new window with a status bar, unless otherwise forced to with something
// like window_set_on_screen(), the window is first rendered at position (0,0);
// then this function shifts it to its correct position of (0, STATUS_BAR_HEIGHT).
// Either this function should set the window not on screen, or we should provide
// an alternate function for initializing the window that takes a frame dimension too.
void window_init(Window *window, const char* debug_name) {
if (window == NULL) {
PBL_LOG_ERR("Tried to init a NULL window");
return;
}
*window = (Window){};
#ifndef RELEASE
window->debug_name = debug_name;
#else
(void)debug_name;
#endif
bool fullscreen = !process_manager_compiled_with_legacy2_sdk();
const GRect frame = window_calc_frame(fullscreen);
layer_init(&window->layer, &frame);
window->is_fullscreen = fullscreen;
window->layer.window = window;
window->layer.update_proc = window_do_layer_update_proc;
window->background_color = system_theme_get_bg_color();
window->in_click_config_provider = false;
window->is_waiting_for_click_config = false;
window->parent_window_stack = NULL;
}
Window* window_create(void) {
Window* window = applib_type_malloc(Window);
if (window) {
window_init(window, "");
}
return window;
}
void window_destroy(Window* window) {
if (window == NULL) {
return;
}
window_deinit(window);
applib_free(window);
}
void window_deinit(Window *window) {
PBL_ASSERTN(window);
// FIXME: is there a way to cancel a pending render event?
window_set_on_screen(window, false, true);
layer_remove_child_layers(&window->layer);
window_unload(window);
}
void window_set_overrides_back_button(Window *window, bool overrides_back_button) {
if (overrides_back_button == window->overrides_back_button) {
return;
}
window->overrides_back_button = overrides_back_button;
}
static ClickManager* prv_get_current_click_manager(void) {
return window_manager_get_window_click_manager(window_manager_get_top_window());
}
static void prv_call_click_provider(Window *window) {
window->is_waiting_for_click_config = false;
app_click_config_setup_with_window(prv_get_current_click_manager(), window);
window->is_click_configured = true;
}
static void prv_check_is_in_click_config_provider(Window *window, char *type) {
PBL_ASSERT(window->in_click_config_provider,
"Click %s must be set from click config provider (Window %p)", type, window);
}
void window_setup_click_config_provider(Window *window) {
prv_call_click_provider(window);
}
void window_set_click_config_provider_with_context(
Window *window, ClickConfigProvider click_config_provider, void *context) {
PBL_ASSERTN(window);
window->click_config_provider = click_config_provider;
window->click_config_context = context;
if (window->on_screen && !window->is_unfocusable) {
// We're already on screen, make the config provider get called.
prv_call_click_provider(window);
} else {
window->is_waiting_for_click_config = true;
}
}
void window_set_click_config_provider(Window *window, ClickConfigProvider click_config_provider) {
window_set_click_config_provider_with_context(window, click_config_provider, NULL);
}
void window_set_click_context(ButtonId button_id, void *context) {
prv_check_is_in_click_config_provider(window_manager_get_top_window(), "context");
ClickManager *mgr = prv_get_current_click_manager();
ClickConfig *cfg = &mgr->recognizers[button_id].config;
cfg->context = context;
}
void window_single_click_subscribe(ButtonId button_id, ClickHandler handler) {
Window *window = window_manager_get_top_window();
prv_check_is_in_click_config_provider(window, "subscribe");
ClickManager *mgr = prv_get_current_click_manager();
ClickConfig *cfg = &mgr->recognizers[button_id].config;
cfg->click.repeat_interval_ms = 0;
cfg->click.handler = handler;
if (button_id == BUTTON_ID_BACK) {
window_set_overrides_back_button(window, true);
}
}
void window_single_repeating_click_subscribe(ButtonId button_id, uint16_t repeat_interval_ms, ClickHandler handler) {
prv_check_is_in_click_config_provider(window_manager_get_top_window(), "subscribe");
if (button_id == BUTTON_ID_BACK) {
PBL_LOG_ERR("Cannot register BUTTON_ID_BACK repeating click handler");
return;
}
ClickManager *mgr = prv_get_current_click_manager();
ClickConfig *cfg = &mgr->recognizers[button_id].config;
cfg->click.repeat_interval_ms = repeat_interval_ms;
cfg->click.handler = handler;
}
void window_multi_click_subscribe(ButtonId button_id, uint8_t min_clicks, uint8_t max_clicks, uint16_t timeout,
bool last_click_only, ClickHandler handler) {
Window *window = window_manager_get_top_window();
prv_check_is_in_click_config_provider(window, "subscribe");
ClickManager *mgr = prv_get_current_click_manager();
ClickConfig *cfg = &mgr->recognizers[button_id].config;
cfg->multi_click.min = (min_clicks == 0) ? 2 : min_clicks;
cfg->multi_click.max = (max_clicks == 0) ? min_clicks : max_clicks;
cfg->multi_click.timeout = (timeout == 0) ? 300 : timeout;
cfg->multi_click.last_click_only = last_click_only;
cfg->multi_click.handler = handler;
if (button_id == BUTTON_ID_BACK) {
window_set_overrides_back_button(window, true);
}
}
void window_long_click_subscribe(ButtonId button_id, uint16_t delay_ms, ClickHandler down_handler, ClickHandler up_handler) {
prv_check_is_in_click_config_provider(window_manager_get_top_window(), "subscribe");
if (button_id == BUTTON_ID_BACK) {
// We only want system apps to be able to override the back button for long
// clicks. Allowing third-party apps to override the back button would make
// long-pressing the back button a normal interaction method, and users may
// unintentionally hold the button too long and force-quit the app.
if (app_install_id_from_app_db(sys_process_manager_get_current_process_id())) {
PBL_LOG_ERR("Cannot register BUTTON_ID_BACK long click handler");
return;
} else {
Window *window = window_manager_get_top_window();
window_set_overrides_back_button(window, true);
}
}
ClickManager *mgr = prv_get_current_click_manager();
ClickConfig *cfg = &mgr->recognizers[button_id].config;
cfg->long_click.delay_ms = (delay_ms == 0) ? 500 : delay_ms;
cfg->long_click.handler = down_handler;
cfg->long_click.release_handler = up_handler;
}
void window_raw_click_subscribe(ButtonId button_id, ClickHandler down_handler, ClickHandler up_handler, void *context) {
prv_check_is_in_click_config_provider(window_manager_get_top_window(), "subscribe");
if (button_id == BUTTON_ID_BACK) {
PBL_LOG_ERR("Cannot register BUTTON_ID_BACK raw handler");
return;
}
ClickManager *mgr = prv_get_current_click_manager();
ClickConfig *cfg = &mgr->recognizers[button_id].config;
cfg->raw.up_handler = up_handler;
cfg->raw.down_handler = down_handler;
cfg->raw.context = context;
}
ClickConfigProvider window_get_click_config_provider(const Window *window) {
return window->click_config_provider;
}
void *window_get_click_config_context(Window *window) {
return window->click_config_context;
}
void window_set_window_handlers(Window *window, const WindowHandlers *handlers) {
if (handlers) {
window->window_handlers = *handlers;
}
}
void window_set_window_handlers_by_value(Window *window, WindowHandlers handlers) {
window_set_window_handlers(window, &handlers);
}
void window_set_user_data(Window *window, void *data) {
window->user_data = data;
}
void* window_get_user_data(const Window *window) {
return window->user_data;
}
struct Layer* window_get_root_layer(const Window *window) {
return &((Window *)window)->layer;
}
static void prv_window_load(Window *window) {
if (window->is_loaded) {
return;
}
window_call_handler(window, WindowHandlerOffsetLoad);
window->is_loaded = true;
}
void window_unload(Window *window) {
if (!window->is_loaded) {
return;
}
window->is_loaded = false;
window_call_handler(window, WindowHandlerOffsetUnload);
// Don't touch window after calling it's unload handler. We allow windows to free themselves on unload.
}
// TODO PBL-1769: deal with window unload. In app deinit? When low memory?
void window_set_on_screen(Window *window, bool new_on_screen, bool call_window_appear_handlers) {
PBL_ASSERTN(window != NULL); // This tripped me up for about a day
if (new_on_screen == window->on_screen) {
return;
}
// Window went from offscreen to onscreen (or vice versa)
// Provides internal signaling to ui elements of appear/disappear
layer_property_changed_tree(&window->layer);
window->on_screen = new_on_screen;
if (window->on_screen) {
window_schedule_render(window);
// The click provider was set but not updated
if (window->is_waiting_for_click_config && !window->is_unfocusable) {
prv_call_click_provider(window);
}
} else {
window->is_render_scheduled = false;
window->is_waiting_for_click_config = false;
window->is_click_configured = false;
}
if (call_window_appear_handlers) {
if (window->on_screen) {
prv_window_load(window);
// In our load handler, we may unload ourselves; this is perfectly fine! However,
// if we do that, we never appear on the screen! In that case, window->on_screen
// may have changed between the time we checked and after we called prv_window_load,
// so we need to check it again!
if (window->on_screen) {
// Window has no cache, so when it appears, schedule (re)render:
window_call_handler(window, WindowHandlerOffsetAppear);
}
} else if (window->is_loaded) {
// We have to have loaded (and consequently appeared) to actually disappear because
// we can actually set ourselves off-screen before we've ever been on-screen (this
// happens if we unload ourselves in our load handler), so we have to double check.
window_call_handler(window, WindowHandlerOffsetDisappear);
}
}
}
void window_set_background_color(Window *window, GColor background_color) {
const GColor window_bg_color = window->background_color;
if (gcolor_equal(background_color, window_bg_color)) {
return;
}
window->background_color = background_color;
layer_mark_dirty(&window->layer);
}
void window_set_background_color_2bit(Window *window, GColor2 background_color) {
window_set_background_color(window, get_native_color(background_color));
}
void window_set_fullscreen(Window *window, bool enabled) {
if (window->is_fullscreen == enabled) {
return;
}
window->is_fullscreen = enabled;
window->layer.frame = window_calc_frame(enabled);
window->layer.bounds.size = window->layer.frame.size;
layer_mark_dirty(&window->layer);
}
bool window_get_fullscreen(const Window *window) {
return window->is_fullscreen;
}
void window_set_status_bar_icon(Window *window, const GBitmap *icon) {
return;
}
bool window_is_on_screen(Window *window) {
return (window->on_screen);
}
bool window_is_loaded(Window *window) {
return (window->is_loaded);
}
void window_set_transparent(Window *window, bool transparent) {
window->is_transparent = transparent;
}
bool window_is_transparent(Window *window) {
return window->is_transparent;
}
void window_set_focusable(Window *window, bool focusable) {
window->is_unfocusable = !focusable;
}
bool window_is_focusable(Window *window) {
return !window->is_unfocusable;
}
const char* window_get_debug_name(Window *window) {
#ifndef RELEASE
return window->debug_name;
#else
return "?";
(void)window;
#endif
}
// A simple wrapper so feedback can be given to developers if click config subscriptions are made from outside of the
// click config configuration callback.
void window_call_click_config_provider(Window *window, void *context) {
window->in_click_config_provider = true;
window->click_config_provider(context);
window->in_click_config_provider = false;
}
static bool prv_find_status_bar_layer(Layer *layer, void *ctx) {
if (layer_is_status_bar_layer(layer)) {
*((StatusBarLayer **)ctx) = (StatusBarLayer *)layer;
return false; // prevent further iterating
}
return true;
}
bool window_has_status_bar(Window *window) {
if (!window) {
return false;
}
if (!window->is_fullscreen) {
return true;
}
StatusBarLayer *status_bar = NULL;
layer_process_tree(&window->layer, &status_bar, prv_find_status_bar_layer);
return status_bar && !layer_get_hidden(&status_bar->layer);
}
void window_attach_recognizer(Window *window, Recognizer *recognizer) {
if (!window) {
return;
}
layer_attach_recognizer(window_get_root_layer(window), recognizer);
}
void window_detach_recognizer(Window *window, Recognizer *recognizer) {
if (!window) {
return;
}
layer_detach_recognizer(window_get_root_layer(window), recognizer);
}
RecognizerList *window_get_recognizer_list(Window *window) {
if (!window) {
return NULL;
}
return layer_get_recognizer_list(window_get_root_layer(window));
}
RecognizerManager *window_get_recognizer_manager(Window *window) {
// TODO return the app's recognizer manager
// https://pebbletechnology.atlassian.net/browse/PBL-30957
return NULL;
}