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
+16 -1
View File
@@ -31,6 +31,7 @@ struct SessionClient::Impl {
client::EnvironmentLayer environment;
client::OrganismLayer organisms;
std::vector<ClientEvent> events;
bool awaiting_event_drain{};
void disconnect(Clock::time_point now, std::optional<CloseReason> reason) {
if (step == Step::established || step == Step::awaiting_welcome || reason)
@@ -87,6 +88,13 @@ struct SessionClient::Impl {
return true;
}
bool frame(const OwnedFrame& f, Clock::time_point now) {
// Reserve loss + backpressure + every unresolved intention; never evict an event.
if (events.size() >= max_events - max_outstanding - 2) {
events.push_back({ClientEvent::Kind::backpressure, {}, {}, {}, {}, 0});
awaiting_event_drain = true;
disconnect(now, std::nullopt);
return false;
}
const FrameView v{f.type, f.payload};
last_received = now;
if (step == Step::awaiting_welcome) {
@@ -225,8 +233,14 @@ SessionClient::create(SessionClientConfig config) {
void SessionClient::poll(std::chrono::steady_clock::time_point now) {
auto& m = *impl_;
m.polled_at = now;
if (m.phase == SessionPhase::exhausted)
if (m.awaiting_event_drain || m.phase == SessionPhase::exhausted)
return;
if (m.events.size() >= max_events - max_outstanding - 2) {
m.events.push_back({ClientEvent::Kind::backpressure, {}, {}, {}, {}, 0});
m.awaiting_event_drain = true;
m.disconnect(now, std::nullopt);
return;
}
if (m.phase == SessionPhase::backoff) {
if (now < m.retry_at)
return;
@@ -314,6 +328,7 @@ std::size_t SessionClient::grants_left() const noexcept {
return impl_->config.grants.size() - impl_->next_grant;
}
std::vector<ClientEvent> SessionClient::take_events() {
impl_->awaiting_event_drain = false;
return std::exchange(impl_->events, {});
}
} // namespace evo::network