openpine_vm/visuals/
table.rs1use serde::{Deserialize, Serialize};
2
3use crate::visuals::{Color, FontFamily, HorizontalAlign, Position, TextFormatting, VerticalAlign};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct TableCell {
8 pub text: String,
10 pub width: f32,
12 pub height: f32,
14 pub text_color: Option<Color>,
16 pub text_halign: HorizontalAlign,
18 pub text_valign: VerticalAlign,
20 pub text_size: u32,
22 pub background_color: Option<Color>,
24 pub tooltip: Option<String>,
26 pub text_font_family: FontFamily,
28 pub text_formatting: TextFormatting,
30 pub colspan: usize,
32 pub rowspan: usize,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct Table {
39 pub position: Position,
41 pub(crate) num_columns: usize,
42 pub(crate) cells: Vec<TableCell>,
43 pub background_color: Option<Color>,
45 pub frame_color: Option<Color>,
47 pub frame_width: i32,
49 pub border_color: Option<Color>,
51 pub border_width: i32,
53 pub force_overlay: bool,
55}
56
57impl Table {
58 #[inline]
60 pub fn num_columns(&self) -> usize {
61 self.num_columns
62 }
63
64 #[inline]
66 pub fn num_rows(&self) -> usize {
67 if self.num_columns == 0 {
68 0
69 } else {
70 self.cells.len() / self.num_columns
71 }
72 }
73
74 #[inline]
76 pub fn cell(&self, row: usize, col: usize) -> &TableCell {
77 &self.cells[(row * self.num_columns) + col]
78 }
79
80 #[inline]
82 pub fn cell_opt(&self, row: usize, col: usize) -> Option<&TableCell> {
83 if col >= self.num_columns {
84 return None;
85 }
86 let idx = (row * self.num_columns) + col;
87 self.cells.get(idx)
88 }
89
90 #[inline]
91 pub(crate) fn cell_mut(&mut self, row: usize, col: usize) -> &mut TableCell {
92 &mut self.cells[row * self.num_columns + col]
93 }
94}