mod editing;
use crate::theme::MacTheme;
use egui::{Align, Id, Rect, Response, Ui};
use std::ops::RangeInclusive;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MacControlSize {
#[default]
Regular,
Small,
Mini,
Large,
CompactInfobox,
}
pub struct MacInputCell<'a> {
pub(super) id: Id,
pub(super) value: &'a mut String,
pub(super) theme: Option<&'a MacTheme>,
pub(super) width: Option<f32>,
pub(super) height: Option<f32>,
pub(super) font_size: Option<f32>,
pub(super) corner_radius: Option<f32>,
pub(super) control_size: MacControlSize,
pub(super) explicit_control_size: bool,
pub(super) fixed_rect: Option<Rect>,
pub(super) text_align: Align,
pub(super) enabled: bool,
pub(super) next_focus_id: Option<Id>,
pub(super) prev_focus_id: Option<Id>,
pub(super) placeholder: Option<&'a str>,
pub(super) numeric_config: Option<(RangeInclusive<f64>, f64)>,
}
impl<'a> MacInputCell<'a> {
pub fn new(id_source: impl std::hash::Hash + std::fmt::Debug, value: &'a mut String) -> Self {
let id = Id::NULL.with(id_source);
Self::with_id(id, value)
}
pub fn with_id(id: Id, value: &'a mut String) -> Self {
Self {
id,
value,
theme: None,
width: None,
height: None,
font_size: None,
corner_radius: None,
control_size: MacControlSize::Regular,
explicit_control_size: false,
fixed_rect: None,
text_align: Align::Min,
enabled: true,
next_focus_id: None,
prev_focus_id: None,
placeholder: None,
numeric_config: None,
}
}
pub fn numeric(mut self, range: RangeInclusive<f64>, step: f64) -> Self {
self.numeric_config = Some((range, step));
self
}
pub fn control_size(mut self, control_size: MacControlSize) -> Self {
self.control_size = control_size;
self.explicit_control_size = true;
self
}
pub fn fixed_rect(mut self, rect: Rect) -> Self {
self.fixed_rect = Some(rect);
self
}
pub fn text_align(mut self, align: Align) -> Self {
self.text_align = align;
self
}
pub fn resolved_control_size(&self) -> MacControlSize {
if self.explicit_control_size {
return self.control_size;
}
let Some(rect) = self.fixed_rect else {
return self.control_size;
};
let theme_cow = MacTheme::resolve_or_default(self.theme);
let tf = theme_cow.text_field();
let h = rect.height();
if h >= tf.auto_size_regular_min_height.points() {
MacControlSize::Regular
} else if h >= tf.auto_size_small_min_height.points() {
MacControlSize::Small
} else if h >= tf.auto_size_compact_min_height.points() {
MacControlSize::CompactInfobox
} else {
MacControlSize::Mini
}
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn theme(mut self, theme: &'a MacTheme) -> Self {
self.theme = Some(theme);
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: f32) -> Self {
self.height = Some(height);
self
}
pub fn font_size(mut self, font_size: f32) -> Self {
self.font_size = Some(font_size);
self
}
pub fn corner_radius(mut self, radius: f32) -> Self {
self.corner_radius = Some(radius);
self
}
pub fn placeholder(mut self, text: &'a str) -> Self {
self.placeholder = Some(text);
self
}
pub fn next_focus(mut self, id: Id) -> Self {
self.next_focus_id = Some(id);
self
}
pub fn prev_focus(mut self, id: Id) -> Self {
self.prev_focus_id = Some(id);
self
}
pub fn show(self, ui: &mut Ui) -> Response {
editing::show_input_cell(self, ui)
}
}