Files
plot/tests/plot_widget_behavior.zi

197 lines
7.0 KiB
Ziran

#import "kryon/session"
test_session: Session;
TestSession :: () -> Session {
if !SessionValid(test_session) { test_session = SessionOpen() }
return test_session
}
#import "kryon/geometry"
#import "kryon/drawing_props"
#import "kryon/paint_queue"
#import "plot"
#import "plot_props"
#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;
using WidgetKind;
phase: s32;
#program_export
Frame :: () -> s32 {
lines: [3]float32
lines[0] = 0.0
lines[1] = 0.5
lines[2] = 1.0
bars: [3]float32
bars[0] = 0.25
bars[1] = 0.5
bars[2] = 1.0
automatic: PlotProps
observed: PlotRange = PlotObservedRange(bars[0:3], -1,
automatic)
if observed.min_value != 0.25 ||
observed.max_value != 1.0 ||
observed.range != 0.75 { return -2 }
if PlotOffset(0, 5) != 0 || PlotOffset(4, 6) != 2 ||
PlotOffset(4, -1) != 3 || PlotOffset(4, -6) != 2 ||
PlotOffset(1, 99) != 0 { return -3 }
fixed: PlotRange = PlotRangeFor(0.0, 10.0, -1.0, 20.0)
if fixed.min_value != 0.0 || fixed.max_value != 10.0 ||
fixed.range != 10.0 || PlotNormalize(5.0, fixed) != 0.5 ||
PlotNormalize(-5.0, fixed) != 0.0 ||
PlotNormalize(50.0, fixed) != 1.0 { return -4 }
singleton: PlotRange = PlotRangeFor(10.0, 5.0, 3.0, 3.0)
if singleton.min_value != 2.5 ||
singleton.max_value != 3.5 ||
singleton.range != 1.0 { return -5 }
line_props: PlotProps
line_props.key = cast(u64)11
line_props.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
line_props.label = "Trend"
line_props.semantic_label = "XMR price trend"
line_props.overlay = "Now"
line_props.scale_min = 0.0
line_props.scale_max = 1.0
bar_props: PlotProps
bar_props.key = cast(u64)12
bar_props.bounds = Rectangle.{10.0, 90.0, 100.0, 60.0}
bar_props.label = "Bars"
bar_props.overlay = "Peak"
bar_props.offset = 1
bar_props.scale_min = 0.0
bar_props.scale_max = 1.0
bar_props.mode = cast(PlotMode)PlotBars
TreeStart(TestSession(), cast(u64)1, Rectangle.{0.0, 0.0, 140.0, 180.0})
if Plot(TestSession(), line_props, lines[0:3]) != 1 ||
Plot(TestSession(), bar_props, bars[0:3]) != 2 ||
!TreeFinish(TestSession()) || TreeCount(TestSession()) != 3 ||
TreeNodeAt(TestSession(), 1).kind != WidgetKindPlot ||
TreeNodeAt(TestSession(), 2).kind != WidgetKindPlot ||
TreeNodeAt(TestSession(), 1).semantic_label != "XMR price trend" ||
TreeNodeAt(TestSession(), 2).semantic_label != "Bars" { return -1 }
PaintFlush(TestSession())
old: s32 = phase
phase += 1
return old
}
#import "plot_widget_host"
#import "line_chart"
// This week over last week: five grid lines, then two segments drawn twice
// for each series.
CompareLines :: () -> s32 {
current: [3]float32 = .[1.0, 2.0, 3.0]
previous: [3]float32 = .[4.0, 2.0, 2.0]
both: ChartRange = ChartRangeForBoth(current[:], previous[:])
if both.low != 1.0 || both.high != 4.0 { return 3 }
chart: LineChartProps
chart.key = cast(u64)21
chart.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
chart.semantic_label = "Visits this week and last week"
before: s32 = PlotLineCount()
TreeStart(TestSession(), cast(u64)2, Rectangle.{0.0, 0.0, 140.0, 180.0})
LineChartCompare(TestSession(), chart, current[:], previous[:], both)
if !TreeFinish(TestSession()) { return 4 }
PaintFlush(TestSession())
if PlotLineCount() - before != 5 + 4 + 4 { return 5 }
return 0
}
// The band chart's scale, slots and the point a pointer shows.
BandChecks :: () -> s32 {
props: BandChartProps
props.bounds = Rectangle.{0.0, 0.0, 312.0, 140.0}
props.slots = 30
props.min_value = 0.0
props.max_value = 10.0
props.top_label = "10"
props.bottom_label = "0"
props.font = 14
plot: Rectangle = BandChartPlotBounds(props)
if plot.x != 28.0 || plot.y != 10.0 || plot.width != 276.0 ||
plot.height != 120.0 { return 10 }
if BandChartY(plot, props, 10.0) != 10.0 || BandChartY(plot, props, 0.0) != 130.0 ||
BandChartY(plot, props, 5.0) != 70.0 || BandChartY(plot, props, 20.0) != 10.0 {
return 11
}
first: float32 = BandChartSlotX(plot, props, 0)
if first < 32.5 || first > 32.7 { return 12 }
points: [3]BandChartPoint
points[0] = BandChartPoint.{.slot = 0, .value = 2.0, .good = 1}
points[1] = BandChartPoint.{.slot = 10, .value = 5.0, .good = 0}
points[2] = BandChartPoint.{.slot = 29, .value = 9.0, .good = -1}
if BandChartNearest(props, points[:], BandChartSlotX(plot, props, 10) + 2.0, 20.0) != 1 ||
BandChartNearest(props, points[:], 300.0, 120.0) != 2 ||
BandChartNearest(props, points[0:0], 50.0, 50.0) != -1 { return 13 }
return 0
}
#program_export
main :: () -> s32 {
band := BandChecks()
if band != 0 { return band }
theme := ThemeChecks()
if theme != 0 { return theme }
return CompareLines()
}
ThemeToken :: (name: string, color: u32) -> bool {
bytes: [16]u8
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 {
ClearStyleTokenColors()
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
}