use egui::{Color32, FontId, TextStyle, Ui};
use crate::{ToggleButtonsState, render::ParentStatus, value::BaseValueType};
#[derive(Debug, Clone, Default)]
pub struct JsonTreeStyle {
pub visuals: Option<JsonTreeVisuals>,
pub font_id: Option<FontId>,
pub abbreviate_root: bool,
pub toggle_buttons_state: ToggleButtonsState,
pub wrapping_config: JsonTreeWrappingConfig,
}
impl JsonTreeStyle {
pub fn new() -> Self {
Self::default()
}
pub fn visuals(mut self, visuals: JsonTreeVisuals) -> Self {
self.visuals = Some(visuals);
self
}
pub fn font_id(mut self, font_id: FontId) -> Self {
self.font_id = Some(font_id);
self
}
pub fn abbreviate_root(mut self, abbreviate_root: bool) -> Self {
self.abbreviate_root = abbreviate_root;
self
}
pub fn toggle_buttons_state(mut self, toggle_buttons_state: ToggleButtonsState) -> Self {
self.toggle_buttons_state = toggle_buttons_state;
self
}
pub fn wrapping_config(mut self, wrapping_config: JsonTreeWrappingConfig) -> Self {
self.wrapping_config = wrapping_config;
self
}
pub(crate) fn resolve_visuals(&self, ui: &Ui) -> &JsonTreeVisuals {
if let Some(visuals) = &self.visuals {
visuals
} else if ui.visuals().dark_mode {
&JsonTreeVisuals::DARK
} else {
&JsonTreeVisuals::LIGHT
}
}
pub(crate) fn resolve_font_id(&self, ui: &Ui) -> FontId {
if let Some(font_id) = &self.font_id {
font_id.clone()
} else {
TextStyle::Monospace.resolve(ui.style())
}
}
pub(crate) fn resolve_value_text_wrapping(
&self,
parent_status: ParentStatus,
ui: &Ui,
) -> egui::text::TextWrapping {
let wrap = match parent_status {
ParentStatus::NoParent => self.wrapping_config.value_when_root,
ParentStatus::ExpandedParent => self.wrapping_config.value_with_expanded_parent,
ParentStatus::CollapsedRoot => self.wrapping_config.value_in_collapsed_root,
};
let max_width = match wrap.max_width {
JsonTreeMaxWidth::Points(max_width) => max_width,
JsonTreeMaxWidth::UiAvailableWidth => ui.available_width(),
};
egui::text::TextWrapping {
max_width,
max_rows: wrap.max_rows,
break_anywhere: wrap.break_anywhere,
..Default::default()
}
}
}
#[derive(Debug, Clone, Hash)]
pub struct JsonTreeVisuals {
pub object_key_color: Color32,
pub array_idx_color: Color32,
pub null_color: Color32,
pub bool_color: Color32,
pub number_color: Color32,
pub string_color: Color32,
pub highlight_color: Color32,
pub punctuation_color: Color32,
}
impl Default for JsonTreeVisuals {
fn default() -> Self {
Self::DARK
}
}
impl JsonTreeVisuals {
pub const DARK: Self = Self {
object_key_color: Color32::from_rgb(161, 206, 235),
array_idx_color: Color32::from_rgb(96, 103, 168),
null_color: Color32::from_rgb(103, 154, 209),
bool_color: Color32::from_rgb(103, 154, 209),
number_color: Color32::from_rgb(181, 199, 166),
string_color: Color32::from_rgb(194, 146, 122),
highlight_color: Color32::from_rgba_premultiplied(72, 72, 72, 50),
punctuation_color: Color32::from_gray(140),
};
pub const LIGHT: Self = Self {
object_key_color: Color32::from_rgb(23, 74, 151),
array_idx_color: Color32::from_rgb(158, 46, 103),
null_color: Color32::from_rgb(40, 34, 245),
bool_color: Color32::from_rgb(40, 34, 245),
number_color: Color32::from_rgb(1, 97, 63),
string_color: Color32::from_rgb(149, 38, 31),
highlight_color: Color32::from_rgba_premultiplied(181, 213, 251, 255),
punctuation_color: Color32::from_gray(70),
};
pub fn get_color(&self, base_value_type: &BaseValueType) -> Color32 {
match base_value_type {
BaseValueType::Null => self.null_color,
BaseValueType::Bool => self.bool_color,
BaseValueType::Number => self.number_color,
BaseValueType::String => self.string_color,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct JsonTreeWrappingConfig {
pub value_when_root: JsonTreeWrapping,
pub value_with_expanded_parent: JsonTreeWrapping,
pub value_in_collapsed_root: JsonTreeWrapping,
}
#[derive(Debug, Clone, Copy)]
pub struct JsonTreeWrapping {
pub max_rows: usize,
pub max_width: JsonTreeMaxWidth,
pub break_anywhere: bool,
}
impl Default for JsonTreeWrapping {
fn default() -> Self {
Self {
max_rows: usize::MAX,
max_width: JsonTreeMaxWidth::UiAvailableWidth,
break_anywhere: false,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum JsonTreeMaxWidth {
Points(f32),
UiAvailableWidth,
}