#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Insets {
pub top: f32,
pub right: f32,
pub bottom: f32,
pub left: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorScheme {
#[default]
Light,
Dark,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, crate::ui::PropEnum)]
pub enum Appearance {
#[default]
System,
Light,
Dark,
}
impl Appearance {
pub fn resolve(self, system: ColorScheme) -> ColorScheme {
match self {
Appearance::System => system,
Appearance::Light => ColorScheme::Light,
Appearance::Dark => ColorScheme::Dark,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Os {
Ios,
Android,
MacOs,
Windows,
Linux,
}
impl Os {
pub fn name(self) -> &'static str {
match self {
Os::Ios => "ios",
Os::Android => "android",
Os::MacOs => "macos",
Os::Windows => "windows",
Os::Linux => "linux",
}
}
pub fn is_desktop(self) -> bool {
matches!(self, Os::MacOs | Os::Windows | Os::Linux)
}
}
impl Default for Os {
fn default() -> Self {
if cfg!(target_os = "android") {
Os::Android
} else {
Os::Ios
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Idiom {
#[default]
Phone,
Pad,
Mac,
Tv,
Desktop,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Environment {
pub window: (f32, f32),
pub safe_area: Insets,
pub title_bar: Insets,
pub keyboard_height: f32,
pub keyboard_duration: f32,
pub color_scheme: ColorScheme,
pub font_scale: f32,
pub rtl: bool,
pub reduce_motion: bool,
pub pixel_ratio: f32,
pub screen_reader: bool,
pub os: Os,
pub os_version: (u16, u16),
pub idiom: Idiom,
pub locale: Locale,
}
impl Default for Environment {
fn default() -> Self {
Self {
window: (0.0, 0.0),
safe_area: Insets::default(),
title_bar: Insets::default(),
keyboard_height: 0.0,
keyboard_duration: 0.25,
color_scheme: ColorScheme::Light,
font_scale: 1.0,
rtl: false,
reduce_motion: false,
pixel_ratio: 1.0,
screen_reader: false,
os: Os::default(),
os_version: (0, 0),
idiom: Idiom::Phone,
locale: Locale::EN_US,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Locale {
bytes: [u8; Locale::CAPACITY],
len: u8,
}
impl Locale {
const CAPACITY: usize = 15;
pub const EN_US: Locale = Locale {
bytes: *b"en-US\0\0\0\0\0\0\0\0\0\0",
len: 5,
};
pub fn parse(identifier: &str) -> Option<Locale> {
let identifier = identifier.split(['@', '.']).next().unwrap_or_default();
let mut bytes = [0u8; Self::CAPACITY];
let mut len = 0;
for (i, part) in identifier.split(['-', '_']).enumerate() {
let keep = if i == 0 {
(2..=3).contains(&part.len()) && part.bytes().all(|b| b.is_ascii_alphabetic())
} else {
(part.len() == 4 && part.bytes().all(|b| b.is_ascii_alphabetic()))
|| (part.len() == 2 && part.bytes().all(|b| b.is_ascii_alphabetic()))
|| (part.len() == 3 && part.bytes().all(|b| b.is_ascii_digit()))
};
if !keep {
if i == 0 {
return None;
}
break;
}
if len + part.len() + usize::from(i > 0) > Self::CAPACITY {
break;
}
if i > 0 {
bytes[len] = b'-';
len += 1;
}
for (j, b) in part.bytes().enumerate() {
bytes[len] = match (i, part.len()) {
(0, _) => b.to_ascii_lowercase(),
(_, 4) if j == 0 => b.to_ascii_uppercase(),
(_, 4) => b.to_ascii_lowercase(),
_ => b.to_ascii_uppercase(),
};
len += 1;
}
}
Some(Locale {
bytes,
len: len as u8,
})
}
pub fn as_str(&self) -> &str {
std::str::from_utf8(&self.bytes[..usize::from(self.len)]).unwrap_or("en-US")
}
}
impl Default for Locale {
fn default() -> Self {
Self::EN_US
}
}
impl std::fmt::Debug for Locale {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Locale({})", self.as_str())
}
}
impl std::fmt::Display for Locale {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HttpRequest {
pub method: String,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: HttpBody,
pub follow_redirects: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum HttpBody {
#[default]
Empty,
Bytes(Vec<u8>),
Stream,
}
impl HttpBody {
pub fn is_empty(&self) -> bool {
match self {
HttpBody::Empty => true,
HttpBody::Bytes(b) => b.is_empty(),
HttpBody::Stream => false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct HttpHead {
pub status: u16,
pub status_text: String,
pub url: String,
pub redirected: bool,
pub headers: Vec<(String, String)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FetchEvent {
BodyWanted,
Head(HttpHead),
Data(Vec<u8>),
End,
Failed(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FetchOp {
Start(HttpRequest),
Send(Vec<u8>),
Finish,
Pause(bool),
Cancel,
}
#[cfg(test)]
mod tests {
use super::Locale;
#[test]
fn locales_parse_platform_identifiers() {
let tag = |s: &str| Locale::parse(s).map(|l| l.as_str().to_owned());
assert_eq!(tag("en_US").as_deref(), Some("en-US"));
assert_eq!(tag("zh-hans_cn").as_deref(), Some("zh-Hans-CN"));
assert_eq!(tag("de_DE@calendar=gregorian").as_deref(), Some("de-DE"));
assert_eq!(tag("pt_BR.UTF-8").as_deref(), Some("pt-BR"));
assert_eq!(tag("es-419").as_deref(), Some("es-419"));
assert_eq!(tag("en-US-u-hc-h23").as_deref(), Some("en-US"));
assert_eq!(tag("C"), None);
assert_eq!(Locale::default().as_str(), "en-US");
}
}