use objc2_ui_kit::{UIEvent, UIKey, UIKeyModifierFlags, UIPress, UIPressPhase, UITouch, UITouchPhase}; use crate::win::Factor; use super::{Platform, WinRef}; pub struct WinEvent<'a>(pub &'a UIEvent); impl<'a> types::IntoEvt for WinEvent<'a> { fn to_evt(&self, _: WinRef<'_>) -> Option { // NOT SURPORT None } } impl<'a> types::IntoEvt for WinEvent<'a> { fn to_evt(&self, _win: WinRef<'_>) -> Option { // NOT SURPORT None } } pub struct WinKey<'a>(pub &'a UIKey, pub &'a UIPress); impl<'a> types::IntoEvt for WinKey<'a> { fn to_evt(&self, _: WinRef<'_>) -> Option { let press = self.1; let evt = self.0; let state = match unsafe { press.phase() } { UIPressPhase::Began => types::State::Down, UIPressPhase::Ended => types::State::Up, _ => return None, }; let code = conv_code(evt); let key = conv_key(evt, code); let location = code.into(); let metas = conv_metas(evt); let repeat = false; let composing = false; Some(types::EvtKey::new(key, code, state, location, metas, repeat, composing)) } } pub struct WinTouch<'a>(pub u8, pub &'a UITouch); impl<'a> types::IntoEvt for WinTouch<'a> { fn to_evt(&self, win: WinRef<'_>) -> Option { let point = { let point = self.1.locationInView(None); let factor = Factor::from(win.1); (factor.to_f32(point.x), factor.to_f32(point.y)) }; let metas = types::Metas::EMPTY; let phase = match self.1.phase() { UITouchPhase::Began => types::Phase::Start, UITouchPhase::Moved => types::Phase::Update, UITouchPhase::Ended => types::Phase::End, UITouchPhase::Cancelled => types::Phase::Cancel, _ => return None, }; let mouse = types::EvtMouse::new(types::Button::Touch(self.0), phase, point, metas); let force = 0; Some(types::EvtTouch::new(mouse, force)) } } fn conv_metas(evt: &UIKey) -> types::Metas { let mut metas = types::Metas::EMPTY; let flags = unsafe { evt.modifierFlags() }; use UIKeyModifierFlags as F; if flags.contains(F::UIKeyModifierShift) { metas.insert(types::Metas::SHIFT); } if flags.contains(F::UIKeyModifierControl) { metas.insert(types::Metas::CONTROL); } if flags.contains(F::UIKeyModifierAlternate) { metas.insert(types::Metas::ALT); } if flags.contains(F::UIKeyModifierCommand) { metas.insert(types::Metas::META); } metas } fn conv_key(evt: &UIKey, code: types::Code) -> types::Key { let chars = unsafe { evt.characters() }; if chars.length() > 0 { return types::Key::Character(chars.to_string().into()); } let chars = unsafe { evt.charactersIgnoringModifiers() }; if chars.length() > 0 { return types::Key::Character(chars.to_string().into()); } code.into() } fn conv_code(evt: &UIKey) -> types::Code { let scancode = unsafe { evt.keyCode() }; use types::Code as KeyCode; // TODO KeyCode::Unidentified(scancode.0 as _) }