use iced::widget::markdown;
use iced::widget::text_editor;
use iced::{Point, window};
use serde_json::Value;
use crate::protocol::{KeyModifiers, OutgoingEvent};
#[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 {
TextEditorAction(String, String, text_editor::Action),
MarkdownUrl(markdown::Uri),
Stdin(StdinEvent),
NoOp,
TimerTick(String),
KeyPressed(KeyEventData, window::Id),
KeyReleased(KeyEventData, window::Id),
ModifiersChanged(iced::keyboard::Modifiers, window::Id, bool),
ImeOpened(window::Id, bool),
ImePreedit(String, Option<std::ops::Range<usize>>, window::Id, bool),
ImeCommit(String, window::Id, bool),
ImeClosed(window::Id, 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),
CanvasElementFocusChanged {
window_id: String,
old_element_id: Option<String>,
new_element_id: Option<String>,
},
Diagnostic {
window_id: String,
canvas_id: String,
element_id: Option<String>,
level: String,
code: String,
message: String,
},
PaneResized(String, String, iced::widget::pane_grid::ResizeEvent),
PaneDragged(String, String, iced::widget::pane_grid::DragEvent),
PaneClicked(String, String, iced::widget::pane_grid::Pane),
PaneFocusCycle(String, String, iced::widget::pane_grid::Pane),
Event {
window_id: String,
id: String,
value: Value,
family: String,
},
FlushCoalesce,
}
impl Message {
pub fn node_id(&self) -> Option<&str> {
match self {
Message::TextEditorAction(_, id, ..) => Some(id),
Message::CanvasElementFocusChanged {
old_element_id,
new_element_id,
..
} => new_element_id.as_deref().or(old_element_id.as_deref()),
Message::PaneResized(_, grid_id, ..)
| Message::PaneDragged(_, grid_id, ..)
| Message::PaneClicked(_, grid_id, ..)
| Message::PaneFocusCycle(_, grid_id, ..) => Some(grid_id),
Message::Event { id, .. } => Some(id),
Message::Diagnostic { canvas_id, .. } => Some(canvas_id),
_ => None,
}
}
pub fn to_outgoing_event(&self) -> Option<OutgoingEvent> {
match self {
Message::CanvasElementFocusChanged { .. } => None,
Message::Diagnostic {
canvas_id,
element_id,
level,
code,
message,
..
} => Some(OutgoingEvent::diagnostic(
canvas_id.clone(),
element_id.clone(),
level,
code,
message,
)),
_ => None,
}
}
}
#[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"),
}
}