// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® export module Nostro.Ui:Draw; import std; import :Geometry; import :Text; // What a frame draws, as a list the window turns into its own items and the tests read: rounded boxes, circles and // lines of text, in layers. The window library draws a batch in one pass in which every shape comes before every // text, so a shape meant to cover text drawn earlier needs a pass of its own; and a pass is cut to one box. The list // keeps the order things were drawn in by starting a new batch where one pass could not show it: where the clip // changes, and where a shape would land on text already in the batch. Each batch is a pass over the whole window, so // a screen is drawn with few of them: its layers, and a clip per scrolling area. export namespace Nostro::Ui { enum class TextAlign : std::uint8_t { Left, // x is where the text starts Center, // x is its middle Right // x is where it ends }; // Drawn in this order, whatever order a screen draws them in. enum class Layer : std::uint8_t { Background, Content, // what scrolls Chrome, // the bars around it Overlay, // a sheet or a dialog, and the scrim under it Popup // a menu or a notice, above everything }; inline constexpr std::size_t layerCount = 5; // A box's corner radii, clockwise from the top left. struct Corners { float topLeft = 0; float topRight = 0; float bottomRight = 0; float bottomLeft = 0; static constexpr Corners All(float radius) noexcept { return {radius, radius, radius, radius}; } static constexpr Corners Top(float radius) noexcept { return {radius, radius, 0, 0}; } friend bool operator==(const Corners&, const Corners&) = default; }; struct RectItem { Box box; Color color; Corners corners; float outline = 0; // the width of a line along its edge, inside the box Color outlineColor; }; struct CircleItem { Point center; float radius = 0; Color color; float outline = 0; Color outlineColor; }; struct TextItem { std::string text; float x = 0; float baseline = 0; FontFace face = FontFace::Regular; float px = 0; Color color; TextAlign align = TextAlign::Left; Box bounds; // the line's box: its width, from its ascent to a line's height below that }; // One pass: the shapes, then the text over them, all cut to `clip`. struct Batch { std::optional clip; // nothing: the whole window std::vector rects; std::vector circles; std::vector texts; }; class DrawList { public: // `bounds` is the window: what lies outside it, or outside the clip, is not kept. DrawList(const TextMeasure& measure, Box bounds); // What is drawn from here on goes to this layer. void SetLayer(Layer layer) noexcept; Layer CurrentLayer() const noexcept; // Cuts what is drawn from here on, in any layer, to `box` within the clip there was, until the matching PopClip. void PushClip(Box box); void PopClip(); void Rect(const RectItem& item); void Rect(Box box, Color color, float radius = 0); void Circle(const CircleItem& item); void Circle(Point center, float radius, Color color); // One line; returns its width. float Text(std::string_view text, float x, float baseline, FontFace face, float px, Color color, TextAlign align = TextAlign::Left); std::span Batches(Layer layer) const noexcept; std::size_t BatchCount() const noexcept; // Every line of text in the list, in the order it is drawn: what a screen says. std::vector Texts() const; private: // The batch a shape that covers `box`, or a text, goes into. Batch& BatchFor(std::optional shape); bool Visible(Box box) const noexcept; const TextMeasure* measure; Box bounds; Layer layer = Layer::Background; std::vector clips; std::array, layerCount> layers; }; }