use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct WidgetId(pub u64);
impl WidgetId {
pub const DEAD: WidgetId = WidgetId(0);
}
pub(crate) const MAX_PX: i32 = i16::MAX as i32;
pub(crate) const MAX_WINDOW_PX: i32 = 1 << 14;
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Rect {
pub x: i32,
pub y: i32,
pub w: i32,
pub h: i32,
}
impl Rect {
pub const fn new(x: i32, y: i32, w: i32, h: i32) -> Rect {
Rect { x, y, w, h }
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Size {
pub w: i32,
pub h: i32,
}
impl Size {
pub const fn new(w: i32, h: i32) -> Size {
Size { w, h }
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Date {
pub year: i32,
pub month: u32,
pub day: u32,
}
impl Date {
pub fn new(year: i32, month: u32, day: u32) -> Option<Date> {
let d = Date { year, month, day };
d.is_valid().then_some(d)
}
pub fn is_valid(&self) -> bool {
(1753..=9999).contains(&self.year)
&& (1..=12).contains(&self.month)
&& (1..=days_in_month(self.year, self.month)).contains(&self.day)
}
pub fn today() -> Date {
crate::core::today()
}
pub(crate) fn from_unix_utc(secs: i64) -> Date {
let z = secs.div_euclid(86_400) + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
let year = (yoe + era * 400 + i64::from(month <= 2)) as i32;
Date { year: year.clamp(1753, 9999), month, day }
}
}
pub(crate) fn days_in_month(year: i32, month: u32) -> u32 {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 => 29,
2 => 28,
_ => 0,
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Error {
Unsupported,
AlreadyInitialized,
NotInitialized,
InvalidHandle,
LimitExceeded,
Backend(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Unsupported => write!(f, "not supported by this backend"),
Error::AlreadyInitialized => write!(f, "rungui already initialized on this thread"),
Error::NotInitialized => write!(f, "rungui not initialized on this thread"),
Error::InvalidHandle => write!(f, "invalid or stale widget handle"),
Error::LimitExceeded => write!(f, "widget nesting limit exceeded"),
Error::Backend(s) => write!(f, "backend error: {s}"),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum NativeHandle {
Gtk(usize),
Win32(usize),
Cocoa(usize),
}
impl NativeHandle {
pub fn ptr(self) -> usize {
match self {
NativeHandle::Gtk(p) | NativeHandle::Win32(p) | NativeHandle::Cocoa(p) => p,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum Align {
Start,
Center,
End,
#[default]
Fill,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct ImageData {
pub w: u32,
pub h: u32,
pub rgba: Vec<u8>,
}
pub const MAX_IMAGE_EDGE: u32 = 1 << 15;
impl ImageData {
pub fn is_valid(&self) -> bool {
(1..=MAX_IMAGE_EDGE).contains(&self.w)
&& (1..=MAX_IMAGE_EDGE).contains(&self.h)
&& self.rgba.len() as u64 == u64::from(self.w) * u64::from(self.h) * 4
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum MessageKind {
Info,
Warning,
Error,
Question,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Buttons {
Ok,
OkCancel,
YesNo,
YesNoCancel,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Answer {
Ok,
Cancel,
Yes,
No,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum FileMode {
Open,
OpenMany,
Save,
PickFolder,
}
#[derive(Clone, Debug)]
pub struct MessageSpec {
pub kind: MessageKind,
pub buttons: Buttons,
pub title: String,
pub text: String,
}
#[derive(Clone, Debug)]
pub struct FileSpec {
pub mode: FileMode,
pub title: String,
pub filters: Vec<(String, Vec<String>)>,
pub initial_dir: Option<String>,
pub initial_name: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Accel {
pub ctrl: bool,
pub shift: bool,
pub alt: bool,
pub key: String,
}
impl Accel {
pub fn parse(s: &str) -> Option<Accel> {
let mut a = Accel::default();
for part in s.split('+').map(str::trim).filter(|p| !p.is_empty()) {
match part.to_ascii_lowercase().as_str() {
"ctrl" | "control" | "cmd" | "command" | "meta" | "super" => a.ctrl = true,
"shift" => a.shift = true,
"alt" | "option" | "opt" => a.alt = true,
_ => a.key = part.to_uppercase(),
}
}
if a.key.is_empty() { None } else { Some(a) }
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum ColumnAlign {
#[default]
Left,
Center,
Right,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Column {
pub title: String,
pub width: i32,
pub align: ColumnAlign,
pub sortable: bool,
}
impl Column {
pub fn new(title: &str) -> Column {
Column {
title: title.to_string(),
width: 100,
align: ColumnAlign::Left,
sortable: false,
}
}
pub fn width(mut self, w: i32) -> Self {
self.width = w.clamp(1, MAX_PX);
self
}
pub fn align(mut self, a: ColumnAlign) -> Self {
self.align = a;
self
}
pub fn sortable(mut self, s: bool) -> Self {
self.sortable = s;
self
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct TreeNodeId(pub u64);
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TreeRow {
pub node: u64,
pub depth: u32,
pub text: String,
pub expanded: bool,
pub has_children: bool,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum Orientation {
#[default]
Horizontal,
Vertical,
}