#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]
use super::core::{Position, Size};
use crate::error::{MetricsError, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::time::{Duration, Instant};
pub trait InteractiveWidget: std::fmt::Debug + Send + Sync {
fn id(&self) -> &str;
fn widget_type(&self) -> WidgetType;
fn update_data(&mut self, data: Value) -> Result<()>;
fn handle_interaction(&mut self, event: WidgetEvent) -> Result<Option<WidgetEventResponse>>;
fn render(&self, context: &RenderContext) -> Result<WidgetRender>;
fn config(&self) -> &WidgetConfig;
fn update_config(&mut self, config: WidgetConfig) -> Result<()>;
fn state(&self) -> Value;
fn restore_state(&mut self, state: Value) -> Result<()>;
fn validate_data(&self, data: &Value) -> Result<()>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WidgetType {
Chart(ChartType),
Table,
Text,
Input(InputType),
Container,
Custom(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChartType {
Line,
Bar,
Scatter,
Heatmap,
Pie,
Area,
Histogram,
BoxPlot,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InputType {
Slider,
Dropdown,
TextInput,
Checkbox,
RadioButton,
DatePicker,
FileUpload,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WidgetConfig {
pub id: String,
pub title: String,
pub position: Position,
pub size: Size,
pub style: StyleConfig,
pub data_binding: DataBindingConfig,
pub interactions_enabled: bool,
pub animation_enabled: bool,
pub visible: bool,
pub z_index: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StyleConfig {
pub background_color: String,
pub border: BorderConfig,
pub shadow: ShadowConfig,
pub font: FontConfig,
pub css_classes: Vec<String>,
pub css_properties: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BorderConfig {
pub width: u32,
pub color: String,
pub style: BorderStyle,
pub radius: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BorderStyle {
Solid,
Dashed,
Dotted,
None,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShadowConfig {
pub enabled: bool,
pub offset_x: i32,
pub offset_y: i32,
pub blur_radius: u32,
pub color: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FontConfig {
pub family: String,
pub size: u32,
pub weight: FontWeight,
pub style: FontStyle,
pub color: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FontWeight {
Normal,
Bold,
Light,
Custom(u32),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FontStyle {
Normal,
Italic,
Oblique,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataBindingConfig {
pub source_id: String,
pub field_mappings: HashMap<String, String>,
pub update_frequency: UpdateFrequency,
pub transformations: Vec<DataTransformation>,
pub filters: HashMap<String, Value>,
pub aggregation: Option<AggregationMethod>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateFrequency {
RealTime,
Interval(Duration),
Manual,
OnDemand,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DataTransformation {
Filter(String),
Sort(String, bool), Group(String),
Aggregate(String, AggregationMethod),
Custom(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AggregationMethod {
Sum,
Average,
Count,
Min,
Max,
StdDev,
Custom(String),
}
#[derive(Debug, Clone)]
pub struct RenderContext {
pub canvas_id: String,
pub device: DeviceCapabilities,
pub options: RenderOptions,
pub timestamp: Instant,
pub theme: super::core::ThemeConfig,
}
#[derive(Debug, Clone)]
pub struct DeviceCapabilities {
pub screen_width: u32,
pub screen_height: u32,
pub pixel_ratio: f64,
pub webgl_supported: bool,
pub touch_supported: bool,
pub max_texture_size: u32,
}
#[derive(Debug, Clone)]
pub struct RenderOptions {
pub quality: RenderQuality,
pub antialiasing: bool,
pub transparency: bool,
pub preserve_buffer: bool,
pub power_preference: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RenderQuality {
Low,
Medium,
High,
Ultra,
}
#[derive(Debug, Clone)]
pub struct WidgetRender {
pub content: RenderContent,
pub metadata: RenderMetadata,
pub resources: Vec<String>,
}
#[derive(Debug, Clone)]
pub enum RenderContent {
Html(String),
Svg(String),
Canvas(Vec<CanvasCommand>),
WebGL(ShaderProgram),
}
#[derive(Debug, Clone)]
pub enum CanvasCommand {
DrawLine {
from: Position,
to: Position,
color: String,
width: f64,
},
DrawRect {
position: Position,
size: Size,
color: String,
},
DrawCircle {
center: Position,
radius: f64,
color: String,
},
DrawText {
position: Position,
text: String,
font: FontConfig,
},
Custom(String, HashMap<String, Value>),
}
#[derive(Debug, Clone)]
pub struct ShaderProgram {
pub vertex_shader: String,
pub fragment_shader: String,
pub uniforms: HashMap<String, UniformValue>,
pub attributes: HashMap<String, AttributeBinding>,
}
#[derive(Debug, Clone)]
pub enum UniformValue {
Float(f32),
Vec2([f32; 2]),
Vec3([f32; 3]),
Vec4([f32; 4]),
Mat4([[f32; 4]; 4]),
Texture(String),
}
#[derive(Debug, Clone)]
pub struct AttributeBinding {
pub buffer: String,
pub components: u32,
pub data_type: AttributeType,
pub normalized: bool,
}
#[derive(Debug, Clone)]
pub enum AttributeType {
Float,
UnsignedByte,
Short,
UnsignedShort,
}
#[derive(Debug, Clone)]
pub struct RenderMetadata {
pub render_time: Duration,
pub frame_rate: f64,
pub memory_usage: u64,
pub error_count: u32,
}
#[derive(Debug, Clone)]
pub struct WidgetEvent {
pub id: String,
pub event_type: EventType,
pub timestamp: Instant,
pub data: HashMap<String, Value>,
pub source_widget: String,
pub target: Option<String>,
}
#[derive(Debug, Clone)]
pub enum EventType {
Click { position: Position, button: u32 },
DoubleClick { position: Position },
MouseMove { position: Position, delta: Position },
MouseEnter { position: Position },
MouseLeave { position: Position },
KeyPress { key: String, modifiers: Vec<String> },
Touch { touches: Vec<TouchPoint> },
Resize { new_size: Size },
Focus,
Blur,
Custom { name: String, data: Value },
}
#[derive(Debug, Clone)]
pub struct TouchPoint {
pub id: u32,
pub position: Position,
pub pressure: f64,
pub radius: f64,
}
#[derive(Debug, Clone)]
pub struct WidgetEventResponse {
pub id: String,
pub actions: Vec<ResponseAction>,
pub data_updates: HashMap<String, Value>,
pub state_changes: HashMap<String, Value>,
}
#[derive(Debug, Clone)]
pub enum ResponseAction {
UpdateData { widget_id: String, data: Value },
TriggerEvent { event: WidgetEvent },
Navigate { url: String },
ShowNotification {
message: String,
level: NotificationLevel,
},
Custom {
action: String,
params: HashMap<String, Value>,
},
}
#[derive(Debug, Clone)]
pub enum NotificationLevel {
Info,
Success,
Warning,
Error,
}
impl Default for WidgetConfig {
fn default() -> Self {
Self {
id: "widget".to_string(),
title: "Widget".to_string(),
position: Position::default(),
size: Size::default(),
style: StyleConfig::default(),
data_binding: DataBindingConfig::default(),
interactions_enabled: true,
animation_enabled: true,
visible: true,
z_index: 0,
}
}
}
impl Default for StyleConfig {
fn default() -> Self {
Self {
background_color: "#ffffff".to_string(),
border: BorderConfig::default(),
shadow: ShadowConfig::default(),
font: FontConfig::default(),
css_classes: Vec::new(),
css_properties: HashMap::new(),
}
}
}
impl Default for BorderConfig {
fn default() -> Self {
Self {
width: 1,
color: "#cccccc".to_string(),
style: BorderStyle::Solid,
radius: 4,
}
}
}
impl Default for ShadowConfig {
fn default() -> Self {
Self {
enabled: false,
offset_x: 2,
offset_y: 2,
blur_radius: 4,
color: "rgba(0,0,0,0.1)".to_string(),
}
}
}
impl Default for FontConfig {
fn default() -> Self {
Self {
family: "Arial, sans-serif".to_string(),
size: 14,
weight: FontWeight::Normal,
style: FontStyle::Normal,
color: "#333333".to_string(),
}
}
}
impl Default for DataBindingConfig {
fn default() -> Self {
Self {
source_id: "default".to_string(),
field_mappings: HashMap::new(),
update_frequency: UpdateFrequency::RealTime,
transformations: Vec::new(),
filters: HashMap::new(),
aggregation: None,
}
}
}