Bound engineering UI state and session events for M9.8b
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#include "evo/client/ui_state.hpp"
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
namespace evo::client {
|
||||
namespace {
|
||||
bool valid_text(std::string_view text, std::size_t limit) {
|
||||
return !text.empty() && text.size() <= limit && text.find('\0') == std::string_view::npos;
|
||||
}
|
||||
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
} // namespace
|
||||
std::expected<void, UiError> UiState::advance(std::uint64_t now_ms) {
|
||||
if (now_ms < now_)
|
||||
return std::unexpected(UiError::stale_time);
|
||||
now_ = now_ms;
|
||||
std::erase_if(notices_, [&](const Notice& n) { return n.expires_at_ms <= now_; });
|
||||
return {};
|
||||
}
|
||||
std::expected<std::uint64_t, UiError> UiState::notify(std::string_view text, NoticeLevel level,
|
||||
std::uint64_t lifetime_ms) {
|
||||
if (!valid_text(text, max_text) || lifetime_ms == 0 || lifetime_ms > maximum - now_ ||
|
||||
(level != NoticeLevel::info && level != NoticeLevel::warning &&
|
||||
level != NoticeLevel::error))
|
||||
return std::unexpected(UiError::invalid);
|
||||
if (next_id_ == maximum)
|
||||
return std::unexpected(UiError::exhausted);
|
||||
Notice notice{next_id_, now_ + lifetime_ms, level, std::string{text}};
|
||||
if (notices_.size() == max_notices) {
|
||||
notices_.erase(notices_.begin());
|
||||
if (evicted_ != maximum)
|
||||
++evicted_;
|
||||
}
|
||||
notices_.push_back(std::move(notice));
|
||||
return next_id_++;
|
||||
}
|
||||
bool UiState::dismiss(std::uint64_t id) {
|
||||
return std::erase_if(notices_, [&](const Notice& n) { return n.id == id; }) != 0;
|
||||
}
|
||||
std::expected<std::uint64_t, UiError> UiState::open_dialog(std::string_view title,
|
||||
std::string_view body) {
|
||||
if (!valid_text(title, max_title) || !valid_text(body, max_text))
|
||||
return std::unexpected(UiError::invalid);
|
||||
if (dialog_)
|
||||
return std::unexpected(UiError::busy);
|
||||
if (next_id_ == maximum)
|
||||
return std::unexpected(UiError::exhausted);
|
||||
dialog_.emplace(Dialog{next_id_, std::string{title}, std::string{body}});
|
||||
return next_id_++;
|
||||
}
|
||||
bool UiState::close_dialog(std::uint64_t id) {
|
||||
if (!dialog_ || dialog_->id != id)
|
||||
return false;
|
||||
dialog_.reset();
|
||||
return true;
|
||||
}
|
||||
} // namespace evo::client
|
||||
Reference in New Issue
Block a user