1 /**
2  * @file lv_demo_music_list.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_demo_music_list.h"
10 #if LV_USE_DEMO_MUSIC
11 
12 #include "lv_demo_music_main.h"
13 
14 /*********************
15  *      DEFINES
16  *********************/
17 
18 /**********************
19  *      TYPEDEFS
20  **********************/
21 
22 /**********************
23  *  STATIC PROTOTYPES
24  **********************/
25 static lv_obj_t * add_list_btn(lv_obj_t * parent, uint32_t track_id);
26 static void btn_click_event_cb(lv_event_t * e);
27 
28 /**********************
29  *  STATIC VARIABLES
30  **********************/
31 static lv_obj_t * list;
32 static const lv_font_t * font_small;
33 static const lv_font_t * font_medium;
34 static lv_style_t style_scrollbar;
35 static lv_style_t style_btn;
36 static lv_style_t style_btn_pr;
37 static lv_style_t style_btn_chk;
38 static lv_style_t style_btn_dis;
39 static lv_style_t style_title;
40 static lv_style_t style_artist;
41 static lv_style_t style_time;
42 LV_IMG_DECLARE(img_lv_demo_music_btn_list_play);
43 LV_IMG_DECLARE(img_lv_demo_music_btn_list_pause);
44 
45 /**********************
46  *      MACROS
47  **********************/
48 
49 /**********************
50  *   GLOBAL FUNCTIONS
51  **********************/
52 
_lv_demo_music_list_create(lv_obj_t * parent)53 lv_obj_t * _lv_demo_music_list_create(lv_obj_t * parent)
54 {
55 #if LV_DEMO_MUSIC_LARGE
56     font_small = &lv_font_montserrat_16;
57     font_medium = &lv_font_montserrat_22;
58 #else
59     font_small = &lv_font_montserrat_12;
60     font_medium = &lv_font_montserrat_16;
61 #endif
62 
63     lv_style_init(&style_scrollbar);
64     lv_style_set_width(&style_scrollbar,  4);
65     lv_style_set_bg_opa(&style_scrollbar, LV_OPA_COVER);
66     lv_style_set_bg_color(&style_scrollbar, lv_color_hex3(0xeee));
67     lv_style_set_radius(&style_scrollbar, LV_RADIUS_CIRCLE);
68     lv_style_set_pad_right(&style_scrollbar, 4);
69 
70     static const lv_coord_t grid_cols[] = {LV_GRID_CONTENT, LV_GRID_FR(1), LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
71 #if LV_DEMO_MUSIC_LARGE
72     static const lv_coord_t grid_rows[] = {35,  30, LV_GRID_TEMPLATE_LAST};
73 #else
74     static const lv_coord_t grid_rows[] = {22,  17, LV_GRID_TEMPLATE_LAST};
75 #endif
76     lv_style_init(&style_btn);
77     lv_style_set_bg_opa(&style_btn, LV_OPA_TRANSP);
78     lv_style_set_grid_column_dsc_array(&style_btn, grid_cols);
79     lv_style_set_grid_row_dsc_array(&style_btn, grid_rows);
80     lv_style_set_grid_row_align(&style_btn, LV_GRID_ALIGN_CENTER);
81     lv_style_set_layout(&style_btn, LV_LAYOUT_GRID);
82 #if LV_DEMO_MUSIC_LARGE
83     lv_style_set_pad_right(&style_btn, 30);
84 #else
85     lv_style_set_pad_right(&style_btn, 20);
86 #endif
87     lv_style_init(&style_btn_pr);
88     lv_style_set_bg_opa(&style_btn_pr, LV_OPA_COVER);
89     lv_style_set_bg_color(&style_btn_pr,  lv_color_hex(0x4c4965));
90 
91     lv_style_init(&style_btn_chk);
92     lv_style_set_bg_opa(&style_btn_chk, LV_OPA_COVER);
93     lv_style_set_bg_color(&style_btn_chk, lv_color_hex(0x4c4965));
94 
95     lv_style_init(&style_btn_dis);
96     lv_style_set_text_opa(&style_btn_dis, LV_OPA_40);
97     lv_style_set_img_opa(&style_btn_dis, LV_OPA_40);
98 
99     lv_style_init(&style_title);
100     lv_style_set_text_font(&style_title, font_medium);
101     lv_style_set_text_color(&style_title, lv_color_hex(0xffffff));
102 
103     lv_style_init(&style_artist);
104     lv_style_set_text_font(&style_artist, font_small);
105     lv_style_set_text_color(&style_artist, lv_color_hex(0xb1b0be));
106 
107     lv_style_init(&style_time);
108     lv_style_set_text_font(&style_time, font_medium);
109     lv_style_set_text_color(&style_time, lv_color_hex(0xffffff));
110 
111     /*Create an empty transparent container*/
112     list = lv_obj_create(parent);
113     lv_obj_remove_style_all(list);
114     lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - LV_DEMO_MUSIC_HANDLE_SIZE);
115     lv_obj_set_y(list, LV_DEMO_MUSIC_HANDLE_SIZE);
116     lv_obj_add_style(list, &style_scrollbar, LV_PART_SCROLLBAR);
117     lv_obj_set_flex_flow(list, LV_FLEX_FLOW_COLUMN);
118 
119     uint32_t track_id;
120     for(track_id = 0; _lv_demo_music_get_title(track_id); track_id++) {
121         add_list_btn(list,  track_id);
122     }
123 
124 #if LV_DEMO_MUSIC_SQUARE || LV_DEMO_MUSIC_ROUND
125     lv_obj_set_scroll_snap_y(list, LV_SCROLL_SNAP_CENTER);
126 #endif
127 
128     _lv_demo_music_list_btn_check(0, true);
129 
130     return list;
131 }
132 
_lv_demo_music_list_close(void)133 void _lv_demo_music_list_close(void)
134 {
135     lv_style_reset(&style_scrollbar);
136     lv_style_reset(&style_btn);
137     lv_style_reset(&style_btn_pr);
138     lv_style_reset(&style_btn_chk);
139     lv_style_reset(&style_btn_dis);
140     lv_style_reset(&style_title);
141     lv_style_reset(&style_artist);
142     lv_style_reset(&style_time);
143 }
144 
_lv_demo_music_list_btn_check(uint32_t track_id,bool state)145 void _lv_demo_music_list_btn_check(uint32_t track_id, bool state)
146 {
147     lv_obj_t * btn = lv_obj_get_child(list, track_id);
148     lv_obj_t * icon = lv_obj_get_child(btn, 0);
149 
150     if(state) {
151         lv_obj_add_state(btn, LV_STATE_CHECKED);
152         lv_img_set_src(icon, &img_lv_demo_music_btn_list_pause);
153         lv_obj_scroll_to_view(btn, LV_ANIM_ON);
154     }
155     else {
156         lv_obj_clear_state(btn, LV_STATE_CHECKED);
157         lv_img_set_src(icon, &img_lv_demo_music_btn_list_play);
158     }
159 }
160 
161 /**********************
162  *   STATIC FUNCTIONS
163  **********************/
164 
add_list_btn(lv_obj_t * parent,uint32_t track_id)165 static lv_obj_t * add_list_btn(lv_obj_t * parent, uint32_t track_id)
166 {
167     uint32_t t = _lv_demo_music_get_track_length(track_id);
168     char time[32];
169     lv_snprintf(time, sizeof(time), "%"LV_PRIu32":%02"LV_PRIu32, t / 60, t % 60);
170     const char * title = _lv_demo_music_get_title(track_id);
171     const char * artist = _lv_demo_music_get_artist(track_id);
172 
173     lv_obj_t * btn = lv_obj_create(parent);
174     lv_obj_remove_style_all(btn);
175 #if LV_DEMO_MUSIC_LARGE
176     lv_obj_set_size(btn, lv_pct(100), 110);
177 #else
178     lv_obj_set_size(btn, lv_pct(100), 60);
179 #endif
180 
181     lv_obj_add_style(btn, &style_btn, 0);
182     lv_obj_add_style(btn, &style_btn_pr, LV_STATE_PRESSED);
183     lv_obj_add_style(btn, &style_btn_chk, LV_STATE_CHECKED);
184     lv_obj_add_style(btn, &style_btn_dis, LV_STATE_DISABLED);
185     lv_obj_add_event_cb(btn, btn_click_event_cb, LV_EVENT_CLICKED, NULL);
186 
187     if(track_id >= 3) {
188         lv_obj_add_state(btn, LV_STATE_DISABLED);
189     }
190 
191     lv_obj_t * icon = lv_img_create(btn);
192     lv_img_set_src(icon, &img_lv_demo_music_btn_list_play);
193     lv_obj_set_grid_cell(icon, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 2);
194 
195     lv_obj_t * title_label = lv_label_create(btn);
196     lv_label_set_text(title_label, title);
197     lv_obj_set_grid_cell(title_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
198     lv_obj_add_style(title_label, &style_title, 0);
199 
200     lv_obj_t * artist_label = lv_label_create(btn);
201     lv_label_set_text(artist_label, artist);
202     lv_obj_add_style(artist_label, &style_artist, 0);
203     lv_obj_set_grid_cell(artist_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
204 
205     lv_obj_t * time_label = lv_label_create(btn);
206     lv_label_set_text(time_label, time);
207     lv_obj_add_style(time_label, &style_time, 0);
208     lv_obj_set_grid_cell(time_label, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 0, 2);
209 
210     LV_IMG_DECLARE(img_lv_demo_music_list_border);
211     lv_obj_t * border = lv_img_create(btn);
212     lv_img_set_src(border, &img_lv_demo_music_list_border);
213     lv_obj_set_width(border, lv_pct(120));
214     lv_obj_align(border, LV_ALIGN_BOTTOM_MID, 0, 0);
215     lv_obj_add_flag(border, LV_OBJ_FLAG_IGNORE_LAYOUT);
216 
217     return btn;
218 }
219 
btn_click_event_cb(lv_event_t * e)220 static void btn_click_event_cb(lv_event_t * e)
221 {
222     lv_obj_t * btn = lv_event_get_target(e);
223 
224     uint32_t idx = lv_obj_get_child_id(btn);
225 
226     _lv_demo_music_play(idx);
227 }
228 #endif /*LV_USE_DEMO_MUSIC*/
229