yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
toast_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_TOAST_MANAGER_H
2#define YAZE_APP_EDITOR_SYSTEM_TOAST_MANAGER_H
3
4#include <chrono>
5#include <deque>
6#include <string>
7
8// Must define before including imgui.h
9#ifndef IMGUI_DEFINE_MATH_OPERATORS
10#define IMGUI_DEFINE_MATH_OPERATORS
11#endif
12
13#include "app/gui/core/style.h"
15#include "imgui/imgui.h"
16
17namespace yaze {
18namespace editor {
19
21
22struct Toast {
23 std::string message;
25 float ttl_seconds = 3.0f;
26};
27
32 std::string message;
34 std::chrono::system_clock::time_point timestamp;
35 bool read = false;
36};
37
39 public:
40 static constexpr size_t kMaxHistorySize = 50;
41
42 void Show(const std::string& message, ToastType type = ToastType::kInfo,
43 float ttl_seconds = 3.0f) {
44 toasts_.push_back({message, type, ttl_seconds});
45
46 // Also add to notification history
48 entry.message = message;
49 entry.type = type;
50 entry.timestamp = std::chrono::system_clock::now();
51 entry.read = false;
52
53 notification_history_.push_front(entry);
54
55 // Trim history if too large
56 while (notification_history_.size() > kMaxHistorySize) {
57 notification_history_.pop_back();
58 }
59 }
60
61 void Draw() {
62 if (toasts_.empty())
63 return;
64 ImGuiIO& io = ImGui::GetIO();
65 const auto& theme = gui::ThemeManager::Get().GetCurrentTheme();
66
67 // Position toasts from the top-right, below menu bar
68 ImVec2 pos(io.DisplaySize.x - 16.f, 48.f);
69
70 // Iterate copy so we can mutate ttl while drawing ordered from newest.
71 for (auto it = toasts_.begin(); it != toasts_.end();) {
72 Toast& t = *it;
73
74 // Use theme colors for toast backgrounds
75 ImVec4 bg;
76 ImVec4 text_color;
77 switch (t.type) {
80 bg.w = 0.95f;
81 text_color = gui::ConvertColorToImVec4(theme.text_primary);
82 break;
84 bg = gui::ConvertColorToImVec4(theme.success);
85 bg.w = 0.95f;
86 text_color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
87 break;
89 bg = gui::ConvertColorToImVec4(theme.warning);
90 bg.w = 0.95f;
91 text_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
92 break;
94 bg = gui::ConvertColorToImVec4(theme.error);
95 bg.w = 0.95f;
96 text_color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
97 break;
98 }
99
100 ImGui::SetNextWindowBgAlpha(bg.w);
101 ImGui::SetNextWindowPos(pos, ImGuiCond_Always, ImVec2(1.f, 0.f));
102 ImGuiWindowFlags flags =
103 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize |
104 ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNav |
105 ImGuiWindowFlags_NoFocusOnAppearing;
106
107 ImGui::PushStyleColor(ImGuiCol_WindowBg, bg);
108 ImGui::PushStyleColor(ImGuiCol_Text, text_color);
109 ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 6.0f);
110 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(12.0f, 8.0f));
111
112 // Use unique window name per toast to allow multiple
113 char window_name[32];
114 snprintf(window_name, sizeof(window_name), "##toast_%p", (void*)&t);
115
116 if (ImGui::Begin(window_name, nullptr, flags)) {
117 ImGui::TextUnformatted(t.message.c_str());
118 }
119 ImGui::End();
120
121 ImGui::PopStyleVar(2);
122 ImGui::PopStyleColor(2);
123
124 // Decrease TTL
125 t.ttl_seconds -= io.DeltaTime;
126 if (t.ttl_seconds <= 0.f) {
127 it = toasts_.erase(it);
128 } else {
129 // Next toast stacks below with proper spacing
130 pos.y += ImGui::GetItemRectSize().y + 8.f;
131 ++it;
132 }
133 }
134 }
135
136 // Notification history methods
137 size_t GetUnreadCount() const {
138 size_t count = 0;
139 for (const auto& entry : notification_history_) {
140 if (!entry.read) ++count;
141 }
142 return count;
143 }
144
145 const std::deque<NotificationEntry>& GetHistory() const {
147 }
148
149 void MarkAllRead() {
150 for (auto& entry : notification_history_) {
151 entry.read = true;
152 }
153 }
154
156
157 private:
158 std::deque<Toast> toasts_;
159 std::deque<NotificationEntry> notification_history_;
160};
161
162} // namespace editor
163} // namespace yaze
164
165#endif // YAZE_APP_EDITOR_SYSTEM_TOAST_MANAGER_H
std::deque< NotificationEntry > notification_history_
std::deque< Toast > toasts_
const std::deque< NotificationEntry > & GetHistory() const
void Show(const std::string &message, ToastType type=ToastType::kInfo, float ttl_seconds=3.0f)
static constexpr size_t kMaxHistorySize
const Theme & GetCurrentTheme() const
static ThemeManager & Get()
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:23
ImVec4 GetSurfaceContainerHighVec4()
Entry in the notification history with timestamp.
std::chrono::system_clock::time_point timestamp