-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Gallery app #1384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yannickulrich
wants to merge
41
commits into
InfiniTimeOrg:main
Choose a base branch
from
yannickulrich:dev-imageview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Gallery app #1384
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
bf5c655
Added new app
yannickulrich c712c7b
Pass filesystem
yannickulrich e2bdfc4
List directory
yannickulrich bb5c5da
only count files in DirRead
yannickulrich cf9825b
Renamed app
yannickulrich a619ff8
Added ImageView
yannickulrich e966407
Pass path to ImageView
yannickulrich d7328f5
Added open function
yannickulrich 20548aa
Keep track of open image
yannickulrich ecde3c5
Fixed OBOE in open
yannickulrich d29dc3a
Open first image by default
yannickulrich c6d3adc
Pass full path to ImageView
yannickulrich 74b21e5
Show image
yannickulrich 2d73865
Added swiping through images
yannickulrich 2515609
Store a human readable name in ImageView
yannickulrich 73fe366
Show and hide label
yannickulrich 7cc90f8
Pass i and n to ImageView
yannickulrich ea938da
Added page indicator
yannickulrich 86700f5
Renamed ImageView
yannickulrich b91490c
New abstract class FileView
yannickulrich e45ddce
Check file extension
yannickulrich cff6e89
Added class for text files
yannickulrich 371b32f
Open and read text file
yannickulrich 843e021
Fixed z index
yannickulrich 4082dd3
Ran clang-format
yannickulrich 1935aff
Switched to CamelCase
yannickulrich 519d761
1. Added PageIndicator::CreateHorizontal
yannickulrich b4b96f6
2. Used page indicator
yannickulrich 38bbe60
3. Added PageIndicator::Hide
yannickulrich 6b4b11e
4. Re-implemented FileView::HideInfo
yannickulrich 46f820c
Avoid temp. double allocation of text buffer
yannickulrich 3be86cf
Check file properly
yannickulrich 9d32c32
New concept: index file instead of DirRead
yannickulrich b29a044
Top-align textview
yannickulrich c01bea9
Revert "New concept: index file instead of DirRead"
yannickulrich 480eb14
Removed assert
yannickulrich 12b62fa
Disabled sliding animation
yannickulrich fb0e206
Stop passing app around
yannickulrich a6ba3f9
Fixed Werrors
yannickulrich 370ecde
Fixed format
yannickulrich 3341114
Defined app trait
yannickulrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ namespace Pinetime { | |
| Steps, | ||
| Dice, | ||
| Weather, | ||
| Gallery, | ||
| PassKey, | ||
| QuickSettings, | ||
| Settings, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| #include <nrf_log.h> | ||
| #include "displayapp/screens/FileView.h" | ||
| #include "displayapp/DisplayApp.h" | ||
| #include "displayapp/InfiniTimeTheme.h" | ||
|
|
||
| using namespace Pinetime::Applications::Screens; | ||
|
|
||
| FileView::FileView(uint8_t screenID, uint8_t nScreens, const char* path) | ||
| : Screen(), pageIndicator(screenID, nScreens), screenID(screenID), nScreens(nScreens) { | ||
| label = nullptr; | ||
|
|
||
| const char* c = strrchr(path, '/') + 1; | ||
| if (c == nullptr) | ||
| c = path; | ||
|
|
||
| strncpy(name, c, LFS_NAME_MAX - 1); | ||
| char* pchar = strchr(name, '_'); | ||
| while (pchar != nullptr) { | ||
| *pchar = ' '; | ||
| pchar = strchr(pchar + 1, '_'); | ||
| } | ||
| } | ||
|
|
||
| void FileView::ShowInfo() { | ||
| if (label != nullptr) { | ||
| return; | ||
| } | ||
| label = lv_btn_create(lv_scr_act(), nullptr); | ||
| label->user_data = this; | ||
|
|
||
| lv_obj_set_height(label, 20); | ||
| lv_obj_set_width(label, LV_HOR_RES); | ||
| lv_obj_align(label, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); | ||
|
|
||
| lv_obj_t* txtMessage = lv_label_create(label, nullptr); | ||
| lv_obj_set_style_local_bg_color(label, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_NAVY); | ||
| lv_label_set_text_static(txtMessage, name); | ||
|
|
||
| pageIndicator.CreateHorizontal(); | ||
| } | ||
|
|
||
| void FileView::HideInfo() { | ||
| lv_obj_del(label); | ||
| pageIndicator.Hide(); | ||
|
|
||
| label = nullptr; | ||
| } | ||
|
|
||
| void FileView::ToggleInfo() { | ||
| if (label == nullptr) | ||
| ShowInfo(); | ||
| else | ||
| HideInfo(); | ||
| } | ||
|
|
||
| FileView::~FileView() { | ||
| lv_obj_clean(lv_scr_act()); | ||
| } | ||
|
|
||
| ImageView::ImageView(uint8_t screenID, uint8_t nScreens, const char* path) : FileView(screenID, nScreens, path) { | ||
| lv_obj_t* image = lv_img_create(lv_scr_act(), nullptr); | ||
| lv_img_set_src(image, path); | ||
| lv_obj_align(image, lv_scr_act(), LV_ALIGN_CENTER, 0, 0); | ||
|
|
||
| ShowInfo(); | ||
| } | ||
|
|
||
| TextView::TextView(uint8_t screenID, uint8_t nScreens, const char* path, Pinetime::Controllers::FS& fs) | ||
| : FileView(screenID, nScreens, path) { | ||
|
|
||
| lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr); | ||
| lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK); | ||
| lv_obj_set_width(label, LV_HOR_RES); | ||
|
|
||
| lfs_info info = {0}; | ||
| if (fs.Stat(path + 2, &info) != LFS_ERR_OK) { | ||
| lv_label_set_text_static(label, "could not stat file"); | ||
| return; | ||
| } | ||
| if (info.type != LFS_TYPE_REG) { | ||
| lv_label_set_text_static(label, "not a file"); | ||
| return; | ||
| } | ||
|
|
||
| buf = (char*) lv_mem_alloc(info.size); | ||
| if (buf == nullptr) { | ||
| lv_label_set_text_static(label, "could not allocate buffer"); | ||
| return; | ||
| } | ||
|
|
||
| lfs_file_t fp; | ||
| if (fs.FileOpen(&fp, path + 2, LFS_O_RDONLY) != LFS_ERR_OK) { | ||
| lv_label_set_text_static(label, "could not open file"); | ||
| lv_mem_free(buf); | ||
| return; | ||
| } | ||
|
|
||
| fs.FileRead(&fp, reinterpret_cast<uint8_t*>(buf), info.size); | ||
| lv_label_set_text_static(label, buf); | ||
|
|
||
| fs.FileClose(&fp); | ||
|
|
||
| ShowInfo(); | ||
| } | ||
|
|
||
| TextView::~TextView() { | ||
| if (buf != nullptr) | ||
| lv_mem_free(buf); | ||
| lv_obj_clean(lv_scr_act()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #pragma once | ||
|
|
||
| #include "displayapp/screens/Screen.h" | ||
| #include "displayapp/DisplayApp.h" | ||
| #include "displayapp/widgets/PageIndicator.h" | ||
| #include <lvgl/lvgl.h> | ||
|
|
||
| namespace Pinetime { | ||
| namespace Applications { | ||
| namespace Screens { | ||
| class FileView : public Screen { | ||
| public: | ||
| FileView(uint8_t screenID, uint8_t nScreens, const char* path); | ||
| ~FileView() override; | ||
|
|
||
| void ShowInfo(); | ||
| void HideInfo(); | ||
| void ToggleInfo(); | ||
|
|
||
| private: | ||
| char name[LFS_NAME_MAX]; | ||
| lv_obj_t* label; | ||
|
|
||
| Widgets::PageIndicator pageIndicator; | ||
| uint8_t screenID, nScreens; | ||
| }; | ||
|
|
||
| class ImageView : public FileView { | ||
| public: | ||
| ImageView(uint8_t screenID, uint8_t nScreens, const char* path); | ||
| }; | ||
|
|
||
| class TextView : public FileView { | ||
| public: | ||
| TextView(uint8_t screenID, uint8_t nScreens, const char* path, Pinetime::Controllers::FS& fs); | ||
| ~TextView() override; | ||
|
|
||
| private: | ||
| char* buf; | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #include <nrf_log.h> | ||
| #include "displayapp/screens/Gallery.h" | ||
| #include "displayapp/DisplayApp.h" | ||
|
|
||
| using namespace Pinetime::Applications::Screens; | ||
|
|
||
| Gallery::Gallery(Pinetime::Controllers::FS& filesystem) : Screen(), filesystem(filesystem) { | ||
| ListDir(); | ||
|
|
||
| if (nScreens == 0) { | ||
| lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr); | ||
| lv_label_set_text_static(title, "no images found"); | ||
| lv_label_set_align(title, LV_LABEL_ALIGN_CENTER); | ||
| lv_obj_align(title, lv_scr_act(), LV_ALIGN_CENTER, 0, 0); | ||
| } else { | ||
| Open(0); | ||
| } | ||
| } | ||
|
|
||
| Gallery::~Gallery() { | ||
| lv_obj_clean(lv_scr_act()); | ||
| } | ||
|
|
||
| bool Gallery::OnTouchEvent(Pinetime::Applications::TouchEvents event) { | ||
| switch (event) { | ||
| case Pinetime::Applications::TouchEvents::SwipeRight: | ||
| return Open(index - 1); | ||
| case Pinetime::Applications::TouchEvents::SwipeLeft: | ||
| return Open(index + 1); | ||
| case Pinetime::Applications::TouchEvents::LongTap: | ||
| case Pinetime::Applications::TouchEvents::DoubleTap: | ||
| current->ToggleInfo(); | ||
| return true; | ||
| case Pinetime::Applications::TouchEvents::None: | ||
| case Pinetime::Applications::TouchEvents::Tap: | ||
| case Pinetime::Applications::TouchEvents::SwipeUp: | ||
| case Pinetime::Applications::TouchEvents::SwipeDown: | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void Gallery::ListDir() { | ||
| lfs_dir_t dir = {0}; | ||
| lfs_info info = {0}; | ||
| nScreens = 0; | ||
|
|
||
| int res = filesystem.DirOpen(directory, &dir); | ||
| if (res != 0) { | ||
| NRF_LOG_INFO("[Gallery] can't find directory"); | ||
| return; | ||
| } | ||
| while (filesystem.DirRead(&dir, &info)) { | ||
| if (info.type == LFS_TYPE_DIR) | ||
| continue; | ||
| nScreens++; | ||
| } | ||
| res = filesystem.DirClose(&dir); | ||
| if (res != 0) { | ||
| NRF_LOG_INFO("[Gallery] DirClose failed"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| bool Gallery::Open(int n) { | ||
| if ((n < 0) || (n >= nScreens)) | ||
| return false; | ||
|
|
||
| index = n; | ||
|
|
||
| lfs_dir_t dir = {0}; | ||
| lfs_info info = {0}; | ||
|
|
||
| int res = filesystem.DirOpen(directory, &dir); | ||
| if (res != 0) { | ||
| NRF_LOG_INFO("[Gallery] can't find directory"); | ||
| return false; | ||
| } | ||
| int i = 0; | ||
| while (filesystem.DirRead(&dir, &info)) { | ||
| if (info.type == LFS_TYPE_DIR) | ||
| continue; | ||
| if (n == i) | ||
| break; | ||
| i++; | ||
| } | ||
| res = filesystem.DirClose(&dir); | ||
| if (res != 0) { | ||
| NRF_LOG_INFO("[Gallery] DirClose failed"); | ||
| return false; | ||
| } | ||
|
|
||
| if (current != nullptr) { | ||
| current.reset(nullptr); | ||
| } | ||
|
|
||
| char fullname[LFS_NAME_MAX] = "F:"; | ||
| strncat(fullname, directory, sizeof(fullname) - 2 - 1); | ||
| strncat(fullname, info.name, sizeof(fullname) - strlen(directory) - 2 - 1); | ||
|
|
||
| if (StringEndsWith(fullname, ".bin")) { | ||
| current = std::make_unique<ImageView>(n, nScreens, fullname); | ||
| } else if (StringEndsWith(fullname, ".txt")) { | ||
| current = std::make_unique<TextView>(n, nScreens, fullname, filesystem); | ||
| } else { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int Gallery::StringEndsWith(const char* str, const char* suffix) { | ||
| int str_len = strlen(str); | ||
| int suffix_len = strlen(suffix); | ||
|
|
||
| return (str_len >= suffix_len) && (0 == strcmp(str + (str_len - suffix_len), suffix)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #pragma once | ||
|
|
||
| #include "displayapp/screens/Screen.h" | ||
| #include "displayapp/screens/FileView.h" | ||
| #include "displayapp/DisplayApp.h" | ||
| #include <lvgl/lvgl.h> | ||
| #include "Symbols.h" | ||
|
|
||
| namespace Pinetime { | ||
| namespace Applications { | ||
| namespace Screens { | ||
| class Gallery : public Screen { | ||
| public: | ||
| static constexpr const char* directory = "/gallery/"; | ||
|
|
||
| Gallery(Pinetime::Controllers::FS& filesystem); | ||
| ~Gallery() override; | ||
| bool OnTouchEvent(Pinetime::Applications::TouchEvents event) override; | ||
|
|
||
| private: | ||
| int StringEndsWith(const char* str, const char* suffix); | ||
|
|
||
| Pinetime::Controllers::FS& filesystem; | ||
| std::unique_ptr<FileView> current; | ||
|
|
||
| void ListDir(); | ||
| bool Open(int n); | ||
| int nScreens; | ||
| int index; | ||
| }; | ||
| } | ||
|
|
||
| template <> | ||
| struct AppTraits<Apps::Gallery> { | ||
| static constexpr Apps app = Apps::Gallery; | ||
| static constexpr const char* icon = Screens::Symbols::gallery; | ||
|
|
||
| static Screens::Screen* Create(AppControllers& controllers) { | ||
| return new Screens::Gallery(controllers.filesystem); | ||
| }; | ||
|
|
||
| static bool IsAvailable(Pinetime::Controllers::FS& filesystem) { | ||
| return true; | ||
| }; | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it belongs to some
utilfile and have a more generic name. But maybe it is fine for now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this but I don't think that we currently have
utilfile. Of course I could create one but I wouldn't want to disrupt any plans the maintainers might have. I would argue that this PR should be as minimal as possible, even if that means that code needs to be moved around later on.