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(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,
}