Bound engineering UI state and session events for M9.8b

This commit is contained in:
dingo
2026-10-02 08:22:25 -04:00
parent 22c7c341ce
commit a6e8094f57
20 changed files with 327 additions and 19 deletions
+3 -1
View File
@@ -50,6 +50,7 @@ struct Application::Impl {
std::optional<Error> failure;
InputState input;
bool backlogged{false};
bool external_capture{false};
std::expected<void, Error> refresh() {
if (!SDL_GetWindowSize(window.get(), &state.logical_width, &state.logical_height) ||
@@ -72,6 +73,7 @@ WindowState Application::state() const noexcept {
return result;
}
void Application::set_ui_capture(bool captured) noexcept {
impl_->external_capture = captured;
if (impl_->input.captured() && !captured) {
// An external UI can release capture with old input still queued in SDL.
// Drain that batch before admitting fresh world presses.
@@ -174,7 +176,7 @@ std::expected<void, Error> Application::poll_events() {
event.key.windowID == id) {
const bool down = event.type == SDL_EVENT_KEY_DOWN;
if (event.key.scancode == SDL_SCANCODE_TAB && down && !event.key.repeat &&
p.state.accepts_input() && !p.backlogged)
p.state.accepts_input() && !p.backlogged && !p.external_capture)
p.input.capture(!p.input.captured());
if (const auto key = input_key(event.key.scancode)) {
if (auto result = p.input.key(*key, down, event.key.repeat); !result)
+56
View File
@@ -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