Files
simian/include/gui/Toast.h
Nick Koirala f352b432d9
Some checks failed
CI / build-and-test (push) Has been cancelled
feat: added toast support to scripting
2025-11-14 15:04:00 +13:00

57 lines
1.7 KiB
C++

#pragma once
#include "gui/ImGuiNotify.hpp"
#include <string>
#include <string_view>
#include <vector>
#include <cstdio>
#include <utility>
class Toast
{
public:
template <typename... Args>
static void Info(std::string_view fmt, Args&&... args)
{
ImGui::InsertNotification({ImGuiToastType::Info, 3000, Format(fmt, std::forward<Args>(args)...).c_str()});
}
template <typename... Args>
static void Warning(std::string_view fmt, Args&&... args)
{
ImGui::InsertNotification({ImGuiToastType::Warning, 4000, Format(fmt, std::forward<Args>(args)...).c_str()});
}
template <typename... Args>
static void Error(std::string_view fmt, Args&&... args)
{
ImGui::InsertNotification({ImGuiToastType::Error, 5000, Format(fmt, std::forward<Args>(args)...).c_str()});
}
template <typename... Args>
static void Success(std::string_view fmt, Args&&... args)
{
ImGui::InsertNotification({ImGuiToastType::Success, 2500, Format(fmt, std::forward<Args>(args)...).c_str()});
}
private:
template <typename... Args>
static std::string Format(std::string_view fmt, Args&&... args)
{
if constexpr (sizeof...(Args) == 0)
{
return std::string(fmt);
}
else
{
std::string format(fmt);
int size = std::snprintf(nullptr, 0, format.c_str(), std::forward<Args>(args)...) + 1;
if (size <= 0)
return std::string(fmt);
std::vector<char> buffer(static_cast<size_t>(size));
std::snprintf(buffer.data(), static_cast<size_t>(size), format.c_str(), std::forward<Args>(args)...);
return std::string(buffer.data());
}
}
};