ObsidianDragon - DragonX ImGui Wallet
Full-node GUI wallet for DragonX cryptocurrency. Built with Dear ImGui, SDL3, and OpenGL3/DX11. Features: - Send/receive shielded and transparent transactions - Autoshield with merged transaction display - Built-in CPU mining (xmrig) - Peer management and network monitoring - Wallet encryption with PIN lock - QR code generation for receive addresses - Transaction history with pagination - Console for direct RPC commands - Cross-platform (Linux, Windows)
This commit is contained in:
147
src/ui/notifications.h
Normal file
147
src/ui/notifications.h
Normal file
@@ -0,0 +1,147 @@
|
||||
// DragonX Wallet - ImGui Edition
|
||||
// Copyright 2024-2026 The Hush Developers
|
||||
// Released under the GPLv3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <deque>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <cstdio>
|
||||
#include "../util/logger.h"
|
||||
#include "schema/ui_schema.h"
|
||||
|
||||
namespace dragonx {
|
||||
namespace ui {
|
||||
|
||||
enum class NotificationType {
|
||||
Info,
|
||||
Success,
|
||||
Warning,
|
||||
Error
|
||||
};
|
||||
|
||||
struct Notification {
|
||||
std::string message;
|
||||
NotificationType type;
|
||||
std::chrono::steady_clock::time_point created_at;
|
||||
float duration_seconds;
|
||||
|
||||
Notification(const std::string& msg, NotificationType t, float duration = 5.0f)
|
||||
: message(msg)
|
||||
, type(t)
|
||||
, created_at(std::chrono::steady_clock::now())
|
||||
, duration_seconds(duration)
|
||||
{}
|
||||
|
||||
bool isExpired() const {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration<float>(now - created_at).count();
|
||||
return elapsed >= duration_seconds;
|
||||
}
|
||||
|
||||
float getProgress() const {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration<float>(now - created_at).count();
|
||||
return std::min(1.0f, elapsed / duration_seconds);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Simple notification manager for user feedback
|
||||
*
|
||||
* Usage:
|
||||
* Notifications::instance().info("Transaction sent!");
|
||||
* Notifications::instance().error("Connection failed");
|
||||
*/
|
||||
class Notifications {
|
||||
public:
|
||||
static Notifications& instance() {
|
||||
static Notifications inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
void info(const std::string& message, float duration = -1.0f) {
|
||||
if (duration < 0.0f) duration = schemaDuration("duration-info", 1.0f);
|
||||
push(message, NotificationType::Info, duration);
|
||||
}
|
||||
|
||||
void success(const std::string& message, float duration = -1.0f) {
|
||||
if (duration < 0.0f) duration = schemaDuration("duration-success", 2.5f);
|
||||
push(message, NotificationType::Success, duration);
|
||||
}
|
||||
|
||||
void warning(const std::string& message, float duration = -1.0f) {
|
||||
if (duration < 0.0f) duration = schemaDuration("duration-warning", 3.5f);
|
||||
push(message, NotificationType::Warning, duration);
|
||||
}
|
||||
|
||||
void error(const std::string& message, float duration = -1.0f) {
|
||||
if (duration < 0.0f) duration = schemaDuration("duration-error", 4.0f);
|
||||
push(message, NotificationType::Error, duration);
|
||||
}
|
||||
|
||||
void push(const std::string& message, NotificationType type, float duration = 5.0f) {
|
||||
notifications_.emplace_back(message, type, duration);
|
||||
|
||||
// Log errors and warnings (debug-only output)
|
||||
if (type == NotificationType::Error) {
|
||||
DEBUG_LOGF("[ERROR] Notification: %s\n", message.c_str());
|
||||
} else if (type == NotificationType::Warning) {
|
||||
DEBUG_LOGF("[WARN] Notification: %s\n", message.c_str());
|
||||
}
|
||||
|
||||
// Forward errors and warnings to console callback
|
||||
if (console_callback_ && (type == NotificationType::Error || type == NotificationType::Warning)) {
|
||||
const char* prefix = (type == NotificationType::Error) ? "[ERROR] " : "[WARN] ";
|
||||
console_callback_(prefix + message, type == NotificationType::Error);
|
||||
}
|
||||
|
||||
// Limit max notifications
|
||||
while (notifications_.size() > max_notifications_) {
|
||||
notifications_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a callback that receives error/warning messages (e.g. console tab)
|
||||
void setConsoleCallback(std::function<void(const std::string&, bool is_error)> cb) {
|
||||
console_callback_ = std::move(cb);
|
||||
}
|
||||
|
||||
void render();
|
||||
|
||||
/// @brief Check if there are non-expired notifications (needs continuous frames for animation)
|
||||
bool hasActive() const {
|
||||
for (const auto& n : notifications_) {
|
||||
if (!n.isExpired()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
notifications_.clear();
|
||||
}
|
||||
|
||||
void setMaxNotifications(size_t max) {
|
||||
max_notifications_ = max;
|
||||
}
|
||||
|
||||
private:
|
||||
Notifications() = default;
|
||||
~Notifications() = default;
|
||||
Notifications(const Notifications&) = delete;
|
||||
Notifications& operator=(const Notifications&) = delete;
|
||||
|
||||
std::deque<Notification> notifications_;
|
||||
size_t max_notifications_ = 5;
|
||||
std::function<void(const std::string&, bool)> console_callback_;
|
||||
|
||||
static float schemaDuration(const char* key, float fallback) {
|
||||
float v = schema::UI().drawElement("components.notifications", key).size;
|
||||
return v > 0.0f ? v : fallback;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ui
|
||||
} // namespace dragonx
|
||||
Reference in New Issue
Block a user