From 6b7b22e660111fe65abe55a5a17d9577e96c971b Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Fri, 19 Aug 2022 11:14:13 +0200 Subject: [PATCH] fix: save and load main view ID --- radio/src/gui/colorlcd/layout.cpp | 16 ++++++++---- radio/src/gui/colorlcd/view_main.cpp | 38 +++++++++++++++++----------- radio/src/gui/colorlcd/view_main.h | 2 -- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/radio/src/gui/colorlcd/layout.cpp b/radio/src/gui/colorlcd/layout.cpp index d60874f94..085e8f92c 100644 --- a/radio/src/gui/colorlcd/layout.cpp +++ b/radio/src/gui/colorlcd/layout.cpp @@ -119,18 +119,24 @@ void loadCustomScreens() // layout is ok, let's add it viewMain->addMainView(screen, i); - // screen->attach(viewMain); - // viewMain->setMainViewsCount(i + 1); - // screen->setLeft(viewMain->getMainViewLeftPos(i)); i++; } auto topbar = viewMain->getTopbar(); topbar->load(); + + if (g_model.view < viewMain->getMainViewsCount()) { + viewMain->setCurrentMainView(g_model.view); + } else if (viewMain->getMainViewsCount() > 0) { + g_model.view = viewMain->getMainViewsCount() - 1; + storageDirty(EE_MODEL); + viewMain->setCurrentMainView(g_model.view); + } + // else { + // TODO: load some default view? + // } - viewMain->setCurrentMainView(0); viewMain->updateTopbarVisibility(); - // viewMain->setFocus(); } // diff --git a/radio/src/gui/colorlcd/view_main.cpp b/radio/src/gui/colorlcd/view_main.cpp index a2b7cea85..76cec7fc3 100644 --- a/radio/src/gui/colorlcd/view_main.cpp +++ b/radio/src/gui/colorlcd/view_main.cpp @@ -40,10 +40,27 @@ static void tile_view_deleted_cb(lv_event_t* e) if (obj == target) { lv_obj_del(obj); } } +static void saveViewId(unsigned view) +{ + if (view != g_model.view) { + TRACE("save view #%d", view); + g_model.view = view; + storageDirty(EE_MODEL); + } +} + static void tile_view_scroll(lv_event_t* e) { - (void)e; - if (ViewMain::instance()) ViewMain::instance()->updateTopbarVisibility(); + // (void)e; + auto viewMain = ViewMain::instance(); + if (viewMain) { + if (lv_event_get_code(e) == LV_EVENT_SCROLL_END) { + auto view = viewMain->getCurrentMainView(); + saveViewId(view); + } else { + viewMain->updateTopbarVisibility(); + } + } } ViewMain * ViewMain::_instance = nullptr; @@ -53,11 +70,6 @@ ViewMain::ViewMain(): { Layer::push(this); - // setFocusHandler([&](bool focus) { - // TRACE("[ViewMain] Focus %s", - // focus ? "gained" : "lost"); - // }); - tile_view = lv_tileview_create(lvobj); lv_obj_set_pos(tile_view, rect.x, rect.y); lv_obj_set_size(tile_view, rect.w, rect.h); @@ -68,6 +80,7 @@ ViewMain::ViewMain(): lv_obj_add_flag(tile_view, LV_OBJ_FLAG_EVENT_BUBBLE); lv_obj_set_user_data(tile_view, this); lv_obj_add_event_cb(tile_view, tile_view_scroll, LV_EVENT_SCROLL, nullptr); + lv_obj_add_event_cb(tile_view, tile_view_scroll, LV_EVENT_SCROLL_END, nullptr); lv_obj_add_event_cb(lvobj, ViewMain::long_pressed, LV_EVENT_LONG_PRESSED, nullptr); // create last to be on top @@ -134,6 +147,7 @@ void ViewMain::nextMainView() view = 0; setCurrentMainView(view); + saveViewId(view); } void ViewMain::previousMainView() @@ -145,6 +159,7 @@ void ViewMain::previousMainView() view = getMainViewsCount() - 1; setCurrentMainView(view); + saveViewId(view); } Topbar* ViewMain::getTopbar() @@ -184,11 +199,9 @@ void ViewMain::updateTopbarVisibility() coord_t pageWidth = width(); if (!pageWidth) return; - int view = scrollPos / pageWidth; - // TODO: cap view ??? - int leftScroll = scrollPos % width(); if (leftScroll == 0) { + int view = scrollPos / pageWidth; setTopbarVisible(hasTopbar(view)); if (customScreens[view]) customScreens[view]->adjustLayout(); @@ -338,11 +351,6 @@ void ViewMain::paint(BitmapBuffer * dc) // TODO: set it as "window background" w/ LVGL OpenTxTheme::instance()->drawBackground(dc); - - // TODO: move to "screen setup" - if (g_model.view >= getMainViewsCount()) { - g_model.view = 0; - } } void ViewMain::ws_timer(lv_timer_t* t) diff --git a/radio/src/gui/colorlcd/view_main.h b/radio/src/gui/colorlcd/view_main.h index c92d278cc..f21d690f6 100644 --- a/radio/src/gui/colorlcd/view_main.h +++ b/radio/src/gui/colorlcd/view_main.h @@ -64,8 +64,6 @@ class ViewMain: public Window rect_t getMainZone(rect_t zone, bool hasTopbar) const; unsigned getMainViewsCount() const; - //coord_t getMainViewLeftPos(unsigned view) const; - unsigned getCurrentMainView() const; void setCurrentMainView(unsigned view);