1 /**
2  * @file lv_example_fragment_2.c
3  * @brief Navigation stack using obj fragment
4  */
5 #include "../../lv_examples.h"
6 
7 #if LV_USE_FRAGMENT && LV_USE_WIN && LV_BUILD_EXAMPLES
8 
9 static void sample_fragment_ctor(lv_fragment_t * self, void * args);
10 
11 static lv_obj_t * sample_fragment_create_obj(lv_fragment_t * self, lv_obj_t * parent);
12 
13 static void sample_push_click(lv_event_t * e);
14 
15 static void sample_pop_click(lv_event_t * e);
16 
17 static void sample_container_del(lv_event_t * e);
18 
19 static void sample_fragment_inc_click(lv_event_t * e);
20 
21 typedef struct sample_fragment_t {
22     lv_fragment_t base;
23     lv_obj_t * label;
24     int depth;
25     int counter;
26 } sample_fragment_t;
27 
28 static const lv_fragment_class_t sample_cls = {
29     .constructor_cb = sample_fragment_ctor,
30     .create_obj_cb = sample_fragment_create_obj,
31     .instance_size = sizeof(sample_fragment_t)
32 };
33 
34 static lv_obj_t * container = NULL;
35 
lv_example_fragment_2(void)36 void lv_example_fragment_2(void)
37 {
38     lv_obj_t * root = lv_obj_create(lv_scr_act());
39     lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));
40     lv_obj_set_layout(root, LV_LAYOUT_GRID);
41     static const lv_coord_t col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
42     static const lv_coord_t row_dsc[] = {LV_GRID_FR(1), LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
43     lv_obj_set_grid_dsc_array(root, col_dsc, row_dsc);
44     container = lv_obj_create(root);
45     lv_obj_remove_style_all(container);
46     lv_obj_set_grid_cell(container, LV_GRID_ALIGN_STRETCH, 0, 2, LV_GRID_ALIGN_STRETCH, 0, 1);
47 
48     lv_obj_t * push_btn = lv_btn_create(root);
49     lv_obj_t * push_label = lv_label_create(push_btn);
50     lv_label_set_text(push_label, "Push");
51 
52     lv_obj_t * pop_btn = lv_btn_create(root);
53     lv_obj_t * pop_label = lv_label_create(pop_btn);
54     lv_label_set_text(pop_label, "Pop");
55     lv_obj_set_grid_cell(push_btn, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
56     lv_obj_set_grid_cell(pop_btn, LV_GRID_ALIGN_END, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
57 
58     lv_fragment_manager_t * manager = lv_fragment_manager_create(NULL);
59     /* Clean up the fragment manager before objects in containers got deleted */
60     lv_obj_add_event_cb(root, sample_container_del, LV_EVENT_DELETE, manager);
61 
62     int depth = 0;
63     lv_fragment_t * fragment = lv_fragment_create(&sample_cls, &depth);
64     lv_fragment_manager_push(manager, fragment, &container);
65     lv_obj_add_event_cb(push_btn, sample_push_click, LV_EVENT_CLICKED, manager);
66     lv_obj_add_event_cb(pop_btn, sample_pop_click, LV_EVENT_CLICKED, manager);
67 }
68 
69 
sample_fragment_ctor(lv_fragment_t * self,void * args)70 static void sample_fragment_ctor(lv_fragment_t * self, void * args)
71 {
72     LV_UNUSED(args);
73     ((sample_fragment_t *) self)->depth = *((int *) args);
74     ((sample_fragment_t *) self)->counter = 0;
75 }
76 
sample_fragment_create_obj(lv_fragment_t * self,lv_obj_t * parent)77 static lv_obj_t * sample_fragment_create_obj(lv_fragment_t * self, lv_obj_t * parent)
78 {
79     sample_fragment_t * fragment = (sample_fragment_t *) self;
80     lv_obj_t * content = lv_obj_create(parent);
81     lv_obj_remove_style_all(content);
82     lv_obj_set_style_bg_opa(content, LV_OPA_50, 0);
83     lv_obj_set_style_bg_color(content, lv_palette_main(LV_PALETTE_YELLOW), 0);
84     lv_obj_set_size(content, LV_PCT(100), LV_PCT(100));
85     lv_obj_set_flex_flow(content, LV_FLEX_FLOW_COLUMN);
86     lv_obj_t * depth = lv_label_create(content);
87     lv_label_set_text_fmt(depth, "Depth: %d", fragment->depth);
88     lv_obj_t * label = lv_label_create(content);
89     fragment->label = label;
90     lv_label_set_text_fmt(label, "The button has been pressed %d times", fragment->counter);
91 
92     lv_obj_t * inc_btn = lv_btn_create(content);
93     lv_obj_t * inc_label = lv_label_create(inc_btn);
94     lv_label_set_text(inc_label, "+1");
95     lv_obj_add_event_cb(inc_btn, sample_fragment_inc_click, LV_EVENT_CLICKED, fragment);
96 
97     return content;
98 }
99 
sample_push_click(lv_event_t * e)100 static void sample_push_click(lv_event_t * e)
101 {
102     lv_fragment_manager_t * manager = (lv_fragment_manager_t *) lv_event_get_user_data(e);
103     size_t stack_size = lv_fragment_manager_get_stack_size(manager);
104     lv_fragment_t * fragment = lv_fragment_create(&sample_cls, &stack_size);
105     lv_fragment_manager_push(manager, fragment, &container);
106 }
107 
sample_pop_click(lv_event_t * e)108 static void sample_pop_click(lv_event_t * e)
109 {
110     lv_fragment_manager_t * manager = (lv_fragment_manager_t *) lv_event_get_user_data(e);
111     lv_fragment_manager_pop(manager);
112 }
113 
sample_container_del(lv_event_t * e)114 static void sample_container_del(lv_event_t * e)
115 {
116     lv_fragment_manager_t * manager = (lv_fragment_manager_t *) lv_event_get_user_data(e);
117     lv_fragment_manager_del(manager);
118 }
119 
sample_fragment_inc_click(lv_event_t * e)120 static void sample_fragment_inc_click(lv_event_t * e)
121 {
122     sample_fragment_t * fragment = (sample_fragment_t *) lv_event_get_user_data(e);
123     fragment->counter++;
124     lv_label_set_text_fmt(fragment->label, "The button has been pressed %d times", fragment->counter);
125 }
126 
127 #endif
128