oarust 0.8.6

GPU-first semantic computing engine written in Rust
//! Canvas background and grid overlays. Donor: `oa::Viewer::renderVisualWorkspace`.

use super::config::{ViewerCanvasBackground, ViewerConfig};
use crate::ui::{TextLayoutConfig, Ui, navigation::Navigation, types::PixelRect};
use crate::{Color, Result};

/// Draw the canvas background before the media content.
pub fn draw_canvas_background(
	ui: &mut Ui<'_>,
	rect: PixelRect,
	config: &ViewerConfig,
) -> Result<()> {
	match config.canvas_background {
		ViewerCanvasBackground::Dark => {
			ui.rect_raw(rect, Color::new(0.04, 0.04, 0.05, 1.0), rect, 0.0);
		}
		ViewerCanvasBackground::Gradient => {
			// Use a dark top and slightly lighter bottom as a simple gradient proxy.
			ui.rect_raw(rect, Color::new(0.04, 0.04, 0.05, 1.0), rect, 0.0);
		}
	}
	if config.show_canvas_grid {
		let grid = Color::new(0.12, 0.13, 0.16, 0.75);
		let spacing = 64_u32;
		for x in (0..=rect.w).step_by(spacing as usize) {
			let x = rect.x + x as i32;
			ui.line_at(
				[x as f32, rect.y as f32],
				[x as f32, (rect.y + rect.h as i32) as f32],
				grid,
				1.0,
				rect,
			)?;
		}
		for y in (0..=rect.h).step_by(spacing as usize) {
			let y = rect.y + y as i32;
			ui.line_at(
				[rect.x as f32, y as f32],
				[(rect.x + rect.w as i32) as f32, y as f32],
				grid,
				1.0,
				rect,
			)?;
		}
	}
	Ok(())
}

/// Draw the always-available viewer status/help overlay through the same glyph
/// compositor used by Plot. Donor: `oa::Viewer::renderOverlay`.
pub fn draw_hud(
	ui: &mut Ui<'_>,
	viewport: PixelRect,
	navigation: &Navigation,
	mode: &str,
	config: &ViewerConfig,
) -> Result<()> {
	let text = Color::new(0.90, 0.92, 0.96, 1.0);
	let muted = Color::new(0.62, 0.66, 0.74, 1.0);
	let cfg = TextLayoutConfig {
		size: 12.0,
		..Default::default()
	};
	ui.text_at(
		&format!("{mode}  {:+.0}%", navigation.zoom() * 100.0),
		[12.0, 20.0],
		cfg,
		text,
		viewport,
	)?;
	if config.show_help {
		ui.text_at(
			"wheel: zoom  drag: pan  0: fit  1-5: channels  space: play/pause",
			[12.0, (viewport.y + viewport.h as i32 - 12) as f32],
			cfg,
			muted,
			viewport,
		)?;
	}
	Ok(())
}