diff --git a/README.md b/README.md index ddc3be8..dd1badc 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,11 @@ has a simple line chart. Both accept caller-owned values and colors; market data, networking, price formatting, and time labels stay in the application. -The shared plot surface, text, border and marks follow Kryon's current -`surface`, `text`, `border` and `accent` style tokens on each draw. Installed -style rules still override those defaults. Changing appearance updates an -existing chart without recreating it; callers retain their series colors. +The shared plot surface, text, border and marks use Kryon's active palette +on each draw. The `surface`, `text`, `border` and `accent` style tokens can +supply custom colors, including within a scoped widget palette. Installed +style rules can also override the defaults. Existing charts pick up theme +changes on their next draw, and callers retain control of their series colors. Install the Ziran launcher once with `make install-user` in Ziran, then run `ziran fetch` and `make build` here. `make run` opens a small demo on the Cairo diff --git a/src/app.zi b/src/app.zi index f09ddc2..e596fe1 100644 --- a/src/app.zi +++ b/src/app.zi @@ -1,5 +1,6 @@ using Charts :: #import "Plot"; TextWidget :: #import "kryon/text"; +Theme :: #import "kryon/theme"; #import "kryon/drawing_props" #import "kryon/geometry" #import "kryon/session" @@ -43,7 +44,8 @@ Frame :: (session: Session, viewport: Rectangle) -> s32 { chart.semantic_label = "Example candlestick chart with volume and SMA 20" chart.up_color = ColorFromPacked(cast(u32)0x168265ff) chart.down_color = ColorFromPacked(cast(u32)0xb34d53ff) - chart.grid_color = ColorFromPacked(cast(u32)0xe3eaf0ff) + chart.grid_color = StyleTokenColorOr("grid", + ColorFromPacked(Theme.CurrentPalette().subtle_border)) chart.sma20_color = ColorFromPacked(cast(u32)0xd18a1bff) chart.show_sma20 = true values: []PriceBar = bars[:] diff --git a/src/plot_widget.zi b/src/plot_widget.zi index fbe427d..af12fea 100644 --- a/src/plot_widget.zi +++ b/src/plot_widget.zi @@ -9,6 +9,7 @@ #import "kryon/style" #import "kryon/style_sheet" #import "kryon/surface" +Theme :: #import "kryon/theme"; #import "kryon/tree" #import "kryon/widget_kind" @@ -34,15 +35,16 @@ PlotThemeColor :: (name: string, fallback: u32) -> u32 { #program_export DefaultPlotFaces :: () -> PlotFaces { + palette := Theme.CurrentPalette() faces: PlotFaces faces.plot.value.fields = cast(u32)StyleBackground | cast(u32)StyleForeground | cast(u32)StyleBorder | cast(u32)StyleBorderWidth | cast(u32)StyleRadius | cast(u32)StyleFontSize | cast(u32)StylePaddingX | cast(u32)StylePaddingY | cast(u32)StyleOpacity - faces.plot.value.background = PlotThemeColor("surface", cast(u32)0xf5f5f5ff) - faces.plot.value.foreground = PlotThemeColor("text", cast(u32)0x171717ff) - faces.plot.value.border = PlotThemeColor("border", cast(u32)0x737373ff) + faces.plot.value.background = PlotThemeColor("surface", palette.surface) + faces.plot.value.foreground = PlotThemeColor("text", palette.foreground) + faces.plot.value.border = PlotThemeColor("border", palette.border) faces.plot.value.border_width = 1.0 faces.plot.value.radius = 4.0 faces.plot.value.font_size = 14.0 @@ -51,7 +53,7 @@ DefaultPlotFaces :: () -> PlotFaces { faces.plot.value.opacity = 1.0 faces.mark.value.fields = cast(u32)StyleBackground | cast(u32)StyleOpacity - faces.mark.value.background = PlotThemeColor("accent", cast(u32)0x3b609bff) + faces.mark.value.background = PlotThemeColor("accent", palette.accent) faces.mark.value.opacity = 1.0 return faces } diff --git a/tests/plot_widget_behavior.zi b/tests/plot_widget_behavior.zi index ec9fc09..ccdab90 100644 --- a/tests/plot_widget_behavior.zi +++ b/tests/plot_widget_behavior.zi @@ -13,7 +13,9 @@ TestSession :: () -> Session { #import "plot_widget" #import "band_chart" #import "band_chart_props" +Theme :: #import "kryon/theme"; #import "kryon/tree" +#import "kryon/widget_palette" #import "kryon/widget_kind" using PlotMode; @@ -134,19 +136,10 @@ BandChecks :: () -> s32 { #program_export main :: () -> s32 { - if ThemeChecks() != 0 { return 20 } band := BandChecks() if band != 0 { return band } - index: s32 = 0 - while index < 2 { - if Frame() != index || PlotHostFailed() { return 1 } - if PlotBackgrounds() != (index + 1) * 2 || - PlotMarks() != (index + 1) * 3 || - PlotOutlines() != (index + 1) * 2 || - PlotLineCount() != (index + 1) * 2 || - PlotLabels() != (index + 1) * 4 { return 2 } - index += 1 - } + theme := ThemeChecks() + if theme != 0 { return theme } return CompareLines() } @@ -155,25 +148,49 @@ ThemeToken :: (name: string, color: u32) -> bool { for index: 0..cast(s32)name.count - 1 { bytes[index] = name[index] } return SetStyleTokenColorBytes(bytes[0:name.count], color) } +CheckThemeFrame :: () -> bool { + index := phase + if Frame() != index || PlotHostFailed() { return false } + return PlotBackgrounds() == (index + 1) * 2 && + PlotMarks() == (index + 1) * 3 && PlotOutlines() == (index + 1) * 2 && + PlotLineCount() == (index + 1) * 2 && PlotLabels() == (index + 1) * 4 +} + +ExpectPalette :: (palette: Theme.Palette) { + ExpectPlotColors(palette.surface, palette.border, palette.foreground, palette.accent) +} + ThemeChecks :: () -> s32 { - saved := CaptureStyleTokenColors() - defer { unused RestoreStyleTokenColors(saved) } ClearStyleTokenColors() - fallback := DefaultPlotFaces() - if fallback.plot.value.background != cast(u32)0xf5f5f5ff { return 1 } - unused ThemeToken("surface", cast(u32)0xe8dccaff) - unused ThemeToken("text", cast(u32)0x302820ff) - unused ThemeToken("border", cast(u32)0xd0b898ff) - unused ThemeToken("accent", cast(u32)0x704020ff) - light := PlotFacesFor(PlotProps.{}, DefaultPlotFaces()) - if light.plot.value.background != cast(u32)0xe8dccaff || - light.plot.value.foreground != cast(u32)0x302820ff || - light.plot.value.border != cast(u32)0xd0b898ff || - light.mark.value.background != cast(u32)0x704020ff { return 2 } - unused ThemeToken("surface", cast(u32)0x17212bff) - unused ThemeToken("text", cast(u32)0xf1f5f9ff) - dark := PlotFacesFor(PlotProps.{}, DefaultPlotFaces()) - if dark.plot.value.background != cast(u32)0x17212bff || - dark.plot.value.foreground != cast(u32)0xf1f5f9ff { return 3 } + if !Theme.InstallDefaultTheme(false) { return 20 } + ExpectPalette(Theme.LightPalette()) + if !CheckThemeFrame() { return 21 } + + // Both existing nodes draw again with the new palette and the same keys. + if !Theme.SetThemeMode(true) { return 22 } + dark := Theme.DarkPalette() + ExpectPalette(dark) + if !CheckThemeFrame() { return 23 } + + custom := cast(u32)0xe8dccaff + if !ThemeToken("surface", custom) { return 24 } + ExpectPlotColors(custom, dark.border, dark.foreground, dark.accent) + if !CheckThemeFrame() { return 25 } + + scoped := WidgetPalette.{.base = cast(u32)0x17212bff, + .background = cast(u32)0x101820ff, .text = cast(u32)0xf1f5f9ff, + .border = cast(u32)0x506080ff, .selected = cast(u32)0x90d0e0ff, + .selected_text = cast(u32)0x102030ff} + if !WidgetPaletteUse(TestSession(), scoped) { return 26 } + ExpectPlotColors(scoped.base, scoped.border, scoped.text, scoped.selected) + if !CheckThemeFrame() { return 27 } + if !WidgetPaletteRestore(TestSession()) { return 28 } + ExpectPlotColors(custom, dark.border, dark.foreground, dark.accent) + if !CheckThemeFrame() { return 29 } + + ClearStyleTokenColors() + if !Theme.SetThemeMode(false) { return 30 } + ExpectPalette(Theme.LightPalette()) + if !CheckThemeFrame() { return 31 } return 0 } diff --git a/tests/plot_widget_host.zi b/tests/plot_widget_host.zi index ab0db1d..88e7770 100644 --- a/tests/plot_widget_host.zi +++ b/tests/plot_widget_host.zi @@ -7,6 +7,24 @@ outlines: s32; lines: s32; labels: s32; failed: bool; +expected_surface: u32; +expected_border: u32; +expected_text: u32; +expected_mark: u32; + +#program_export +ExpectPlotColors :: (surface: u32, border: u32, text: u32, mark: u32) { + expected_surface = surface + expected_border = border + expected_text = text + expected_mark = mark +} + +ColorMatches :: (color: Color, packed: u32) -> bool { + expected := ColorFromPacked(packed) + return color.r == expected.r && color.g == expected.g && + color.b == expected.b && color.a == expected.a +} #program_export MeasureGlyphWidth :: (value: string, font: s32, typeface: string) -> s32 { @@ -22,8 +40,7 @@ RasterRoundedRectangle :: (bounds: Rectangle, radius: float32, bounds.x + bounds.width > 110.0 || bounds.y + bounds.height > 150.0 { failed = true } if radius == 0.0 { - if color.r != cast(u8)59 || color.g != cast(u8)96 || - color.b != cast(u8)155 || color.a != cast(u8)255 { + if !ColorMatches(color, expected_mark) { failed = true } which: s32 = marks % 3 @@ -35,9 +52,7 @@ RasterRoundedRectangle :: (bounds: Rectangle, radius: float32, bounds.y != 135.0 || bounds.height != 15.0) { failed = true } marks += 1 } else { - if radius != 4.0 || color.r != cast(u8)245 || - color.g != cast(u8)245 || color.b != cast(u8)245 || - color.a != cast(u8)255 { failed = true } + if radius != 4.0 || !ColorMatches(color, expected_surface) { failed = true } backgrounds += 1 } } @@ -50,8 +65,7 @@ RasterRoundedRectangleOutline :: (bounds: Rectangle, radius: float32, bounds.height != 60.0 || (bounds.y != 10.0 && bounds.y != 90.0) || radius != 4.0 || width != 1.0 || - color.r != cast(u8)115 || color.g != cast(u8)115 || - color.b != cast(u8)115 || color.a != cast(u8)255 { failed = true } + !ColorMatches(color, expected_border) { failed = true } outlines += 1 } @@ -60,8 +74,7 @@ RasterLine :: (line: Rectangle, color: Color) { if line.x < 10.0 || line.x + line.width > 110.0 || line.y < 10.0 || line.y > 70.0 || line.y + line.height < 10.0 || line.y + line.height > 70.0 || - color.r != cast(u8)59 || color.g != cast(u8)96 || - color.b != cast(u8)155 || color.a != cast(u8)255 { failed = true } + !ColorMatches(color, expected_mark) { failed = true } lines += 1 } @@ -82,8 +95,7 @@ RasterTextClipped :: (value: string, x: s32, y: s32, font: s32, if value.count == 0 || x < 10 || x > 110 || y < 10 || y > 150 || font != 14 || clip.x != 10.0 || clip.width != 100.0 || (clip.y != 10.0 && clip.y != 90.0) || clip.height != 60.0 || - color.r != cast(u8)23 || color.g != cast(u8)23 || - color.b != cast(u8)23 || color.a != cast(u8)255 { failed = true } + !ColorMatches(color, expected_text) { failed = true } labels += 1 }