From 25ba0df6af55406b5d86526441100a085d0f4229 Mon Sep 17 00:00:00 2001 From: Mairon <3043462073@qq.com> Date: Wed, 25 Sep 2024 23:09:49 +0800 Subject: [PATCH] perf: carousel animation --- src/Controller/View/DiscoveryPage.cc | 32 +++++++++------------------- src/Controller/View/DiscoveryPage.h | 1 + ui/views/page/discovery.slint | 26 ++++++---------------- 3 files changed, 17 insertions(+), 42 deletions(-) diff --git a/src/Controller/View/DiscoveryPage.cc b/src/Controller/View/DiscoveryPage.cc index 1ba17921..609727ce 100644 --- a/src/Controller/View/DiscoveryPage.cc +++ b/src/Controller/View/DiscoveryPage.cc @@ -1,11 +1,10 @@ -#include "Controller/AsyncExecutor.hh" -#include "Controller/Convert.h" -#include "Controller/Core/ViewManager.h" -#include "Controller/UiBridge.h" -#include "Infrastructure/Network/NetworkClient.h" +#include +#include +#include +#include #include +#include #include -#include #include EVENTO_UI_START @@ -18,6 +17,7 @@ void DiscoveryPage::onCreate() { auto& self = *this; self->on_load_active_events([this] { loadActiveEvents(); }); self->on_load_latest_events([this] { loadLatestEvents(); }); + self->on_image_manually_changed([this] { timer.restart(); }); self->on_navigate_to_detail([this](EventStruct eventStruct) { spdlog::debug("navigate to DetailPage, current event is {}", eventStruct.summary.data()); bridge.getViewManager().navigateTo(ViewName::DetailPage, eventStruct); @@ -97,22 +97,10 @@ void DiscoveryPage::loadHomeSlides() { } void DiscoveryPage::slidesAutoRotation() { - executor()->asyncExecute([]() -> Task { co_return; }, - [&self = *this]() { - int cntIndex = self->get_image_index(); - bool manuallyChanged = self->get_image_manually_changed(); - if (!manuallyChanged) { - if (cntIndex == self->get_carousel_source()->row_count() - 1) { - self->set_image_index(0); - return; - } - self->set_image_index(cntIndex + 1); - } else { - self->set_image_manually_changed(false); - } - }, - std::chrono::milliseconds(5000), - AsyncExecutor::Periodic | AsyncExecutor::Delay); + timer.start(slint::TimerMode::Repeated, 5s, [&self = *this] { + self->set_image_index((self->get_image_index() + 1) + % static_cast(self->get_carousel_source()->row_count())); + }); } EVENTO_UI_END diff --git a/src/Controller/View/DiscoveryPage.h b/src/Controller/View/DiscoveryPage.h index 407d857f..3deb4031 100644 --- a/src/Controller/View/DiscoveryPage.h +++ b/src/Controller/View/DiscoveryPage.h @@ -11,6 +11,7 @@ class DiscoveryPage : public BasicView, private GlobalAgent DiscoveryPage(slint::ComponentHandle uiEntry, UiBridge& bridge); private: + slint::Timer timer; void onCreate() override; void onShow() override; diff --git a/ui/views/page/discovery.slint b/ui/views/page/discovery.slint index 89237ecb..908ba601 100644 --- a/ui/views/page/discovery.slint +++ b/ui/views/page/discovery.slint @@ -1,23 +1,18 @@ -import { Page } from "../../components/index.slint"; -import { EventCardGroup } from "../../components/event_card.slint"; -import { EventStruct } from "../../components/event_struct.slint"; -import { SCarousel } from "../../modules/surrealism-ui/index.slint"; -import { EventoImageToken } from "../../assets/image/image_token.slint"; import { Token } from "../../global.slint"; import { ScrollView } from "std-widgets.slint"; -import { Button, ButtonType, LoadingAnimation, PageState, Empty } from "../../components/index.slint"; +import { Button, ButtonType, LoadingAnimation, Page, PageState, Empty, EventStruct, EventCardGroup } from "../../components/index.slint"; export global DiscoveryPageBridge { in-out property image-index: 0; - in-out property image-manually-changed: false; + callback image-manually-changed; in property active-events-state: PageState.normal; in property latest-events-state: PageState.normal; in property <[EventStruct]> active-events: []; in property <[EventStruct]> latest-events: []; in property <[image]> carousel-source: [ - EventoImageToken.image.display.event.default[0], - EventoImageToken.image.display.event.default[1], - EventoImageToken.image.display.event.default[2], + Token.image.display.event.default[0], + Token.image.display.event.default[1], + Token.image.display.event.default[2], ]; callback load-active-events(); callback load-latest-events(); @@ -27,7 +22,6 @@ export global DiscoveryPageBridge { export component ImageCarousel inherits Rectangle { in property <[image]> sources; in-out property current-index: 0; - in-out property image-manually-changed: false; border-radius: 12px; clip: true; @@ -68,19 +62,12 @@ export component ImageCarousel inherits Rectangle { height: 100%; clicked => { current-index = index; - image-manually-changed = true; + DiscoveryPageBridge.image-manually-changed(); } } } } } - - // ensure image-switching animation works fine - LoadingAnimation { - z: -1; - color: Token.color.on-surface; - height: 40px; - } } export component DiscoveryPage inherits Page { @@ -93,7 +80,6 @@ export component DiscoveryPage inherits Page { width: 100%; height: root.height * 0.5; current-index <=> DiscoveryPageBridge.image-index; - image-manually-changed <=> DiscoveryPageBridge.image-manually-changed; sources: DiscoveryPageBridge.carousel-source; }