Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion applications/services/gui/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

View* view_alloc(void) {
View* view = malloc(sizeof(View));
view->orientation = ViewOrientationHorizontal;
view_init(view);
return view;
}

Expand All @@ -12,6 +12,10 @@ void view_free(View* view) {
free(view);
}

void view_init(View* view) {
view->orientation = ViewOrientationHorizontal;
}

void view_tie_icon_animation(View* view, IconAnimation* icon_animation) {
furi_check(view);
icon_animation_set_update_callback(icon_animation, view_icon_animation_callback, view);
Expand Down
3 changes: 3 additions & 0 deletions applications/services/gui/view_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ struct View {
void* context;
};

/** Initialize View (for internal use) */
void view_init(View* view);

/** IconAnimation tie callback */
void view_icon_animation_callback(IconAnimation* instance, void* context);

Expand Down
53 changes: 25 additions & 28 deletions applications/services/gui/view_stack.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#include "view_stack.h"
#include "view_i.h"

#include <gui/view.h>
#include <core/memmgr.h>

#define MAX_VIEWS 3

typedef struct {
View* views[MAX_VIEWS];
} ViewStackModel;

struct ViewStack {
View* view;
View view;
};

static void view_stack_draw(Canvas* canvas, void* model);
Expand All @@ -32,62 +29,62 @@ static void view_stack_enter(void* context) {
furi_assert(context);

ViewStack* view_stack = context;
ViewStackModel* model = view_get_model(view_stack->view);
ViewStackModel* model = view_get_model(&view_stack->view);

/* if more than 1 Stack View hold same view they have to reassign update_callback_context */
for(int i = 0; i < MAX_VIEWS; ++i) {
if(model->views[i]) {
view_set_update_callback_context(model->views[i], view_stack->view);
view_set_update_callback_context(model->views[i], &view_stack->view);
if(model->views[i]->enter_callback) {
model->views[i]->enter_callback(model->views[i]->context);
}
}
}

view_commit_model(view_stack->view, false);
view_commit_model(&view_stack->view, false);
}

static void view_stack_exit(void* context) {
furi_assert(context);

ViewStack* view_stack = context;
ViewStackModel* model = view_get_model(view_stack->view);
ViewStackModel* model = view_get_model(&view_stack->view);

for(int i = 0; i < MAX_VIEWS; ++i) {
if(model->views[i] && model->views[i]->exit_callback) {
model->views[i]->exit_callback(model->views[i]->context);
}
}

view_commit_model(view_stack->view, false);
view_commit_model(&view_stack->view, false);
}

ViewStack* view_stack_alloc(void) {
ViewStack* view_stack = malloc(sizeof(ViewStack));
view_stack->view = view_alloc();

view_allocate_model(view_stack->view, ViewModelTypeLocking, sizeof(ViewStackModel));
view_set_draw_callback(view_stack->view, view_stack_draw);
view_set_input_callback(view_stack->view, view_stack_input);
view_set_context(view_stack->view, view_stack);
view_set_enter_callback(view_stack->view, view_stack_enter);
view_set_exit_callback(view_stack->view, view_stack_exit);

view_init(&view_stack->view);
view_allocate_model(&view_stack->view, ViewModelTypeLocking, sizeof(ViewStackModel));
view_set_draw_callback(&view_stack->view, view_stack_draw);
view_set_input_callback(&view_stack->view, view_stack_input);
view_set_context(&view_stack->view, view_stack);
view_set_enter_callback(&view_stack->view, view_stack_enter);
view_set_exit_callback(&view_stack->view, view_stack_exit);
return view_stack;
}

void view_stack_free(ViewStack* view_stack) {
furi_assert(view_stack);

ViewStackModel* model = view_get_model(view_stack->view);
ViewStackModel* model = view_get_model(&view_stack->view);
for(int i = 0; i < MAX_VIEWS; ++i) {
if(model->views[i]) {
view_set_update_callback(model->views[i], NULL);
view_set_update_callback_context(model->views[i], NULL);
}
}
view_commit_model(view_stack->view, false);
view_commit_model(&view_stack->view, false);

view_free(view_stack->view);
view_free_model(&view_stack->view);
free(view_stack);
}

Expand All @@ -109,14 +106,14 @@ static bool view_stack_input(InputEvent* event, void* context) {
ViewStack* view_stack = context;

bool consumed = false;
ViewStackModel* model = view_get_model(view_stack->view);
ViewStackModel* model = view_get_model(&view_stack->view);
for(int i = MAX_VIEWS - 1; i >= 0; i--) {
if(model->views[i] && view_input(model->views[i], event)) {
consumed = true;
break;
}
}
view_commit_model(view_stack->view, false);
view_commit_model(&view_stack->view, false);

return consumed;
}
Expand All @@ -126,20 +123,20 @@ void view_stack_add_view(ViewStack* view_stack, View* view) {
furi_assert(view);

bool result = false;
ViewStackModel* model = view_get_model(view_stack->view);
ViewStackModel* model = view_get_model(&view_stack->view);
for(int i = 0; i < MAX_VIEWS; ++i) {
if(!model->views[i]) {
model->views[i] = view;
view_set_update_callback(model->views[i], view_stack_update_callback);
view_set_update_callback_context(model->views[i], view_stack->view);
view_set_update_callback_context(model->views[i], &view_stack->view);
if(view->enter_callback) {
view->enter_callback(view->context);
}
result = true;
break;
}
}
view_commit_model(view_stack->view, result);
view_commit_model(&view_stack->view, result);
furi_assert(result);
}

Expand All @@ -150,7 +147,7 @@ void view_stack_remove_view(ViewStack* view_stack, View* view) {
/* Removing view on-the-go is dangerous, but it is protected with
* Locking model, so system is consistent at any time. */
bool result = false;
ViewStackModel* model = view_get_model(view_stack->view);
ViewStackModel* model = view_get_model(&view_stack->view);
for(int i = 0; i < MAX_VIEWS; ++i) {
if(model->views[i] == view) {
if(view->exit_callback) {
Expand All @@ -163,11 +160,11 @@ void view_stack_remove_view(ViewStack* view_stack, View* view) {
break;
}
}
view_commit_model(view_stack->view, result);
view_commit_model(&view_stack->view, result);
furi_assert(result);
}

View* view_stack_get_view(ViewStack* view_stack) {
furi_assert(view_stack);
return view_stack->view;
return &view_stack->view;
}