feat(layer): bar layer

This commit is contained in:
mikhail "synzr" 2025-10-18 13:01:18 +05:00
parent 9f6f83e296
commit da3ff8d5e6
6 changed files with 74 additions and 8 deletions

View file

@ -38,12 +38,12 @@
"spaceOptimization": "memory"
}, {
"type": "font",
"name": "FONT_TORO_38",
"name": "FONT_TORO_36",
"file": "font_toro.ttf",
"characterRange": "[0-9:]"
}, {
"type": "font",
"name": "FONT_KONEKO_TORO_28",
"name": "FONT_KONEKO_TORO_24",
"file": "font_koneko_toro.ttf",
"characterRange": "[0-9/WTFSMedhuriaton]"
}]

Binary file not shown.

53
src/c/bar_layer.c Normal file
View file

@ -0,0 +1,53 @@
#include "bar_layer.h"
#include "resources_service.h"
#define BAR_PAD_W 8
static Layer *s_bar_layer;
static void bar_layer_update_proc(Layer *layer, GContext *ctx) {
GRect window_rect = layer_get_unobstructed_bounds(layer);
time_t tmp = time(NULL);
struct tm *time = localtime(&tmp);
static char date_text[6], weekday_text[4];
GFont font = resources_service_get_custom_font(CustomFontKonekoToro);
strftime(date_text, sizeof(date_text), "%m/%d", time);
GSize date_text_size = graphics_text_layout_get_content_size(
date_text, font, window_rect, GTextOverflowModeFill, GTextAlignmentLeft);
GRect date_text_rect = GRect(0, 0, date_text_size.w, date_text_size.h);
grect_align(&date_text_rect, &window_rect, GAlignLeft, true);
strftime(weekday_text, sizeof(weekday_text), "%a", time);
GSize weekday_text_size = graphics_text_layout_get_content_size(
weekday_text, font, window_rect, GTextOverflowModeFill, GTextAlignmentRight);
GRect weekday_text_rect = GRect(0, 0, weekday_text_size.w, weekday_text_size.h);
grect_align(&weekday_text_rect, &window_rect, GAlignRight, true);
graphics_context_set_text_color(ctx, GColorBlack);
graphics_draw_text(ctx, date_text, font, date_text_rect, GTextOverflowModeFill,
GTextAlignmentLeft, NULL);
graphics_draw_text(ctx, weekday_text, font, weekday_text_rect, GTextOverflowModeFill,
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);
s_bar_layer = layer_create(GRect(BAR_PAD_W, 0, window_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);
}
void bar_layer_tick(void) {
layer_mark_dirty(s_bar_layer);
}
void bar_layer_deinit(void) {
layer_destroy(s_bar_layer);
}

10
src/c/bar_layer.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef BAR_LAYER_H_
#define BAR_LAYER_H_
#include <pebble.h>
void bar_layer_init(Window *window);
void bar_layer_tick(void);
void bar_layer_deinit(void);
#endif

View file

@ -1,4 +1,6 @@
#include "main_window.h"
#include "bar_layer.h"
#include "character_layer.h"
#include "clock_layer.h"
@ -7,26 +9,27 @@ static Window *s_main_window;
static void main_window_tick(struct tm *time, TimeUnits units) {
character_layer_tick();
clock_layer_tick();
bar_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);
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();
}
void main_window_init(void) {
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
window_set_window_handlers(
s_main_window, (WindowHandlers){.load = main_window_load, .unload = main_window_unload});
window_stack_push(s_main_window, true);
}

View file

@ -12,9 +12,9 @@ static ResourcesService *s_resources_service;
void resources_service_init(void) {
s_resources_service = malloc(sizeof(ResourcesService));
s_resources_service->font_koneko_toro =
fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_KONEKO_TORO_28));
fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_KONEKO_TORO_24));
s_resources_service->font_toro =
fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TORO_38));
fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TORO_36));
s_resources_service->character_even = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_EVEN);
s_resources_service->character_odd = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_ODD);
}