1 #include "../../lv_examples.h"
2 #if LV_USE_MENU && LV_USE_MSGBOX && LV_BUILD_EXAMPLES
3 
4 enum {
5     LV_MENU_ITEM_BUILDER_VARIANT_1,
6     LV_MENU_ITEM_BUILDER_VARIANT_2
7 };
8 typedef uint8_t lv_menu_builder_variant_t;
9 
10 static void back_event_handler(lv_event_t * e);
11 static void switch_handler(lv_event_t * e);
12 lv_obj_t * root_page;
13 static lv_obj_t * create_text(lv_obj_t * parent, const char * icon, const char * txt,
14                               lv_menu_builder_variant_t builder_variant);
15 static lv_obj_t * create_slider(lv_obj_t * parent,
16                                 const char * icon, const char * txt, int32_t min, int32_t max, int32_t val);
17 static lv_obj_t * create_switch(lv_obj_t * parent,
18                                 const char * icon, const char * txt, bool chk);
19 
lv_example_menu_5(void)20 void lv_example_menu_5(void)
21 {
22     lv_obj_t * menu = lv_menu_create(lv_scr_act());
23 
24     lv_color_t bg_color = lv_obj_get_style_bg_color(menu, 0);
25     if(lv_color_brightness(bg_color) > 127) {
26         lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 10), 0);
27     }
28     else {
29         lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 50), 0);
30     }
31     lv_menu_set_mode_root_back_btn(menu, LV_MENU_ROOT_BACK_BTN_ENABLED);
32     lv_obj_add_event_cb(menu, back_event_handler, LV_EVENT_CLICKED, menu);
33     lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
34     lv_obj_center(menu);
35 
36     lv_obj_t * cont;
37     lv_obj_t * section;
38 
39     /*Create sub pages*/
40     lv_obj_t * sub_mechanics_page = lv_menu_page_create(menu, NULL);
41     lv_obj_set_style_pad_hor(sub_mechanics_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
42     lv_menu_separator_create(sub_mechanics_page);
43     section = lv_menu_section_create(sub_mechanics_page);
44     create_slider(section, LV_SYMBOL_SETTINGS, "Velocity", 0, 150, 120);
45     create_slider(section, LV_SYMBOL_SETTINGS, "Acceleration", 0, 150, 50);
46     create_slider(section, LV_SYMBOL_SETTINGS, "Weight limit", 0, 150, 80);
47 
48     lv_obj_t * sub_sound_page = lv_menu_page_create(menu, NULL);
49     lv_obj_set_style_pad_hor(sub_sound_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
50     lv_menu_separator_create(sub_sound_page);
51     section = lv_menu_section_create(sub_sound_page);
52     create_switch(section, LV_SYMBOL_AUDIO, "Sound", false);
53 
54     lv_obj_t * sub_display_page = lv_menu_page_create(menu, NULL);
55     lv_obj_set_style_pad_hor(sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
56     lv_menu_separator_create(sub_display_page);
57     section = lv_menu_section_create(sub_display_page);
58     create_slider(section, LV_SYMBOL_SETTINGS, "Brightness", 0, 150, 100);
59 
60     lv_obj_t * sub_software_info_page = lv_menu_page_create(menu, NULL);
61     lv_obj_set_style_pad_hor(sub_software_info_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
62     section = lv_menu_section_create(sub_software_info_page);
63     create_text(section, NULL, "Version 1.0", LV_MENU_ITEM_BUILDER_VARIANT_1);
64 
65     lv_obj_t * sub_legal_info_page = lv_menu_page_create(menu, NULL);
66     lv_obj_set_style_pad_hor(sub_legal_info_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
67     section = lv_menu_section_create(sub_legal_info_page);
68     for(uint32_t i = 0; i < 15; i++) {
69         create_text(section, NULL,
70                     "This is a long long long long long long long long long text, if it is long enough it may scroll.",
71                     LV_MENU_ITEM_BUILDER_VARIANT_1);
72     }
73 
74     lv_obj_t * sub_about_page = lv_menu_page_create(menu, NULL);
75     lv_obj_set_style_pad_hor(sub_about_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
76     lv_menu_separator_create(sub_about_page);
77     section = lv_menu_section_create(sub_about_page);
78     cont = create_text(section, NULL, "Software information", LV_MENU_ITEM_BUILDER_VARIANT_1);
79     lv_menu_set_load_page_event(menu, cont, sub_software_info_page);
80     cont = create_text(section, NULL, "Legal information", LV_MENU_ITEM_BUILDER_VARIANT_1);
81     lv_menu_set_load_page_event(menu, cont, sub_legal_info_page);
82 
83     lv_obj_t * sub_menu_mode_page = lv_menu_page_create(menu, NULL);
84     lv_obj_set_style_pad_hor(sub_menu_mode_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
85     lv_menu_separator_create(sub_menu_mode_page);
86     section = lv_menu_section_create(sub_menu_mode_page);
87     cont = create_switch(section, LV_SYMBOL_AUDIO, "Sidebar enable", true);
88     lv_obj_add_event_cb(lv_obj_get_child(cont, 2), switch_handler, LV_EVENT_VALUE_CHANGED, menu);
89 
90     /*Create a root page*/
91     root_page = lv_menu_page_create(menu, "Settings");
92     lv_obj_set_style_pad_hor(root_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);
93     section = lv_menu_section_create(root_page);
94     cont = create_text(section, LV_SYMBOL_SETTINGS, "Mechanics", LV_MENU_ITEM_BUILDER_VARIANT_1);
95     lv_menu_set_load_page_event(menu, cont, sub_mechanics_page);
96     cont = create_text(section, LV_SYMBOL_AUDIO, "Sound", LV_MENU_ITEM_BUILDER_VARIANT_1);
97     lv_menu_set_load_page_event(menu, cont, sub_sound_page);
98     cont = create_text(section, LV_SYMBOL_SETTINGS, "Display", LV_MENU_ITEM_BUILDER_VARIANT_1);
99     lv_menu_set_load_page_event(menu, cont, sub_display_page);
100 
101     create_text(root_page, NULL, "Others", LV_MENU_ITEM_BUILDER_VARIANT_1);
102     section = lv_menu_section_create(root_page);
103     cont = create_text(section, NULL, "About", LV_MENU_ITEM_BUILDER_VARIANT_1);
104     lv_menu_set_load_page_event(menu, cont, sub_about_page);
105     cont = create_text(section, LV_SYMBOL_SETTINGS, "Menu mode", LV_MENU_ITEM_BUILDER_VARIANT_1);
106     lv_menu_set_load_page_event(menu, cont, sub_menu_mode_page);
107 
108     lv_menu_set_sidebar_page(menu, root_page);
109 
110     lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED, NULL);
111 }
112 
back_event_handler(lv_event_t * e)113 static void back_event_handler(lv_event_t * e)
114 {
115     lv_obj_t * obj = lv_event_get_target(e);
116     lv_obj_t * menu = lv_event_get_user_data(e);
117 
118     if(lv_menu_back_btn_is_root(menu, obj)) {
119         lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "Root back btn click.", NULL, true);
120         lv_obj_center(mbox1);
121     }
122 }
123 
switch_handler(lv_event_t * e)124 static void switch_handler(lv_event_t * e)
125 {
126     lv_event_code_t code = lv_event_get_code(e);
127     lv_obj_t * menu = lv_event_get_user_data(e);
128     lv_obj_t * obj = lv_event_get_target(e);
129     if(code == LV_EVENT_VALUE_CHANGED) {
130         if(lv_obj_has_state(obj, LV_STATE_CHECKED)) {
131             lv_menu_set_page(menu, NULL);
132             lv_menu_set_sidebar_page(menu, root_page);
133             lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED, NULL);
134         }
135         else {
136             lv_menu_set_sidebar_page(menu, NULL);
137             lv_menu_clear_history(menu); /* Clear history because we will be showing the root page later */
138             lv_menu_set_page(menu, root_page);
139         }
140     }
141 }
142 
create_text(lv_obj_t * parent,const char * icon,const char * txt,lv_menu_builder_variant_t builder_variant)143 static lv_obj_t * create_text(lv_obj_t * parent, const char * icon, const char * txt,
144                               lv_menu_builder_variant_t builder_variant)
145 {
146     lv_obj_t * obj = lv_menu_cont_create(parent);
147 
148     lv_obj_t * img = NULL;
149     lv_obj_t * label = NULL;
150 
151     if(icon) {
152         img = lv_img_create(obj);
153         lv_img_set_src(img, icon);
154     }
155 
156     if(txt) {
157         label = lv_label_create(obj);
158         lv_label_set_text(label, txt);
159         lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
160         lv_obj_set_flex_grow(label, 1);
161     }
162 
163     if(builder_variant == LV_MENU_ITEM_BUILDER_VARIANT_2 && icon && txt) {
164         lv_obj_add_flag(img, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
165         lv_obj_swap(img, label);
166     }
167 
168     return obj;
169 }
170 
create_slider(lv_obj_t * parent,const char * icon,const char * txt,int32_t min,int32_t max,int32_t val)171 static lv_obj_t * create_slider(lv_obj_t * parent, const char * icon, const char * txt, int32_t min, int32_t max,
172                                 int32_t val)
173 {
174     lv_obj_t * obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_2);
175 
176     lv_obj_t * slider = lv_slider_create(obj);
177     lv_obj_set_flex_grow(slider, 1);
178     lv_slider_set_range(slider, min, max);
179     lv_slider_set_value(slider, val, LV_ANIM_OFF);
180 
181     if(icon == NULL) {
182         lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
183     }
184 
185     return obj;
186 }
187 
create_switch(lv_obj_t * parent,const char * icon,const char * txt,bool chk)188 static lv_obj_t * create_switch(lv_obj_t * parent, const char * icon, const char * txt, bool chk)
189 {
190     lv_obj_t * obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_1);
191 
192     lv_obj_t * sw = lv_switch_create(obj);
193     lv_obj_add_state(sw, chk ? LV_STATE_CHECKED : 0);
194 
195     return obj;
196 }
197 
198 #endif
199 
200