From 90c1888ba3dc31d90b9a97e316eaf9244494b7d0 Mon Sep 17 00:00:00 2001 From: synzr Date: Sat, 18 Oct 2025 16:30:19 +0500 Subject: [PATCH] fix(layers): pass the layer instead of window --- src/c/bar_layer.c | 10 ++++------ src/c/bar_layer.h | 2 +- src/c/character_layer.c | 12 ++++++------ src/c/character_layer.h | 2 +- src/c/clock_layer.c | 31 ++++++++++++++----------------- src/c/clock_layer.h | 2 +- src/c/main_window.c | 14 +++++++++----- 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/c/bar_layer.c b/src/c/bar_layer.c index 3abf993..5e604ea 100644 --- a/src/c/bar_layer.c +++ b/src/c/bar_layer.c @@ -34,14 +34,12 @@ static void bar_layer_update_proc(Layer *layer, GContext *ctx) { GTextAlignmentRight, NULL); } -void bar_layer_init(Window *window) { - Layer *window_layer = window_get_root_layer(window); - GRect window_rect = layer_get_unobstructed_bounds(window_layer); +void bar_layer_init(Layer *layer, int y) { + GRect rect = layer_get_unobstructed_bounds(layer); - s_bar_layer = layer_create(GRect(BAR_PAD_W, 0, window_rect.size.w - (BAR_PAD_W * 2), 28)); + s_bar_layer = layer_create(GRect(BAR_PAD_W, y, rect.size.w - (BAR_PAD_W * 2), 28)); layer_set_update_proc(s_bar_layer, bar_layer_update_proc); - - layer_add_child(window_layer, s_bar_layer); + layer_add_child(layer, s_bar_layer); } void bar_layer_tick(void) { diff --git a/src/c/bar_layer.h b/src/c/bar_layer.h index ba45645..318a06e 100644 --- a/src/c/bar_layer.h +++ b/src/c/bar_layer.h @@ -3,7 +3,7 @@ #include -void bar_layer_init(Window *window); +void bar_layer_init(Layer *layer, int y); void bar_layer_tick(void); void bar_layer_deinit(void); diff --git a/src/c/character_layer.c b/src/c/character_layer.c index 818b5c4..5e9430d 100644 --- a/src/c/character_layer.c +++ b/src/c/character_layer.c @@ -1,4 +1,5 @@ #include "character_layer.h" + #include "resources_service.h" #define CHARACTER_H 80 @@ -35,18 +36,17 @@ static void character_layer_update_proc(Layer *layer, GContext *ctx) { graphics_draw_bitmap_in_rect(ctx, bitmap, frame); } -void character_layer_init(Window *window) { - Layer *window_root_layer = window_get_root_layer(window); - GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer); +void character_layer_init(Layer *layer) { + GRect rect = layer_get_unobstructed_bounds(layer); - s_character_layer = - layer_create_with_data(GRect(0, window_bounds.size.h - CHARACTER_H, window_bounds.size.w, CHARACTER_H), sizeof(CharacterLayerData)); + s_character_layer = layer_create_with_data( + GRect(0, rect.size.h - CHARACTER_H, rect.size.w, CHARACTER_H), sizeof(CharacterLayerData)); CharacterLayerData *layer_data = layer_get_data(s_character_layer); layer_data->ticks = 0; layer_set_update_proc(s_character_layer, character_layer_update_proc); - layer_add_child(window_root_layer, s_character_layer); + layer_add_child(layer, s_character_layer); } void character_layer_tick(void) { diff --git a/src/c/character_layer.h b/src/c/character_layer.h index 0a68128..4baf6b1 100644 --- a/src/c/character_layer.h +++ b/src/c/character_layer.h @@ -3,7 +3,7 @@ #include -void character_layer_init(Window *window); +void character_layer_init(Layer *layer); void character_layer_tick(void); void character_layer_deinit(void); diff --git a/src/c/clock_layer.c b/src/c/clock_layer.c index 84dc703..665065e 100644 --- a/src/c/clock_layer.c +++ b/src/c/clock_layer.c @@ -7,6 +7,8 @@ #define CLOCK_OFFSET 4 static Layer *s_clock_layer; +static char s_clock_time_text[6] = "00:00"; +static char s_clock_seconds_text[3] = "00"; static void clock_layer_update_proc(Layer *layer, GContext *ctx) { // Fill a whole layer with the black color. @@ -20,21 +22,19 @@ static void clock_layer_update_proc(Layer *layer, GContext *ctx) { struct tm *time = localtime(&tmp); // Drawing the time and seconds. - static char time_text[6], seconds_text[3] = "00\0"; - GFont font_small = resources_service_get_custom_font(CustomFontKonekoToro); GFont font_big = resources_service_get_custom_font(CustomFontToro); graphics_context_set_text_color(ctx, GColorWhite); - strftime(time_text, sizeof(time_text), clock_is_24h_style() ? "%H:%M" : "%I:%M", time); + strftime(s_clock_time_text, sizeof(s_clock_time_text), clock_is_24h_style() ? "%H:%M" : "%I:%M", + time); GSize time_text_size = graphics_text_layout_get_content_size( - time_text, font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); + s_clock_time_text, font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); - seconds_text[0] = seconds_text[1] = '0'; GSize seconds_text_size = graphics_text_layout_get_content_size( - seconds_text, font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); - strftime(seconds_text, sizeof(seconds_text), "%S", time); + "00", font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); + strftime(s_clock_seconds_text, sizeof(s_clock_seconds_text), "%S", time); GRect timestamp_rect = GRect(0, 0, time_text_size.w + seconds_text_size.w + CLOCK_SPACE, time_text_size.h); @@ -43,24 +43,21 @@ static void clock_layer_update_proc(Layer *layer, GContext *ctx) { GRect time_rect = GRect(timestamp_rect.origin.x, timestamp_rect.origin.y, time_text_size.w, time_text_size.h); - graphics_draw_text(ctx, time_text, font_big, time_rect, GTextOverflowModeFill, GTextAlignmentLeft, - NULL); + graphics_draw_text(ctx, s_clock_time_text, font_big, time_rect, GTextOverflowModeFill, + GTextAlignmentLeft, NULL); GRect seconds_rect = GRect(0, 0, seconds_text_size.w, seconds_text_size.h); grect_align(&seconds_rect, ×tamp_rect, GAlignBottomRight, true); - graphics_draw_text(ctx, seconds_text, font_small, seconds_rect, GTextOverflowModeFill, + graphics_draw_text(ctx, s_clock_seconds_text, font_small, seconds_rect, GTextOverflowModeFill, GTextAlignmentLeft, NULL); } -void clock_layer_init(Window *window, GPoint position) { - Layer *window_root_layer = window_get_root_layer(window); - GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer); - - s_clock_layer = - layer_create(GRect(position.x, position.y, window_bounds.size.w - position.x, CLOCK_H)); +void clock_layer_init(Layer *layer, int y) { + GRect rect = layer_get_unobstructed_bounds(layer); + s_clock_layer = layer_create(GRect(0, y, rect.size.w, CLOCK_H)); layer_set_update_proc(s_clock_layer, clock_layer_update_proc); - layer_add_child(window_root_layer, s_clock_layer); + layer_add_child(layer, s_clock_layer); } void clock_layer_tick(void) { diff --git a/src/c/clock_layer.h b/src/c/clock_layer.h index 2525132..b76a9f7 100644 --- a/src/c/clock_layer.h +++ b/src/c/clock_layer.h @@ -3,7 +3,7 @@ #include -void clock_layer_init(Window *window, GPoint position); +void clock_layer_init(Layer *layer, int y); void clock_layer_tick(void); void clock_layer_deinit(void); diff --git a/src/c/main_window.c b/src/c/main_window.c index 6a65444..6dfa677 100644 --- a/src/c/main_window.c +++ b/src/c/main_window.c @@ -7,20 +7,24 @@ static Window *s_main_window; static void main_window_tick(struct tm *time, TimeUnits units) { - character_layer_tick(); - clock_layer_tick(); bar_layer_tick(); + clock_layer_tick(); + character_layer_tick(); } static void main_window_load(Window *window) { - character_layer_init(s_main_window); - clock_layer_init(s_main_window, GPoint(0, 40)); - bar_layer_init(s_main_window); + Layer *layer = window_get_root_layer(window); + + bar_layer_init(layer, 0); + clock_layer_init(layer, 40); + character_layer_init(layer); + tick_timer_service_subscribe(SECOND_UNIT, main_window_tick); } static void main_window_unload(Window *window) { tick_timer_service_unsubscribe(); + clock_layer_deinit(); bar_layer_deinit(); character_layer_deinit();