#pragma once // Spec: SPEC.md UX-HOST-01, FR-CLI-01. SDL3 window host: event routing, frame loop, headless capture. #include #include #include #include #include "ui/App.hpp" #include "ui/Canvas.hpp" namespace dp::ui { struct HostOptions { std::string title = "DevPlace"; Size initialSize{430, 900}; Size minSize{320, 480}; bool headless = false; float forcedScale = 0; // 0 = display scale int fitScreenPercent = 0; // >0: window height capped at this percent of the display, centered (UX-HOST-02) int forcedDark = -1; // -1 = follow the app, 0 light, 1 dark (headless default follows app) }; class Host { public: bool init(const HostOptions& options); void shutdown(); int run(App& app); // Headless: render frames for up to `maxSeconds` (stops early when the app is idle for `settleSeconds`), // then write a PNG. Returns false when the file cannot be written. bool capture(App& app, const std::string& pngPath, double maxSeconds, double settleSeconds); // Scripted input for smoke tests. void injectTap(App& app, Point p); void injectScroll(App& app, Point p, float dy); void injectText(App& app, const std::string& utf8); void injectKey(App& app, std::uint32_t key, std::uint16_t mods); void resizeLogical(Size s); // Smoke-script helpers: on-screen rect of the first view whose label matches (exact or substring). std::optional findLabel(const std::string& text, bool contains = false); std::vector labels(); void injectBack(App& app); void frame(App& app); Size logicalSize() const { return canvas_.logicalSize(); } const Canvas& canvas() const { return canvas_; } private: void layoutCanvas(); void handleEvent(App& app, const SDL_Event& e); void mouseDown(App& app, Point p); void mouseMove(App& app, Point p); void mouseUp(App& app, Point p); void setFocus(View* v); float computeScale() const; void fitToDisplay(); void refitOnceFrameKnown(); HostOptions options_; SDL_Window* window_ = nullptr; SDL_Renderer* renderer_ = nullptr; SDL_Texture* texture_ = nullptr; Canvas canvas_; ViewPtr root_; Size logical_{430, 900}; float scale_ = 1; bool systemDark_ = true; bool dirty_ = true; bool frameFitted_ = false; // fit re-applied once the window manager reported its frame int fitTries_ = 0; SDL_Rect fitBounds_{}; // display bounds the last fit used int fitX_ = 0, fitY_ = 0, fitW_ = 0, fitH_ = 0; // client rect the last fit asked for Uint64 fitStartTicks_ = 0; Uint64 fitCheckTicks_ = 0; std::string title_; // Gesture state (view ids, never raw pointers, because the tree is rebuilt every frame). bool pressed_ = false; std::uint64_t pressTargetId_ = 0; std::uint64_t scrollTargetId_ = 0; bool pressTargetIsText_ = false; bool dragging_ = false; bool longFired_ = false; Point pressStart_{}, lastPoint_{}; Uint64 pressTicks_ = 0; float velX_ = 0, velY_ = 0; Uint64 lastMoveTicks_ = 0; }; } // namespace dp::ui