~aleteoryx/muditaos

ref: 6e600784c7a54d8969e9a6c15e0dd385f8cbf8fd muditaos/module-apps/apps-common/popups/WindowWithTimer.cpp -rw-r--r-- 2.6 KiB
6e600784 — Lefucjusz [MOS-1069] Change A2DP stream volume scale to exponential 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "WindowWithTimer.hpp"
#include "ApplicationCommon.hpp"
#include "GuiTimer.hpp"

namespace gui
{
    namespace popup
    {
        constexpr auto timerName = "PopupTimer";
    } // namespace popup

    WindowWithTimer::WindowWithTimer(app::ApplicationCommon *app,
                                     const std::string &name,
                                     std::chrono::milliseconds timeout)
        : AppWindow{app, name}, timeout{timeout}
    {
        popupTimer    = app::GuiTimerFactory::createSingleShotTimer(application, this, popup::timerName, timeout);
        timerCallback = [this, name](Item &, sys::Timer &timer) {
            if (windowTimerActionCallback) {
                windowTimerActionCallback();
            }

            const auto &currentWindowName = application->getCurrentWindow()->getName();
            if (currentWindowName == name) {
                LOG_INFO("Delayed exit timer callback from '%s'", name.c_str());
                application->returnToPreviousWindow();
                return true;
            }
            LOG_WARN("Delayed exit from '%s' unsuccessful, different window '%s' displayed already",
                     name.c_str(),
                     currentWindowName.c_str());
            return false;
        };
    }

    void WindowWithTimer::resetTimer(std::chrono::seconds newTimeout)
    {
        if (newTimeout != noTimeoutChange) {
            timeout = newTimeout;
        }
        if (!popupTimer.isValid()) {
            popupTimer = app::GuiTimerFactory::createSingleShotTimer(application, this, popup::timerName, timeout);
        }
        popupTimer.restart(timeout);
    }

    void WindowWithTimer::detachTimerIfExists()
    {
        if (popupTimer.isValid()) {
            popupTimer.stop();
            popupTimer.reset(nullptr);
        }
    }

    void WindowWithTimer::destroyInterface()
    {
        erase();
    }

    void WindowWithTimer::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        resetTimer();
    }

    void WindowWithTimer::onClose([[maybe_unused]] CloseReason reason)
    {
        detachTimerIfExists();
    }

    WindowWithTimer::~WindowWithTimer()
    {
        destroyInterface();
        detachTimerIfExists();
    }

    bool WindowWithTimer::onInput([[maybe_unused]] const gui::InputEvent &inputEvent)
    {
        return false;
    }

    void WindowWithTimer::setWindowTimerActionCallback(std::function<void()> &&callback)
    {
        windowTimerActionCallback = std::move(callback);
    }
} // namespace gui