use dpi::LogicalPosition;
use kurbo::Rect;
use ui_events::keyboard::{Code, Key, KeyState, KeyboardEvent};
use crate::core::TimerKey;
use crate::dpi::PhysicalSize;
use crate::util::Duration;
#[derive(Debug, Clone, PartialEq)]
pub enum WindowEvent {
Rescale(f64),
Resize(PhysicalSize<u32>),
AnimFrame(Duration),
EnableAccessTree,
DisableAccessTree,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TextEvent {
Keyboard(KeyboardEvent),
Ime(Ime),
WindowFocusChange(bool),
ClipboardPaste(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct AccessEvent {
pub action: accesskit::Action,
pub data: Option<accesskit::ActionData>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WindowTheme {
Light,
Dark,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum Update {
WidgetAdded,
DisabledChanged(bool),
StashedChanged(bool),
RequestPanToChild(Rect),
HoveredChanged(bool),
ChildHoveredChanged(bool),
TimerFired(TimerKey),
TooltipRequested(LogicalPosition<f64>),
ActiveChanged(bool),
ChildActiveChanged(bool),
FocusChanged(bool),
ChildFocusChanged(bool),
FontsChanged,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Handled {
Yes,
No,
}
impl TextEvent {
pub fn key_down(key: Key) -> Self {
Self::Keyboard(KeyboardEvent {
state: KeyState::Down,
key,
code: Code::Unidentified,
..Default::default()
})
}
pub fn key_up(key: Key) -> Self {
Self::Keyboard(KeyboardEvent {
state: KeyState::Up,
key,
code: Code::Unidentified,
..Default::default()
})
}
pub fn preedit(text: String) -> Self {
Self::Ime(Ime::Preedit(text, None))
}
pub fn preedit_with_cursor(text: String, selected: String) -> Self {
let Some(offset) = text.find(&selected) else {
panic!("Error building Preedit event: '{selected}' not found in '{text}'");
};
let span = (offset, selected.len());
Self::Ime(Ime::Preedit(text, Some(span)))
}
pub fn short_name(&self) -> &'static str {
match self {
Self::Keyboard(KeyboardEvent { repeat: true, .. }) => "Keyboard(repeat)",
Self::Keyboard(..) => "Keyboard",
Self::Ime(Ime::Disabled) => "Ime::Disabled",
Self::Ime(Ime::Enabled) => "Ime::Enabled",
Self::Ime(Ime::Commit(_)) => "Ime::Commit",
Self::Ime(Ime::Preedit(s, _)) if s.is_empty() => "Ime::Preedit(\"\")",
Self::Ime(Ime::Preedit(_, _)) => "Ime::Preedit(\"...\")",
Self::WindowFocusChange(_) => "WindowFocusChange",
Self::ClipboardPaste(_) => "ClipboardPaste",
}
}
}
impl AccessEvent {
pub fn short_name(&self) -> &'static str {
match self.action {
accesskit::Action::Click => "Click",
accesskit::Action::Focus => "Focus",
accesskit::Action::Blur => "Blur",
accesskit::Action::Collapse => "Collapse",
accesskit::Action::Expand => "Expand",
accesskit::Action::CustomAction => "CustomAction",
accesskit::Action::Decrement => "Decrement",
accesskit::Action::Increment => "Increment",
accesskit::Action::HideTooltip => "HideTooltip",
accesskit::Action::ShowTooltip => "ShowTooltip",
accesskit::Action::ReplaceSelectedText => "ReplaceSelectedText",
accesskit::Action::ScrollDown => "ScrollDown",
accesskit::Action::ScrollLeft => "ScrollLeft",
accesskit::Action::ScrollRight => "ScrollRight",
accesskit::Action::ScrollUp => "ScrollUp",
accesskit::Action::ScrollIntoView => "ScrollIntoView",
accesskit::Action::ScrollToPoint => "ScrollToPoint",
accesskit::Action::SetScrollOffset => "SetScrollOffset",
accesskit::Action::SetTextSelection => "SetTextSelection",
accesskit::Action::SetSequentialFocusNavigationStartingPoint => {
"SetSequentialFocusNavigationStartingPoint"
}
accesskit::Action::SetValue => "SetValue",
accesskit::Action::ShowContextMenu => "ShowContextMenu",
}
}
}
impl Update {
pub fn short_name(&self) -> &str {
match self {
Self::WidgetAdded => "WidgetAdded",
Self::DisabledChanged(false) => "DisabledChanged(false)",
Self::StashedChanged(false) => "StashedChanged(false)",
Self::HoveredChanged(false) => "HoveredChanged(false)",
Self::ChildHoveredChanged(false) => "ChildHoveredChanged(false)",
Self::ActiveChanged(false) => "ActiveChanged(false)",
Self::ChildActiveChanged(false) => "ChildActiveChanged(false)",
Self::FocusChanged(false) => "FocusChanged(false)",
Self::ChildFocusChanged(false) => "ChildFocusChanged(false)",
Self::TooltipRequested(_) => "TooltipRequested(_)",
Self::DisabledChanged(true) => "DisabledChanged(true)",
Self::StashedChanged(true) => "StashedChanged(true)",
Self::HoveredChanged(true) => "HoveredChanged(true)",
Self::ChildHoveredChanged(true) => "ChildHoveredChanged(true)",
Self::ActiveChanged(true) => "ActiveChanged(true)",
Self::ChildActiveChanged(true) => "ChildActiveChanged(true)",
Self::FocusChanged(true) => "FocusChanged(true)",
Self::ChildFocusChanged(true) => "ChildFocusChanged(true)",
Self::RequestPanToChild(_) => "RequestPanToChild(_)",
Self::TimerFired(_) => "TimerFired(_)",
Self::FontsChanged => "FontsChanged",
}
}
}
impl Handled {
pub fn is_handled(self) -> bool {
self == Self::Yes
}
}
impl From<bool> for Handled {
fn from(handled: bool) -> Self {
if handled { Self::Yes } else { Self::No }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Ime {
Enabled,
Preedit(String, Option<(usize, usize)>),
Commit(String),
Disabled,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[expect(missing_docs, reason = "Copied from winit")]
pub enum ResizeDirection {
East,
North,
NorthEast,
NorthWest,
South,
SouthEast,
SouthWest,
West,
}