use crate::types::*;
#[cfg(any(feature = "mock", test))]
pub mod mock;
#[cfg(any(feature = "mock", test))]
pub use mock::Mock as Native;
#[cfg(all(rungui_gtk, not(any(feature = "mock", test))))]
pub mod gtk;
#[cfg(all(rungui_gtk, not(any(feature = "mock", test))))]
pub use gtk::Gtk as Native;
#[cfg(all(rungui_win32, not(any(feature = "mock", test))))]
pub mod win32;
#[cfg(all(rungui_win32, not(any(feature = "mock", test))))]
pub use win32::Win32 as Native;
#[cfg(all(rungui_cocoa, not(any(feature = "mock", test))))]
pub mod cocoa;
#[cfg(all(rungui_cocoa, not(any(feature = "mock", test))))]
pub use cocoa::Cocoa as Native;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Kind {
Window,
Label,
Button,
CheckBox,
RadioButton,
TextInput,
PasswordInput,
TextArea,
ComboBox,
ListBox,
Slider,
ProgressBar,
SpinBox,
Tabs,
Page,
GroupBox,
Image,
MenuBar,
Menu,
MenuItem,
CheckMenuItem,
MenuSeparator,
Table,
Tree,
PopupMenu,
Sash,
HBox,
VBox,
Grid,
Spacer,
Splitter,
}
impl Kind {
pub fn is_native(self) -> bool {
!matches!(
self,
Kind::HBox | Kind::VBox | Kind::Grid | Kind::Spacer | Kind::Splitter
)
}
pub fn in_layout(self) -> bool {
!matches!(
self,
Kind::MenuBar
| Kind::Menu
| Kind::MenuItem
| Kind::CheckMenuItem
| Kind::MenuSeparator
| Kind::PopupMenu
)
}
pub fn is_layout_container(self) -> bool {
matches!(
self,
Kind::Window
| Kind::Page
| Kind::GroupBox
| Kind::HBox
| Kind::VBox
| Kind::Grid
| Kind::Splitter
)
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Prop<'a> {
Text(&'a str),
Tooltip(&'a str),
Placeholder(&'a str),
Enabled(bool),
Visible(bool),
Checked(bool),
Value(f64),
Range {
min: f64,
max: f64,
step: f64,
},
Items(&'a [String]),
Selected(Option<usize>),
Bounds(Rect),
Image(Option<&'a ImageData>),
Accel(&'a str),
ReadOnly(bool),
Indeterminate(bool),
Resizable(bool),
Focus,
Columns(&'a [Column]),
Rows(&'a [Vec<String>]),
Cell {
row: usize,
col: usize,
text: &'a str,
},
SortIndicator(Option<(usize, bool)>),
TreeRows(&'a [TreeRow]),
TreeSelected(Option<u64>),
Orientation(Orientation),
Monospace(bool),
Wrap(bool),
Position {
x: i32,
y: i32,
},
MinSize(Size),
}
impl Prop<'_> {
pub fn applies_to(&self, kind: Kind) -> bool {
use Kind::*;
let text_input = matches!(kind, TextInput | PasswordInput | TextArea);
let range = matches!(kind, Slider | SpinBox | ProgressBar);
match self {
Prop::Text(_) => {
text_input
|| matches!(
kind,
Window
| Label
| Button
| CheckBox
| RadioButton
| Page
| GroupBox
| MenuItem
| CheckMenuItem
| Menu
)
}
Prop::Placeholder(_) => matches!(kind, TextInput | PasswordInput),
Prop::Checked(_) => matches!(kind, CheckBox | RadioButton | CheckMenuItem),
Prop::Value(_) | Prop::Range { .. } => range,
Prop::Items(_) => matches!(kind, ComboBox | ListBox),
Prop::Selected(_) => matches!(kind, ComboBox | ListBox | Tabs | Table),
Prop::Image(_) => kind == Image,
Prop::Accel(_) => matches!(kind, MenuItem | CheckMenuItem),
Prop::ReadOnly(_) | Prop::Monospace(_) => text_input,
Prop::Indeterminate(_) => kind == ProgressBar,
Prop::Resizable(_) | Prop::Position { .. } | Prop::MinSize(_) => kind == Window,
Prop::Columns(_) | Prop::Rows(_) | Prop::Cell { .. } | Prop::SortIndicator(_) => {
kind == Table
}
Prop::TreeRows(_) | Prop::TreeSelected(_) => kind == Tree,
Prop::Orientation(_) => kind == Sash,
Prop::Wrap(_) => kind == TextArea,
Prop::Visible(_) => kind != PopupMenu,
_ => true,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum SashKey {
Prev,
Next,
PrevLarge,
NextLarge,
Min,
Max,
}
#[derive(Clone, PartialEq, Debug)]
#[non_exhaustive]
pub enum Event {
Click,
Text(String),
Toggled(bool),
Selected(Option<usize>),
Value(f64),
Activated(usize),
Resized {
w: i32,
h: i32,
},
Focus(bool),
ColumnClicked(usize),
TreeSelected(Option<u64>),
TreeActivated(u64),
TreeExpanded(u64, bool),
ContextMenu {
x: i32,
y: i32,
},
SashDragged(i32),
SashKey(SashKey),
Moved {
x: i32,
y: i32,
},
}
pub trait Backend {
fn init(app_name: &str) -> Result<()>;
fn run();
fn quit();
fn wake();
fn timer_start(token: u64, millis: u32, repeat: bool) -> Result<()>;
fn timer_stop(token: u64);
fn create(id: WidgetId, kind: Kind, parent: Option<WidgetId>) -> Result<()>;
fn destroy(id: WidgetId);
fn set(id: WidgetId, prop: &Prop);
fn preferred_size(id: WidgetId) -> Size;
fn chrome(_id: WidgetId) -> Size {
Size::default()
}
fn native_handle(id: WidgetId) -> Option<NativeHandle>;
fn message_box(parent: Option<WidgetId>, spec: &MessageSpec) -> Answer;
fn file_dialog(parent: Option<WidgetId>, spec: &FileSpec) -> Vec<String>;
fn popup_menu(_menu: WidgetId, _parent_window: Option<WidgetId>, _at: Option<(i32, i32)>) {}
fn a11y_changed(_window: WidgetId) {}
}