use super::text_control::TextControl;
use crate::Brush;
use std::{fmt, rc::Rc};
use waft_embedder::Color;
use waft_foundation::{App, Handle};
use waft_services::TextInputFormatterRef;
use waft_widgets::*;
use super::text_box::{TextBox, TextChangedCallback};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PasswordRevealMode {
#[default]
Peek,
Hidden,
Visible,
}
#[derive(Clone)]
pub struct PasswordBox {
pub controller: TextEditingControllerRef,
pub focus_node: Option<FocusNodeRef>,
pub header: Option<WidgetRef>,
pub description: Option<WidgetRef>,
pub placeholder_text: String,
pub is_enabled: bool,
pub font_size: Option<f64>,
pub padding: Option<[f64; 4]>,
pub corner_radius: Option<f64>,
pub foreground: Option<Color>,
pub background: Option<Brush>,
pub border_brush: Option<Brush>,
pub selection_highlight_color: Option<Color>,
pub max_length: Option<i32>,
pub input_formatters: Vec<TextInputFormatterRef>,
pub autofocus: bool,
pub context_menu_builder: Option<EditableTextContextMenuBuilder>,
pub password_reveal_mode: PasswordRevealMode,
pub password_char: char,
pub password_changed: Option<TextChangedCallback>,
pub key: Option<KeyRef>,
}
impl PasswordBox {
pub fn new(controller: TextEditingControllerRef) -> Self {
Self {
controller,
focus_node: None,
header: None,
description: None,
placeholder_text: String::new(),
is_enabled: true,
font_size: None,
padding: None,
corner_radius: None,
foreground: None,
background: None,
border_brush: None,
selection_highlight_color: None,
max_length: None,
input_formatters: Vec::new(),
autofocus: false,
context_menu_builder: None,
password_reveal_mode: PasswordRevealMode::Peek,
password_char: 'â—Ź',
password_changed: None,
key: None,
}
}
pub fn focus_node(mut self, value: FocusNodeRef) -> Self {
self.focus_node = Some(value);
self
}
pub fn header<K>(mut self, value: impl IntoWidget<K>) -> Self {
self.header = Some(value.into_widget());
self
}
pub fn description<K>(mut self, value: impl IntoWidget<K>) -> Self {
self.description = Some(value.into_widget());
self
}
pub fn placeholder_text(mut self, value: impl Into<String>) -> Self {
self.placeholder_text = value.into();
self
}
pub fn is_enabled(mut self, value: bool) -> Self {
self.is_enabled = value;
self
}
pub fn font_size(mut self, value: f64) -> Self {
self.font_size = Some(value);
self
}
pub fn padding(mut self, value: [f64; 4]) -> Self {
self.padding = Some(value);
self
}
pub fn corner_radius(mut self, value: f64) -> Self {
self.corner_radius = Some(value);
self
}
pub fn foreground(mut self, value: Color) -> Self {
self.foreground = Some(value);
self
}
pub fn background(mut self, value: Brush) -> Self {
self.background = Some(value);
self
}
pub fn border_brush(mut self, value: Brush) -> Self {
self.border_brush = Some(value);
self
}
pub fn selection_highlight_color(mut self, value: Color) -> Self {
self.selection_highlight_color = Some(value);
self
}
pub fn max_length(mut self, value: i32) -> Self {
assert!(value >= 0, "MaxLength must be non-negative");
self.max_length = Some(value);
self
}
pub fn input_formatters(mut self, value: Vec<TextInputFormatterRef>) -> Self {
self.input_formatters = value;
self
}
pub fn autofocus(mut self, value: bool) -> Self {
self.autofocus = value;
self
}
pub fn context_menu_builder(
mut self,
value: impl Fn(&mut App, BuildContext, Handle<EditableTextState>) -> WidgetRef + 'static,
) -> Self {
self.context_menu_builder = Some(Rc::new(value));
self
}
pub fn password_reveal_mode(mut self, value: PasswordRevealMode) -> Self {
self.password_reveal_mode = value;
self
}
pub fn password_char(mut self, value: char) -> Self {
assert!(
value != '\0' && value.len_utf16() == 1,
"PasswordChar must be one non-null UTF-16 character"
);
self.password_char = value;
self
}
pub fn password_changed(mut self, value: impl Fn(&mut App, String) + 'static) -> Self {
self.password_changed = Some(Rc::new(value));
self
}
pub fn key(mut self, key: KeyRef) -> Self {
self.key = Some(key);
self
}
}
impl fmt::Debug for PasswordBox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PasswordBox")
.field("is_enabled", &self.is_enabled)
.finish_non_exhaustive()
}
}
impl StatelessWidget for PasswordBox {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let mut editor = TextBox::new(self.controller.clone());
editor.focus_node = self.focus_node.clone();
editor.header = self.header.clone();
editor.description = self.description.clone();
editor.placeholder_text = self.placeholder_text.clone();
editor.is_enabled = self.is_enabled;
editor.font_size = self.font_size;
editor.padding = self.padding;
editor.corner_radius = self.corner_radius;
editor.foreground = self.foreground;
editor.background = self.background;
editor.border_brush = self.border_brush;
editor.selection_highlight_color = self.selection_highlight_color;
editor.max_length = self.max_length;
editor.input_formatters = self.input_formatters.clone();
editor.autofocus = self.autofocus;
editor.context_menu_builder = self.context_menu_builder.clone();
editor.text_changed = self.password_changed.clone();
TextControl::new(
editor,
Some((self.password_reveal_mode, self.password_char)),
)
.into_widget()
}
}