diff options
Diffstat (limited to 'app/src/display/widgets/layer_status.c')
-rw-r--r-- | app/src/display/widgets/layer_status.c | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/app/src/display/widgets/layer_status.c b/app/src/display/widgets/layer_status.c index 9960f2a..3c63565 100644 --- a/app/src/display/widgets/layer_status.c +++ b/app/src/display/widgets/layer_status.c @@ -4,9 +4,11 @@ * SPDX-License-Identifier: MIT */ +#include <kernel.h> #include <logging/log.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include <zmk/display.h> #include <zmk/display/widgets/layer_status.h> #include <zmk/events/layer_state_changed.h> #include <zmk/event_manager.h> @@ -18,7 +20,12 @@ static lv_style_t label_style; static bool style_initialized = false; -void layer_status_init() { +struct layer_status_state { + uint8_t index; + const char *label; +}; + +static void layer_status_init() { if (style_initialized) { return; } @@ -31,49 +38,50 @@ void layer_status_init() { lv_style_set_text_line_space(&label_style, LV_STATE_DEFAULT, 1); } -void set_layer_symbol(lv_obj_t *label) { - int active_layer_index = zmk_keymap_highest_layer_active(); - - LOG_DBG("Layer changed to %i", active_layer_index); - - const char *layer_label = zmk_keymap_layer_label(active_layer_index); - if (layer_label == NULL) { +static void set_layer_symbol(lv_obj_t *label, struct layer_status_state state) { + if (state.label == NULL) { char text[6] = {}; - sprintf(text, LV_SYMBOL_KEYBOARD "%i", active_layer_index); + sprintf(text, LV_SYMBOL_KEYBOARD "%i", state.index); lv_label_set_text(label, text); } else { char text[12] = {}; - snprintf(text, 12, LV_SYMBOL_KEYBOARD "%s", layer_label); + snprintf(text, 12, LV_SYMBOL_KEYBOARD "%s", state.label); lv_label_set_text(label, text); } } +static void layer_status_update_cb(struct layer_status_state state) { + struct zmk_widget_layer_status *widget; + SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_layer_symbol(widget->obj, state); } +} + +static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { + uint8_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_label(index)}; +} + +ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, + layer_status_get_state) + +ZMK_SUBSCRIPTION(widget_layer_status, zmk_layer_state_changed); + int zmk_widget_layer_status_init(struct zmk_widget_layer_status *widget, lv_obj_t *parent) { layer_status_init(); widget->obj = lv_label_create(parent, NULL); lv_obj_add_style(widget->obj, LV_LABEL_PART_MAIN, &label_style); lv_obj_set_size(widget->obj, 40, 15); - set_layer_symbol(widget->obj); sys_slist_append(&widgets, &widget->node); + widget_layer_status_init(); return 0; } lv_obj_t *zmk_widget_layer_status_obj(struct zmk_widget_layer_status *widget) { return widget->obj; -} - -int layer_status_listener(const zmk_event_t *eh) { - struct zmk_widget_layer_status *widget; - SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_layer_symbol(widget->obj); } - return 0; -} - -ZMK_LISTENER(widget_layer_status, layer_status_listener) -ZMK_SUBSCRIPTION(widget_layer_status, zmk_layer_state_changed);
\ No newline at end of file +}
\ No newline at end of file |