a7283280ef
* GUI: Icons and IconsAnimation refactoring. Switch assets to new Icon API. * Gui: icon and animation draw now do not accept null pointer * Format Sources * Fix no debug build * Furi: stricter checks in memmgr
307 lines
11 KiB
C
307 lines
11 KiB
C
#include "view_dispatcher_i.h"
|
|
|
|
ViewDispatcher* view_dispatcher_alloc() {
|
|
ViewDispatcher* view_dispatcher = furi_alloc(sizeof(ViewDispatcher));
|
|
|
|
view_dispatcher->view_port = view_port_alloc();
|
|
view_port_draw_callback_set(
|
|
view_dispatcher->view_port, view_dispatcher_draw_callback, view_dispatcher);
|
|
view_port_input_callback_set(
|
|
view_dispatcher->view_port, view_dispatcher_input_callback, view_dispatcher);
|
|
view_port_enabled_set(view_dispatcher->view_port, false);
|
|
|
|
ViewDict_init(view_dispatcher->views);
|
|
|
|
return view_dispatcher;
|
|
}
|
|
|
|
void view_dispatcher_free(ViewDispatcher* view_dispatcher) {
|
|
// Detach from gui
|
|
if(view_dispatcher->gui) {
|
|
gui_remove_view_port(view_dispatcher->gui, view_dispatcher->view_port);
|
|
}
|
|
// Free views
|
|
ViewDict_it_t it;
|
|
ViewDict_it(it, view_dispatcher->views);
|
|
while(!ViewDict_end_p(it)) {
|
|
ViewDict_itref_t* ref = ViewDict_ref(it);
|
|
// Crash if view wasn't freed
|
|
furi_check(ref->value);
|
|
ViewDict_next(it);
|
|
}
|
|
ViewDict_clear(view_dispatcher->views);
|
|
// Free ViewPort
|
|
view_port_free(view_dispatcher->view_port);
|
|
// Free internal queue
|
|
if(view_dispatcher->queue) {
|
|
osMessageQueueDelete(view_dispatcher->queue);
|
|
}
|
|
// Free View Navigator
|
|
if(view_dispatcher->view_navigator) {
|
|
view_navigator_free(view_dispatcher->view_navigator);
|
|
}
|
|
// Free dispatcher
|
|
free(view_dispatcher);
|
|
}
|
|
|
|
void view_dispatcher_enable_queue(ViewDispatcher* view_dispatcher) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->queue == NULL);
|
|
view_dispatcher->queue = osMessageQueueNew(8, sizeof(ViewDispatcherMessage), NULL);
|
|
}
|
|
|
|
void view_dispatcher_enable_navigation(ViewDispatcher* view_dispatcher, void* context) {
|
|
furi_assert(view_dispatcher);
|
|
view_dispatcher->view_navigator = view_navigator_alloc(context);
|
|
}
|
|
|
|
void view_dispatcher_add_scene(ViewDispatcher* view_dispatcher, AppScene* scene) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->view_navigator);
|
|
furi_assert(scene);
|
|
view_navigator_add_next_scene(view_dispatcher->view_navigator, scene);
|
|
}
|
|
|
|
void view_dispatcher_run(ViewDispatcher* view_dispatcher) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->queue);
|
|
|
|
if(view_dispatcher->view_navigator) {
|
|
view_navigator_start(view_dispatcher->view_navigator);
|
|
}
|
|
|
|
ViewDispatcherMessage message;
|
|
while(osMessageQueueGet(view_dispatcher->queue, &message, NULL, osWaitForever) == osOK) {
|
|
if(message.type == ViewDispatcherMessageTypeStop) {
|
|
break;
|
|
} else if(message.type == ViewDispatcherMessageTypeInput) {
|
|
view_dispatcher_handle_input(view_dispatcher, &message.input);
|
|
} else if(message.type == ViewDispatcherMessageTypeCustomEvent) {
|
|
view_dispatcher_handle_custom_event(view_dispatcher, message.custom_event);
|
|
} else if(message.type == ViewDispatcherMessageTypeNavigationEvent) {
|
|
view_navigator_handle_navigation_event(
|
|
view_dispatcher->view_navigator, message.navigator_event);
|
|
} else if(message.type == ViewDispatcherMessageTypeBackSearchScene) {
|
|
view_navigator_handle_back_search_scene_event(
|
|
view_dispatcher->view_navigator, message.navigator_event);
|
|
}
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_stop(ViewDispatcher* view_dispatcher) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->queue);
|
|
ViewDispatcherMessage message;
|
|
message.type = ViewDispatcherMessageTypeStop;
|
|
furi_check(osMessageQueuePut(view_dispatcher->queue, &message, 0, osWaitForever) == osOK);
|
|
}
|
|
|
|
void view_dispatcher_add_view(ViewDispatcher* view_dispatcher, uint32_t view_id, View* view) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view);
|
|
// Check if view id is not used and register view
|
|
furi_check(ViewDict_get(view_dispatcher->views, view_id) == NULL);
|
|
|
|
// Lock gui
|
|
if(view_dispatcher->gui) {
|
|
gui_lock(view_dispatcher->gui);
|
|
}
|
|
|
|
ViewDict_set_at(view_dispatcher->views, view_id, view);
|
|
view_set_update_callback(view, view_dispatcher_update);
|
|
view_set_update_callback_context(view, view_dispatcher);
|
|
|
|
// Unlock gui
|
|
if(view_dispatcher->gui) {
|
|
gui_unlock(view_dispatcher->gui);
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_remove_view(ViewDispatcher* view_dispatcher, uint32_t view_id) {
|
|
furi_assert(view_dispatcher);
|
|
|
|
// Lock gui
|
|
if(view_dispatcher->gui) {
|
|
gui_lock(view_dispatcher->gui);
|
|
}
|
|
// Get View by ID
|
|
View* view = *ViewDict_get(view_dispatcher->views, view_id);
|
|
|
|
// Disable the view if it is active
|
|
if(view_dispatcher->current_view == view) {
|
|
view_dispatcher_set_current_view(view_dispatcher, NULL);
|
|
}
|
|
// Remove view
|
|
ViewDict_erase(view_dispatcher->views, view_id);
|
|
|
|
view_set_update_callback(view, NULL);
|
|
view_set_update_callback_context(view, NULL);
|
|
|
|
// Unlock gui
|
|
if(view_dispatcher->gui) {
|
|
gui_unlock(view_dispatcher->gui);
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_switch_to_view(ViewDispatcher* view_dispatcher, uint32_t view_id) {
|
|
furi_assert(view_dispatcher);
|
|
if(view_id == VIEW_NONE) {
|
|
view_dispatcher_set_current_view(view_dispatcher, NULL);
|
|
} else if(view_id == VIEW_IGNORE) {
|
|
} else if(view_id == VIEW_DESTROY) {
|
|
view_dispatcher_free(view_dispatcher);
|
|
} else {
|
|
View** view_pp = ViewDict_get(view_dispatcher->views, view_id);
|
|
furi_check(view_pp != NULL);
|
|
view_dispatcher_set_current_view(view_dispatcher, *view_pp);
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_attach_to_gui(
|
|
ViewDispatcher* view_dispatcher,
|
|
Gui* gui,
|
|
ViewDispatcherType type) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->gui == NULL);
|
|
furi_assert(gui);
|
|
|
|
if(type == ViewDispatcherTypeNone) {
|
|
gui_add_view_port(gui, view_dispatcher->view_port, GuiLayerNone);
|
|
} else if(type == ViewDispatcherTypeFullscreen) {
|
|
gui_add_view_port(gui, view_dispatcher->view_port, GuiLayerFullscreen);
|
|
} else if(type == ViewDispatcherTypeWindow) {
|
|
gui_add_view_port(gui, view_dispatcher->view_port, GuiLayerMain);
|
|
} else {
|
|
furi_check(NULL);
|
|
}
|
|
view_dispatcher->gui = gui;
|
|
}
|
|
|
|
void view_dispatcher_draw_callback(Canvas* canvas, void* context) {
|
|
ViewDispatcher* view_dispatcher = context;
|
|
if(view_dispatcher->current_view) {
|
|
view_draw(view_dispatcher->current_view, canvas);
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_input_callback(InputEvent* event, void* context) {
|
|
ViewDispatcher* view_dispatcher = context;
|
|
if(view_dispatcher->queue) {
|
|
ViewDispatcherMessage message;
|
|
message.type = ViewDispatcherMessageTypeInput;
|
|
message.input = *event;
|
|
furi_check(osMessageQueuePut(view_dispatcher->queue, &message, 0, osWaitForever) == osOK);
|
|
} else {
|
|
view_dispatcher_handle_input(view_dispatcher, event);
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_handle_input(ViewDispatcher* view_dispatcher, InputEvent* event) {
|
|
bool is_consumed = false;
|
|
if(view_dispatcher->current_view) {
|
|
is_consumed = view_input(view_dispatcher->current_view, event);
|
|
}
|
|
if(!is_consumed && event->type == InputTypeShort) {
|
|
// TODO remove view navigation handlers
|
|
uint32_t view_id = VIEW_IGNORE;
|
|
if(event->key == InputKeyBack) {
|
|
view_id = view_previous(view_dispatcher->current_view);
|
|
if((view_id == VIEW_IGNORE) && (view_dispatcher->view_navigator)) {
|
|
is_consumed = view_navigator_handle_navigation_event(
|
|
view_dispatcher->view_navigator, ViewNavigatorEventBack);
|
|
if(!is_consumed) {
|
|
view_dispatcher_stop(view_dispatcher);
|
|
return;
|
|
}
|
|
}
|
|
} else if(event->key == InputKeyOk) {
|
|
view_id = view_next(view_dispatcher->current_view);
|
|
}
|
|
if(!is_consumed) {
|
|
view_dispatcher_switch_to_view(view_dispatcher, view_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_handle_custom_event(ViewDispatcher* view_dispatcher, uint32_t event) {
|
|
bool is_consumed = false;
|
|
if(view_dispatcher->current_view) {
|
|
is_consumed = view_custom(view_dispatcher->current_view, event);
|
|
}
|
|
// If custom event is not consumed in View, handle it in Scene
|
|
if(!is_consumed) {
|
|
is_consumed = view_navigator_handle_custom_event(view_dispatcher->view_navigator, event);
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_send_custom_event(ViewDispatcher* view_dispatcher, uint32_t event) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->queue);
|
|
|
|
ViewDispatcherMessage message;
|
|
message.type = ViewDispatcherMessageTypeCustomEvent;
|
|
message.custom_event = event;
|
|
|
|
furi_check(osMessageQueuePut(view_dispatcher->queue, &message, 0, osWaitForever) == osOK);
|
|
}
|
|
|
|
void view_dispatcher_send_navigation_event(ViewDispatcher* view_dispatcher, uint32_t event) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->queue);
|
|
furi_assert(view_dispatcher->view_navigator);
|
|
|
|
ViewDispatcherMessage message;
|
|
message.type = ViewDispatcherMessageTypeNavigationEvent;
|
|
message.custom_event = event;
|
|
|
|
furi_check(osMessageQueuePut(view_dispatcher->queue, &message, 0, osWaitForever) == osOK);
|
|
}
|
|
|
|
void view_dispatcher_send_back_search_scene_event(ViewDispatcher* view_dispatcher, uint32_t event) {
|
|
furi_assert(view_dispatcher);
|
|
furi_assert(view_dispatcher->queue);
|
|
furi_assert(view_dispatcher->view_navigator);
|
|
|
|
ViewDispatcherMessage message;
|
|
message.type = ViewDispatcherMessageTypeBackSearchScene;
|
|
message.custom_event = event;
|
|
|
|
furi_check(osMessageQueuePut(view_dispatcher->queue, &message, 0, osWaitForever) == osOK);
|
|
}
|
|
|
|
void view_dispatcher_set_current_view(ViewDispatcher* view_dispatcher, View* view) {
|
|
furi_assert(view_dispatcher);
|
|
// Dispatch view exit event
|
|
if(view_dispatcher->current_view) {
|
|
view_exit(view_dispatcher->current_view);
|
|
}
|
|
// Set current view
|
|
view_dispatcher->current_view = view;
|
|
// Dispatch view enter event
|
|
if(view_dispatcher->current_view) {
|
|
if(view->orientation == ViewOrientationVertical)
|
|
view_port_set_orientation(view_dispatcher->view_port, ViewPortOrientationVertical);
|
|
else if(view->orientation == ViewOrientationHorizontal)
|
|
view_port_set_orientation(view_dispatcher->view_port, ViewPortOrientationHorizontal);
|
|
view_enter(view_dispatcher->current_view);
|
|
view_port_enabled_set(view_dispatcher->view_port, true);
|
|
view_port_update(view_dispatcher->view_port);
|
|
} else {
|
|
view_port_enabled_set(view_dispatcher->view_port, false);
|
|
if(view_dispatcher->queue) {
|
|
view_dispatcher_stop(view_dispatcher);
|
|
}
|
|
}
|
|
}
|
|
|
|
void view_dispatcher_update(View* view, void* context) {
|
|
furi_assert(view);
|
|
furi_assert(context);
|
|
|
|
ViewDispatcher* view_dispatcher = context;
|
|
|
|
if(view_dispatcher->current_view == view) {
|
|
view_port_update(view_dispatcher->view_port);
|
|
}
|
|
}
|