use super::*;
#[must_use]
fn tb_icon_pt(ox: f32, oy: f32, tex_u: f32, pos_x: f32, pos_y: f32) -> Point {
Point::new(ox + pos_x * tex_u, oy + pos_y * tex_u)
}
fn tb_icon_line(display_list: &mut DisplayList, from: Point, to: Point, color: Color, width: f32) {
display_list.push(DrawCommand::Line {
from,
to,
color,
width,
});
}
fn tb_icon_bar(display_list: &mut DisplayList, rect: Rect, radius: f32, color: Color) {
display_list.push(DrawCommand::FillRoundedRect {
rect,
color,
radius: CornerRadius::all(radius),
border: Border::default(),
});
}
#[must_use]
pub(crate) fn resolve_chart_header_meta(
option: &EuvChartOption,
) -> (String, String, TextAlign, bool) {
let Some(title) = option.title_opt() else {
return (String::new(), String::new(), TextAlign::Left, false);
};
let title_text: String = title.get_text().clone();
let subtext: String = title.get_subtext().clone();
let align: TextAlign = match title.get_align() {
EuvChartAlign::Left => TextAlign::Left,
EuvChartAlign::Center => TextAlign::Center,
EuvChartAlign::Right => TextAlign::Right,
};
let has_any: bool = !title_text.is_empty() || !subtext.is_empty();
(title_text, subtext, align, has_any)
}
pub fn draw_chart_toolbox_icon(
icon: ChartToolboxIcon,
bounds: Rect,
display_list: &mut DisplayList,
color: Color,
) {
let width_val: f32 = bounds.get_width().max(8.0);
let height_val: f32 = bounds.get_height().max(8.0);
let side: f32 = width_val.min(height_val);
let ox: f32 = bounds.get_x() + (width_val - side) * 0.5;
let oy: f32 = bounds.get_y() + (height_val - side) * 0.5;
let tex_u: f32 = side / 24.0;
let stroke_width: f32 = (side * 0.095).clamp(1.3, 2.0);
let arm_r: f32 = (stroke_width * 0.45).max(0.5);
match icon {
ChartToolboxIcon::Restore => {
let ring_rect: Rect = Rect::new(
ox + 5.0 * tex_u,
oy + 5.0 * tex_u,
14.0 * tex_u,
14.0 * tex_u,
);
let split_x: f32 = ox + 15.6 * tex_u;
let split_y: f32 = oy + 10.2 * tex_u;
let bottom_clip: Rect = Rect::new(
ox + 3.6 * tex_u,
split_y,
16.8 * tex_u,
(oy + 20.4 * tex_u - split_y).max(1.0),
);
let top_left_clip: Rect = Rect::new(
ox + 3.6 * tex_u,
oy + 3.6 * tex_u,
(split_x - (ox + 3.6 * tex_u)).max(1.0),
(split_y - (oy + 3.6 * tex_u)).max(1.0),
);
display_list.push(DrawCommand::PushClip(bottom_clip));
display_list.push(DrawCommand::FillRoundedRect {
rect: ring_rect,
color: COLOR_TRANSPARENT,
radius: CornerRadius::all(RADIUS_PILL),
border: Border::new(stroke_width, color),
});
display_list.push(DrawCommand::PopClip);
display_list.push(DrawCommand::PushClip(top_left_clip));
display_list.push(DrawCommand::FillRoundedRect {
rect: ring_rect,
color: COLOR_TRANSPARENT,
radius: CornerRadius::all(RADIUS_PILL),
border: Border::new(stroke_width, color),
});
display_list.push(DrawCommand::PopClip);
display_list.push(DrawCommand::Triangle {
p0: tb_icon_pt(ox, oy, tex_u, 19.2, 8.8),
p1: tb_icon_pt(ox, oy, tex_u, 13.8, 8.6),
p2: tb_icon_pt(ox, oy, tex_u, 18.2, 3.8),
color,
});
}
ChartToolboxIcon::MagicType => {
let bar_r: f32 = (1.0 * tex_u).max(0.75);
let bars: [(f32, f32, f32); 3] =
[(4.2, 13.8, 6.0), (10.3, 10.8, 9.0), (16.4, 12.4, 7.4)];
for (bx, by, bh) in bars {
tb_icon_bar(
display_list,
Rect::new(ox + bx * tex_u, oy + by * tex_u, 3.4 * tex_u, bh * tex_u),
bar_r,
color,
);
}
tb_icon_line(
display_list,
tb_icon_pt(ox, oy, tex_u, 4.8, 9.8),
tb_icon_pt(ox, oy, tex_u, 12.0, 5.4),
color,
stroke_width,
);
tb_icon_line(
display_list,
tb_icon_pt(ox, oy, tex_u, 12.0, 5.4),
tb_icon_pt(ox, oy, tex_u, 19.2, 4.4),
color,
stroke_width,
);
}
ChartToolboxIcon::DataView => {
let frame_x: f32 = ox + 3.8 * tex_u;
let frame_y: f32 = oy + 4.2 * tex_u;
let frame_w: f32 = 16.4 * tex_u;
let frame_h: f32 = 15.6 * tex_u;
display_list.push(DrawCommand::FillRoundedRect {
rect: Rect::new(frame_x, frame_y, frame_w, frame_h),
color: COLOR_TRANSPARENT,
radius: CornerRadius::all((2.2 * tex_u).max(1.2)),
border: Border::new(stroke_width, color),
});
let inner_x: f32 = frame_x + stroke_width * 0.5;
let inner_w: f32 = (frame_w - stroke_width).max(1.0);
let row1_y: f32 = oy + 9.2 * tex_u - stroke_width * 0.5;
let row2_y: f32 = oy + 14.4 * tex_u - stroke_width * 0.5;
tb_icon_bar(
display_list,
Rect::new(inner_x, row1_y, inner_w, stroke_width),
0.0,
color,
);
tb_icon_bar(
display_list,
Rect::new(inner_x, row2_y, inner_w, stroke_width),
0.0,
color,
);
let col_x: f32 = ox + 10.2 * tex_u - stroke_width * 0.5;
let col_y: f32 = row1_y + stroke_width;
let col_h: f32 = (frame_y + frame_h - stroke_width * 0.5 - col_y).max(1.0);
tb_icon_bar(
display_list,
Rect::new(col_x, col_y, stroke_width, col_h),
0.0,
color,
);
}
ChartToolboxIcon::Fullscreen => {
let tr_x1: f32 = ox + 19.8 * tex_u;
let tr_y0: f32 = oy + 4.2 * tex_u;
let arm_len: f32 = 5.8 * tex_u;
tb_icon_bar(
display_list,
Rect::new(tr_x1 - arm_len, tr_y0, arm_len, stroke_width),
arm_r,
color,
);
tb_icon_bar(
display_list,
Rect::new(
tr_x1 - stroke_width,
tr_y0 + stroke_width * 0.5,
stroke_width,
(arm_len - stroke_width * 0.5).max(1.0),
),
arm_r,
color,
);
tb_icon_line(
display_list,
tb_icon_pt(ox, oy, tex_u, 13.8, 10.2),
tb_icon_pt(ox, oy, tex_u, 18.5, 5.5),
color,
stroke_width,
);
let bl_x0: f32 = ox + 4.2 * tex_u;
let bl_y1: f32 = oy + 19.8 * tex_u;
tb_icon_bar(
display_list,
Rect::new(bl_x0, bl_y1 - stroke_width, arm_len, stroke_width),
arm_r,
color,
);
tb_icon_bar(
display_list,
Rect::new(
bl_x0,
bl_y1 - arm_len,
stroke_width,
(arm_len - stroke_width * 0.5).max(1.0),
),
arm_r,
color,
);
tb_icon_line(
display_list,
tb_icon_pt(ox, oy, tex_u, 10.2, 13.8),
tb_icon_pt(ox, oy, tex_u, 5.5, 18.5),
color,
stroke_width,
);
}
ChartToolboxIcon::ExitFullscreen => {
let arm_len: f32 = 5.6 * tex_u;
let tr_cx: f32 = ox + 14.0 * tex_u;
let tr_cy: f32 = oy + 10.0 * tex_u;
tb_icon_bar(
display_list,
Rect::new(tr_cx, tr_cy - arm_len, stroke_width, arm_len),
arm_r,
color,
);
tb_icon_bar(
display_list,
Rect::new(
tr_cx + stroke_width * 0.5,
tr_cy - stroke_width,
(arm_len - stroke_width * 0.5).max(1.0),
stroke_width,
),
arm_r,
color,
);
tb_icon_line(
display_list,
tb_icon_pt(ox, oy, tex_u, 19.5, 4.5),
tb_icon_pt(ox, oy, tex_u, 15.2, 8.8),
color,
stroke_width,
);
let bl_cx: f32 = ox + 10.0 * tex_u;
let bl_cy: f32 = oy + 14.0 * tex_u;
tb_icon_bar(
display_list,
Rect::new(bl_cx - arm_len, bl_cy, arm_len, stroke_width),
arm_r,
color,
);
tb_icon_bar(
display_list,
Rect::new(
bl_cx - stroke_width,
bl_cy + stroke_width * 0.5,
stroke_width,
(arm_len - stroke_width * 0.5).max(1.0),
),
arm_r,
color,
);
tb_icon_line(
display_list,
tb_icon_pt(ox, oy, tex_u, 4.5, 19.5),
tb_icon_pt(ox, oy, tex_u, 8.8, 15.2),
color,
stroke_width,
);
}
}
}
pub(crate) fn build_toolbox_icon_button<CallbackFn>(
tokens: ThemeTokens,
icon: ChartToolboxIcon,
a11y_label: &'static str,
on_click: CallbackFn,
) -> NodeId
where
CallbackFn: FnMut(&UiEvent) + 'static,
{
let init_tok: ThemeTokens = live_tokens_untracked(tokens);
let palette: ThemePalette = init_tok.get_palette();
let geometry: ThemeGeometry = init_tok.get_geometry();
let btn_sz: f32 = geometry.get_control_sm();
let paint_icon = move |bounds: Rect, display_list: &mut DisplayList| {
let live_p: ThemePalette = live_tokens_untracked(tokens).get_palette();
let eff_bg: Color = blend_over(live_p.get_muted().multiply_alpha(0.55), live_p.get_card());
let fg: Color = live_p.readable_foreground_with(live_p.get_muted_foreground(), eff_bg);
draw_chart_toolbox_icon(icon, bounds, display_list, fg);
};
let mut icon_canvas: NodeId = NodeId::default();
let row_style: Style = style! {
Width(Length::Px(btn_sz))
Height(Length::Px(btn_sz))
FlexShrink(0.0)
JustifyContent::Center
AlignItems::Center
Radius(geometry.get_radius_sm())
Overflow::Hidden
Background(palette.get_muted().multiply_alpha(0.55))
Border::new(1.0, palette.get_border())
CursorIcon::Pointer
};
let canvas_style: Style = style! {
Width(Length::Px(16.0))
Height(Length::Px(16.0))
FlexShrink(0.0)
};
let node_id: NodeId = view! {
row {
style: row_style
role: A11yRole::Button
a11y_label: a11y_label.to_string()
on_click: on_click
canvas {
node_ref: icon_canvas
caching: CanvasCaching::RetainedStatic
style: canvas_style
painter: paint_icon
}
}
};
let ctx: Context = current_context();
let scene: SceneHandle = ctx.get_scene().clone();
let prev_key: Cell<ThemeKey> = Cell::new(init_tok.key());
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let live_tok: ThemeTokens = use_live_tokens(tokens);
let next_key: ThemeKey = live_tok.key();
if prev_key.replace(next_key) == next_key {
return;
}
let live_p: ThemePalette = live_tok.get_palette();
scene.with_write(
|tree: &mut SceneTree| {
if let Some(item_count) = tree.get_node_mut(node_id) {
item_count
.get_mut_style()
.get_mut_paint()
.set_background(Some(live_p.get_muted().multiply_alpha(0.55)))
.set_border(Border::new(1.0, live_p.get_border()));
tree.mark_dirty(node_id, DIRTY_PAINT);
}
tree.mark_dirty(icon_canvas, DIRTY_PAINT);
},
(),
);
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
tree.attach_scope(node_id, scope);
},
(),
);
node_id
}
#[must_use]
pub(crate) fn to_canvas_point(
scene: &SceneHandle,
node: NodeId,
screen_pt: Point,
last_bounds: &RefCell<Rect>,
) -> Option<Point> {
let sb: Rect = scene.node_screen_bounds(node)?;
let paint_b: Rect = last_bounds
.try_borrow()
.map(|rect_bounds: Ref<'_, Rect>| *rect_bounds)
.unwrap_or(sb);
Some(Point::new(
paint_b.get_x() + (screen_pt.get_x() - sb.get_x()),
paint_b.get_y() + (screen_pt.get_y() - sb.get_y()),
))
}
#[must_use]
pub(crate) fn rect_contains(rect: Rect, point_coords: Point) -> bool {
point_coords.get_x() >= rect.get_x()
&& point_coords.get_x() <= rect.get_x() + rect.get_width()
&& point_coords.get_y() >= rect.get_y()
&& point_coords.get_y() <= rect.get_y() + rect.get_height()
}
#[must_use]
pub(crate) fn compute_visible_chart_viewport(
bounds: Rect,
scroll_view: Option<(Point, Size)>,
) -> Rect {
let Some((off, viewport_rect)) = scroll_view else {
return bounds;
};
let vw: f32 = if viewport_rect.get_width() > 16.0 {
viewport_rect.get_width().min(bounds.get_width())
} else {
bounds.get_width()
};
let vh: f32 = if viewport_rect.get_height() > 16.0 {
viewport_rect.get_height().min(bounds.get_height())
} else {
bounds.get_height()
};
let max_x: f32 = (bounds.get_width() - vw).max(0.0);
let max_y: f32 = (bounds.get_height() - vh).max(0.0);
Rect::new(
bounds.get_x() + off.get_x().clamp(0.0, max_x),
bounds.get_y() + off.get_y().clamp(0.0, max_y),
vw,
vh,
)
}
#[must_use]
pub(crate) fn compute_data_view_col_widths(table: &EuvChartDataViewTable) -> (Vec<f32>, f32) {
let col_count: usize = table.get_headers().len().max(1);
let mut widths: Vec<f32> = Vec::with_capacity(col_count);
for ci in 0..col_count {
let mut max_chars: usize = table
.get_headers()
.get(ci)
.map(|text_value: &String| text_value.chars().count())
.unwrap_or(6);
for red_val in table.get_rows().iter().take(40) {
if let Some(cell) = red_val.get(ci) {
max_chars = max_chars.max(cell.chars().count());
}
}
let col_w: f32 = ((max_chars as f32) * 7.2 + 24.0)
.clamp(ECHART_DATA_VIEW_MIN_COL_W, ECHART_DATA_VIEW_MAX_COL_W);
widths.push(col_w);
}
let total_w: f32 = widths
.iter()
.copied()
.sum::<f32>()
.max(ECHART_DATA_VIEW_MIN_COL_W);
(widths, total_w)
}
#[must_use]
pub fn euv_chart_data_view_build_count() -> u64 {
ECHART_DATA_VIEW_BUILD_COUNT.with(Cell::get)
}
pub(crate) fn record_chart_data_view_build() {
ECHART_DATA_VIEW_BUILD_COUNT.with(|counter: &Cell<u64>| {
counter.set(counter.get().wrapping_add(1));
});
}
#[must_use]
pub fn euv_chart_lazy_materialize_count() -> u64 {
ECHART_LAZY_MATERIALIZE_COUNT.with(Cell::get)
}
pub(crate) fn record_chart_lazy_materialize() {
ECHART_LAZY_MATERIALIZE_COUNT.with(|counter: &Cell<u64>| {
counter.set(counter.get().wrapping_add(1));
});
}
#[must_use]
pub fn euv_chart_paint_build_count() -> u64 {
ECHART_PAINT_BUILD_COUNT.with(Cell::get)
}
pub(crate) fn record_chart_paint_build() {
ECHART_PAINT_BUILD_COUNT.with(|counter: &Cell<u64>| {
counter.set(counter.get().wrapping_add(1));
});
}
#[must_use]
pub(crate) fn is_chart_rect_in_viewport(bounds: Rect, viewport: Size) -> bool {
let vp_w: f32 = viewport.get_width();
let vp_h: f32 = viewport.get_height();
if vp_w <= 0.0 || vp_h <= 0.0 {
return true;
}
bounds.right() > 0.0 && bounds.get_x() < vp_w && bounds.bottom() > 0.0 && bounds.get_y() < vp_h
}
pub(crate) fn wire_chart_pointer_events(ctx: &mut Context, width_val: ChartPointerWiring) {
let canvas_node: NodeId = width_val.get_canvas_node();
let state_sig: Signal<EuvChartState> = width_val.get_state_sig();
let hover_pos_sig: Signal<Option<Point>> = width_val.get_hover_pos_sig();
let opt_sig: Option<Signal<EuvChartOption>> = *width_val.try_get_opt_sig();
if width_val.get_enable_drag() {
let scene_down: SceneHandle = ctx.get_scene().clone();
let res_down: Rc<RefCell<EuvChartRenderResult>> = Rc::clone(width_val.get_last_render());
let b_down: Rc<RefCell<Rect>> = Rc::clone(width_val.get_last_bounds());
let drag_down: Rc<Cell<(ChartDragTarget, f32, f32, f32)>> =
Rc::clone(width_val.get_drag_state());
let opt_down: Rc<LazyChartOption> = Rc::clone(width_val.get_static_opt());
let z_cb_down: Option<Rc<dyn Fn(f32, f32)>> = width_val.try_get_on_zoom_change().clone();
ctx.on_pointer_down(canvas_node, move |ui_event: &UiEvent| {
let UiEventKind::PointerDown(raw_pt) = ui_event.get_kind() else {
return;
};
let Some(sample_point) = to_canvas_point(&scene_down, canvas_node, *raw_pt, &b_down)
else {
return;
};
let cur_st: EuvChartState = state_sig.get_untracked();
let cur_opt: EuvChartOption = match opt_sig {
Some(sig) => sig.get_untracked(),
None => opt_down.resolve_clone(),
};
let min_span: f32 = cur_opt
.get_data_zoom()
.first()
.map(EuvChartDataZoom::get_min_span_pct)
.unwrap_or(ECHART_ZOOM_DEFAULT_MIN_SPAN);
let mut next_jump: Option<(f32, f32)> = None;
if let Ok(r_guard) = res_down.try_borrow() {
if let Some(sl) = r_guard.slider_rects_opt() {
let s_pct: f32 = cur_st.get_zoom_start_pct();
let e_pct: f32 = cur_st.get_zoom_end_pct();
let span: f32 = e_pct - s_pct;
let in_left: bool = rect_contains(sl.get_left_handle(), sample_point);
let in_right: bool = rect_contains(sl.get_right_handle(), sample_point);
if in_left && in_right {
let lc: f32 =
sl.get_left_handle().get_x() + sl.get_left_handle().get_width() * 0.5;
let rc: f32 =
sl.get_right_handle().get_x() + sl.get_right_handle().get_width() * 0.5;
let target: ChartDragTarget = if (sample_point.get_x() - lc).abs()
<= (sample_point.get_x() - rc).abs()
{
ChartDragTarget::SliderLeft
} else {
ChartDragTarget::SliderRight
};
drag_down.set((target, sample_point.get_x(), s_pct, e_pct));
return;
}
if in_left {
drag_down.set((
ChartDragTarget::SliderLeft,
sample_point.get_x(),
s_pct,
e_pct,
));
return;
}
if in_right {
drag_down.set((
ChartDragTarget::SliderRight,
sample_point.get_x(),
s_pct,
e_pct,
));
return;
}
let tw: f32 = sl.get_track_rect().get_width().max(1.0);
let click_pct: f32 =
(((sample_point.get_x() - sl.get_track_rect().get_x()) / tw) * 100.0)
.clamp(0.0, 100.0);
if rect_contains(sl.get_window_rect(), sample_point) {
if span >= 99.5 {
drag_down.set((ChartDragTarget::SliderBrush, click_pct, s_pct, e_pct));
} else {
drag_down.set((
ChartDragTarget::SliderWindow,
sample_point.get_x(),
s_pct,
e_pct,
));
}
return;
}
if rect_contains(sl.get_track_rect(), sample_point) {
if span >= 99.5 {
let (lo, hi): (f32, f32) = if click_pct < 50.0 {
(click_pct.min(100.0 - min_span), 100.0)
} else {
(0.0, click_pct.max(min_span))
};
next_jump = Some((lo, hi));
let target: ChartDragTarget = if click_pct < 50.0 {
ChartDragTarget::SliderLeft
} else {
ChartDragTarget::SliderRight
};
drag_down.set((target, sample_point.get_x(), lo, hi));
} else {
let lo: f32 = (click_pct - span * 0.5).clamp(0.0, 100.0 - span);
let hi: f32 = lo + span;
next_jump = Some((lo, hi));
drag_down.set((
ChartDragTarget::SliderWindow,
sample_point.get_x(),
lo,
hi,
));
}
}
}
if next_jump.is_none() {
let has_inside: bool = cur_opt
.get_data_zoom()
.iter()
.any(|dz: &EuvChartDataZoom| dz.get_kind() == EuvChartDataZoomKind::Inside);
let cur_span: f32 = cur_st.get_zoom_end_pct() - cur_st.get_zoom_start_pct();
if has_inside
&& cur_span < 99.9
&& rect_contains(r_guard.get_plot_rect(), sample_point)
{
drag_down.set((
ChartDragTarget::InsidePan,
sample_point.get_x(),
cur_st.get_zoom_start_pct(),
cur_st.get_zoom_end_pct(),
));
}
}
}
if let Some((lo, hi)) = next_jump {
let _: bool = {
if let Some(mut updated_value) = state_sig.try_get_untracked() {
{
let node_style: &mut EuvChartState = &mut updated_value;
node_style.set_zoom(lo, hi, min_span);
}
state_sig.set(updated_value)
} else {
false
}
};
scene_down.mark_dirty(canvas_node, DIRTY_PAINT);
if let Some(callback_fn) = z_cb_down.as_ref() {
callback_fn(lo, hi);
}
}
});
}
let scene_move: SceneHandle = ctx.get_scene().clone();
let res_move: Rc<RefCell<EuvChartRenderResult>> = Rc::clone(width_val.get_last_render());
let b_move: Rc<RefCell<Rect>> = Rc::clone(width_val.get_last_bounds());
let drag_move: Rc<Cell<(ChartDragTarget, f32, f32, f32)>> =
Rc::clone(width_val.get_drag_state());
let opt_move: Rc<LazyChartOption> = Rc::clone(width_val.get_static_opt());
let z_cb_move: Option<Rc<dyn Fn(f32, f32)>> = width_val.try_get_on_zoom_change().clone();
ctx.on_pointer_move(canvas_node, move |interaction_event: &UiEvent| {
let UiEventKind::PointerMove(raw_pt) = interaction_event.get_kind() else {
return;
};
let Some(target_point) = to_canvas_point(&scene_move, canvas_node, *raw_pt, &b_move) else {
return;
};
let (target, start_x, orig_s, orig_e): (ChartDragTarget, f32, f32, f32) = drag_move.get();
if target != ChartDragTarget::None
&& let Ok(r_guard) = res_move.try_borrow()
{
let cur_opt: EuvChartOption = match opt_sig {
Some(sig) => sig.get_untracked(),
None => opt_move.resolve_clone(),
};
let min_span: f32 = cur_opt
.get_data_zoom()
.first()
.map(EuvChartDataZoom::get_min_span_pct)
.unwrap_or(ECHART_ZOOM_DEFAULT_MIN_SPAN);
let mut next_range: Option<(f32, f32)> = None;
match target {
ChartDragTarget::SliderLeft => {
if let Some(sl) = r_guard.slider_rects_opt() {
let tw: f32 = sl.get_track_rect().get_width().max(1.0);
let frac: f32 = ((target_point.get_x() - sl.get_track_rect().get_x()) / tw)
.clamp(0.0, 1.0);
let lo: f32 = (frac * 100.0).min(orig_e - min_span);
next_range = Some((lo.max(0.0), orig_e));
}
}
ChartDragTarget::SliderRight => {
if let Some(sl) = r_guard.slider_rects_opt() {
let tw: f32 = sl.get_track_rect().get_width().max(1.0);
let frac: f32 = ((target_point.get_x() - sl.get_track_rect().get_x()) / tw)
.clamp(0.0, 1.0);
let hi: f32 = (frac * 100.0).max(orig_s + min_span);
next_range = Some((orig_s, hi.min(100.0)));
}
}
ChartDragTarget::SliderWindow => {
if let Some(sl) = r_guard.slider_rects_opt() {
let tw: f32 = sl.get_track_rect().get_width().max(1.0);
let delta_pct: f32 = ((target_point.get_x() - start_x) / tw) * 100.0;
let span: f32 = orig_e - orig_s;
let lo: f32 = (orig_s + delta_pct).clamp(0.0, 100.0 - span);
next_range = Some((lo, lo + span));
}
}
ChartDragTarget::SliderBrush => {
if let Some(sl) = r_guard.slider_rects_opt() {
let tw: f32 = sl.get_track_rect().get_width().max(1.0);
let cur_pct: f32 =
(((target_point.get_x() - sl.get_track_rect().get_x()) / tw) * 100.0)
.clamp(0.0, 100.0);
if (cur_pct - start_x).abs() >= 1.5 {
let raw_lo: f32 = start_x.min(cur_pct);
let raw_hi: f32 = start_x.max(cur_pct);
let span: f32 = (raw_hi - raw_lo).max(min_span);
let lo: f32 = if raw_lo + span > 100.0 {
(100.0 - span).max(0.0)
} else {
raw_lo
};
next_range = Some((lo, (lo + span).min(100.0)));
}
}
}
ChartDragTarget::InsidePan => {
let pw: f32 = r_guard.get_plot_rect().get_width().max(1.0);
let span: f32 = orig_e - orig_s;
let delta_pct: f32 = -((target_point.get_x() - start_x) / pw) * span;
let lo: f32 = (orig_s + delta_pct).clamp(0.0, 100.0 - span);
next_range = Some((lo, lo + span));
}
ChartDragTarget::None => {}
}
if let Some((lo, hi)) = next_range {
let _: bool = {
if let Some(mut updated_value) = state_sig.try_get_untracked() {
{
let active_style: &mut EuvChartState = &mut updated_value;
active_style.set_zoom(lo, hi, min_span);
}
state_sig.set(updated_value)
} else {
false
}
};
scene_move.mark_dirty(canvas_node, DIRTY_PAINT);
if let Some(handler_cb) = z_cb_move.as_ref() {
handler_cb(lo, hi);
}
return;
}
}
let prev_hover: Option<Point> = hover_pos_sig.get_untracked();
let moved_enough: bool = match prev_hover {
Some(prev) => {
(prev.get_x() - target_point.get_x()).abs() > 0.25
|| (prev.get_y() - target_point.get_y()).abs() > 0.25
}
None => true,
};
if moved_enough {
let _: bool = hover_pos_sig.set(Some(target_point));
scene_move.mark_dirty(canvas_node, DIRTY_PAINT);
}
});
let drag_up: Rc<Cell<(ChartDragTarget, f32, f32, f32)>> = Rc::clone(width_val.get_drag_state());
ctx.on_pointer_up(canvas_node, move |_: &UiEvent| {
drag_up.set((ChartDragTarget::None, 0.0, 0.0, 100.0));
});
let drag_cancel: Rc<Cell<(ChartDragTarget, f32, f32, f32)>> =
Rc::clone(width_val.get_drag_state());
let scene_cancel: SceneHandle = ctx.get_scene().clone();
ctx.on_pointer_cancel(canvas_node, move |_: &UiEvent| {
drag_cancel.set((ChartDragTarget::None, 0.0, 0.0, 100.0));
if hover_pos_sig.set_if_changed(None) {
scene_cancel.mark_dirty(canvas_node, DIRTY_PAINT);
}
});
let drag_leave: Rc<Cell<(ChartDragTarget, f32, f32, f32)>> =
Rc::clone(width_val.get_drag_state());
let scene_leave: SceneHandle = ctx.get_scene().clone();
ctx.on_pointer_leave(canvas_node, move |_: &UiEvent| {
if drag_leave.get().0 == ChartDragTarget::None && hover_pos_sig.set_if_changed(None) {
scene_leave.mark_dirty(canvas_node, DIRTY_PAINT);
}
});
let scene_click: SceneHandle = ctx.get_scene().clone();
let res_click: Rc<RefCell<EuvChartRenderResult>> = Rc::clone(width_val.get_last_render());
let b_click: Rc<RefCell<Rect>> = Rc::clone(width_val.get_last_bounds());
let opt_click: Rc<LazyChartOption> = Rc::clone(width_val.get_static_opt());
let click_cb: Option<EuvChartHitCallback> = width_val.try_get_on_point_click().clone();
let legend_cb: Option<EuvChartLegendCallback> = width_val.try_get_on_legend_change().clone();
ctx.on_click(canvas_node, move |event_payload: &UiEvent| {
let UiEventKind::Click(raw_pt) = event_payload.get_kind() else {
return;
};
let Some(vertex_point) = to_canvas_point(&scene_click, canvas_node, *raw_pt, &b_click)
else {
return;
};
let Ok(r_guard) = res_click.try_borrow() else {
return;
};
for l_hit in r_guard.get_legend_hits().iter() {
if rect_contains(l_hit.get_rect(), vertex_point) {
let name: String = l_hit.get_name().clone();
let mut now_vis: bool = true;
let _: bool = {
if let Some(mut updated_value) = state_sig.try_get_untracked() {
{
let node_style_next: &mut EuvChartState = &mut updated_value;
now_vis = node_style_next.toggle_series(name.as_str());
}
state_sig.set(updated_value)
} else {
false
}
};
scene_click.mark_dirty(canvas_node, DIRTY_PAINT);
if let Some(callback_fn_primary) = legend_cb.as_ref() {
callback_fn_primary(name.as_str(), now_vis);
}
return;
}
}
let matched: Vec<EuvChartDatumHit> = hit_test_chart(
r_guard.get_hits(),
vertex_point,
EuvChartTooltipTrigger::Item,
);
if let Some(hit) = matched.first() {
let cur_opt: EuvChartOption = match opt_sig {
Some(sig) => sig.get_untracked(),
None => opt_click.resolve_clone(),
};
if let Some(source_val) = cur_opt.get_series().get(hit.get_series_index())
&& matches!(source_val.get_data(), EuvChartSeriesData::Pie(_))
{
let d_idx: usize = hit.get_data_index();
let _: bool = {
if let Some(mut updated_value) = state_sig.try_get_untracked() {
{
let node_style_alt: &mut EuvChartState = &mut updated_value;
node_style_alt.toggle_pie_slice(d_idx);
}
state_sig.set(updated_value)
} else {
false
}
};
scene_click.mark_dirty(canvas_node, DIRTY_PAINT);
}
if let Some(callback_fn_secondary) = click_cb.as_ref() {
callback_fn_secondary(hit);
}
}
});
let opt_scroll: Rc<LazyChartOption> = Rc::clone(width_val.get_static_opt());
let res_scroll: Rc<RefCell<EuvChartRenderResult>> = Rc::clone(width_val.get_last_render());
let scene_scroll: SceneHandle = ctx.get_scene().clone();
let z_cb_scroll: Option<Rc<dyn Fn(f32, f32)>> = width_val.try_get_on_zoom_change().clone();
ctx.on_scroll(canvas_node, move |dispatched_event: &UiEvent| {
let UiEventKind::Scroll(delta) = dispatched_event.get_kind() else {
return;
};
if delta.get_x().abs() <= delta.get_y().abs() || delta.get_x().abs() < 1e-4 {
return;
}
let cur_opt: EuvChartOption = match opt_sig {
Some(sig) => sig.get_untracked(),
None => opt_scroll.resolve_clone(),
};
if cur_opt.get_data_zoom().is_empty() {
return;
}
let plot_w: f32 = res_scroll
.try_borrow()
.ok()
.map(|red_val: Ref<'_, EuvChartRenderResult>| red_val.get_plot_rect().get_width())
.filter(|span_width: &f32| *span_width > 16.0)
.unwrap_or(240.0);
let mut out_range: Option<(f32, f32)> = None;
let _: bool = {
if let Some(mut updated_value) = state_sig.try_get_untracked() {
{
let node_style_inner: &mut EuvChartState = &mut updated_value;
let span: f32 =
node_style_inner.get_zoom_end_pct() - node_style_inner.get_zoom_start_pct();
if span < 99.9 {
let delta_pct: f32 = (delta.get_x() / plot_w) * span;
if node_style_inner.pan_by_pct(delta_pct) {
out_range = Some((
node_style_inner.get_zoom_start_pct(),
node_style_inner.get_zoom_end_pct(),
));
}
}
}
state_sig.set(updated_value)
} else {
false
}
};
if let Some((lo, hi)) = out_range {
scene_scroll.mark_dirty(canvas_node, DIRTY_PAINT);
if let Some(callback_fn_next) = z_cb_scroll.as_ref() {
callback_fn_next(lo, hi);
}
}
});
}