use std::io;
use iced::{Point, Task, window};
use plushie_widget_sdk::protocol::OutgoingEvent;
use plushie_widget_sdk::protocol::OutgoingEventKeyExt;
use plushie_widget_sdk::runtime::{
KeyEventData, Message, serialize_modifiers, serialize_mouse_button, serialize_scroll_delta,
};
use crate::App;
use crate::constants::*;
fn path_to_string(path: std::path::PathBuf) -> String {
match path.to_str() {
Some(s) => s.to_string(),
None => {
log::warn!(
"file path contains non-UTF-8 bytes, using lossy conversion: {}",
path.display()
);
path.to_string_lossy().into_owned()
}
}
}
impl App {
fn resolve_window_id(&self, iced_id: &window::Id) -> Option<&str> {
let resolved = self.windows.get_window_id(iced_id);
if resolved.is_none() {
log::debug!("event for unknown iced window {:?}, suppressing", iced_id);
}
resolved
}
pub fn handle_key_pressed(&self, data: KeyEventData, iced_id: window::Id) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_KEY_PRESS, Some(window_id), data.captured, |tag| {
OutgoingEvent::key_press(tag, &data)
})
}
pub fn handle_key_released(&self, data: KeyEventData, iced_id: window::Id) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_KEY_RELEASE, Some(window_id), data.captured, |tag| {
OutgoingEvent::key_release(tag, &data)
})
}
pub fn handle_modifiers_changed(
&mut self,
mods: iced::keyboard::Modifiers,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
self.current_modifiers = mods;
let Some(window_id) = self.windows.get_window_id(&iced_id) else {
log::debug!("event for unknown iced window {:?}, suppressing", iced_id);
return Task::none();
};
crate::app::coalesce_subscription_into(
&self.core,
&mut self.emitter,
SUB_MODIFIERS_CHANGED,
Some(window_id),
captured,
|tag| OutgoingEvent::modifiers_changed(tag, serialize_modifiers(mods)),
)
}
pub fn handle_cursor_moved(
&mut self,
pos: Point,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.windows.get_window_id(&iced_id) else {
log::debug!("event for unknown iced window {:?}, suppressing", iced_id);
return Task::none();
};
crate::app::coalesce_subscription_into(
&self.core,
&mut self.emitter,
SUB_POINTER_MOVE,
Some(window_id),
captured,
|tag| OutgoingEvent::cursor_moved(tag, pos.x, pos.y),
)
}
pub fn handle_cursor_entered(&self, iced_id: window::Id, captured: bool) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_MOVE, Some(window_id), captured, |tag| {
OutgoingEvent::cursor_entered(tag)
})
}
pub fn handle_cursor_left(&self, iced_id: window::Id, captured: bool) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_MOVE, Some(window_id), captured, |tag| {
OutgoingEvent::cursor_left(tag)
})
}
pub fn handle_mouse_button_pressed(
&self,
button: iced::mouse::Button,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_BUTTON, Some(window_id), captured, |tag| {
OutgoingEvent::button_pressed(tag, serialize_mouse_button(&button))
})
}
pub fn handle_mouse_button_released(
&self,
button: iced::mouse::Button,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_BUTTON, Some(window_id), captured, |tag| {
OutgoingEvent::button_released(tag, serialize_mouse_button(&button))
})
}
pub fn handle_wheel_scrolled(
&mut self,
delta: iced::mouse::ScrollDelta,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.windows.get_window_id(&iced_id) else {
log::debug!("event for unknown iced window {:?}, suppressing", iced_id);
return Task::none();
};
crate::app::coalesce_subscription_into(
&self.core,
&mut self.emitter,
SUB_POINTER_SCROLL,
Some(window_id),
captured,
|tag| {
let (dx, dy, unit) = serialize_scroll_delta(&delta);
OutgoingEvent::wheel_scrolled(tag, dx, dy, unit)
},
)
}
pub fn handle_finger_pressed(
&self,
finger: iced::touch::Finger,
pos: Point,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_TOUCH, Some(window_id), captured, |tag| {
OutgoingEvent::finger_pressed(tag, finger.0, pos.x, pos.y)
})
}
pub fn handle_finger_moved(
&mut self,
finger: iced::touch::Finger,
pos: Point,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.windows.get_window_id(&iced_id) else {
log::debug!("event for unknown iced window {:?}, suppressing", iced_id);
return Task::none();
};
crate::app::coalesce_subscription_into(
&self.core,
&mut self.emitter,
SUB_POINTER_TOUCH,
Some(window_id),
captured,
|tag| OutgoingEvent::finger_moved(tag, finger.0, pos.x, pos.y),
)
}
pub fn handle_finger_lifted(
&self,
finger: iced::touch::Finger,
pos: Point,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_TOUCH, Some(window_id), captured, |tag| {
OutgoingEvent::finger_lifted(tag, finger.0, pos.x, pos.y)
})
}
pub fn handle_finger_lost(
&self,
finger: iced::touch::Finger,
pos: Point,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_POINTER_TOUCH, Some(window_id), captured, |tag| {
OutgoingEvent::finger_lost(tag, finger.0, pos.x, pos.y)
})
}
pub fn handle_ime_opened(&self, iced_id: window::Id, captured: bool) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_IME, Some(window_id), captured, |tag| {
OutgoingEvent::ime_opened(tag)
})
}
pub fn handle_ime_preedit(
&self,
text: String,
cursor: Option<std::ops::Range<usize>>,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_IME, Some(window_id), captured, |tag| {
OutgoingEvent::ime_preedit(tag, text.as_str(), cursor.clone())
})
}
pub fn handle_ime_commit(
&self,
text: String,
iced_id: window::Id,
captured: bool,
) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_IME, Some(window_id), captured, |tag| {
OutgoingEvent::ime_commit(tag, text.as_str())
})
}
pub fn handle_ime_closed(&self, iced_id: window::Id, captured: bool) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id) else {
return Task::none();
};
self.emit_subscription_for_window(SUB_IME, Some(window_id), captured, |tag| {
OutgoingEvent::ime_closed(tag)
})
}
fn emit_window_event(
&self,
specific_key: Option<&str>,
event_fn: impl Fn(&str, &str) -> OutgoingEvent,
window_id: String,
) -> io::Result<()> {
let wid = Some(window_id.as_str());
for entry in self.core.matching_entries(SUB_WINDOW_EVENT, wid) {
self.emitter
.emit_event(event_fn(entry.tag.as_str(), window_id.as_str()))?;
}
if let Some(key) = specific_key {
for entry in self.core.matching_entries(key, wid) {
self.emitter
.emit_event(event_fn(entry.tag.as_str(), window_id.as_str()))?;
}
}
Ok(())
}
pub fn handle_window_event(&self, iced_id: window::Id, evt: window::Event) -> Task<Message> {
let Some(window_id) = self.resolve_window_id(&iced_id).map(str::to_string) else {
return Task::none();
};
let result: io::Result<()> = (|| {
match evt {
window::Event::Opened {
position,
size,
scale_factor,
} => {
let wid = Some(window_id.as_str());
let pos = position.map(|p| (p.x, p.y));
for entry in self.core.matching_entries(SUB_WINDOW_EVENT, wid) {
self.emitter.emit_event(OutgoingEvent::window_opened(
entry.tag.as_str(),
window_id.as_str(),
pos,
size.width,
size.height,
scale_factor,
))?;
}
for entry in self.core.matching_entries(SUB_WINDOW_OPEN, wid) {
self.emitter.emit_event(OutgoingEvent::window_opened(
entry.tag.as_str(),
window_id.as_str(),
pos,
size.width,
size.height,
scale_factor,
))?;
}
}
window::Event::Closed => {
let wid = Some(window_id.as_str());
for entry in self.core.matching_entries(SUB_WINDOW_EVENT, wid) {
self.emitter.emit_event(OutgoingEvent::window_closed(
entry.tag.as_str(),
window_id.as_str(),
))?;
}
}
window::Event::Moved(point) => {
self.emit_window_event(
Some(SUB_WINDOW_MOVE),
|tag, jid| OutgoingEvent::window_moved(tag, jid, point.x, point.y),
window_id,
)?;
}
window::Event::Resized(size) => {
self.emit_window_event(
Some(SUB_WINDOW_RESIZE),
|tag, jid| OutgoingEvent::window_resized(tag, jid, size.width, size.height),
window_id,
)?;
}
window::Event::Rescaled(factor) => {
let wid = Some(window_id.as_str());
for entry in self.core.matching_entries(SUB_WINDOW_EVENT, wid) {
self.emitter.emit_event(OutgoingEvent::window_rescaled(
entry.tag.as_str(),
window_id.as_str(),
factor,
))?;
}
}
window::Event::Focused => {
self.emit_window_event(
Some(SUB_WINDOW_FOCUS),
|tag, jid| OutgoingEvent::window_focused(tag, jid),
window_id,
)?;
}
window::Event::Unfocused => {
self.emit_window_event(
Some(SUB_WINDOW_UNFOCUS),
|tag, jid| OutgoingEvent::window_unfocused(tag, jid),
window_id,
)?;
}
window::Event::FileHovered(path) => {
let wid = Some(window_id.as_str());
let path_str = path_to_string(path);
for entry in self.core.matching_entries(SUB_FILE_DROP, wid) {
self.emitter.emit_event(OutgoingEvent::file_hovered(
entry.tag.as_str(),
window_id.as_str(),
path_str.as_str(),
))?;
}
}
window::Event::FileDropped(path) => {
let wid = Some(window_id.as_str());
let path_str = path_to_string(path);
for entry in self.core.matching_entries(SUB_FILE_DROP, wid) {
self.emitter.emit_event(OutgoingEvent::file_dropped(
entry.tag.as_str(),
window_id.as_str(),
path_str.as_str(),
))?;
}
}
window::Event::FilesHoveredLeft => {
let wid = Some(window_id.as_str());
for entry in self.core.matching_entries(SUB_FILE_DROP, wid) {
self.emitter.emit_event(OutgoingEvent::files_hovered_left(
entry.tag.as_str(),
window_id.as_str(),
))?;
}
}
window::Event::CloseRequested => {
}
window::Event::RedrawRequested(_) => {
}
}
Ok(())
})();
if let Err(e) = result {
log::error!("write error: {e}");
return iced::exit();
}
Task::none()
}
}