126 lines
4.7 KiB
Text
126 lines
4.7 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
export module Nostro.Ui:Geometry;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
|
||
|
|
// Points, boxes and colours, and the two things that decide how a screen is laid out: the scale that turns the design's
|
||
|
|
// sizes into the window's pixels, and how wide the window is. Everything the toolkit positions is in the window's
|
||
|
|
// physical pixels; a size in the design's units (dp) exists only as a constant, turned into pixels by Metrics::Dp.
|
||
|
|
|
||
|
|
export namespace Nostro::Ui {
|
||
|
|
struct Point {
|
||
|
|
float x = 0;
|
||
|
|
float y = 0;
|
||
|
|
friend bool operator==(const Point&, const Point&) = default;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Space taken off a box's sides.
|
||
|
|
struct Insets {
|
||
|
|
float left = 0;
|
||
|
|
float top = 0;
|
||
|
|
float right = 0;
|
||
|
|
float bottom = 0;
|
||
|
|
|
||
|
|
static constexpr Insets All(float each) noexcept {
|
||
|
|
return {each, each, each, each};
|
||
|
|
}
|
||
|
|
static constexpr Insets Symmetric(float horizontal, float vertical) noexcept {
|
||
|
|
return {horizontal, vertical, horizontal, vertical};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
struct Box {
|
||
|
|
float x = 0;
|
||
|
|
float y = 0;
|
||
|
|
float w = 0;
|
||
|
|
float h = 0;
|
||
|
|
|
||
|
|
constexpr float Right() const noexcept {
|
||
|
|
return x + w;
|
||
|
|
}
|
||
|
|
constexpr float Bottom() const noexcept {
|
||
|
|
return y + h;
|
||
|
|
}
|
||
|
|
constexpr Point Center() const noexcept {
|
||
|
|
return {x + w / 2, y + h / 2};
|
||
|
|
}
|
||
|
|
constexpr bool Empty() const noexcept {
|
||
|
|
return w <= 0 || h <= 0;
|
||
|
|
}
|
||
|
|
// The left and top edges are inside, the right and bottom ones are not, so boxes that share an edge share no point.
|
||
|
|
constexpr bool Contains(Point p) const noexcept {
|
||
|
|
return p.x >= x && p.x < x + w && p.y >= y && p.y < y + h;
|
||
|
|
}
|
||
|
|
// The box within the insets; one they leave nothing of is empty at its place, never inside out.
|
||
|
|
constexpr Box Inset(Insets by) const noexcept {
|
||
|
|
return {x + by.left, y + by.top, std::max(0.0f, w - by.left - by.right), std::max(0.0f, h - by.top - by.bottom)};
|
||
|
|
}
|
||
|
|
// What two boxes share, or an empty box when they share nothing.
|
||
|
|
constexpr Box Intersect(Box other) const noexcept {
|
||
|
|
const float left = std::max(x, other.x);
|
||
|
|
const float top = std::max(y, other.y);
|
||
|
|
const float right = std::min(Right(), other.Right());
|
||
|
|
const float bottom = std::min(Bottom(), other.Bottom());
|
||
|
|
if(right <= left || bottom <= top) {
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
return {left, top, right - left, bottom - top};
|
||
|
|
}
|
||
|
|
constexpr bool Overlaps(Box other) const noexcept {
|
||
|
|
return !Intersect(other).Empty();
|
||
|
|
}
|
||
|
|
friend bool operator==(const Box&, const Box&) = default;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Red, green, blue and opacity, each from 0 to 1.
|
||
|
|
struct Color {
|
||
|
|
float r = 0;
|
||
|
|
float g = 0;
|
||
|
|
float b = 0;
|
||
|
|
float a = 1;
|
||
|
|
|
||
|
|
constexpr Color WithAlpha(float alpha) const noexcept {
|
||
|
|
return {r, g, b, alpha};
|
||
|
|
}
|
||
|
|
// This colour with `over` laid on it at `amount` of its strength: how a pressed or hovered control is tinted.
|
||
|
|
constexpr Color Mixed(Color over, float amount) const noexcept {
|
||
|
|
return {r + (over.r - r) * amount, g + (over.g - g) * amount, b + (over.b - b) * amount, a};
|
||
|
|
}
|
||
|
|
// How bright the eye takes it for, from 0 to 1: what decides whether text on it is dark or light.
|
||
|
|
constexpr float Luminance() const noexcept {
|
||
|
|
return 0.2126f * r + 0.7152f * g + 0.0722f * b;
|
||
|
|
}
|
||
|
|
friend bool operator==(const Color&, const Color&) = default;
|
||
|
|
};
|
||
|
|
|
||
|
|
// The window's scale: how many of its pixels one of the design's units (dp) is.
|
||
|
|
struct Metrics {
|
||
|
|
float scale = 1;
|
||
|
|
|
||
|
|
// A size of the design in whole pixels, so edges land on the pixel grid and stay sharp.
|
||
|
|
float Dp(float dp) const noexcept {
|
||
|
|
return std::round(dp * scale);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// How much room a window gives its screens: a phone held upright, a phone on its side or a narrow window, a desktop.
|
||
|
|
enum class SizeClass : std::uint8_t {
|
||
|
|
Compact,
|
||
|
|
Medium,
|
||
|
|
Expanded
|
||
|
|
};
|
||
|
|
|
||
|
|
inline constexpr float mediumFromDp = 600;
|
||
|
|
inline constexpr float expandedFromDp = 900;
|
||
|
|
|
||
|
|
// The class of a window `widthPx` pixels wide at `scale`.
|
||
|
|
constexpr SizeClass SizeClassFor(float widthPx, float scale) noexcept {
|
||
|
|
const float dp = widthPx / (scale > 0 ? scale : 1.0f);
|
||
|
|
if(dp < mediumFromDp) {
|
||
|
|
return SizeClass::Compact;
|
||
|
|
}
|
||
|
|
return dp < expandedFromDp ? SizeClass::Medium : SizeClass::Expanded;
|
||
|
|
}
|
||
|
|
}
|