Initial code commit
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
/* SPDX-FileCopyrightText: 2024 Google LLC */
|
||||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
|
||||
#include "neat_bar.h"
|
||||
#include <pebble.h>
|
||||
|
||||
#if PBL_PLATFORM_EMERY
|
||||
const uint16_t UP_Y = 45;
|
||||
const uint16_t SELECT_Y = 105;
|
||||
const uint16_t DOWN_Y = 165;
|
||||
#else
|
||||
const uint16_t UP_Y = 50;
|
||||
const uint16_t SELECT_Y = 90;
|
||||
const uint16_t DOWN_Y = 120;
|
||||
#endif
|
||||
|
||||
static NeatBarLayer *s_current_neat_bar = NULL;
|
||||
|
||||
static void s_up_down_handler(ClickRecognizerRef recognizer, void *context) {}
|
||||
static void s_up_up_handler(ClickRecognizerRef recognizer, void *context) {}
|
||||
static void s_down_down_handler(ClickRecognizerRef recognizer, void *context) {}
|
||||
static void s_down_up_handler(ClickRecognizerRef recognizer, void *context) {}
|
||||
static void s_select_down_handler(ClickRecognizerRef recognizer, void *context) {}
|
||||
static void s_select_up_handler(ClickRecognizerRef recognizer, void *context) {}
|
||||
|
||||
static void s_main_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||
graphics_context_set_fill_color(ctx, s_current_neat_bar->background_color);
|
||||
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone); // TODO enable legacy corners
|
||||
}
|
||||
|
||||
static void s_base_click_config_provider(void *config_context) {
|
||||
NeatBarLayer *neat_bar = config_context;
|
||||
void *context = neat_bar->context ? neat_bar->context : neat_bar;
|
||||
window_raw_click_subscribe(BUTTON_ID_UP, s_up_down_handler, s_up_up_handler, neat_bar);
|
||||
window_raw_click_subscribe(BUTTON_ID_DOWN, s_down_down_handler, s_down_up_handler, neat_bar);
|
||||
window_raw_click_subscribe(BUTTON_ID_SELECT, s_select_down_handler, s_select_up_handler, neat_bar);
|
||||
window_set_click_context(BUTTON_ID_UP, context);
|
||||
window_set_click_context(BUTTON_ID_DOWN, context);
|
||||
window_set_click_context(BUTTON_ID_SELECT, context);
|
||||
if (neat_bar->click_config_provider) {
|
||||
neat_bar->click_config_provider(context);
|
||||
}
|
||||
}
|
||||
|
||||
inline static void s_update_click_config_provider(NeatBarLayer *neat_bar) {
|
||||
if (neat_bar->window) {
|
||||
window_set_click_config_provider_with_context(neat_bar->window, s_base_click_config_provider, neat_bar);
|
||||
}
|
||||
}
|
||||
|
||||
void neat_bar_layer_add_to_window(NeatBarLayer *neat_bar, Window *window) {
|
||||
layer_add_child(window_get_root_layer(window), neat_bar->main_layer);
|
||||
neat_bar->window = window;
|
||||
s_update_click_config_provider(neat_bar);
|
||||
}
|
||||
|
||||
void neat_bar_layer_set_click_config_provider(NeatBarLayer *neat_bar, ClickConfigProvider click_config_provider) {
|
||||
neat_bar->click_config_provider = click_config_provider;
|
||||
s_update_click_config_provider(neat_bar);
|
||||
}
|
||||
|
||||
void neat_bar_layer_set_background_color(NeatBarLayer *neat_bar, GColor background_color) {
|
||||
if (gcolor_equal(background_color, neat_bar->background_color)) {
|
||||
return;
|
||||
}
|
||||
neat_bar->background_color = background_color;
|
||||
layer_mark_dirty(neat_bar->main_layer);
|
||||
}
|
||||
|
||||
void neat_bar_layer_set_icon(NeatBarLayer *neat_bar, ButtonId button_id, GBitmap *icon) {
|
||||
BitmapLayer *current_bitmap_layer;
|
||||
uint16_t y_pos;
|
||||
|
||||
switch (button_id) {
|
||||
case BUTTON_ID_UP:
|
||||
current_bitmap_layer = neat_bar->up_layer;
|
||||
y_pos = UP_Y;
|
||||
break;
|
||||
case BUTTON_ID_DOWN:
|
||||
current_bitmap_layer = neat_bar->down_layer;
|
||||
y_pos = DOWN_Y;
|
||||
break;
|
||||
default:
|
||||
current_bitmap_layer = neat_bar->select_layer;
|
||||
y_pos = SELECT_Y;
|
||||
}
|
||||
|
||||
if (!current_bitmap_layer) {
|
||||
current_bitmap_layer = bitmap_layer_create(GRect(1, y_pos, ACTION_BAR_WIDTH, 18));
|
||||
layer_add_child(neat_bar->main_layer, bitmap_layer_get_layer(current_bitmap_layer));
|
||||
switch (button_id) {
|
||||
case BUTTON_ID_UP:
|
||||
neat_bar->up_layer = current_bitmap_layer;
|
||||
break;
|
||||
case BUTTON_ID_DOWN:
|
||||
neat_bar->down_layer = current_bitmap_layer;
|
||||
break;
|
||||
default:
|
||||
neat_bar->select_layer = current_bitmap_layer;
|
||||
break;
|
||||
}
|
||||
}
|
||||
bitmap_layer_set_alignment(current_bitmap_layer, GAlignCenter);
|
||||
bitmap_layer_set_bitmap(current_bitmap_layer, icon);
|
||||
bitmap_layer_set_compositing_mode(current_bitmap_layer, GCompOpSet);
|
||||
}
|
||||
|
||||
NeatBarLayer *neat_bar_layer_create() {
|
||||
NeatBarLayer *neat_bar = malloc(sizeof(NeatBarLayer));
|
||||
neat_bar->main_layer = layer_create(GRect(PBL_DISPLAY_WIDTH - ACTION_BAR_WIDTH, 0, ACTION_BAR_WIDTH, PBL_DISPLAY_HEIGHT));
|
||||
neat_bar->up_layer = NULL;
|
||||
neat_bar->down_layer = NULL;
|
||||
neat_bar->select_layer = NULL;
|
||||
neat_bar->click_config_provider = NULL;
|
||||
neat_bar->background_color = GColorBlack;
|
||||
neat_bar->context = NULL;
|
||||
layer_set_update_proc(neat_bar->main_layer, s_main_layer_update_proc);
|
||||
s_current_neat_bar = neat_bar;
|
||||
return neat_bar;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/* SPDX-FileCopyrightText: 2024 Google LLC */
|
||||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <pebble.h>
|
||||
|
||||
typedef struct {
|
||||
struct Window *window;
|
||||
Layer *main_layer;
|
||||
BitmapLayer *up_layer;
|
||||
BitmapLayer *down_layer;
|
||||
BitmapLayer *select_layer;
|
||||
ClickConfigProvider click_config_provider;
|
||||
GColor8 background_color;
|
||||
//int8_t spread; // positive integers push the icons further apart; negative integers pull them together
|
||||
//uint8_t animation_type; // 0 = press, 1 = invert
|
||||
//uint8_t shape; // 0 = rect/round, 1 = rect/rect, 2 = rect/round (legacy), 3 = rect/rect (legacy)
|
||||
void *context;
|
||||
} NeatBarLayer;
|
||||
|
||||
NeatBarLayer *neat_bar_layer_create();
|
||||
void neat_bar_layer_set_icon(NeatBarLayer *layer, ButtonId button_id, GBitmap *icon);
|
||||
void neat_bar_layer_set_background_color(NeatBarLayer *neat_bar, GColor background_color);
|
||||
void neat_bar_layer_set_click_config_provider(NeatBarLayer *neat_bar, ClickConfigProvider click_config_provider);
|
||||
void neat_bar_layer_add_to_window(NeatBarLayer *neat_bar, Window *window);
|
||||
Reference in New Issue
Block a user