Files
chandra/compositor/scene/scene.mbt
T
nandi 52b42890d6
CI / test (push) Canceled after 0s
Camera: C ISP pipeline, contrast autofocus, and VFR video
- Add chandra-camera-isp.c (C/OpenMP) for Quad-Bayer unpack, CCM, and tone mapping, with a NumPy fallback when it is not built
- Add non-blocking contrast-detection VCM autofocus, with lens subdev discovery on the sensor's I2C bus
- Tune Quad-Bayer AE: a video-mode vblank cap, damped steps, and full analog gain before extending blanking
- Record variable-frame-rate video through a streaming Matroska muxer, and count dropped frames
- Ship libcamera IPA tuning files for imx376, imx519, and uncalibrated sensors
- Map camera buffers by fd and sync them with dma-buf ioctls
2026-11-28 19:25:43 -08:00

998 lines
28 KiB
MoonBit

///| Scene graph, theme tokens, damage tracking, and Breeze-style SSD + shell draw-ops in pure MoonBit.
///|
#derive(Eq, Debug)
pub(all) struct Rgba {
r : Byte
g : Byte
b : Byte
a : Byte
}
///|
pub fn Rgba::new(r : Int, g : Int, b : Int, a : Int) -> Rgba {
{
r: r.clamp(min=0, max=255).to_byte(),
g: g.clamp(min=0, max=255).to_byte(),
b: b.clamp(min=0, max=255).to_byte(),
a: a.clamp(min=0, max=255).to_byte(),
}
}
///|
pub fn Rgba::with_alpha(self : Rgba, a : Int) -> Rgba {
{ r: self.r, g: self.g, b: self.b, a: a.clamp(min=0, max=255).to_byte() }
}
///|
fn hex_nibble(c : Int) -> Int {
if c >= 48 && c <= 57 {
c - 48
} else if c >= 97 && c <= 102 {
c - 97 + 10
} else if c >= 65 && c <= 70 {
c - 65 + 10
} else {
0
}
}
///|
fn parse_hex_byte(s : String, idx : Int) -> Int {
let hi = hex_nibble(s[idx].to_int())
let lo = hex_nibble(s[idx + 1].to_int())
(hi << 4) | lo
}
///|
pub fn Rgba::from_hex(s : String) -> Rgba {
let hex = if s.has_prefix("#") {
s.clamped_view(start=1, end=s.length()).to_owned()
} else {
s
}
if hex.length() == 6 {
Rgba::new(
parse_hex_byte(hex, 0),
parse_hex_byte(hex, 2),
parse_hex_byte(hex, 4),
255,
)
} else if hex.length() == 8 {
Rgba::new(
parse_hex_byte(hex, 0),
parse_hex_byte(hex, 2),
parse_hex_byte(hex, 4),
parse_hex_byte(hex, 6),
)
} else {
Rgba::new(0, 0, 0, 255)
}
}
///|
#derive(Eq)
pub(all) struct ThemePalette {
desktop_top : Rgba
desktop_bottom : Rgba
surface : Rgba
surface_hover : Rgba
surface_popup : Rgba
foreground : Rgba
foreground_muted : Rgba
accent : Rgba
accent_foreground : Rgba
border : Rgba
}
///|
#derive(Eq)
pub(all) struct ThemeTokens {
name : String
palette : ThemePalette
font_size : Int
radius : Int
spacing : Int
panel_height : Int
}
///|
pub fn ThemeTokens::moon_night() -> ThemeTokens {
{
name: "Moon Night",
palette: {
desktop_top: Rgba::from_hex("#16213e"),
desktop_bottom: Rgba::from_hex("#0b1020"),
surface: Rgba::from_hex("#1f2937f2"),
surface_hover: Rgba::from_hex("#374151"),
surface_popup: Rgba::from_hex("#111827f5"),
foreground: Rgba::from_hex("#f3f4f6"),
foreground_muted: Rgba::from_hex("#9ca3af"),
accent: Rgba::from_hex("#7c9cff"),
accent_foreground: Rgba::from_hex("#0b1020"),
border: Rgba::from_hex("#3b4658"),
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///| The light counterpart of `moon_night` (config/themes/moon-day.json): what the settings
/// module's "Dark mode" switch turns to when off.
pub fn ThemeTokens::moon_day() -> ThemeTokens {
{
name: "Moon Day",
palette: {
desktop_top: Rgba::from_hex("#cfe3ff"),
desktop_bottom: Rgba::from_hex("#8fb4e8"),
surface: Rgba::from_hex("#f4f6fbf2"),
surface_hover: Rgba::from_hex("#dde3f0"),
surface_popup: Rgba::from_hex("#fbfcfff5"),
foreground: Rgba::from_hex("#1b2233"),
foreground_muted: Rgba::from_hex("#5b6577"),
accent: Rgba::from_hex("#3b6bff"),
accent_foreground: Rgba::from_hex("#ffffff"),
border: Rgba::from_hex("#b9c3d6"),
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///|
fn json_str(obj : Map[String, Json], key : String, fallback : String) -> String {
match obj.get(key) {
Some(String(s)) => s
_ => fallback
}
}
///|
fn json_int(obj : Map[String, Json], key : String, fallback : Int) -> Int {
match obj.get(key) {
Some(Number(n, ..)) => n.to_int()
_ => fallback
}
}
///|
pub fn ThemeTokens::from_json_string(text : String) -> ThemeTokens {
let def = ThemeTokens::moon_night()
let parsed = try {
@json.parse(text)
} catch {
_ => return def
}
guard parsed is Object(root) else { return def }
let pal_obj = match root.get("palette") {
Some(Object(m)) => m
_ => return def
}
{
name: json_str(root, "name", def.name),
palette: {
desktop_top: Rgba::from_hex(json_str(pal_obj, "desktop-top", "#16213e")),
desktop_bottom: Rgba::from_hex(
json_str(pal_obj, "desktop-bottom", "#0b1020"),
),
surface: Rgba::from_hex(json_str(pal_obj, "surface", "#1f2937f2")),
surface_hover: Rgba::from_hex(
json_str(pal_obj, "surface-hover", "#374151"),
),
surface_popup: Rgba::from_hex(
json_str(pal_obj, "surface-popup", "#111827f5"),
),
foreground: Rgba::from_hex(json_str(pal_obj, "foreground", "#f3f4f6")),
foreground_muted: Rgba::from_hex(
json_str(pal_obj, "foreground-muted", "#9ca3af"),
),
accent: Rgba::from_hex(json_str(pal_obj, "accent", "#7c9cff")),
accent_foreground: Rgba::from_hex(
json_str(pal_obj, "accent-foreground", "#0b1020"),
),
border: Rgba::from_hex(json_str(pal_obj, "border", "#3b4658")),
},
font_size: json_int(root, "font-size", def.font_size),
radius: json_int(root, "radius", def.radius),
spacing: json_int(root, "spacing", def.spacing),
panel_height: json_int(root, "panel-height", def.panel_height),
}
}
///|
#derive(Eq, Debug)
pub(all) struct Rect {
x : Int
y : Int
w : Int
h : Int
}
///|
pub fn Rect::new(x : Int, y : Int, w : Int, h : Int) -> Rect {
{ x, y, w: if w < 0 { 0 } else { w }, h: if h < 0 { 0 } else { h } }
}
///|
pub fn Rect::union(self : Rect, other : Rect) -> Rect {
let x0 = if self.x < other.x { self.x } else { other.x }
let y0 = if self.y < other.y { self.y } else { other.y }
let self_x1 = self.x + self.w
let other_x1 = other.x + other.w
let x1 = if self_x1 > other_x1 { self_x1 } else { other_x1 }
let self_y1 = self.y + self.h
let other_y1 = other.y + other.h
let y1 = if self_y1 > other_y1 { self_y1 } else { other_y1 }
Rect::new(x0, y0, x1 - x0, y1 - y0)
}
///|
pub fn Rect::contains(self : Rect, px : Int, py : Int) -> Bool {
px >= self.x && py >= self.y && px < self.x + self.w && py < self.y + self.h
}
///|
#derive(Eq, Debug)
pub(all) enum TextAlign {
Left
Center
Right
}
///|
#derive(Eq)
pub(all) enum DrawOp {
FillRect(Rect, Rgba, Int)
StrokeRect(Rect, Rgba, Int, Int)
Line(Int, Int, Int, Int, Rgba, Int)
Circle(Int, Int, Int, Rgba, Bool)
Text(Int, Int, String, Int, Rgba, TextAlign, Bool)
/// Same as Text but in the monospace face (code listings).
MonoText(Int, Int, String, Int, Rgba, TextAlign, Bool)
PushClip(Rect)
PopClip
/// A loaded image (`@renderer.add_image` / `raster.load-image` handle) scaled into the rect.
Image(Rect, UInt)
}
///|
pub(all) struct ShmBufferSnapshot {
width : Int
height : Int
stride : Int
/// Wayland wl_shm format: 0 = ARGB8888, 1 = XRGB8888
format : Int
bytes : Bytes
/// With the host rasteriser (the wasm compositor) the pixels live in the host, not in `bytes`
/// (empty): this is the host's surface id, 0 when there is none.
host_id : UInt
}
///|
pub(all) struct WindowSceneNode {
id : UInt64
title : String
app_id : String
focused : Bool
fullscreen : Bool
geometry : Rect
/// (top, right, bottom, left)
deco_insets : (Int, Int, Int, Int)
deco_ops : Array[DrawOp]
surface : ShmBufferSnapshot?
}
///|
pub(all) struct PanelWidgetNode {
id : String
rect : Rect
ops : Array[DrawOp]
popup : (Rect, Array[DrawOp])?
}
///|
pub(all) struct DamageTracker {
regions : Array[Rect]
mut full_damage : Bool
}
///|
pub fn DamageTracker::new() -> DamageTracker {
{ regions: [], full_damage: true }
}
///|
pub fn DamageTracker::mark_full(self : DamageTracker) -> Unit {
self.full_damage = true
self.regions.clear()
}
///|
pub fn DamageTracker::mark_rect(self : DamageTracker, rect : Rect) -> Unit {
if self.full_damage || rect.w <= 0 || rect.h <= 0 {
return
}
self.regions.push(rect)
}
///|
pub fn DamageTracker::take_damage(
self : DamageTracker,
output_w : Int,
output_h : Int,
) -> Array[Rect] {
if self.full_damage {
self.full_damage = false
self.regions.clear()
return [Rect::new(0, 0, output_w, output_h)]
}
let out : Array[Rect] = []
for r in self.regions {
out.push(r)
}
self.regions.clear()
out
}
///|
pub(all) struct SceneGraph {
output_w : Int
output_h : Int
/// The static wallpaper, drawn first; the renderer keeps its pixels between frames.
mut wallpaper_ops : Array[DrawOp]
mut background_ops : Array[DrawOp]
mut windows : Array[WindowSceneNode]
mut panel_ops : Array[DrawOp]
mut widgets : Array[PanelWidgetNode]
/// Client popup surfaces (menus, tooltips), drawn above everything.
mut popups : Array[(Rect, ShmBufferSnapshot)]
}
///|
pub fn SceneGraph::new(output_w : Int, output_h : Int) -> SceneGraph {
{
output_w,
output_h,
wallpaper_ops: [],
background_ops: [],
windows: [],
panel_ops: [],
widgets: [],
popups: [],
}
}
///| Generate Breeze-style Server-Side Decoration draw-ops for a window.
pub fn build_breeze_decoration(
title : String,
app_id : String,
focused : Bool,
fullscreen : Bool,
outer_w : Int,
outer_h : Int,
tokens : ThemeTokens,
) -> ((Int, Int, Int, Int), Array[DrawOp]) {
if fullscreen {
return ((0, 0, 0, 0), [])
}
let p = tokens.palette
let title_h = 32
let border = 2
let radius = tokens.radius
let title_bg = if focused { p.surface } else { p.surface.with_alpha(210) }
let border_col = if focused { p.accent } else { p.border }
let title_fg = if focused { p.foreground } else { p.foreground_muted }
let ops : Array[DrawOp] = [
FillRect(Rect::new(0, 0, outer_w, title_h), title_bg, radius),
StrokeRect(Rect::new(0, 0, outer_w, outer_h), border_col, border, radius),
Line(border, title_h - 1, outer_w - border, title_h - 1, p.border, 1),
]
// Window control buttons on the right: [ _ ] [ [] ] [ X ]
let btn_size = 18
let btn_y = (title_h - btn_size) / 2
let close_x = outer_w - 10 - btn_size
let max_x = close_x - 8 - btn_size
let min_x = max_x - 8 - btn_size
// Title: truncated so it never runs under the buttons or past the window edge.
let label = if title.length() > 0 { title } else { app_id }
let char_w = if tokens.font_size >= 14 { 12 } else { 6 }
let max_chars = (min_x - 8 - 14) / char_w
let chars = label.to_array()
let shown = if max_chars < 3 {
""
} else if chars.length() <= max_chars {
label
} else {
let sb = StringBuilder::new()
for i = 0; i < max_chars - 2; i = i + 1 {
sb.write_char(chars[i])
}
sb.write_string("..")
sb.to_string()
}
ops.push(
Text(14, title_h / 2, shown, tokens.font_size, title_fg, Left, focused),
)
let close_bg = if focused {
Rgba::new(218, 68, 83, 255)
} else {
p.surface_hover
}
let btn_bg = p.surface_hover
ops.push(FillRect(Rect::new(min_x, btn_y, btn_size, btn_size), btn_bg, 9))
ops.push(
Line(
min_x + 5,
btn_y + btn_size - 6,
min_x + btn_size - 5,
btn_y + btn_size - 6,
title_fg,
2,
),
)
ops.push(FillRect(Rect::new(max_x, btn_y, btn_size, btn_size), btn_bg, 9))
ops.push(
StrokeRect(
Rect::new(max_x + 5, btn_y + 5, btn_size - 10, btn_size - 10),
title_fg,
1,
0,
),
)
ops.push(FillRect(Rect::new(close_x, btn_y, btn_size, btn_size), close_bg, 9))
ops.push(
Line(
close_x + 5,
btn_y + 5,
close_x + btn_size - 5,
btn_y + btn_size - 5,
p.accent_foreground,
2,
),
)
ops.push(
Line(
close_x + btn_size - 5,
btn_y + 5,
close_x + 5,
btn_y + btn_size - 5,
p.accent_foreground,
2,
),
)
((title_h, border, border, border), ops)
}
///| Generate desktop background gradient + bottom panel draw-ops.
pub fn build_shell_ops(
w : Int,
h : Int,
tokens : ThemeTokens,
layout_label : String,
window_titles : Array[String],
) -> (Array[DrawOp], Array[DrawOp]) {
let p = tokens.palette
let bg_ops : Array[DrawOp] = []
let bands = 16
let band_h = (h + bands - 1) / bands
for i = 0; i < bands; i = i + 1 {
let t_r = p.desktop_top.r.to_int()
let t_g = p.desktop_top.g.to_int()
let t_b = p.desktop_top.b.to_int()
let b_r = p.desktop_bottom.r.to_int()
let b_g = p.desktop_bottom.g.to_int()
let b_b = p.desktop_bottom.b.to_int()
let r = t_r + (b_r - t_r) * i / (bands - 1)
let g = t_g + (b_g - t_g) * i / (bands - 1)
let b = t_b + (b_b - t_b) * i / (bands - 1)
bg_ops.push(
FillRect(
Rect::new(0, i * band_h, w, band_h + 1),
Rgba::new(r, g, b, 255),
0,
),
)
}
let ph = tokens.panel_height
let py = h - ph
let panel_ops : Array[DrawOp] = [
FillRect(Rect::new(0, py, w, ph), p.surface, 0),
Line(0, py, w, py, p.border, 1),
// Launcher pill
FillRect(Rect::new(8, py + 6, 92, ph - 12), p.accent, tokens.radius),
Text(
54,
py + ph / 2,
"Moon",
tokens.font_size,
p.accent_foreground,
Center,
true,
),
// Layout badge
FillRect(
Rect::new(108, py + 6, 96, ph - 12),
p.surface_hover,
tokens.radius,
),
Text(
156,
py + ph / 2,
layout_label,
tokens.font_size - 2,
p.foreground,
Center,
false,
),
]
let mut tx = 216
for title in window_titles {
let pill_w = 140
if tx + pill_w < w - 100 {
panel_ops.push(
FillRect(
Rect::new(tx, py + 6, pill_w, ph - 12),
p.surface_hover,
tokens.radius,
),
)
panel_ops.push(
Text(
tx + 10,
py + ph / 2,
title,
tokens.font_size - 2,
p.foreground,
Left,
false,
),
)
tx = tx + pill_w + 6
}
}
(bg_ops, panel_ops)
}
///| How Chandra draws the frame around a server-decorated window. `Flat` follows V's Flatkit
/// Aurorae theme (github.com/codegod100/kde, aurorae/): the kit's neutral title bar, a 1px
/// outline with 10px top corners, a hairline under the title, a soft 16px shadow all round, a
/// centred title and 24px flat buttons that show a hover / pressed fill (close turns red).
/// `Breeze` is the frame Chandra drew before: accent outline, 18px round buttons, offset shadow.
#derive(Eq, Debug)
pub(all) enum WindowStyle {
Flat
Breeze
}
///| The name state.edn stores (`:window-style :flat` / `:breeze`).
pub fn WindowStyle::name(self : WindowStyle) -> String {
match self {
Flat => "flat"
Breeze => "breeze"
}
}
///|
pub fn WindowStyle::parse(s : String) -> WindowStyle? {
match s {
"flat" => Some(Flat)
"breeze" => Some(Breeze)
_ => None
}
}
///| Height of the frame above the client: the title bar, its outline and the line under it.
pub fn WindowStyle::frame_top(self : WindowStyle) -> Int {
match self {
Flat => flat_title_h
Breeze => 32
}
}
///| Width of the frame left, right and below the client.
pub fn WindowStyle::frame_side(self : WindowStyle) -> Int {
match self {
Flat => 1
Breeze => 2
}
}
///| How far around the frame the decoration's ops may draw (its shadow): hosts redraw this much
/// around a window that moved.
pub let decoration_shadow_reach : Int = 18
///| Flat: the outline row, 36px of title bar with the 24px buttons centred, and the hairline:
/// 38px, TitleEdgeTop 7 + TitleHeight 24 + TitleEdgeBottom 7 in Flatkitrc.
let flat_title_h : Int = 38
///|
let flat_radius : Int = 10
///|
let flat_button : Int = 24
///|
let flat_button_gap : Int = 4
///| From the frame's right edge to the close button (TitleEdgeRight 7 + the outline).
let flat_button_margin : Int = 8
///|
let flat_shadow : Int = 16
///| The title-bar buttons of a frame `outer_w` wide, in the frame's coordinates, as
/// (button, rect, slop): 1 minimize, 2 maximize, 3 close; slop widens the click target.
pub fn WindowStyle::buttons(self : WindowStyle, outer_w : Int) -> Array[(Int, Rect, Int)] {
match self {
Flat => {
let y = (flat_title_h - flat_button) / 2
let close_x = outer_w - flat_button_margin - flat_button
let max_x = close_x - flat_button_gap - flat_button
let min_x = max_x - flat_button_gap - flat_button
[
(1, Rect::new(min_x, y, flat_button, flat_button), 2),
(2, Rect::new(max_x, y, flat_button, flat_button), 2),
(3, Rect::new(close_x, y, flat_button, flat_button), 2),
]
}
Breeze => {
let size = 18
let y = (32 - size) / 2
let close_x = outer_w - 10 - size
let max_x = close_x - 8 - size
let min_x = max_x - 8 - size
[
(1, Rect::new(min_x, y, size, size), 3),
(2, Rect::new(max_x, y, size, size), 3),
(3, Rect::new(close_x, y, size, size), 3),
]
}
}
}
///| The button (1 minimize, 2 maximize, 3 close) at (lx, ly) in a frame `outer_w` wide; 0 none.
pub fn WindowStyle::button_at(self : WindowStyle, outer_w : Int, lx : Int, ly : Int) -> Int {
for b in self.buttons(outer_w) {
let (id, r, slop) = b
if lx >= r.x - slop && lx < r.x + r.w + slop && ly >= r.y - slop && ly < r.y + r.h + slop {
return id
}
}
0
}
///| One window's decoration: what it shows and the pointer's state over its buttons.
pub(all) struct DecorationState {
title : String
app_id : String
focused : Bool
maximized : Bool
/// The button under the pointer (1 minimize, 2 maximize, 3 close; 0 none) ...
hover : Int
/// ... and the one held down.
pressed : Int
}
///| Colours of the Flat frame: flatkit's KitColors (lib/src/theme.dart in codegod100/flutterkit),
/// as aurorae/generate.py has them.
struct FlatColors {
background : Rgba
hover : Rgba
pressed : Rgba
border : Rgba
text : Rgba
text_dim : Rgba
danger : Rgba
danger_hover : Rgba
on_danger : Rgba
/// Shadow opacity (0-255) at the outline, focused and unfocused.
shadow : Int
shadow_inactive : Int
}
///|
fn flat_colors(dark : Bool) -> FlatColors {
if dark {
{
background: Rgba::from_hex("#1C1C1F"),
hover: Rgba::from_hex("#323237"),
pressed: Rgba::from_hex("#3C3C42"),
border: Rgba::from_hex("#3A3A40"),
text: Rgba::from_hex("#EDEDF0"),
text_dim: Rgba::from_hex("#A0A0A8"),
danger: Rgba::from_hex("#E5484D"),
danger_hover: Rgba::from_hex("#EC5F63"),
on_danger: Rgba::from_hex("#FFFFFF"),
shadow: 115, // 0.45
shadow_inactive: 71, // 0.28
}
} else {
{
background: Rgba::from_hex("#F6F6F7"),
hover: Rgba::from_hex("#EDEDEF"),
pressed: Rgba::from_hex("#DEDEE2"),
border: Rgba::from_hex("#DADADE"),
text: Rgba::from_hex("#1E1E22"),
text_dim: Rgba::from_hex("#66666E"),
danger: Rgba::from_hex("#D12D3A"),
danger_hover: Rgba::from_hex("#B8232F"),
on_danger: Rgba::from_hex("#FFFFFF"),
shadow: 51, // 0.20
shadow_inactive: 28, // 0.11
}
}
}
///| True for a dark theme (its surface colour is dark): Moon Night, not Moon Day.
pub fn ThemeTokens::is_dark(self : ThemeTokens) -> Bool {
let s = self.palette.surface
s.r.to_int() * 299 + s.g.to_int() * 587 + s.b.to_int() * 114 < 128 * 1000
}
///| The shadow's falloff from the outline out, as (fraction of the reach, fraction of the
/// opacity): SHADOW_STOPS in aurorae/generate.py.
let shadow_stops : Array[(Double, Double)] = [
(0.0, 1.0),
(0.15, 0.72),
(0.35, 0.40),
(0.6, 0.15),
(0.8, 0.05),
(1.0, 0.0),
]
///| Shadow opacity (0-255) at `t` (0 at the outline, 1 at the shadow's reach).
fn shadow_alpha(max_a : Int, t : Double) -> Int {
let mut i = 0
while i + 1 < shadow_stops.length() && shadow_stops[i + 1].0 < t {
i = i + 1
}
if i + 1 >= shadow_stops.length() {
return 0
}
let (t0, f0) = shadow_stops[i]
let (t1, f1) = shadow_stops[i + 1]
let f = f0 + (f1 - f0) * ((t - t0) / (t1 - t0))
(max_a.to_double() * f + 0.5).to_int().clamp(min=0, max=255)
}
///| A soft shadow all round a frame `w` x `h` with rounded top corners (`radius`) and square
/// bottom ones, drawn in 2px bands out to `flat_shadow`. The straight edges are 1-band rows and
/// columns; each corner stacks translucent rounded rectangles inside a square clip, so their sum
/// fades with the distance from the corner's arc (the radial gradients of decoration.svg).
/// Everything is plain spans: cheap for the software rasterisers.
fn push_flat_shadow(ops : Array[DrawOp], w : Int, h : Int, radius : Int, max_a : Int) -> Unit {
let step = 2
let bands = flat_shadow / step
// Opacity of band k (1 = next to the outline), and of the layer that adds it over band k + 1.
let total = Array::make(bands + 2, 0)
for k = 1; k <= bands; k = k + 1 {
total[k] = shadow_alpha(max_a, (k.to_double() - 0.5) / bands.to_double())
}
let layer = Array::make(bands + 2, 0)
for k = 1; k <= bands; k = k + 1 {
let outer = 255 - total[k + 1]
layer[k] = if outer <= 0 {
0
} else {
(255.0 * (1.0 - (255 - total[k]).to_double() / outer.to_double()) + 0.5).to_int().clamp(min=0, max=255)
}
}
let black = fn(a : Int) { Rgba::new(0, 0, 0, a) }
// Edges.
for k = 1; k <= bands; k = k + 1 {
let c = black(total[k])
if total[k] == 0 {
continue
}
let d = k * step
ops.push(FillRect(Rect::new(radius, -d, w - 2 * radius, step), c, 0))
ops.push(FillRect(Rect::new(0, h + d - step, w, step), c, 0))
ops.push(FillRect(Rect::new(-d, radius, step, h - radius), c, 0))
ops.push(FillRect(Rect::new(w + d - step, radius, step, h - radius), c, 0))
}
// Corners: top ones round about the outline's arc, bottom ones about the square corner.
let reach = flat_shadow
let corners = [
(Rect::new(-reach, -reach, reach + radius, reach + radius), radius),
(Rect::new(w - radius, -reach, reach + radius, reach + radius), radius),
(Rect::new(-reach, h, reach, reach), 0),
(Rect::new(w, h, reach, reach), 0),
]
for corner in corners {
let (clip, r0) = corner
ops.push(PushClip(clip))
for k = bands; k >= 1; k = k - 1 {
if layer[k] > 0 {
let d = k * step
ops.push(FillRect(Rect::new(-d, -d, w + 2 * d, h + 2 * d), black(layer[k]), r0 + d))
}
}
ops.push(PopClip)
}
}
///| The Breeze frame's drop shadow, offset 4px right and 6px down; only the strips that stick
/// out from under the window (and the part under the translucent title bar) are blended.
fn push_breeze_shadow(ops : Array[DrawOp], w : Int, h : Int, top : Int) -> Unit {
let shadow = Rect::new(4, 6, w, h)
let col = Rgba::new(0, 0, 0, 80)
for strip in [Rect::new(w, 0, 4, h + 6), Rect::new(0, h, w + 4, 6), Rect::new(0, 0, w, top + 2)] {
ops.push(PushClip(strip))
ops.push(FillRect(shadow, col, 8))
ops.push(PopClip)
}
}
///| Shorten `label` with ".." until `text_w` says it fits in `max_w` pixels ("" if nothing does).
fn fit_title(label : String, max_w : Int, text_w : (String) -> Int) -> String {
if text_w(label) <= max_w {
return label
}
let chars = label.to_array()
let mut n = chars.length() - 1
while n > 0 {
let s = String::from_array(chars[:n]) + ".."
if text_w(s) <= max_w {
return s
}
n = n - 1
}
""
}
///| A window's frame (title bar, outline, shadow and buttons) in `style`, as draw ops in the
/// coordinates of its outer box (`outer_w` x `outer_h`: the client plus `style`'s frame), and the
/// frame's insets (top, right, bottom, left). `text_w(s, size, bold)` measures title text.
pub fn build_window_decoration(
style : WindowStyle,
d : DecorationState,
outer_w : Int,
outer_h : Int,
tokens : ThemeTokens,
text_w : (String, Int, Bool) -> Int,
) -> ((Int, Int, Int, Int), Array[DrawOp]) {
match style {
Breeze => {
let (insets, deco) = build_breeze_decoration(d.title, d.app_id, d.focused, false, outer_w, outer_h, tokens)
let ops : Array[DrawOp] = []
push_breeze_shadow(ops, outer_w, outer_h, insets.0)
// Hover / pressed: a ring around the button, in the accent colour while pressed.
for b in style.buttons(outer_w) {
let (id, r, _) = b
if id == d.pressed {
deco.push(StrokeRect(Rect::new(r.x - 1, r.y - 1, r.w + 2, r.h + 2), tokens.palette.accent, 2, 10))
} else if id == d.hover {
deco.push(StrokeRect(Rect::new(r.x - 1, r.y - 1, r.w + 2, r.h + 2), tokens.palette.foreground_muted, 1, 10))
}
}
ops.append(deco)
(insets, ops)
}
Flat => build_flat_decoration(d, outer_w, outer_h, tokens, text_w)
}
}
///| Flatkit's frame (see `WindowStyle::Flat`). Maximized: no shadow and square corners.
fn build_flat_decoration(
d : DecorationState,
w : Int,
h : Int,
tokens : ThemeTokens,
text_w : (String, Int, Bool) -> Int,
) -> ((Int, Int, Int, Int), Array[DrawOp]) {
let c = flat_colors(tokens.is_dark())
let top = flat_title_h
let radius = if d.maximized { 0 } else { flat_radius }
let ops : Array[DrawOp] = []
if !d.maximized {
push_flat_shadow(ops, w, h, radius, if d.focused { c.shadow } else { c.shadow_inactive })
}
// Title bar inside its outline: two rounded fills, clipped to the title bar so nothing is
// drawn under the client (a translucent client shows what is behind it, not the frame).
ops.push(PushClip(Rect::new(0, 0, w, top)))
ops.push(FillRect(Rect::new(0, 0, w, top + radius), c.border, radius))
ops.push(FillRect(Rect::new(1, 1, w - 2, top + radius), c.background, if radius > 1 { radius - 1 } else { 0 }))
ops.push(PopClip)
// The hairline under the title, then the outline down the sides and along the bottom.
ops.push(FillRect(Rect::new(1, top - 1, w - 2, 1), c.border, 0))
ops.push(FillRect(Rect::new(0, top, 1, h - top), c.border, 0))
ops.push(FillRect(Rect::new(w - 1, top, 1, h - top), c.border, 0))
ops.push(FillRect(Rect::new(1, h - 1, w - 2, 1), c.border, 0))
// Buttons: nothing behind the glyph at rest, the hover / pressed fill under the pointer.
let buttons = WindowStyle::Flat.buttons(w)
for b in buttons {
let (id, r, _) = b
let close = id == 3
let state = if id == d.pressed { 2 } else if id == d.hover { 1 } else { 0 }
let fill = match (state, close) {
(1, true) => Some(c.danger)
(2, true) => Some(c.danger_hover)
(1, false) => Some(c.hover)
(2, false) => Some(c.pressed)
_ => None
}
match fill {
Some(col) => ops.push(FillRect(r, col, 6))
None => ()
}
let fg = if close && state > 0 {
c.on_danger
} else if d.focused || state > 0 {
c.text
} else {
c.text_dim
}
push_flat_glyph(ops, id, d.maximized, r.x, r.y, fg)
}
// Title: centred on the whole bar while it fits between the buttons on both sides, else
// shortened and left of the buttons.
let size = if tokens.font_size > 1 { tokens.font_size - 1 } else { tokens.font_size }
let fg = if d.focused { c.text } else { c.text_dim }
let label = if d.title.length() > 0 { d.title } else { d.app_id }
let first_btn = buttons[0].1.x
let side = w - first_btn + 8
let measure = fn(s : String) { text_w(s, size, true) }
let tw = measure(label)
let cy = top / 2
if tw <= w - 2 * side {
ops.push(Text(w / 2, cy, label, size, fg, Center, true))
} else {
let shown = fit_title(label, first_btn - 8 - 12, measure)
if shown.length() > 0 {
ops.push(Text(12, cy, shown, size, fg, Left, true))
}
}
((top, 1, 1, 1), ops)
}
///| A button glyph in its 24px box at (x, y): the 1.5px line icons of aurorae/generate.py, as
/// 1px lines with a faint second line beside them for the half pixel.
fn push_flat_glyph(ops : Array[DrawOp], id : Int, maximized : Bool, x : Int, y : Int, fg : Rgba) -> Unit {
let soft = fg.with_alpha(fg.a.to_int() * 2 / 5)
match id {
1 => {
ops.push(FillRect(Rect::new(x + 8, y + 12, 9, 1), fg, 0))
ops.push(FillRect(Rect::new(x + 8, y + 13, 9, 1), soft, 0))
}
2 =>
if maximized {
// Restore: a front square and the corner of one behind it.
ops.push(StrokeRect(Rect::new(x + 7, y + 9, 8, 8), fg, 1, 2))
ops.push(StrokeRect(Rect::new(x + 8, y + 10, 6, 6), soft, 1, 1))
ops.push(FillRect(Rect::new(x + 10, y + 7, 6, 1), fg, 0))
ops.push(FillRect(Rect::new(x + 17, y + 8, 1, 6), fg, 0))
ops.push(FillRect(Rect::new(x + 16, y + 7, 1, 1), soft, 0))
ops.push(FillRect(Rect::new(x + 10, y + 8, 6, 1), soft, 0))
ops.push(FillRect(Rect::new(x + 16, y + 8, 1, 6), soft, 0))
} else {
ops.push(StrokeRect(Rect::new(x + 7, y + 7, 10, 10), fg, 1, 2))
ops.push(StrokeRect(Rect::new(x + 8, y + 8, 8, 8), soft, 1, 1))
}
_ => {
ops.push(Line(x + 8, y + 8, x + 16, y + 16, fg, 1))
ops.push(Line(x + 16, y + 8, x + 8, y + 16, fg, 1))
ops.push(Line(x + 9, y + 8, x + 16, y + 15, soft, 1))
ops.push(Line(x + 8, y + 9, x + 15, y + 16, soft, 1))
ops.push(Line(x + 15, y + 8, x + 8, y + 15, soft, 1))
ops.push(Line(x + 16, y + 9, x + 9, y + 16, soft, 1))
}
}
}