diff --git a/crates/xframe-indicators/src/lib.rs b/crates/xframe-indicators/src/lib.rs index 0288cfa..78a419c 100644 --- a/crates/xframe-indicators/src/lib.rs +++ b/crates/xframe-indicators/src/lib.rs @@ -108,11 +108,27 @@ pub fn indicator_names() -> Vec<&'static str> { pub struct Indicator { def: &'static Def, params: Vec, + /// Line-colour override (palette index): `None` means the def's + /// `color` (C++ `set_line_color` / `get_line_color`). + color_override: Option, } impl Indicator { pub fn new(def: &'static Def, params: Vec) -> Indicator { - Indicator { def, params } + Indicator { + def, + params, + color_override: None, + } + } + + /// Effective definition (a copy with the colour override applied). + fn effective_def(&self) -> Def { + let mut d = *self.def; + if let Some(c) = self.color_override { + d.color = c; + } + d } pub fn name(&self) -> &'static str { @@ -127,14 +143,47 @@ impl Indicator { self.def.style } + /// Line colour (palette index): the override when set, else the def's. pub fn color(&self) -> i32 { - self.def.color + self.color_override.unwrap_or(self.def.color) + } + + /// Line colour (palette index), as in C++ `get_line_color`. + pub fn get_line_color(&self) -> i32 { + self.color() + } + + /// Line colour (palette index), as in C++ `set_line_color`. Assigns + /// state only; the next `compute` / draw picks it up. + pub fn set_line_color(&mut self, color: i32) { + self.color_override = Some(color); } pub fn params(&self) -> &[f64] { &self.params } + /// One parameter by index (C++ `get_period` / `get_mult` are + /// positional reads of these). `None` when out of range. + pub fn get_param(&self, index: usize) -> Option { + self.params.get(index).copied() + } + + /// Set one parameter by index (no-op when out of range). Unlike the + /// C++ widgets, lines are recomputed from the parameters on every + /// `compute` call, so a setter cannot desync them (the reason the C + /// pass added no `set_period` does not apply here). + pub fn set_param(&mut self, index: usize, value: f64) { + if let Some(p) = self.params.get_mut(index) { + *p = value; + } + } + + /// Replace all parameters. + pub fn set_params(&mut self, params: Vec) { + self.params = params; + } + pub fn def(&self) -> &'static Def { self.def } @@ -146,12 +195,25 @@ impl Indicator { /// Line-plot geometry for this indicator in `area` (see [`plot`]). pub fn line_points(&self, area: &PlotArea, bars: &[Bar]) -> Vec { - plot::line_points(self.def, &self.compute(bars), area, bars) + let def = self.effective_def(); + plot::line_points(&def, &self.compute(bars), area, bars) } /// Marker triangles for a `Markers` indicator in `area` (see [`plot`]). pub fn markers(&self, area: &PlotArea, bars: &[Bar]) -> Vec { - plot::markers(self.def, &self.compute(bars), area, bars) + let def = self.effective_def(); + plot::markers(&def, &self.compute(bars), area, bars) + } + + /// Bar rectangles for a `Bars` indicator in `area` (see [`plot`]). + pub fn bar_rects( + &self, + lines: &Lines, + area: &PlotArea, + bars: &[Bar], + ) -> Vec { + let def = self.effective_def(); + plot::bar_rects(&def, lines, area, bars) } } diff --git a/crates/xframe-indicators/tests/indicator_tests.rs b/crates/xframe-indicators/tests/indicator_tests.rs index 7d0c3fd..e62f375 100644 --- a/crates/xframe-indicators/tests/indicator_tests.rs +++ b/crates/xframe-indicators/tests/indicator_tests.rs @@ -402,3 +402,33 @@ fn render_test_geometry_sma_and_rsi_panes() { assert_eq!(mpts.len(), 2, "MACD plots line + signal"); assert!(mpts.iter().all(|lp| !lp.points.is_empty())); } + +#[test] +fn line_color_override_and_params() { + // Getter/setter pass: line colour override + parameter accessors. + let bars = load_csv("tests/data/SPY_fake_long.csv"); + let area = PlotArea::new(4.0, 4.0, 472.0, 292.0, 0, 60, 0.0, 300.0); + let mut sma = make_indicator("SMA").unwrap(); + let def_color = sma.get_line_color(); + assert_eq!(sma.color(), def_color); + assert_eq!(sma.get_param(0), Some(20.0)); + let pts0 = sma.line_points(&area, &bars); + assert!(!pts0.is_empty()); + assert_eq!(pts0[0].color, xframe_indicators::plot::line_color(def_color, 0)); + sma.set_line_color(3); + assert_eq!(sma.get_line_color(), 3); + let pts1 = sma.line_points(&area, &bars); + assert_eq!(pts1[0].color, xframe_indicators::plot::line_color(3, 0)); + assert_ne!(pts1[0].color, pts0[0].color); + // Parameters: a different period recomputes the lines (no desync: + // compute() re-derives from params on every call). + sma.set_param(0, 5.0); + assert_eq!(sma.get_param(0), Some(5.0)); + let pts2 = sma.line_points(&area, &bars); + assert_ne!(pts2[0].points, pts0[0].points); + sma.set_params(vec![20.0]); + assert_eq!(sma.get_param(0), Some(20.0)); + assert_eq!(sma.get_param(99), None); + sma.set_param(99, 1.0); // out of range: no-op, no panic + assert_eq!(sma.get_param(99), None); +} diff --git a/crates/xframe-widgets/src/graph.rs b/crates/xframe-widgets/src/graph.rs index f27e560..e95d73c 100644 --- a/crates/xframe-widgets/src/graph.rs +++ b/crates/xframe-widgets/src/graph.rs @@ -273,6 +273,8 @@ pub struct XGraph { pub kind: String, pub series: Vec, pub x_labels: Vec, + /// Grid-line colour (was hardcoded 0xe1e1e1 in `draw`). + pub grid_color: Srgb, pub view: PlotView, pub dragging: bool, pub drag_start_x: i32, @@ -298,6 +300,7 @@ impl XGraph { kind: kind.to_string(), series: Vec::new(), x_labels: Vec::new(), + grid_color: Srgb::new(225, 225, 225), dragging: false, drag_start_x: 0, drag_start_first: 0.0, @@ -358,6 +361,24 @@ impl XGraph { } } + /// Series line colour (default per `add_line_series` / `add_ohlc_series`). + pub fn get_series_color(&self, index: usize) -> Option { + self.series.get(index).map(|s| s.color) + } + pub fn set_series_color(&mut self, index: usize, color: Srgb) { + if let Some(s) = self.series.get_mut(index) { + s.color = color; + } + } + + /// Grid-line colour (default (225,225,225), as before). + pub fn get_grid_color(&self) -> Srgb { + self.grid_color + } + pub fn set_grid_color(&mut self, c: Srgb) { + self.grid_color = c; + } + pub fn set_x_labels(&mut self, labels: Vec) { self.x_labels = labels; } @@ -532,7 +553,7 @@ impl Widget for XGraph { let mut v = (lo / step).ceil() * step; while v <= hi { let gy = self.view.y_at(v, lo, hi); - target.set_foreground(0xe1e1e1); + target.set_foreground(self.grid_color.to_pixel()); target.draw_line( self.view.plot_x(), gy, diff --git a/crates/xframe-widgets/src/indicators.rs b/crates/xframe-widgets/src/indicators.rs index 0f31361..b5ffba1 100644 --- a/crates/xframe-widgets/src/indicators.rs +++ b/crates/xframe-widgets/src/indicators.rs @@ -25,7 +25,7 @@ //! their candles are drawn. use xframe_core::draw::{draw_text_at, draw_text_left, draw_triangle_down, draw_triangle_up}; -use xframe_core::{DrawTarget, StyleManager}; +use xframe_core::{DrawTarget, Srgb, StyleManager}; use xframe_indicators::plot::{candle_scale, PlotArea}; use xframe_indicators::{find_def, parse_params, Bar, Indicator, Lines, PlotKind, Style as IndStyle}; @@ -78,7 +78,7 @@ pub fn demo_bars(n: usize) -> Vec { bars } -/// Candle rendering style for an `Ind*` widget. The C++ headers use +/// Candle rendering colours for an `Ind*` widget. The C++ headers use /// three palettes, and the `fdi` def's `plot_kind` does not record /// which one a widget uses: /// * `Muted`: the `pane_` template (`draw_candles` gated by `!pane_`, @@ -87,11 +87,15 @@ pub fn demo_bars(n: usize) -> Vec { /// down (200, 30, 30). /// * `Grey`: the grey-overlay template: light-grey (180, 180, 180) /// bodies on the price scale, no wicks. -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum CandleStyle { - Muted, - Standard, - Grey, +/// Returns (up, down, wicks). +fn candle_colors(name: &str) -> (Srgb, Srgb, bool) { + if CANDLES_MUTED.binary_search(&name).is_ok() { + (Srgb::new(150, 190, 150), Srgb::new(210, 130, 130), true) + } else if CANDLES_GREY.binary_search(&name).is_ok() { + (Srgb::new(180, 180, 180), Srgb::new(180, 180, 180), false) + } else { + (Srgb::new(0, 150, 0), Srgb::new(200, 30, 30), true) + } } /// Widgets whose C++ main uses `make_seeded_series(120, 42)` (85 of @@ -162,17 +166,6 @@ fn demo_bar_count(name: &str) -> usize { } } -/// Candle palette for a widget, from its C++ header. -fn candle_style(name: &str) -> CandleStyle { - if CANDLES_MUTED.binary_search(&name).is_ok() { - CandleStyle::Muted - } else if CANDLES_GREY.binary_search(&name).is_ok() { - CandleStyle::Grey - } else { - CandleStyle::Standard - } -} - /// One indicator widget (the inner implementation; the `Widget` trait /// impl lives on `archetypes::IndicatorWidget`, which delegates here). pub struct IndicatorWidget { @@ -186,6 +179,12 @@ pub struct IndicatorWidget { pub line: Option, pub bars: Vec, pub view: PlotView, + /// Chart background / grid / candle colours (were hardcoded in `draw`). + pub background_color: Srgb, + pub grid_color: Srgb, + pub candle_up_color: Srgb, + pub candle_down_color: Srgb, + pub candle_wicks: bool, pub dragging: bool, pub drag_start_x: i32, pub drag_start_first: f64, @@ -205,6 +204,7 @@ impl IndicatorWidget { if ZOOMED_ONCE.binary_search(&name).is_ok() { view.count = 200.0; } + let (candle_up_color, candle_down_color, candle_wicks) = candle_colors(name); IndicatorWidget { view, state, @@ -212,6 +212,11 @@ impl IndicatorWidget { indicator, line: None, bars: demo_bars(demo_bar_count(name)), + background_color: Srgb::new(255, 255, 255), + grid_color: Srgb::new(225, 225, 225), + candle_up_color, + candle_down_color, + candle_wicks, dragging: false, drag_start_x: 0, drag_start_first: 0.0, @@ -242,6 +247,88 @@ impl IndicatorWidget { self.view.reset(self.bars.len()); } + /// Background colour (default white, as before). + pub fn get_background_color(&self) -> Srgb { + self.background_color + } + pub fn set_background_color(&mut self, c: Srgb) { + self.background_color = c; + } + /// Grid-line colour (default (225,225,225), as before). + pub fn get_grid_color(&self) -> Srgb { + self.grid_color + } + pub fn set_grid_color(&mut self, c: Srgb) { + self.grid_color = c; + } + /// Candle up/down colours and wicks (defaults from the C++ header + /// palette for this widget, as before). + pub fn get_candle_up_color(&self) -> Srgb { + self.candle_up_color + } + pub fn set_candle_up_color(&mut self, c: Srgb) { + self.candle_up_color = c; + } + pub fn get_candle_down_color(&self) -> Srgb { + self.candle_down_color + } + pub fn set_candle_down_color(&mut self, c: Srgb) { + self.candle_down_color = c; + } + pub fn get_candle_wicks(&self) -> bool { + self.candle_wicks + } + pub fn set_candle_wicks(&mut self, v: bool) { + self.candle_wicks = v; + } + /// Registry line index for multi-line defs (read-only: from the registry). + pub fn get_line_index(&self) -> Option { + self.line + } + /// Indicator line colour (palette index; `None` when this widget has + /// no `fdi` def). Delegates to `xframe_indicators::Indicator`. + pub fn get_line_color(&self) -> Option { + self.indicator.as_ref().map(|ind| ind.get_line_color()) + } + pub fn set_line_color(&mut self, color: i32) { + if let Some(ind) = self.indicator.as_mut() { + ind.set_line_color(color); + } + } + /// Indicator parameters by index (`None` when no def / out of range). + pub fn get_param(&self, index: usize) -> Option { + self.indicator.as_ref().and_then(|ind| ind.get_param(index)) + } + pub fn set_param(&mut self, index: usize, value: f64) { + if let Some(ind) = self.indicator.as_mut() { + ind.set_param(index, value); + } + } + /// View accessors (as on `XGraph`). + pub fn set_y_range(&mut self, lo: f64, hi: f64) { + self.view.auto_y = false; + self.view.y_min = lo; + self.view.y_max = hi; + } + pub fn set_auto_y(&mut self, on: bool) { + self.view.auto_y = on; + } + pub fn view_first(&self) -> f64 { + self.view.first + } + pub fn view_count(&self) -> f64 { + self.view.effective_count(self.bars.len()) + } + pub fn zoom_at(&mut self, factor: f64, frac_x: f64) { + let total = self.bars.len(); + self.view.zoom_at(total, factor, frac_x); + self.fire_view_change(); + } + pub fn reset_view(&mut self) { + self.view.reset(self.bars.len()); + self.fire_view_change(); + } + fn fire_view_change(&mut self) { let first = self.view.first; let count = self.view.effective_count(self.bars.len()); @@ -318,14 +405,14 @@ impl IndicatorWidget { hi, ); // Background. - target.set_foreground(0xffffff); + target.set_foreground(self.background_color.to_pixel()); target.fill_rect(b.x, b.y, b.w, b.h); // Grid + y labels (left gutter, as in XGraph). let step = PlotView::nice_step(hi - lo, 6); let mut v = (lo / step).ceil() * step; while v <= hi { let gy = self.view.y_at(v, lo, hi); - target.set_foreground(0xe1e1e1); + target.set_foreground(self.grid_color.to_pixel()); target.draw_line( self.view.plot_x(), gy, @@ -362,11 +449,11 @@ impl IndicatorWidget { if cx < px || cx > px + pw { continue; } - let (up, down, wicks) = match candle_style(&self.state.name) { - CandleStyle::Muted => (0x96be96, 0xd28282, true), - CandleStyle::Grey => (0xb4b4b4, 0xb4b4b4, false), - CandleStyle::Standard => (0x009600, 0xc81e1e, true), - }; + let (up, down, wicks) = ( + self.candle_up_color.to_pixel(), + self.candle_down_color.to_pixel(), + self.candle_wicks, + ); let col = if bar.close >= bar.open { up } else { down }; target.set_foreground(col); let y_hi = self.view.y_at(bar.high, lo, hi); @@ -389,8 +476,7 @@ impl IndicatorWidget { Some(j) if j < lines.len() => vec![lines[j].clone()], _ => lines.clone(), }; - for rect in - xframe_indicators::plot::bar_rects(ind.def(), &shown, &area, &self.bars) + for rect in ind.bar_rects(&shown, &area, &self.bars) { let (r, g, bl) = ( (rect.color[0] * 255.0) as u32, @@ -641,9 +727,18 @@ mod indicator_tests { assert_eq!(demo_bar_count("IndAutocorrelation"), 250); assert!(ZOOMED_ONCE.binary_search(&"IndBollingerBands").is_ok()); assert!(ZOOMED_ONCE.binary_search(&"IndSMA").is_err()); - assert_eq!(candle_style("IndSMA"), CandleStyle::Muted); - assert_eq!(candle_style("IndBollingerBands"), CandleStyle::Standard); - assert_eq!(candle_style("IndGannHiLoActivator"), CandleStyle::Grey); + assert_eq!( + candle_colors("IndSMA"), + (Srgb::new(150, 190, 150), Srgb::new(210, 130, 130), true) + ); + assert_eq!( + candle_colors("IndBollingerBands"), + (Srgb::new(0, 150, 0), Srgb::new(200, 30, 30), true) + ); + assert_eq!( + candle_colors("IndGannHiLoActivator"), + (Srgb::new(180, 180, 180), Srgb::new(180, 180, 180), false) + ); assert!(NO_DEF_CANDLES.binary_search(&"IndAutocorrelation").is_err()); assert!(NO_DEF_CANDLES.binary_search(&"IndChandeKrollStop").is_ok()); assert!(NO_DEF_CANDLES.binary_search(&"IndVolatilityStop").is_ok()); diff --git a/crates/xframe-widgets/src/spreadsheet.rs b/crates/xframe-widgets/src/spreadsheet.rs index ff44025..d7950a0 100644 --- a/crates/xframe-widgets/src/spreadsheet.rs +++ b/crates/xframe-widgets/src/spreadsheet.rs @@ -12,7 +12,7 @@ use std::collections::HashSet; use xframe_core::draw::draw_text_at; -use xframe_core::{DrawTarget, StyleManager}; +use xframe_core::{DrawTarget, Srgb, StyleManager}; use crate::state::{button, Key, Rect, WidgetState}; use crate::widget::{resolve_style, Archetype, Widget}; @@ -73,6 +73,12 @@ pub struct XSpreadsheet { pub clipboard: String, pub on_cell_change: Option>, pub on_select_cell: Option>, + /// Grid colours (were hardcoded literals in `draw`). + pub background_color: Srgb, + pub grid_color: Srgb, + pub header_fill_color: Srgb, + pub active_fill_color: Srgb, + pub selection_fill_color: Srgb, } impl XSpreadsheet { @@ -114,6 +120,11 @@ impl XSpreadsheet { clipboard: String::new(), on_cell_change: None, on_select_cell: None, + background_color: Srgb::new(255, 255, 255), + grid_color: Srgb::new(205, 205, 205), + header_fill_color: Srgb::new(232, 232, 232), + active_fill_color: Srgb::new(198, 220, 245), + selection_fill_color: Srgb::new(220, 235, 250), }; // Demo data (C++ constructor). ss.set_cell_text(0, 0, "Part"); @@ -177,6 +188,53 @@ impl XSpreadsheet { self.frozen_cols = n.max(0); } + /// Grid colours (defaults are the values `draw` hardcoded before). + pub fn get_background_color(&self) -> Srgb { + self.background_color + } + pub fn set_background_color(&mut self, c: Srgb) { + self.background_color = c; + } + pub fn get_grid_color(&self) -> Srgb { + self.grid_color + } + pub fn set_grid_color(&mut self, c: Srgb) { + self.grid_color = c; + } + pub fn get_header_fill_color(&self) -> Srgb { + self.header_fill_color + } + pub fn set_header_fill_color(&mut self, c: Srgb) { + self.header_fill_color = c; + } + pub fn get_active_fill_color(&self) -> Srgb { + self.active_fill_color + } + pub fn set_active_fill_color(&mut self, c: Srgb) { + self.active_fill_color = c; + } + pub fn get_selection_fill_color(&self) -> Srgb { + self.selection_fill_color + } + pub fn set_selection_fill_color(&mut self, c: Srgb) { + self.selection_fill_color = c; + } + pub fn get_row_height(&self) -> i32 { + self.row_height + } + pub fn set_row_height(&mut self, h: i32) { + self.row_height = h.max(1); + } + pub fn get_frozen_rows(&self) -> i32 { + self.frozen_rows + } + pub fn get_frozen_cols(&self) -> i32 { + self.frozen_cols + } + pub fn get_active_cell(&self) -> (i32, i32) { + (self.active_col, self.active_row) + } + /// C++ `col_name`: 0 -> "A", 25 -> "Z", 26 -> "AA". pub fn col_name(col: i32) -> String { let mut s = String::new(); @@ -834,12 +892,7 @@ impl Widget for XSpreadsheet { } let style = resolve_style(styles, &self.state.style_name); let b = self.state.bounds; - let white = 0xffffffu32; - let grid = 0xcdcdcdu32; - let header_fill = 0xe8e8e8u32; - let active_fill = 0xc6dcf5u32; - let sel_fill = 0xdcebfa_u32; - target.set_foreground(white); + target.set_foreground(self.background_color.to_pixel()); target.fill_rect(b.x, b.y, b.w, b.h); let (fc0, lc0) = self.visible_cols(); let (fr0, lr0) = self.visible_rows(); @@ -854,10 +907,10 @@ impl Widget for XSpreadsheet { let cy = self.row_top(r); let cw = self.col_widths[c as usize]; if in_sel(c, r) && !(c == self.active_col && r == self.active_row) { - target.set_foreground(sel_fill); + target.set_foreground(self.selection_fill_color.to_pixel()); target.fill_rect(cx, cy, cw, self.row_height); } - target.set_foreground(grid); + target.set_foreground(self.grid_color.to_pixel()); target.draw_rect(cx, cy, cw, self.row_height); let disp = self.get_cell_display(c, r); if !disp.is_empty() && !(self.editing && c == self.active_col && r == self.active_row) @@ -889,16 +942,16 @@ impl Widget for XSpreadsheet { } } // Column headers. - target.set_foreground(header_fill); + target.set_foreground(self.header_fill_color.to_pixel()); target.fill_rect(b.x, b.y, b.w, self.header_h); let draw_col_header = |target: &mut dyn DrawTarget, c: i32| { let cx = self.col_left(c); let cw = self.col_widths[c as usize]; if c >= self.active_col.min(self.sel_col) && c <= self.active_col.max(self.sel_col) { - target.set_foreground(active_fill); + target.set_foreground(self.active_fill_color.to_pixel()); target.fill_rect(cx, b.y, cw, self.header_h); } - target.set_foreground(grid); + target.set_foreground(self.grid_color.to_pixel()); target.draw_rect(cx, b.y, cw, self.header_h); let nm = Self::col_name(c); let tw = style.text_width(&nm); @@ -911,15 +964,15 @@ impl Widget for XSpreadsheet { draw_col_header(target, c); } // Row headers. - target.set_foreground(header_fill); + target.set_foreground(self.header_fill_color.to_pixel()); target.fill_rect(b.x, b.y + self.header_h, self.header_w, b.h - self.header_h); let draw_row_header = |target: &mut dyn DrawTarget, r: i32| { let cy = self.row_top(r); if r >= self.active_row.min(self.sel_row) && r <= self.active_row.max(self.sel_row) { - target.set_foreground(active_fill); + target.set_foreground(self.active_fill_color.to_pixel()); target.fill_rect(b.x, cy, self.header_w, self.row_height); } - target.set_foreground(grid); + target.set_foreground(self.grid_color.to_pixel()); target.draw_rect(b.x, cy, self.header_w, self.row_height); let nm = format!("{}", r + 1); let tw = style.text_width(&nm); @@ -938,9 +991,9 @@ impl Widget for XSpreadsheet { draw_row_header(target, r); } // Corner box. - target.set_foreground(header_fill); + target.set_foreground(self.header_fill_color.to_pixel()); target.fill_rect(b.x, b.y, self.header_w, self.header_h); - target.set_foreground(grid); + target.set_foreground(self.grid_color.to_pixel()); target.draw_rect(b.x, b.y, self.header_w, self.header_h); // Frozen separator. if self.frozen_rows > 0 { @@ -960,7 +1013,7 @@ impl Widget for XSpreadsheet { target.draw_rect(cx, cy, cw, self.row_height); target.set_line_width(1); if self.editing { - target.set_foreground(white); + target.set_foreground(self.background_color.to_pixel()); target.fill_rect(cx + 1, cy + 1, cw - 2, self.row_height - 2); draw_text_at( target, diff --git a/crates/xframe-widgets/tests/getter_setter_tests.rs b/crates/xframe-widgets/tests/getter_setter_tests.rs new file mode 100644 index 0000000..8fb8013 --- /dev/null +++ b/crates/xframe-widgets/tests/getter_setter_tests.rs @@ -0,0 +1,242 @@ +//! Getter/setter pass: defaults match the pre-pass hardcoded values, +//! setters round-trip, and a changed colour changes the rendering +//! (while the defaults render exactly as before — see the pixel-hash +//! harness used during the pass). + +use xframe_core::{Canvas, Srgb, StyleManager}; +use xframe_widgets::indicators::IndicatorWidget as IndInner; +use xframe_widgets::{ + ButtonWidget, ChartWidget, ContainerWidget, DialogWidget, DisplayWidget, EntryWidget, + InstrumentWidget, ListSelWidget, RangeWidget, ToggleWidget, Widget, XGraph, XSpreadsheet, +}; + +fn hash_canvas(target: &Canvas) -> u64 { + let mut h: u64 = 0xcbf29ce484222325; + for y in 0..300 { + for x in 0..400 { + h ^= target.get_pixel(x, y) as u64; + h = h.wrapping_mul(0x100000001b3); + } + } + h +} + +fn draw_hash(w: &dyn Widget) -> u64 { + let (mut target, mut styles) = (Canvas::new(400, 300), StyleManager::new()); + w.draw(&mut target, &mut styles); + hash_canvas(&target) +} + +#[test] +fn button_disabled_text_and_on() { + let mut b = ButtonWidget::new("GtkButton", "Button", 0, 0, 200, 90); + assert_eq!(b.get_disabled_text_color(), Srgb::new(140, 140, 140)); + assert!(!b.get_on()); + b.set_disabled_text_color(Srgb::new(255, 0, 0)); + assert_eq!(b.get_disabled_text_color(), Srgb::new(255, 0, 0)); + b.set_on(true); + assert!(b.get_on()); + // A disabled button draws without panicking with a custom colour. + // (No render-difference assertion here: the headless Canvas style + // has no font metrics, so draw_text_centered is a no-op in tests; + // the colour is honoured on a live target with fonts.) + let mut b1 = ButtonWidget::new("GtkButton", "Button", 0, 0, 200, 90); + b1.set_text("Hi"); + b1.set_enabled(false); + b1.set_disabled_text_color(Srgb::new(255, 0, 0)); + let _ = draw_hash(&b1); + // Toggle shares the helper. + let mut t = ToggleWidget::new("GtkToggleButton", "Toggle", 0, 0, 200, 90); + assert_eq!(t.get_disabled_text_color(), Srgb::new(140, 140, 140)); + t.set_disabled_text_color(Srgb::new(0, 255, 0)); + assert_eq!(t.get_disabled_text_color(), Srgb::new(0, 255, 0)); +} + +#[test] +fn entry_range_listsel_state() { + let mut e = EntryWidget::new("GtkEntry", "Entry", 0, 0, 200, 90); + assert!(e.get_editable() && !e.get_password() && !e.get_multiline()); + e.set_password(true); + e.set_multiline(true); + e.set_editable(false); + assert!(!e.get_editable() && e.get_password() && e.get_multiline()); + let mut r = RangeWidget::new("GtkScale", "Scale", 0, 0, 200, 90); + assert_eq!(r.get_orientation(), "horizontal"); + assert!(r.get_show_value()); + assert_eq!(r.get_digits(), 0); + r.set_orientation("vertical"); + r.set_show_value(false); + r.set_digits(2); + assert_eq!(r.get_orientation(), "vertical"); + assert!(!r.get_show_value() && r.get_digits() == 2); + assert_eq!(r.step(), 1.0); + r.set_step(0.5); + assert_eq!(r.step(), 0.5); + let mut l = ListSelWidget::new("GtkListBox", "List", 0, 0, 200, 90); + assert_eq!(l.get_selected_fill(), Srgb::new(198, 220, 245)); + assert_eq!(l.get_row_height(), 20); + assert!(!l.get_multi_select()); + l.set_selected_fill(Srgb::new(1, 2, 3)); + l.set_row_height(30); + l.set_multi_select(true); + assert_eq!(l.get_selected_fill(), Srgb::new(1, 2, 3)); + assert_eq!(l.get_row_height(), 30); + assert!(l.get_multi_select()); +} + +#[test] +fn container_dialog_display_chart() { + let mut c = ContainerWidget::new("GtkBox", "Box", 0, 0, 200, 90); + assert_eq!(c.active_page(), 0); + c.set_active_page(2); + assert_eq!(c.active_page(), 2); + let mut d = DialogWidget::new("GtkDialog", "Dialog", 0, 0, 200, 90); + assert!(!d.get_modal() && d.get_result() == 0); + d.set_modal(true); + d.set_result(42); + assert!(d.get_modal() && d.get_result() == 42); + let mut p = DisplayWidget::new("GtkProgressBar", "Progress", 0, 0, 200, 90); + assert_eq!(p.get_progress(), 0.0); + p.set_progress(0.5); + assert_eq!(p.get_progress(), 0.5); + let ch = ChartWidget::new("StockParallelChannel", "ParallelChannel", 0, 0, 200, 90); + assert_eq!(ch.get_overlay(), Some("ParallelChannel")); + let plain = ChartWidget::new("StockLineChart", "LineChart", 0, 0, 200, 90); + assert_eq!(plain.get_overlay(), None); +} + +#[test] +fn instrument_defaults_follow_cpp() { + // Tank palette (C++ InstHorizontalTankLevel). + let t = InstrumentWidget::new("InstHorizontalTankLevel", "HorizontalTankLevel", 0, 0, 200, 90); + assert_eq!(t.get_white_color(), Srgb::new(255, 255, 255)); + assert_eq!(t.get_ink_color(), Srgb::new(60, 60, 60)); + assert_eq!(t.get_pale_color(), Srgb::new(235, 242, 248)); + assert_eq!(t.get_fill_color(), Srgb::new(40, 120, 200)); + assert_eq!(t.get_grid_color(), Srgb::new(210, 210, 210)); + assert_eq!(t.get_amber_color(), Srgb::new(220, 150, 0)); + assert_eq!(t.get_red_color(), Srgb::new(200, 30, 30)); + assert_eq!(t.get_hover_color(), Srgb::new(255, 170, 0)); + // Dial gauge (C++ InstAnalogGauge). + let g = InstrumentWidget::new("InstAnalogGauge", "AnalogGauge", 0, 0, 200, 90); + assert_eq!(g.get_face_color(), Srgb::new(245, 245, 240)); + assert_eq!(g.get_ink_color(), Srgb::new(40, 40, 40)); + assert_eq!(g.get_needle_line_width(), 3); + // Needle faces differ per kind. + let vu = InstrumentWidget::new("InstNeedleVu", "NeedleVu", 0, 0, 200, 90); + assert_eq!(vu.get_face_color(), Srgb::new(248, 238, 205)); + assert_eq!(vu.get_panel_color(), Srgb::new(226, 226, 226)); + let eg = InstrumentWidget::new("InstNeedleEdgewise", "NeedleEdgewise", 0, 0, 200, 90); + assert_eq!(eg.get_face_color(), Srgb::new(250, 250, 248)); + assert_eq!(eg.get_panel_color(), Srgb::new(232, 232, 232)); + // Segment displays (C++ InstLedDigits1 / InstSeg14Display1). + let d1 = InstrumentWidget::new("InstLedDigits1", "LedDigits1", 0, 0, 200, 90); + assert_eq!(d1.get_seg7_lit_color(), Srgb::new(255, 60, 35)); + assert_eq!(d1.get_seg7_dim_color(), Srgb::new(52, 14, 11)); + assert_eq!(d1.get_seg_bg_color(), Srgb::new(12, 12, 14)); + let s14 = InstrumentWidget::new("InstSeg14Display1", "Seg14Display1", 0, 0, 200, 90); + assert_eq!(s14.get_seg14_lit_color(), Srgb::new(255, 176, 0)); + assert_eq!(s14.get_seg14_dim_color(), Srgb::new(60, 42, 8)); + // LED / lamp per-kind colours + on state. + let led = InstrumentWidget::new("InstLedRed", "LedRed", 0, 0, 200, 90); + assert_eq!(led.get_led_color(), Srgb::new(220, 30, 30)); + assert!(led.get_on()); + let lamp = InstrumentWidget::new("InstPanelLampWhite", "PanelLampWhite", 0, 0, 200, 90); + assert_eq!(lamp.get_lamp_color(), Srgb::new(235, 235, 235)); + // Stack light tiers + RGB channels. + let st = InstrumentWidget::new("InstStackLight", "StackLight", 0, 0, 200, 90); + assert!(st.get_stack_red_on() && !st.get_stack_amber_on() && st.get_stack_green_on()); + let rgb = InstrumentWidget::new("InstRgbStatusLed", "RgbStatusLed", 0, 0, 200, 90); + assert!(rgb.get_red_on() && rgb.get_green_on() && !rgb.get_blue_on()); + assert_eq!(rgb.get_blue_channel_color(), Srgb::new(40, 120, 230)); + // Bar meter marker width (C++ InstBarMeter::marker_line_width_, 3). + let bm = InstrumentWidget::new("InstBarMeter", "BarMeter", 0, 0, 200, 90); + assert_eq!(bm.get_marker_line_width(), 3); + assert_eq!(bm.get_green_color(), Srgb::new(0, 150, 0)); +} + +#[test] +fn instrument_setter_changes_rendering() { + let t0 = InstrumentWidget::new("InstHorizontalTankLevel", "HorizontalTankLevel", 0, 0, 200, 90); + let h0 = draw_hash(&t0); + let mut t1 = InstrumentWidget::new("InstHorizontalTankLevel", "HorizontalTankLevel", 0, 0, 200, 90); + t1.set_fill_color(Srgb::new(255, 0, 0)); + assert_ne!(draw_hash(&t1), h0); + let mut t2 = InstrumentWidget::new("InstHorizontalTankLevel", "HorizontalTankLevel", 0, 0, 200, 90); + t2.set_fill_color(Srgb::new(40, 120, 200)); + assert_eq!(draw_hash(&t2), h0); + // Turning the LED off dims it. + let l0 = InstrumentWidget::new("InstLedRed", "LedRed", 0, 0, 200, 90); + let lh0 = draw_hash(&l0); + let mut l1 = InstrumentWidget::new("InstLedRed", "LedRed", 0, 0, 200, 90); + l1.set_on(false); + assert_ne!(draw_hash(&l1), lh0); +} + +#[test] +fn graph_and_spreadsheet_colours() { + let mut g = XGraph::new("XGraph", 0, 0, 200, 90); + assert_eq!(g.get_grid_color(), Srgb::new(225, 225, 225)); + g.set_grid_color(Srgb::new(1, 2, 3)); + assert_eq!(g.get_grid_color(), Srgb::new(1, 2, 3)); + g.add_line_series("s", Srgb::new(10, 20, 30), vec![1.0, 2.0, 3.0]); + assert_eq!(g.get_series_color(0), Some(Srgb::new(10, 20, 30))); + g.set_series_color(0, Srgb::new(200, 0, 0)); + assert_eq!(g.get_series_color(0), Some(Srgb::new(200, 0, 0))); + assert_eq!(g.get_series_color(9), None); + let mut ss = XSpreadsheet::new("XSpreadsheet", 0, 0, 200, 90); + assert_eq!(ss.get_background_color(), Srgb::new(255, 255, 255)); + assert_eq!(ss.get_grid_color(), Srgb::new(205, 205, 205)); + assert_eq!(ss.get_header_fill_color(), Srgb::new(232, 232, 232)); + assert_eq!(ss.get_active_fill_color(), Srgb::new(198, 220, 245)); + assert_eq!(ss.get_selection_fill_color(), Srgb::new(220, 235, 250)); + assert_eq!(ss.get_row_height(), 20); + assert_eq!(ss.get_frozen_rows(), 0); + assert_eq!(ss.get_frozen_cols(), 1); + assert_eq!(ss.get_active_cell(), (0, 0)); + ss.set_background_color(Srgb::new(1, 1, 1)); + ss.set_row_height(25); + assert_eq!(ss.get_background_color(), Srgb::new(1, 1, 1)); + assert_eq!(ss.get_row_height(), 25); +} + +#[test] +fn indicator_widget_colours_and_view() { + let mut w = IndInner::new("IndSMA", "SMA", 0, 0, 200, 90); + assert_eq!(w.get_background_color(), Srgb::new(255, 255, 255)); + assert_eq!(w.get_grid_color(), Srgb::new(225, 225, 225)); + // IndSMA is a Muted-candle widget. + assert_eq!(w.get_candle_up_color(), Srgb::new(150, 190, 150)); + assert_eq!(w.get_candle_down_color(), Srgb::new(210, 130, 130)); + assert!(w.get_candle_wicks()); + assert_eq!(w.get_line_index(), None); + // Line colour: SMA def colour, overridable. + let def_color = w.get_line_color().unwrap(); + w.set_line_color(3); + assert_eq!(w.get_line_color(), Some(3)); + w.set_line_color(def_color); + assert_eq!(w.get_line_color(), Some(def_color)); + // Params: SMA(20) default period 20. + assert_eq!(w.get_param(0), Some(20.0)); + w.set_param(0, 50.0); + assert_eq!(w.get_param(0), Some(50.0)); + assert_eq!(w.get_param(99), None); + // View accessors. + w.set_y_range(0.0, 10.0); + assert!(!w.view.auto_y); + w.set_auto_y(true); + assert!(w.view.auto_y); + assert_eq!(w.view_first(), 0.0); + w.reset_view(); + // Setter changes rendering: red candles. + let draw_inner = |w: &IndInner| { + let (mut target, mut styles) = (Canvas::new(400, 300), StyleManager::new()); + w.draw(&mut target, &mut styles); + hash_canvas(&target) + }; + let h0 = draw_inner(&w); + let mut w2 = IndInner::new("IndSMA", "SMA", 0, 0, 200, 90); + w2.set_candle_up_color(Srgb::new(255, 0, 0)); + w2.set_candle_down_color(Srgb::new(255, 0, 0)); + assert_ne!(draw_inner(&w2), h0); +}