Permalink
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
Cannot retrieve contributors at this time
/** | |
* Example implementation of the radio button list UI pattern. | |
*/ | |
#include "radio_button_window.h" | |
static Window *s_main_window; | |
static MenuLayer *s_menu_layer; | |
static int s_current_selection = 0; | |
static uint16_t get_num_rows_callback(MenuLayer *menu_layer, uint16_t section_index, void *context) { | |
return RADIO_BUTTON_WINDOW_NUM_ROWS + 1; | |
} | |
static void draw_row_callback(GContext *ctx, const Layer *cell_layer, MenuIndex *cell_index, void *context) { | |
if(cell_index->row == RADIO_BUTTON_WINDOW_NUM_ROWS) { | |
// This is the submit item | |
menu_cell_basic_draw(ctx, cell_layer, "Submit", NULL, NULL); | |
} else { | |
// This is a choice item | |
static char s_buff[16]; | |
snprintf(s_buff, sizeof(s_buff), "Choice %d", (int)cell_index->row); | |
menu_cell_basic_draw(ctx, cell_layer, s_buff, NULL, NULL); | |
GRect bounds = layer_get_bounds(cell_layer); | |
GPoint p = GPoint(bounds.size.w - (3 * RADIO_BUTTON_WINDOW_RADIO_RADIUS), (bounds.size.h / 2)); | |
// Selected? | |
if(menu_cell_layer_is_highlighted(cell_layer)) { | |
graphics_context_set_stroke_color(ctx, GColorWhite); | |
graphics_context_set_fill_color(ctx, GColorWhite); | |
} else { | |
graphics_context_set_fill_color(ctx, GColorBlack); | |
} | |
// Draw radio filled/empty | |
graphics_draw_circle(ctx, p, RADIO_BUTTON_WINDOW_RADIO_RADIUS); | |
if(cell_index->row == s_current_selection) { | |
// This is the selection | |
graphics_fill_circle(ctx, p, RADIO_BUTTON_WINDOW_RADIO_RADIUS - 3); | |
} | |
} | |
} | |
static int16_t get_cell_height_callback(struct MenuLayer *menu_layer, MenuIndex *cell_index, void *context) { | |
return PBL_IF_ROUND_ELSE( | |
menu_layer_is_index_selected(menu_layer, cell_index) ? | |
MENU_CELL_ROUND_FOCUSED_SHORT_CELL_HEIGHT : MENU_CELL_ROUND_UNFOCUSED_TALL_CELL_HEIGHT, | |
44); | |
} | |
static void select_callback(struct MenuLayer *menu_layer, MenuIndex *cell_index, void *callback_context) { | |
if(cell_index->row == RADIO_BUTTON_WINDOW_NUM_ROWS) { | |
// Do something with user choice | |
APP_LOG(APP_LOG_LEVEL_INFO, "Submitted choice %d", s_current_selection); | |
window_stack_pop(true); | |
} else { | |
// Change selection | |
s_current_selection = cell_index->row; | |
menu_layer_reload_data(menu_layer); | |
} | |
} | |
static void window_load(Window *window) { | |
Layer *window_layer = window_get_root_layer(window); | |
GRect bounds = layer_get_bounds(window_layer); | |
s_menu_layer = menu_layer_create(bounds); | |
menu_layer_set_click_config_onto_window(s_menu_layer, window); | |
menu_layer_set_callbacks(s_menu_layer, NULL, (MenuLayerCallbacks) { | |
.get_num_rows = get_num_rows_callback, | |
.draw_row = draw_row_callback, | |
.get_cell_height = get_cell_height_callback, | |
.select_click = select_callback, | |
}); | |
layer_add_child(window_layer, menu_layer_get_layer(s_menu_layer)); | |
} | |
static void window_unload(Window *window) { | |
menu_layer_destroy(s_menu_layer); | |
window_destroy(window); | |
s_main_window = NULL; | |
} | |
void radio_button_window_push() { | |
if(!s_main_window) { | |
s_main_window = window_create(); | |
window_set_window_handlers(s_main_window, (WindowHandlers) { | |
.load = window_load, | |
.unload = window_unload, | |
}); | |
} | |
window_stack_push(s_main_window, true); | |
} |