// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® export module Nostro.Ui.Shell:Window; import std; import Crafter.Graphics; import Crafter.Event; import Nostro.Ui; import :Loop; import :Canvas; // The app's window: a toolkit Context shown on a Canvas and fed from it. What the compositor reports of the pointer, // the fingers, the keys and the input method becomes the Context's events, in pixels and key names; what the Context // wants of the window afterwards (a frame, a timer, the on-screen keyboard, the clipboard) is done here. Nothing in // here knows what the screens are: they are built through onBuild. // // Three rules hold it together. An event acts on a frame that is true: before one is handed on, time is brought up to // it and a frame that went stale is built again. A frame is drawn only when the compositor asks for one, after // something changed. And what cannot be done inside a listener waits for the loop: the clipboard is read from there. export namespace Nostro::Ui::Shell { class Window { public: // Makes the window; throws what Canvas does. Nothing is drawn before Start. Window(Loop& loop, Context& context, Fonts& fonts, const CanvasOptions& options); ~Window(); Window(const Window&) = delete; Window& operator=(const Window&) = delete; // Builds the screens into a frame. Called for every frame drawn, and whenever an event needs a true one. std::function onBuild; // The user closed the window. Called from inside the compositor's events: whoever takes the window down for it // does so from the loop, not from here. std::function onClose; // The window can draw no more, and why: a graphics error in a frame, or the compositor's connection gone. // Nothing is drawn on it from here. std::function onFailed; // A frame was drawn. std::function onFrame; // A line for whoever keeps a log. std::function onLog; // Draws the first frame, which shows the window. Throws when it cannot be drawn. void Start(); // Looks at what the Context wants since the last time and does it. The loop calls this after every source // that ran (Loop::onSettle), which covers the window's own events and whatever else changed the screens. void Settle(); // Draws a frame now and writes it to the file as a PNG. Throws when that fails. void Screenshot(const std::filesystem::path& png); // Brings the screens up to now, as before an event of the window's own: time has passed for them, and a frame // that went stale is built again. For what acts on the Context from outside the window, a debugger's command: // it then acts on what the window shows, and Settle does what it led to. void Prepare(); Canvas& Surface() noexcept; bool Failed() const noexcept; private: void Layout(); // Brings time up to an event and makes the frame true, before the event is handed to the Context. void Before(std::int64_t atMs); std::int64_t EventTime(); Point Pointer() const noexcept; void Press(PointerPhase phase); void OnKey(Crafter::KeyCode code); void OnText(std::string_view text); void Apply(const Context::Wants& wants, bool redraw); void ApplyTextInput(const TextInputRequest& want); void DrawFrame(); void AfterFrame(); void Paste(); void Fail(std::string_view why); void Log(std::string_view line) const; Loop& loop; Context& context; Canvas canvas; WaylandSource source; FrameDriver frames; Timer wake; // the Context's own: a long press, a caret's blink, a notice's end Timer repeat; // a held key's repeats Defer paste; bool failed = false; bool overflowSaid = false; bool down = false; // the pointer's button, or the first finger // Where the mouse is, while it is in the window; a finger is nowhere once it lifts. What is under a pointer that // rests changes when the screens do or a list moves, so it is looked up again with every frame built. std::optional resting; EventClock clock; FrameWait atlasWait; TextInputRequest applied; // what the compositor was last told std::string pasteInto; // the field that asked for the clipboard // The first finger drives the pointer. A touch the compositor takes over for a gesture of its own arrives as a // release without the finger's lifting: that is a cancel, which taps nothing. std::optional touch; bool touchLifted = false; // A key whose press changed the screen or moved the focus, and the text it types: that text is the old // screen's, at the press and at every repeat while the key stays down. struct Spent { Crafter::KeyCode code = 0; std::string text; }; std::optional spent; // The listeners on the window library's events; they go before the canvas does. std::deque> plain; std::optional> touchDown; std::optional> touchUp; std::optional> scroll; std::optional> keyDown; std::optional> keyUp; std::optional> text; std::optional> preedit; std::optional> textDelete; }; }