|
| 1 | +#include "displayapp/screens/settings/SettingAutoOpen.h" |
| 2 | +#include <lvgl/lvgl.h> |
| 3 | +#include "displayapp/DisplayApp.h" |
| 4 | +#include "displayapp/screens/Screen.h" |
| 5 | +#include "displayapp/screens/Symbols.h" |
| 6 | + |
| 7 | +using namespace Pinetime::Applications::Screens; |
| 8 | + |
| 9 | +constexpr std::array<SettingAutoOpen::Option, 3> SettingAutoOpen::options; |
| 10 | + |
| 11 | +namespace { |
| 12 | + void event_handler(lv_obj_t* obj, lv_event_t event) { |
| 13 | + auto* screen = static_cast<SettingAutoOpen*>(obj->user_data); |
| 14 | + if (event == LV_EVENT_VALUE_CHANGED) { |
| 15 | + screen->UpdateSelected(obj); |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +SettingAutoOpen::SettingAutoOpen(Pinetime::Controllers::Settings& settingsController) : settingsController {settingsController} { |
| 21 | + lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr); |
| 22 | + |
| 23 | + lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP); |
| 24 | + lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10); |
| 25 | + lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5); |
| 26 | + lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0); |
| 27 | + |
| 28 | + lv_obj_set_pos(container1, 10, 35); |
| 29 | + lv_obj_set_width(container1, LV_HOR_RES - 20); |
| 30 | + lv_obj_set_height(container1, LV_VER_RES - 20); |
| 31 | + lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT); |
| 32 | + |
| 33 | + lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr); |
| 34 | + lv_label_set_text_static(title, "Auto Open"); |
| 35 | + lv_label_set_align(title, LV_LABEL_ALIGN_CENTER); |
| 36 | + lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15); |
| 37 | + |
| 38 | + lv_obj_t* icon = lv_label_create(lv_scr_act(), nullptr); |
| 39 | + lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE); |
| 40 | + lv_label_set_text_static(icon, Symbols::wrench); |
| 41 | + lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER); |
| 42 | + lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0); |
| 43 | + |
| 44 | + for (unsigned int i = 0; i < options.size(); i++) { |
| 45 | + cbOption[i] = lv_checkbox_create(container1, nullptr); |
| 46 | + lv_checkbox_set_text(cbOption[i], options[i].name); |
| 47 | + if (settingsController.IsAutoOpenOn(static_cast<Controllers::Settings::AutoOpen>(i))) { |
| 48 | + lv_checkbox_set_checked(cbOption[i], true); |
| 49 | + } |
| 50 | + cbOption[i]->user_data = this; |
| 51 | + lv_obj_set_event_cb(cbOption[i], event_handler); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +SettingAutoOpen::~SettingAutoOpen() { |
| 56 | + lv_obj_clean(lv_scr_act()); |
| 57 | + settingsController.SaveSettings(); |
| 58 | +} |
| 59 | + |
| 60 | +void SettingAutoOpen::UpdateSelected(lv_obj_t* object) { |
| 61 | + // Find the index of the checkbox that triggered the event |
| 62 | + for (size_t i = 0; i < options.size(); i++) { |
| 63 | + if (cbOption[i] == object) { |
| 64 | + bool currentState = settingsController.IsAutoOpenOn(options[i].app); |
| 65 | + settingsController.SetAutoOpen(options[i].app, !currentState); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Update checkbox according to current apps. |
| 71 | + auto apps = settingsController.GetAutoOpen(); |
| 72 | + for (size_t i = 0; i < options.size(); ++i) { |
| 73 | + lv_checkbox_set_checked(cbOption[i], apps[i]); |
| 74 | + } |
| 75 | +} |
0 commit comments