use iced::widget::markdown;
use iced::widget::text_editor;
use iced::{Point, window};
use serde_json::Value;
use crate::protocol::KeyModifiers;
#[derive(Debug, Clone, Copy)]
pub struct ScrollViewport {
pub absolute_x: f32,
pub absolute_y: f32,
pub relative_x: f32,
pub relative_y: f32,
pub content_width: f32,
pub content_height: f32,
pub viewport_width: f32,
pub viewport_height: f32,
}
#[derive(Debug, Clone)]
pub struct KeyEventData {
pub key: iced::keyboard::Key,
pub modified_key: iced::keyboard::Key,
pub physical_key: iced::keyboard::key::Physical,
pub location: iced::keyboard::Location,
pub modifiers: iced::keyboard::Modifiers,
pub text: Option<String>,
pub repeat: bool,
pub captured: bool,
}
#[derive(Debug, Clone)]
pub enum Message {
Click(String),
Input(String, String),
Submit(String, String),
Toggle(String, bool),
Slide(String, f64),
SlideRelease(String),
Select(String, String),
TextEditorAction(String, text_editor::Action),
MarkdownUrl(markdown::Uri),
Stdin(StdinEvent),
NoOp,
KeyPressed(KeyEventData),
KeyReleased(KeyEventData),
ModifiersChanged(iced::keyboard::Modifiers, bool),
ImeOpened(bool),
ImePreedit(String, Option<std::ops::Range<usize>>, bool),
ImeCommit(String, bool),
ImeClosed(bool),
WindowCloseRequested(window::Id),
WindowClosed(window::Id),
WindowOpened(window::Id, String),
CursorMoved(Point, window::Id, bool),
CursorEntered(window::Id, bool),
CursorLeft(window::Id, bool),
MouseButtonPressed(iced::mouse::Button, window::Id, bool),
MouseButtonReleased(iced::mouse::Button, window::Id, bool),
WheelScrolled(iced::mouse::ScrollDelta, window::Id, bool),
FingerPressed(iced::touch::Finger, Point, window::Id, bool),
FingerMoved(iced::touch::Finger, Point, window::Id, bool),
FingerLifted(iced::touch::Finger, Point, window::Id, bool),
FingerLost(iced::touch::Finger, Point, window::Id, bool),
WindowEvent(window::Id, window::Event),
AnimationFrame(iced::time::Instant),
ThemeChanged(iced::theme::Mode),
SensorResize(String, f32, f32),
CanvasEvent {
id: String,
kind: String,
x: f32,
y: f32,
extra: String,
},
CanvasScroll {
id: String,
x: f32,
y: f32,
delta_x: f32,
delta_y: f32,
},
CanvasShapeEnter {
canvas_id: String,
shape_id: String,
x: f32,
y: f32,
},
CanvasShapeLeave { canvas_id: String, shape_id: String },
CanvasShapeClick {
canvas_id: String,
shape_id: String,
x: f32,
y: f32,
button: String,
},
CanvasShapeDrag {
canvas_id: String,
shape_id: String,
x: f32,
y: f32,
delta_x: f32,
delta_y: f32,
},
CanvasShapeDragEnd {
canvas_id: String,
shape_id: String,
x: f32,
y: f32,
},
CanvasShapeFocused { canvas_id: String, shape_id: String },
PaneResized(String, iced::widget::pane_grid::ResizeEvent),
PaneDragged(String, iced::widget::pane_grid::DragEvent),
PaneClicked(String, iced::widget::pane_grid::Pane),
PaneFocusCycle(String, iced::widget::pane_grid::Pane),
ScrollEvent(String, ScrollViewport),
Paste(String, String),
OptionHovered(String, String),
MouseAreaEvent(String, String),
MouseAreaMove(String, f32, f32),
MouseAreaScroll(String, f32, f32),
Event {
id: String,
data: Value,
family: String,
},
FlushCoalesce,
}
impl Message {
pub fn widget_event(id: impl Into<String>, family: impl Into<String>, data: Value) -> Self {
Message::Event {
id: id.into(),
family: family.into(),
data,
}
}
}
#[derive(Debug, Clone)]
pub enum StdinEvent {
Message(crate::protocol::IncomingMessage),
Closed,
Warning(String),
}
pub fn serialize_key(key: &iced::keyboard::Key) -> String {
match key {
iced::keyboard::Key::Named(named) => format!("{named:?}"),
iced::keyboard::Key::Character(c) => c.to_string(),
iced::keyboard::Key::Unidentified => "Unidentified".to_string(),
}
}
pub fn serialize_modifiers(mods: iced::keyboard::Modifiers) -> KeyModifiers {
KeyModifiers {
shift: mods.shift(),
ctrl: mods.control(),
alt: mods.alt(),
logo: mods.logo(),
command: mods.command(),
}
}
pub fn serialize_physical_key(physical: &iced::keyboard::key::Physical) -> String {
match physical {
iced::keyboard::key::Physical::Code(code) => format!("{code:?}"),
iced::keyboard::key::Physical::Unidentified(code) => {
format!("Unidentified({code:?})")
}
}
}
pub fn serialize_location(location: &iced::keyboard::Location) -> &'static str {
match location {
iced::keyboard::Location::Standard => "standard",
iced::keyboard::Location::Left => "left",
iced::keyboard::Location::Right => "right",
iced::keyboard::Location::Numpad => "numpad",
}
}
pub fn serialize_mouse_button(button: &iced::mouse::Button) -> String {
match button {
iced::mouse::Button::Left => "left".to_string(),
iced::mouse::Button::Right => "right".to_string(),
iced::mouse::Button::Middle => "middle".to_string(),
iced::mouse::Button::Back => "back".to_string(),
iced::mouse::Button::Forward => "forward".to_string(),
iced::mouse::Button::Other(n) => format!("other_{n}"),
}
}
pub fn serialize_scroll_delta(delta: &iced::mouse::ScrollDelta) -> (f32, f32, &'static str) {
match delta {
iced::mouse::ScrollDelta::Lines { x, y } => (*x, *y, "lines"),
iced::mouse::ScrollDelta::Pixels { x, y } => (*x, *y, "pixels"),
}
}