//SPDX-License-Identifier: LGPL-3.0-only //SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Router implementation. The popState handler table lives in // Crafter.Graphics-Dom.cpp (alongside every other event-kind table); // we reach it through the opaque `PopStateRegister`/`PopStateUnregister` // helpers it exposes, avoiding any cross-TU duplication of the // HandlerTable definition. module; module Crafter.Graphics; import std; namespace Crafter::DomBindings { __attribute__((import_module("env"), import_name("pushState"))) void PushState(const char* data, std::int32_t dataLength, const char* title, std::int32_t titleLength, const char* url, std::int32_t urlLength); __attribute__((import_module("env"), import_name("addPopStateListener"))) void AddPopStateListener(std::int32_t id); __attribute__((import_module("env"), import_name("removePopStateListener"))) void RemovePopStateListener(std::int32_t id); __attribute__((import_module("env"), import_name("getPathName"))) const char* GetPathName(); // Defined in Crafter.Graphics-Dom.cpp. std::int32_t PopStateRegister(std::function cb); void PopStateUnregister(std::int32_t id); } namespace Crafter::Router { void PushState(std::string_view data, std::string_view title, std::string_view url) { Crafter::DomBindings::PushState( data.data(), static_cast(data.size()), title.data(), static_cast(title.size()), url.data(), static_cast(url.size())); } std::int32_t AddPopStateListener(std::function callback) { std::int32_t id = Crafter::DomBindings::PopStateRegister(std::move(callback)); Crafter::DomBindings::AddPopStateListener(id); return id; } void RemovePopStateListener(std::int32_t id) { Crafter::DomBindings::RemovePopStateListener(id); Crafter::DomBindings::PopStateUnregister(id); } std::string GetPath() { const char* raw = Crafter::DomBindings::GetPathName(); if (!raw) return {}; std::string out(raw); std::free(const_cast(raw)); return out; } }