2019-05-31 10:17:13 +08:00
|
|
|
// Aseprite
|
2025-10-02 03:53:07 +08:00
|
|
|
// Copyright (C) 2019-2025 Igara Studio S.A.
|
2019-05-31 10:17:13 +08:00
|
|
|
//
|
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/ui/task_widget.h"
|
|
|
|
|
|
|
|
#include "app/i18n/strings.h"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/ui/skin/skin_theme.h"
|
|
|
|
#include "ui/scale.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
using namespace ui;
|
|
|
|
using namespace app::skin;
|
|
|
|
|
|
|
|
TaskWidget::TaskWidget(const Type type, base::task::func_t&& func)
|
|
|
|
: Box(HORIZONTAL | HOMOGENEOUS)
|
|
|
|
, m_monitorTimer(25)
|
|
|
|
, m_cancelButton("Cancel")
|
|
|
|
, m_progressBar(0, 100, 0)
|
|
|
|
{
|
|
|
|
if (int(type) & int(kCanCancel)) {
|
|
|
|
addChild(&m_cancelButton);
|
|
|
|
|
|
|
|
m_cancelButton.Click.connect([this]() {
|
|
|
|
m_task.cancel();
|
|
|
|
m_cancelButton.setEnabled(false);
|
|
|
|
m_progressBar.setEnabled(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (int(type) & int(kWithProgress)) {
|
|
|
|
m_progressBar.setReadOnly(true);
|
|
|
|
addChild(&m_progressBar);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_monitorTimer.Tick.connect([this] {
|
|
|
|
if (m_task.completed()) {
|
|
|
|
m_monitorTimer.stop();
|
|
|
|
onComplete();
|
|
|
|
}
|
|
|
|
else if (m_progressBar.parent()) {
|
|
|
|
float v = m_task.progress();
|
2025-10-02 03:53:07 +08:00
|
|
|
if (v > 0.0f)
|
2022-06-10 05:28:06 +08:00
|
|
|
m_progressBar.setValue(int(std::clamp(v * 100.0f, 0.0f, 100.0f)));
|
2024-12-17 01:52:19 +08:00
|
|
|
}
|
2019-05-31 10:17:13 +08:00
|
|
|
});
|
|
|
|
m_monitorTimer.start();
|
|
|
|
|
|
|
|
InitTheme.connect([this] {
|
2022-02-19 06:01:46 +08:00
|
|
|
auto theme = SkinTheme::get(this);
|
2019-05-31 10:17:13 +08:00
|
|
|
setTransparent(true);
|
|
|
|
setBgColor(gfx::ColorNone);
|
|
|
|
m_cancelButton.setTransparent(true);
|
|
|
|
m_cancelButton.setStyle(theme->styles.miniButton());
|
|
|
|
m_cancelButton.setBgColor(gfx::ColorNone);
|
|
|
|
m_progressBar.setTransparent(true);
|
|
|
|
m_progressBar.setBgColor(gfx::ColorNone);
|
|
|
|
setup_mini_font(&m_cancelButton);
|
|
|
|
setup_mini_look(&m_progressBar);
|
|
|
|
setMaxSize(gfx::Size(std::numeric_limits<int>::max(), textHeight()));
|
|
|
|
});
|
|
|
|
initTheme();
|
|
|
|
|
|
|
|
m_task.run(std::move(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskWidget::onComplete()
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|