Paint linear gradients at any angle with up to six stops
Row, Column and Grid paint a CSS linear-gradient() at any angle or toward a side or corner, with two to six stops, from the vertical gradient the paint queue already draws: a vertical gradient is one strip split at its stops, any other up to 64 narrow strips, each split where a stop line crosses it, so colors are exact along each strip's center. StyleBoxData carries the gradient direction (the stylesheet computes it), corner, stop positions and middle colors; the face keeps the first and last.
This commit is contained in:
1 parent
374358cd89
commit
d0711324df
4 files changed
+143
-4
No files matched your search
@@ -2,6 +2,8 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Row, Column and Grid paint `linear-gradient()` at any angle or toward a side or corner with two to six stops, from vertical gradient strips (one strip split at its stops for a vertical gradient, up to 64 otherwise). `StyleBoxData` carries the gradient direction, corner, stops and middle colors.
|
||||
|
||||
- `Layout.Box(session, props, face)` opens a padded column (or row) that takes a widget's own resolved face for its padding, gap and paint, so a composite widget such as Card can be a self-sizing container that paints at `End`; Row, Column and Grid also paint a CSS `box-shadow` (an elevation role) under their box.
|
||||
|
||||
- CSS selectors carry tree context natively: one ancestor matched by class or id (`.card Text`, `#sidebar .item`), the parent (`.toolbar > Button`), `:first-child`, and `:not(.class)` or `:not(:state)`. `StyleSelector` gains `context`, `ancestor_class`, `ancestor_id`, `not_class` and `not_state`; the style_tree module, installed by `BeginFrame`, matches the widget being styled as the next child of the open container, and rules without context skip it. The starter budget is raised to 3,950,000 bytes (3,885,985 measured).
|
||||
|
||||
@@ -230,7 +230,7 @@ Parse once per stylesheet in a running app. The example reparses to show the com
|
||||
### Supported Properties & Values
|
||||
|
||||
- **Native StyleData properties**:
|
||||
- Colors: `color`, `background` (color or two-stop vertical `linear-gradient([to bottom | 180deg,] top, bottom)`), `background-color`, `border-color`.
|
||||
- Colors: `color`, `background` (a color or `linear-gradient()`), `background-image` (`none` or `linear-gradient()`), `background-color`, `border-color`. A gradient takes an angle (`deg`, `turn`, `rad`, `grad`) or `to` a side or corner, and two to six color stops with optional `%` positions. `Row`, `Column` and `Grid` paint any of them; other widgets paint a top-to-bottom gradient of the first and last colors (`css_tool check` says so).
|
||||
- Focus ring: `outline` (`[width] [solid | auto | none] [color]`), `outline-color`, `outline-width` (`px`, `rem`, `thin`, `medium`, `thick`) and `outline-offset` (signed) size the ring Button, ConfirmButton, Selectable and Radio draw on keyboard focus, from their `:focus-visible` rules. Keyboard focus always keeps a ring, so `outline: none` changes nothing (`css_tool check` says so).
|
||||
- Borders & shape: `border` (`width` or `thin`/`medium`/`thick`, `solid` or `none`, `color`), `border-width`, `border-style` (`solid` | `none`), `border-radius`, `opacity` (unitless number or clamped `0%`–`100%`).
|
||||
- Per-side borders on `Row`, `Column` and `Grid`: `border-top|right|bottom|left` and the logical `border-block-start|end` and `border-inline-start|end`, each with `-width`, `-color` and `-style`, for dividers, accent bars and tab underlines. A side without its own width or color takes the uniform `border`, and a uniform border later in the same rule clears earlier sides. The sides paint as square bands inside the box; with a `border-radius` the box paints only its uniform border, and `css_tool check` reports it.
|
||||
@@ -244,7 +244,7 @@ Parse once per stylesheet in a running app. The example reparses to show the com
|
||||
- Kryon extensions: `-kryon-material` (`lightfield` | `flat` | `glass`), `-kryon-icon-size`, `-kryon-font-role` (`inherit` | `control` | `body` | `heading` | `code`), `-kryon-elevation` (`none` | `rest` | `raised` | `floating`), `-kryon-motion-duration`, `-kryon-motion-multiplier`, `-kryon-motion-easing` (`inherit` | `standard` | `linear` | `instant`), `-kryon-motion-reduced` (`reduce` | `no-preference`).
|
||||
- **Units & values**: hex (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`), CSS named colors, `rgb()`, `rgba()`, `hsl()`, `hsla()`, `hwb(hue white% black% [/ alpha])`, hue angle units (`deg`, `rad`, `grad`, `turn`), `px`, `rem` (`16px`), `s`, `ms`, `em` (the rule's font-size, else 16px), `vw`, `vh`, `calc()`, `min()`, `max()`, `clamp()`, `:root` custom properties (`--token: ...;`) with `var(--token, fallback)` whose fallback may be one more `var()`, and `var(--kryon-surface)`-style theme colors (`background`, `surface`, `text`, `muted`, `border`, `accent`, `success`, `warning`, `danger` and more). `%` sizes as above; other `%` lengths are refused. The full property table is generated from the parser in the CSS package README.
|
||||
- **At-rules**: `@import` (before rules), `@layer`, `@keyframes`, `@media` (`prefers-color-scheme`, `prefers-contrast`, `prefers-reduced-motion`, `pointer`, `width`, `min-width`, `max-width`, `-kryon-theme`, `-kryon-density`, `-kryon-pointer`, `-kryon-platform`).
|
||||
- **Browser-only layout and paint** includes grid declarations outside the native track subset, radial, angled and multi-stop gradients, `transform`, `text-decoration`, `text-transform`, `white-space`, `cursor`, `z-index` and `visibility`. A plain `CssSheetParse` refuses them; read a sheet shared with the browser export through `CssSheetParseWithDiagnostics`, which reports them. CSS `display` maps to an authored native flex or grid container. `wrap-reverse`, intrinsic flex bases, baseline alignment, and cross-line distribution remain browser-only.
|
||||
- **Browser-only layout and paint** includes grid declarations outside the native track subset, radial, conic and repeating gradients (Kryon backends have no such paint primitive), `transform`, `text-decoration`, `text-transform`, `white-space`, `cursor`, `z-index` and `visibility`. A plain `CssSheetParse` refuses them; read a sheet shared with the browser export through `CssSheetParseWithDiagnostics`, which reports them. CSS `display` maps to an authored native flex or grid container. `wrap-reverse`, intrinsic flex bases, baseline alignment, and cross-line distribution remain browser-only.
|
||||
|
||||
Use `color-mix(in srgb, #2076c0 35%, transparent)` in a native color
|
||||
declaration or custom property to derive a tint from two literal colors.
|
||||
|
||||
+116
-1
@@ -24,7 +24,9 @@ LayoutPaintBox :: (session: Session, bounds: Rectangle, face: StyleData, box: St
|
||||
}
|
||||
if StyleHasField(face, cast(u32)StyleBackground) {
|
||||
top := ColorFromPacked(Opacity(face.background, opacity))
|
||||
if StyleHasField(face, cast(u32)StyleBackgroundEnd) {
|
||||
if StyleHasField(face, cast(u32)StyleBackgroundEnd) && (box.fields & cast(u32)StyleBoxGradient) != 0 {
|
||||
LayoutPaintGradient(session, bounds, face, box, opacity)
|
||||
} else if StyleHasField(face, cast(u32)StyleBackgroundEnd) {
|
||||
PaintVerticalGradient(session, -1, bounds, top,
|
||||
ColorFromPacked(Opacity(face.background_end, opacity)))
|
||||
} else if top.a != cast(u8)0 {
|
||||
@@ -41,6 +43,119 @@ LayoutPaintBox :: (session: Session, bounds: Rectangle, face: StyleData, box: St
|
||||
if mark >= 0 { PaintMoveBelow(session, mark, from) }
|
||||
}
|
||||
|
||||
LayoutGradientStops :: struct {
|
||||
count: s32
|
||||
positions: [6]float64
|
||||
colors: [6]Color
|
||||
}
|
||||
|
||||
LayoutMix :: (a: u8, b: u8, t: float64) -> u8 {
|
||||
return cast(u8)(cast(float64)a + (cast(float64)b - cast(float64)a) * t + 0.5)
|
||||
}
|
||||
|
||||
// The gradient's color at `t` along its line, clamped to the end stops.
|
||||
LayoutGradientAt :: (stops: *LayoutGradientStops, t: float64) -> Color {
|
||||
if t <= stops.positions[0] { return stops.colors[0] }
|
||||
last := stops.count - 1
|
||||
for index: 1..last {
|
||||
if t <= stops.positions[index] {
|
||||
start := stops.positions[index - 1]
|
||||
span := stops.positions[index] - start
|
||||
f := ifx span > 0.0 then (t - start) / span else 1.0
|
||||
a := stops.colors[index - 1]
|
||||
b := stops.colors[index]
|
||||
return Color.{LayoutMix(a.r, b.r, f), LayoutMix(a.g, b.g, f), LayoutMix(a.b, b.b, f), LayoutMix(a.a, b.a, f)}
|
||||
}
|
||||
}
|
||||
return stops.colors[last]
|
||||
}
|
||||
|
||||
LayoutAbs :: (value: float64) -> float64 { return ifx value < 0.0 then -value else value }
|
||||
|
||||
LayoutRound :: (value: float64) -> float64 {
|
||||
if value >= 0.0 { return cast(float64)cast(s64)(value + 0.5) }
|
||||
return -cast(float64)cast(s64)(0.5 - value)
|
||||
}
|
||||
|
||||
// A linear gradient at any angle with up to six stops, from vertical
|
||||
// gradients: a vertical one is one strip split at its stops; any other is
|
||||
// up to 64 narrow strips, each split where a stop line crosses it, so the
|
||||
// colors are exact along each strip's center line.
|
||||
LayoutPaintGradient :: (session: Session, bounds: Rectangle, face: StyleData, box: StyleBoxData, opacity: float32) {
|
||||
if bounds.width <= 0.0 || bounds.height <= 0.0 { return }
|
||||
stops: LayoutGradientStops
|
||||
stops.count = ifx box.gradient_count >= 2 && box.gradient_count <= 6 then box.gradient_count else 2
|
||||
last := stops.count - 1
|
||||
for index: 0..last {
|
||||
stops.positions[index] = cast(float64)box.gradient_stops[index]
|
||||
packed := face.background_end
|
||||
if index == 0 { packed = face.background }
|
||||
else if index < last { packed = box.gradient_colors[index - 1] }
|
||||
stops.colors[index] = ColorFromPacked(Opacity(packed, opacity))
|
||||
}
|
||||
w := cast(float64)bounds.width
|
||||
h := cast(float64)bounds.height
|
||||
// The gradient line's direction; a corner runs perpendicular to the
|
||||
// diagonal that misses it, as in CSS. Its scale cancels below.
|
||||
dx := cast(float64)box.gradient_dx
|
||||
dy := cast(float64)box.gradient_dy
|
||||
corner := box.gradient_corner
|
||||
if corner >= 1 && corner <= 4 {
|
||||
dx = ifx corner <= 2 then h else -h
|
||||
dy = ifx corner == 2 || corner == 3 then w else -w
|
||||
}
|
||||
length := LayoutAbs(w * dx) + LayoutAbs(h * dy)
|
||||
if length <= 0.0 { return }
|
||||
cx := cast(float64)bounds.x + w / 2.0
|
||||
cy := cast(float64)bounds.y + h / 2.0
|
||||
strips: s32 = 1
|
||||
if LayoutAbs(dx) * w > 0.0001 * length {
|
||||
strips = cast(s32)(w / 4.0 + 0.999)
|
||||
if strips > 64 { strips = 64 }
|
||||
if strips < 1 { strips = 1 }
|
||||
}
|
||||
top := cast(float64)bounds.y
|
||||
bottom := top + h
|
||||
for strip: 0..strips - 1 {
|
||||
left := LayoutRound(cast(float64)bounds.x + w * cast(float64)strip / cast(float64)strips)
|
||||
right := LayoutRound(cast(float64)bounds.x + w * cast(float64)(strip + 1) / cast(float64)strips)
|
||||
if strip == strips - 1 { right = cast(float64)bounds.x + w }
|
||||
if right <= left { continue }
|
||||
center := (left + right) / 2.0
|
||||
along := (center - cx) * dx
|
||||
// t(y) = (along + (y - cy) * dy) / length + 0.5 down the strip.
|
||||
if LayoutAbs(dy) * h < 0.000001 * length {
|
||||
color := LayoutGradientAt(*stops, along / length + 0.5)
|
||||
PaintRoundedFill(session, -1, Rectangle.{cast(float32)left, bounds.y, cast(float32)(right - left), bounds.height},
|
||||
0.0, 4, color)
|
||||
continue
|
||||
}
|
||||
cuts: [8]float64
|
||||
cut_count: s32 = 0
|
||||
cuts[0] = top
|
||||
cut_count = 1
|
||||
for index: 0..last {
|
||||
// The y where the strip crosses stop `index`, in top-down order.
|
||||
stop := ifx dy > 0.0 then index else last - index
|
||||
y := cy + ((stops.positions[stop] - 0.5) * length - along) / dy
|
||||
if y > cuts[cut_count - 1] && y < bottom {
|
||||
cuts[cut_count] = y
|
||||
cut_count += 1
|
||||
}
|
||||
}
|
||||
cuts[cut_count] = bottom
|
||||
for segment: 0..cut_count - 1 {
|
||||
y0 := cuts[segment]
|
||||
y1 := cuts[segment + 1]
|
||||
if y1 - y0 <= 0.0 { continue }
|
||||
upper := LayoutGradientAt(*stops, (along + (y0 - cy) * dy) / length + 0.5)
|
||||
lower := LayoutGradientAt(*stops, (along + (y1 - cy) * dy) / length + 0.5)
|
||||
PaintVerticalGradient(session, -1, Rectangle.{cast(float32)left, cast(float32)y0,
|
||||
cast(float32)(right - left), cast(float32)(y1 - y0)}, upper, lower)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each side is a band inside the border box: top and bottom span its width,
|
||||
// left and right the height between them.
|
||||
LayoutPaintSides :: (session: Session, bounds: Rectangle, face: StyleData, box: StyleBoxData,
|
||||
|
||||
+23
-1
@@ -435,6 +435,9 @@ StyleBoxField :: enum {
|
||||
// A focused widget's ring (CSS outline), not a container field.
|
||||
StyleBoxOutlineWidth :: 131072
|
||||
StyleBoxOutlineOffset :: 262144
|
||||
// A linear gradient past top to bottom with two stops: its angle or
|
||||
// corner, stops and middle colors (the face holds the first and last).
|
||||
StyleBoxGradient :: 524288
|
||||
}
|
||||
using StyleBoxField;
|
||||
|
||||
@@ -461,10 +464,21 @@ StyleBoxData :: struct {
|
||||
border_colors: [4]u32
|
||||
outline_width: float32
|
||||
outline_offset: float32
|
||||
// The gradient line's direction for a CSS angle (sin, -cos: 0 degrees
|
||||
// points up), or a corner: 1 top right, 2 bottom right, 3 bottom left,
|
||||
// 4 top left, which follows the box's diagonal.
|
||||
gradient_dx: float32
|
||||
gradient_dy: float32
|
||||
gradient_corner: s32
|
||||
// 2 to 6 stops at increasing positions from 0 to 1.
|
||||
gradient_count: s32
|
||||
gradient_stops: [6]float32
|
||||
// Stops 1 to 4; stop 0 is the face's background, the last its end.
|
||||
gradient_colors: [4]u32
|
||||
}
|
||||
|
||||
// Every box field but text-align, which only Text reads.
|
||||
StyleBoxContainerFields :: 131055
|
||||
StyleBoxContainerFields :: 655343
|
||||
// The per-side border widths and colors.
|
||||
StyleBoxBorderSides :: 130560
|
||||
StyleBoxOutlineFields :: 393216
|
||||
@@ -1303,6 +1317,14 @@ ApplyBoxFields :: (target: *StyleBoxData, value: *StyleBoxData, fields: u32) {
|
||||
if (fields & cast(u32)StyleBoxMarginRight) != 0 { target.margin_right = value.margin_right }
|
||||
if (fields & cast(u32)StyleBoxMarginBottom) != 0 { target.margin_bottom = value.margin_bottom }
|
||||
if (fields & cast(u32)StyleBoxMarginLeft) != 0 { target.margin_left = value.margin_left }
|
||||
if (fields & cast(u32)StyleBoxGradient) != 0 {
|
||||
target.gradient_dx = value.gradient_dx
|
||||
target.gradient_dy = value.gradient_dy
|
||||
target.gradient_corner = value.gradient_corner
|
||||
target.gradient_count = value.gradient_count
|
||||
target.gradient_stops = value.gradient_stops
|
||||
target.gradient_colors = value.gradient_colors
|
||||
}
|
||||
if (fields & cast(u32)StyleBoxOutlineWidth) != 0 { target.outline_width = value.outline_width }
|
||||
if (fields & cast(u32)StyleBoxOutlineOffset) != 0 { target.outline_offset = value.outline_offset }
|
||||
if (fields & cast(u32)StyleBoxBorderSides) != 0 {
|
||||
|
||||
Reference in new issue
Block a user