use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::rc::Rc;
use glory_style::Style;
use crate::{Change, Error, normalize_text};
mod live;
pub use live::LiveDescriptor;
mod view;
pub use view::DescriptorView;
fn normalize_input(props: &mut Props) -> Result<(), Error> {
if let Props::Input(input) = props {
if !input.multiline && input.value.contains(['\r', '\n']) {
return Err(Error::InvalidProps(
"single-line input cannot contain line breaks",
));
}
if let std::borrow::Cow::Owned(value) = normalize_text(&input.value, input.multiline) {
input.value = value;
}
}
if let Props::Image(image) = props
&& let std::borrow::Cow::Owned(source) = normalize_text(&image.source, true)
{
image.source = source;
}
Ok(())
}
#[derive(Clone, Default)]
pub struct Options(HashMap<TypeId, Rc<dyn Any>>);
impl Options {
pub fn insert<T: 'static>(&mut self, value: T) {
self.0.insert(TypeId::of::<T>(), Rc::new(value));
}
pub fn get<T: 'static>(&self) -> Option<&T> {
self.0.get(&TypeId::of::<T>())?.downcast_ref()
}
}
impl std::fmt::Debug for Options {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Options")
.field("types", &self.0.len())
.finish()
}
}
impl PartialEq for Options {
fn eq(&self, other: &Self) -> bool {
self.0.len() == other.0.len()
&& self.0.iter().all(|(key, value)| {
other
.0
.get(key)
.is_some_and(|other| Rc::ptr_eq(value, other))
})
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextKind {
#[default]
Inline,
Paragraph,
Heading(u8),
Code,
Kbd,
Mark,
}
impl TextKind {
pub const fn is_inline(self) -> bool {
matches!(self, Self::Inline | Self::Code | Self::Kbd | Self::Mark)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Purpose {
#[default]
Text,
Password,
Email,
Search,
Url,
Tel,
Number,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Role {
Slider,
SpinButton,
ProgressBar,
Separator,
Group,
Main,
Banner,
ContentInfo,
Section,
Navigation,
List,
ListItem,
Status,
Alert,
Table,
Grid,
GridCell,
Tree,
TreeItem,
Row,
ColumnHeader,
Cell,
Dialog,
AlertDialog,
Tooltip,
TabList,
TabPanel,
RadioGroup,
Toolbar,
Menu,
Menubar,
Listbox,
Heading(u8),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TextProps {
pub content: String,
pub kind: TextKind,
pub selectable: bool,
}
impl Default for TextProps {
fn default() -> Self {
Self {
content: String::new(),
kind: TextKind::Inline,
selectable: true,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ButtonProps {
pub label: String,
pub enabled: bool,
}
impl Default for ButtonProps {
fn default() -> Self {
Self {
label: String::new(),
enabled: true,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InputProps {
pub value: String,
pub revision: u64,
pub purpose: Purpose,
pub multiline: bool,
pub enabled: bool,
}
impl Default for InputProps {
fn default() -> Self {
Self {
value: String::new(),
revision: 0,
purpose: Purpose::Text,
multiline: false,
enabled: true,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CheckMode {
#[default]
Checkbox,
Mixed,
Switch,
Radio,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CheckProps {
pub mode: CheckMode,
pub checked: bool,
pub label: String,
pub enabled: bool,
}
impl Default for CheckProps {
fn default() -> Self {
Self {
mode: CheckMode::Checkbox,
checked: false,
label: String::new(),
enabled: true,
}
}
}
pub const MAX_IMAGE_CANDIDATES: usize = 16;
pub const MAX_IMAGE_SIZE_HINTS: usize = 8;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageCandidate {
pub source: String,
pub width: u16,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageSlotSize {
Pixels(u16),
ViewportWidth(u8),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ImageSizeHint {
pub max_viewport_width: Option<u16>,
pub slot: ImageSlotSize,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ResponsiveImage {
pub candidates: Vec<ImageCandidate>,
pub sizes: Vec<ImageSizeHint>,
}
impl ResponsiveImage {
pub fn validate(&self) -> Result<(), Error> {
if self.candidates.len() > MAX_IMAGE_CANDIDATES {
return Err(Error::InvalidProps("too many responsive image candidates"));
}
let mut previous_width = 0;
for candidate in &self.candidates {
if candidate.source.is_empty()
|| candidate.source.len() > 4096
|| candidate.source.chars().any(|character| {
character.is_control() || character.is_whitespace() || character == ','
})
|| candidate.width == 0
|| candidate.width > 16_384
|| candidate.width <= previous_width
{
return Err(Error::InvalidProps(
"responsive image candidates require ordered unique 1..=16384 widths and bounded sources",
));
}
previous_width = candidate.width;
}
if self.sizes.len() > MAX_IMAGE_SIZE_HINTS
|| self.candidates.is_empty() != self.sizes.is_empty()
{
return Err(Error::InvalidProps(
"responsive image candidates and sizes must be configured together",
));
}
let mut previous_breakpoint = 0;
for (index, hint) in self.sizes.iter().enumerate() {
match hint.max_viewport_width {
Some(width)
if width > 0
&& width <= 16_384
&& width > previous_breakpoint
&& index + 1 < self.sizes.len() =>
{
previous_breakpoint = width;
}
None if index + 1 == self.sizes.len() => {}
_ => {
return Err(Error::InvalidProps(
"responsive image sizes require ordered breakpoints and one final fallback",
));
}
}
if !matches!(
hint.slot,
ImageSlotSize::Pixels(1..=16_384) | ImageSlotSize::ViewportWidth(1..=100)
) {
return Err(Error::InvalidProps(
"responsive image slot size is outside its finite range",
));
}
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ImageLoading {
#[default]
Auto,
Eager,
Lazy,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ImageProps {
pub source: String,
pub revision: u64,
pub alternative: Option<String>,
pub responsive: ResponsiveImage,
pub loading: ImageLoading,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageStatus {
Loaded,
Failed,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LinkProps {
pub target: String,
pub label: String,
}
impl LinkProps {
pub fn validate(&self) -> Result<(), Error> {
validate_link_target(&self.target)
}
}
fn validate_link_target(target: &str) -> Result<(), Error> {
let invalid =
|| Error::InvalidProps("link target requires a relative or http/https/mailto/tel URL");
if target
.chars()
.any(|ch| ch.is_control() || ch.is_whitespace())
{
return Err(invalid());
}
let prefix = target.split(['/', '?', '#']).next().unwrap_or_default();
if let Some((scheme, _)) = prefix.split_once(':')
&& !["http", "https", "mailto", "tel"]
.iter()
.any(|allowed| scheme.eq_ignore_ascii_case(allowed))
{
return Err(invalid());
}
Ok(())
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Props {
#[default]
View,
Group,
Text(TextProps),
Button(ButtonProps),
Input(InputProps),
Select(crate::SelectProps),
File(crate::FileProps),
RichEditor(crate::RichEditorProps),
Check(CheckProps),
Image(ImageProps),
Graphic(crate::GraphicProps),
Link(LinkProps),
}
impl Props {
pub fn interactive(&self) -> bool {
matches!(
self,
Self::Button(_)
| Self::Input(_)
| Self::Select(_)
| Self::File(_)
| Self::RichEditor(_)
| Self::Check(_)
| Self::Link(_)
)
}
pub fn enabled(&self) -> bool {
match self {
Self::Button(p) => p.enabled,
Self::Input(p) => p.enabled,
Self::Select(p) => p.enabled,
Self::File(p) => p.enabled,
Self::RichEditor(p) => p.enabled,
Self::Check(p) => p.enabled,
_ => true,
}
}
pub fn accepts_children(&self) -> bool {
matches!(
self,
Self::View | Self::Group | Self::Button(_) | Self::Link(_)
)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Descriptor {
pub props: Props,
pub classes: Vec<String>,
pub style: Style,
pub role: Option<Role>,
pub name: Option<String>,
pub focus: FocusMode,
pub item: Option<ItemPosition>,
pub options: Options,
pub semantics: Option<Box<crate::Semantics>>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FocusMode {
#[default]
Auto,
Programmatic,
TabStop,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ItemPosition {
pub index: usize,
pub count: Option<usize>,
}
impl Descriptor {
#[inline]
pub fn view(&self) -> DescriptorView<'_> {
self.into()
}
pub fn semantic_id(&self) -> Option<&crate::SemanticId> {
self.view().semantic_id()
}
pub fn hidden(&self) -> bool {
self.view().hidden()
}
pub fn activation_enabled(&self) -> bool {
self.view().activation_enabled()
}
pub fn readonly(&self) -> bool {
self.view().readonly()
}
pub fn field_part(&self) -> Option<crate::FieldPart> {
self.view().field_part()
}
pub fn document_part(&self) -> Option<crate::DocumentPart> {
self.view().document_part()
}
pub fn effective_role(&self) -> Option<Role> {
self.view().effective_role()
}
pub fn validate(&self) -> Result<(), Error> {
self.view().validate()
}
pub fn new(props: Props) -> Self {
Self {
props,
..Self::default()
}
}
pub fn normalize_input(&mut self) -> Result<(), Error> {
normalize_input(&mut self.props)
}
fn validate_input_revision(&self, next: &Self) -> Result<(), Error> {
if let (Some(old), Some(next)) = (
self.semantics.as_ref().and_then(|s| s.clipboard.as_ref()),
next.semantics.as_ref().and_then(|s| s.clipboard.as_ref()),
) {
next.validate_update(old)?;
}
crate::swipe::revision(
self.semantics.as_ref().and_then(|s| s.swipe_capture),
next.semantics.as_ref().and_then(|s| s.swipe_capture),
)?;
crate::presence::revision(
self.semantics.as_ref().and_then(|s| s.presence.as_deref()),
next.semantics.as_ref().and_then(|s| s.presence.as_deref()),
)?;
crate::collapse::revision(
self.semantics.as_ref().and_then(|s| s.collapse.as_ref()),
next.semantics.as_ref().and_then(|s| s.collapse.as_ref()),
)?;
if let (Props::RichEditor(old), Props::RichEditor(next)) = (&self.props, &next.props) {
next.validate_update(old)?;
}
if let (Props::File(old), Props::File(new)) = (&self.props, &next.props) {
new.validate_update(old)?;
}
if let (Props::Graphic(old), Props::Graphic(new)) = (&self.props, &next.props) {
new.transition_from(old)?;
}
crate::sheet::revision(
self.semantics.as_ref().and_then(|s| s.layer.as_ref()),
next.semantics.as_ref().and_then(|s| s.layer.as_ref()),
)?;
if let (Props::Input(old), Props::Input(next)) = (&self.props, &next.props)
&& (next.revision < old.revision
|| next.revision == old.revision
&& normalize_text(&old.value, old.multiline).as_ref() != next.value)
{
return Err(Error::InputRevision {
current: old.revision,
incoming: next.revision,
});
}
if let (Props::Select(old), Props::Select(next)) = (&self.props, &next.props) {
next.selection
.validate_revision(old.selection.revision, &old.selection.keys)?;
}
if let (Props::Image(old), Props::Image(next)) = (&self.props, &next.props)
&& (next.revision < old.revision
|| next.revision == old.revision
&& normalize_text(&old.source, true).as_ref() != next.source)
{
return Err(Error::ImageRevision {
current: old.revision,
incoming: next.revision,
});
}
Ok(())
}
pub fn patch(&mut self, patch: Patch) -> Result<(), Error> {
*self = self.patched(patch)?;
Ok(())
}
pub fn patched(&self, patch: Patch) -> Result<Self, Error> {
let mut next = self.clone();
next.apply_patch(patch)?;
next.normalize_input()?;
self.validate_input_revision(&next)?;
next.validate()?;
Ok(next)
}
pub fn patches(&mut self, patches: impl IntoIterator<Item = Patch>) -> Result<(), Error> {
let mut next = self.clone();
for patch in patches {
next.apply_patch(patch)?;
}
next.normalize_input()?;
self.validate_input_revision(&next)?;
next.validate()?;
*self = next;
Ok(())
}
fn apply_patch(&mut self, patch: Patch) -> Result<(), Error> {
match (patch, &mut self.props) {
(Patch::Text(change), Props::Text(p)) => change.apply(&mut p.content),
(Patch::TextKind(change), Props::Text(p)) => change.apply(&mut p.kind),
(Patch::Selectable(change), Props::Text(p)) => change.apply_or(&mut p.selectable, true),
(Patch::Label(change), Props::File(p)) => {
change.apply_or(&mut p.label, "Choose file".into())
}
(Patch::Enabled(change), Props::RichEditor(p)) => change.apply_or(&mut p.enabled, true),
(Patch::RichDocument { document, revision }, Props::RichEditor(p)) => {
if !matches!(document, Change::Keep) {
document.apply_or(
&mut p.document,
std::rc::Rc::new(crate::RichDocument::default()),
);
p.revision = revision;
}
}
(Patch::Enabled(change), Props::File(p)) => change.apply_or(&mut p.enabled, true),
(
Patch::File {
configuration,
revision,
},
Props::File(p),
) => {
if !matches!(configuration, Change::Keep) {
match configuration {
Change::Set(value) => p.configuration = *value,
Change::Clear => p.configuration = crate::FileConfig::default(),
Change::Keep => {}
}
p.revision = revision;
}
}
(Patch::Label(change), Props::Button(p)) => change.apply(&mut p.label),
(Patch::Label(change), Props::Check(p)) => change.apply(&mut p.label),
(Patch::Label(change), Props::Link(p)) => change.apply(&mut p.label),
(Patch::Enabled(change), Props::Button(p)) => change.apply_or(&mut p.enabled, true),
(Patch::Enabled(change), Props::Check(p)) => change.apply_or(&mut p.enabled, true),
(Patch::Enabled(change), Props::Input(p)) => change.apply_or(&mut p.enabled, true),
(Patch::Value { value, revision }, Props::Input(p)) => {
if !matches!(value, Change::Keep) {
value.apply(&mut p.value);
p.revision = revision;
}
}
(Patch::Enabled(change), Props::Select(p)) => change.apply_or(&mut p.enabled, true),
(Patch::SelectSelection { keys, revision }, Props::Select(p)) => {
if !matches!(keys, Change::Keep) {
keys.apply(&mut p.selection.keys);
p.selection.revision = revision;
}
}
(Patch::Purpose(change), Props::Input(p)) => change.apply(&mut p.purpose),
(Patch::Multiline(change), Props::Input(p)) => change.apply(&mut p.multiline),
(Patch::Checked(change), Props::Check(p)) => change.apply(&mut p.checked),
(Patch::CheckMode(change), Props::Check(p)) => change.apply(&mut p.mode),
(Patch::Source { source, revision }, Props::Image(p)) => {
if !matches!(source, Change::Keep) {
source.apply(&mut p.source);
p.revision = revision;
}
}
(Patch::Alternative(change), Props::Image(p)) => change.apply(&mut p.alternative),
(Patch::Graphic(change), Props::Graphic(p)) => match change {
Change::Keep => {}
Change::Clear => *p = crate::GraphicProps::default(),
Change::Set(value) => *p = *value,
},
(Patch::Target(change), Props::Link(p)) => change.apply(&mut p.target),
(Patch::Classes(change), _) => change.apply(&mut self.classes),
(Patch::Style(change), _) => match change {
Change::Keep => {}
Change::Clear => self.style = Style::default(),
Change::Set(style) => self.style = *style,
},
(Patch::Role(change), _) => change.apply(&mut self.role),
(Patch::Name(change), _) => change.apply(&mut self.name),
(Patch::Focus(change), _) => change.apply(&mut self.focus),
(Patch::Item(change), _) => change.apply(&mut self.item),
(Patch::Semantics(change), _) => change.apply(&mut self.semantics),
_ => {
return Err(Error::InvalidProps(
"property does not belong to this widget",
));
}
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Patch {
Text(Change<String>),
TextKind(Change<TextKind>),
Selectable(Change<bool>),
Label(Change<String>),
Enabled(Change<bool>),
Value {
value: Change<String>,
revision: u64,
},
RichDocument {
document: Change<std::rc::Rc<crate::RichDocument>>,
revision: u64,
},
SelectSelection {
keys: Change<Vec<crate::SemanticId>>,
revision: u64,
},
Purpose(Change<Purpose>),
Multiline(Change<bool>),
Checked(Change<bool>),
CheckMode(Change<CheckMode>),
Source {
source: Change<String>,
revision: u64,
},
Alternative(Change<Option<String>>),
Graphic(Change<Box<crate::GraphicProps>>),
File {
configuration: Change<Box<crate::FileConfig>>,
revision: u64,
},
Target(Change<String>),
Classes(Change<Vec<String>>),
Style(Change<Box<Style>>),
Role(Change<Option<Role>>),
Name(Change<Option<String>>),
Focus(Change<FocusMode>),
Item(Change<Option<ItemPosition>>),
Semantics(Change<Option<Box<crate::Semantics>>>),
}