feat: Added basic toast popups

This commit is contained in:
WerWolv
2023-12-19 23:21:20 +01:00
parent a6025e72fb
commit 2b5789631f
7 changed files with 183 additions and 3 deletions

View File

@@ -56,8 +56,9 @@ namespace hex {
return m_close;
}
protected:
static std::mutex& getMutex();
private:
UnlocalizedString m_unlocalizedName;
bool m_closeButton, m_modal;
std::atomic<bool> m_close = false;
@@ -74,8 +75,7 @@ namespace hex {
public:
template<typename ...Args>
static void open(Args && ... args) {
static std::mutex mutex;
std::lock_guard lock(mutex);
std::lock_guard lock(getMutex());
auto popup = std::make_unique<T>(std::forward<Args>(args)...);

View File

@@ -0,0 +1,60 @@
#pragma once
#include <hex.hpp>
#include <imgui.h>
#include <list>
#include <memory>
namespace hex {
namespace impl {
class ToastBase {
public:
ToastBase(ImColor color) : m_color(color) {}
virtual ~ToastBase() = default;
virtual void draw() { drawContent(); }
virtual void drawContent() = 0;
[[nodiscard]] static std::list<std::unique_ptr<ToastBase>> &getQueuedToasts();
[[nodiscard]] const ImColor& getColor() const {
return m_color;
}
void setAppearTime(double appearTime) {
m_appearTime = appearTime;
}
[[nodiscard]] double getAppearTime() const {
return m_appearTime;
}
constexpr static double VisibilityTime = 4.0;
protected:
static std::mutex& getMutex();
double m_appearTime = 0;
ImColor m_color;
};
}
template<typename T>
class Toast : public impl::ToastBase {
public:
using impl::ToastBase::ToastBase;
template<typename ...Args>
static void open(Args && ... args) {
std::lock_guard lock(getMutex());
auto toast = std::make_unique<T>(std::forward<Args>(args)...);
getQueuedToasts().emplace_back(std::move(toast));
}
};
}