|
| 1 | +#include "PawnList.h" |
| 2 | + |
| 3 | +#include "components/fs/FS.h" |
| 4 | +#include "displayapp/DisplayApp.h" |
| 5 | + |
| 6 | +using namespace Pinetime::Applications::Screens; |
| 7 | + |
| 8 | +#define PAWN_EXTENSION ".amx" |
| 9 | +constexpr int appsPerScreen = 4; |
| 10 | + |
| 11 | +void ButtonCallback(lv_obj_t* obj, lv_event_t event) { |
| 12 | + if (event == LV_EVENT_CLICKED) { |
| 13 | + PawnList::AppListing* data = static_cast<PawnList::AppListing*>(obj->user_data); |
| 14 | + data->pawnList->ButtonClickedHandler(data->index); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +PawnList::PawnList(Pinetime::Controllers::FS& filesystem, Applications::DisplayApp* displayApp) : displayApp(displayApp) { |
| 19 | + lfs_dir_t dir; |
| 20 | + lfs_info info; |
| 21 | + if (filesystem.DirOpen("/apps", &dir) == LFS_ERR_OK) { |
| 22 | + int i = 0; |
| 23 | + |
| 24 | + while (filesystem.DirRead(&dir, &info)) { |
| 25 | + if (info.type == LFS_TYPE_DIR) |
| 26 | + continue; |
| 27 | + |
| 28 | + size_t len = strlen(info.name); |
| 29 | + if (len < sizeof(PAWN_EXTENSION) || strcmp(info.name + len - (sizeof(PAWN_EXTENSION) - 1), PAWN_EXTENSION) != 0) |
| 30 | + continue; |
| 31 | + |
| 32 | + pawnApps.push_back(AppListing { |
| 33 | + .pawnList = this, |
| 34 | + .name = std::string(info.name, len - (sizeof(PAWN_EXTENSION) - 1)), // Remove extension |
| 35 | + .index = i++, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + filesystem.DirClose(&dir); |
| 40 | + } |
| 41 | + |
| 42 | + ShowScreen(); |
| 43 | +} |
| 44 | + |
| 45 | +PawnList::~PawnList() { |
| 46 | + lv_obj_clean(lv_scr_act()); |
| 47 | +} |
| 48 | + |
| 49 | +void PawnList::ButtonClickedHandler(int index) { |
| 50 | + char path[LFS_NAME_MAX]; |
| 51 | + snprintf(path, sizeof(path), "/apps/%s" PAWN_EXTENSION, pawnApps[index].name.c_str()); |
| 52 | + displayApp->StartPawnApp(path, Applications::DisplayApp::FullRefreshDirections::LeftAnim); |
| 53 | +} |
| 54 | + |
| 55 | +bool PawnList::OnTouchEvent(TouchEvents event) { |
| 56 | + switch (event) { |
| 57 | + case TouchEvents::SwipeUp: |
| 58 | + if (currentScreen < pawnApps.size() / appsPerScreen) { |
| 59 | + currentScreen++; |
| 60 | + displayApp->SetFullRefresh(DisplayApp::FullRefreshDirections::Up); |
| 61 | + ShowScreen(); |
| 62 | + return true; |
| 63 | + } |
| 64 | + break; |
| 65 | + |
| 66 | + case TouchEvents::SwipeDown: |
| 67 | + if (currentScreen > 0) { |
| 68 | + currentScreen--; |
| 69 | + displayApp->SetFullRefresh(DisplayApp::FullRefreshDirections::Down); |
| 70 | + ShowScreen(); |
| 71 | + return true; |
| 72 | + } |
| 73 | + break; |
| 74 | + |
| 75 | + default: |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | +} |
| 81 | + |
| 82 | +void PawnList::ShowScreen() { |
| 83 | + int relCount = 0; |
| 84 | + |
| 85 | + lv_obj_clean(lv_scr_act()); |
| 86 | + |
| 87 | + for (size_t i = appsPerScreen * currentScreen; i < pawnApps.size(); i++) { |
| 88 | + lv_obj_t* btn = lv_btn_create(lv_scr_act(), nullptr); |
| 89 | + lv_obj_set_size(btn, 240, 240 / appsPerScreen); |
| 90 | + lv_obj_set_pos(btn, 0, relCount * 240 / appsPerScreen); |
| 91 | + lv_obj_set_event_cb(btn, ButtonCallback); |
| 92 | + lv_obj_set_user_data(btn, &pawnApps[i]); |
| 93 | + |
| 94 | + lv_obj_t* label = lv_label_create(btn, nullptr); |
| 95 | + lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL); |
| 96 | + lv_label_set_align(label, LV_LABEL_ALIGN_CENTER); |
| 97 | + lv_label_set_text_static(label, pawnApps[i].name.c_str()); |
| 98 | + lv_obj_set_style_local_pad_hor(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, 10); |
| 99 | + lv_obj_set_width(label, 240); |
| 100 | + |
| 101 | + relCount++; |
| 102 | + } |
| 103 | +} |
0 commit comments