1
0
mirror of synced 2024-12-15 01:01:15 +01:00
ImHex/lib/libimhex/include/hex/ui/popup.hpp

87 lines
2.3 KiB
C++
Raw Normal View History

#pragma once
#include <hex.hpp>
#include <hex/api/localization_manager.hpp>
#include <memory>
#include <string>
#include <imgui.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <hex/api/task_manager.hpp>
#include <hex/helpers/utils.hpp>
namespace hex {
namespace impl {
class PopupBase {
public:
explicit PopupBase(UnlocalizedString unlocalizedName, bool closeButton, bool modal)
: m_unlocalizedName(std::move(unlocalizedName)), m_closeButton(closeButton), m_modal(modal) { }
virtual ~PopupBase() = default;
virtual void drawContent() = 0;
[[nodiscard]] virtual ImGuiWindowFlags getFlags() const { return ImGuiWindowFlags_None; }
[[nodiscard]] virtual ImVec2 getMinSize() const {
return { 0, 0 };
}
[[nodiscard]] virtual ImVec2 getMaxSize() const {
return { 0, 0 };
}
[[nodiscard]] static std::vector<std::unique_ptr<PopupBase>> &getOpenPopups();
[[nodiscard]] const UnlocalizedString &getUnlocalizedName() const {
2023-12-19 13:10:25 +01:00
return m_unlocalizedName;
}
[[nodiscard]] bool hasCloseButton() const {
2023-12-19 13:10:25 +01:00
return m_closeButton;
}
[[nodiscard]] bool isModal() const {
2023-12-19 13:10:25 +01:00
return m_modal;
}
void close() {
2023-12-19 13:10:25 +01:00
m_close = true;
}
[[nodiscard]] bool shouldClose() const {
2023-12-19 13:10:25 +01:00
return m_close;
}
2023-12-19 23:21:20 +01:00
protected:
static std::mutex& getMutex();
private:
UnlocalizedString m_unlocalizedName;
bool m_closeButton, m_modal;
std::atomic<bool> m_close = false;
};
}
template<typename T>
class Popup : public impl::PopupBase {
protected:
explicit Popup(UnlocalizedString unlocalizedName, bool closeButton = true, bool modal = true) : PopupBase(std::move(unlocalizedName), closeButton, modal) { }
public:
template<typename ...Args>
static void open(Args && ... args) {
2023-12-19 23:21:20 +01:00
std::lock_guard lock(getMutex());
auto popup = std::make_unique<T>(std::forward<Args>(args)...);
getOpenPopups().emplace_back(std::move(popup));
}
};
}