1// Aseprite
2// Copyright (C) 2019-2022 Igara Studio S.A.
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/ui/task_widget.h"
12
13#include "app/i18n/strings.h"
14#include "app/modules/gui.h"
15#include "app/ui/skin/skin_theme.h"
16#include "ui/scale.h"
17
18#include <limits>
19
20namespace app {
21
22using namespace ui;
23using namespace app::skin;
24
25TaskWidget::TaskWidget(const Type type,
26 base::task::func_t&& func)
27 : Box(HORIZONTAL | HOMOGENEOUS)
28 , m_monitorTimer(25)
29 , m_cancelButton("Cancel")
30 , m_progressBar(0, 100, 0)
31{
32 if (int(type) & int(kCanCancel)) {
33 addChild(&m_cancelButton);
34
35 m_cancelButton.Click.connect(
36 [this](Event&){
37 m_task.cancel();
38 m_cancelButton.setEnabled(false);
39 m_progressBar.setEnabled(false);
40 });
41 }
42
43 if (int(type) & int(kWithProgress)) {
44 m_progressBar.setReadOnly(true);
45 addChild(&m_progressBar);
46 }
47
48 m_monitorTimer.Tick.connect(
49 [this]{
50 if (m_task.completed()) {
51 m_monitorTimer.stop();
52 onComplete();
53 }
54 else if (m_progressBar.parent()) {
55 float v = m_task.progress();
56 if (v > 0.0f) {
57 TRACEARGS("progressBar setValue",
58 int(std::clamp(v*100.0f, 0.0f, 100.0f)));
59 m_progressBar.setValue(
60 int(std::clamp(v*100.0f, 0.0f, 100.0f)));
61 }
62 }
63 });
64 m_monitorTimer.start();
65
66 InitTheme.connect(
67 [this]{
68 auto theme = SkinTheme::get(this);
69 setTransparent(true);
70 setBgColor(gfx::ColorNone);
71 m_cancelButton.setTransparent(true);
72 m_cancelButton.setStyle(theme->styles.miniButton());
73 m_cancelButton.setBgColor(gfx::ColorNone);
74 m_progressBar.setTransparent(true);
75 m_progressBar.setBgColor(gfx::ColorNone);
76 setup_mini_font(&m_cancelButton);
77 setup_mini_look(&m_progressBar);
78 setMaxSize(gfx::Size(std::numeric_limits<int>::max(), textHeight()));
79 });
80 initTheme();
81
82 m_task.run(std::move(func));
83}
84
85void TaskWidget::onComplete()
86{
87 // Do nothing
88}
89
90} // namespace app
91