From a620a952eec88fc550d38c9406de1a2ac4949e08 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Thu, 14 Nov 2024 20:29:02 +0700 Subject: [PATCH] add custom display format for EventModifierBitFlag, printing key symbols --- src/appkit/event/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/appkit/event/test.rs | 21 +++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/appkit/event/test.rs diff --git a/src/appkit/event/mod.rs b/src/appkit/event/mod.rs index cd929af..342c156 100644 --- a/src/appkit/event/mod.rs +++ b/src/appkit/event/mod.rs @@ -8,6 +8,8 @@ use objc::{class, msg_send, msg_send_id, sel}; use crate::events::EventType; use crate::foundation::{id, nil, NSInteger, NSPoint, NSString}; +mod test; + /// An EventMask describes the type of event. #[bitmask(u64)] pub enum EventMask { @@ -250,3 +252,41 @@ pub enum EventModifierBitFlag { Function = 1 << 23, DeviceIndependentFlagsMask = 0xffff0000, } + +use std::fmt; +impl fmt::Display for EventModifierBitFlag { + fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result { + // key combos with flagmasks don't make sense? so just print the mask and return, ignoring other bits + if self.contains(EventModifierBitFlag::DeviceIndependentFlagsMask) {write!(f,"!🖮")?;return fmt::Result::Ok(())}; + + if self.contains(EventModifierBitFlag::CapsLock ) {write!(f,"⇪")?;} + if self.contains(EventModifierBitFlag::Shift ) {write!(f,"‹⇧›")?; + } else { + if self.contains(EventModifierBitFlag::LeftShift ) {write!(f,"‹⇧")?;} + if self.contains(EventModifierBitFlag::RightShift ) {write!(f,"⇧›")?;} + } ; + if self.contains(EventModifierBitFlag::Control ) {write!(f,"‹⌃›")?; + } else { + if self.contains(EventModifierBitFlag::LeftControl ) {write!(f,"‹⌃")?;} + if self.contains(EventModifierBitFlag::RightControl ) {write!(f,"⌃›")?;} + } ; + if self.contains(EventModifierBitFlag::Option ) {write!(f,"‹⌥›")?; + } else { + if self.contains(EventModifierBitFlag::LeftOption ) {write!(f,"‹⌥")?;} + if self.contains(EventModifierBitFlag::RightOption ) {write!(f,"⌥›")?;} + } ; + if self.contains(EventModifierBitFlag::Command ) {write!(f,"‹⌘›")?; + } else { + if self.contains(EventModifierBitFlag::LeftCommand ) {write!(f,"‹⌘")?;} + if self.contains(EventModifierBitFlag::RightCommand ) {write!(f,"⌘›")?;} + } ; + if self.contains(EventModifierBitFlag::Function ) { + if f.alternate() {write!(f,"🌐")?; // when it's a modifier key + } else {write!(f,"ƒ")?;}} + if self.contains(EventModifierBitFlag::Numpad ) { + if f.alternate() {write!(f,"⇭")?; // when it's a modifier key + } else {write!(f,"🔢")?;}} + if self.contains(EventModifierBitFlag::Help ) {write!(f,"ℹ")?;} + fmt::Result::Ok(()) + } +} diff --git a/src/appkit/event/test.rs b/src/appkit/event/test.rs new file mode 100644 index 0000000..02921b2 --- /dev/null +++ b/src/appkit/event/test.rs @@ -0,0 +1,21 @@ +#[cfg(test)] +mod event_test { + use crate::appkit::EventModifierBitFlag; + #[test] + fn test_event_modifier_bit_flag_display() { + assert_eq!("‹⇧" , format!("{}",EventModifierBitFlag::LeftShift)); + assert_eq!("‹⇧›" , format!("{}",EventModifierBitFlag::Shift)); + assert_eq!("⇧›" , format!("{}",EventModifierBitFlag::RightShift)); + assert_eq!("‹⇧‹⌃‹⌥‹⌘" , format!("{}",EventModifierBitFlag::LeftModi)); + assert_eq!("⇧›⌃›⌥›⌘›" , format!("{}",EventModifierBitFlag::RightModi)); + assert_eq!("‹⇧›‹⌃›‹⌥›‹⌘›" , format!("{}",EventModifierBitFlag::LRModi)); + assert_eq!("🌐" , format!("{:#}",EventModifierBitFlag::Function)); // when it's a modifier key + assert_eq!("ƒ" , format!("{}",EventModifierBitFlag::Function)); + assert_eq!("⇭" , format!("{:#}",EventModifierBitFlag::Numpad)); // when it's a modifier key + assert_eq!("🔢" , format!("{}",EventModifierBitFlag::Numpad)); + assert_eq!("ℹ" , format!("{}",EventModifierBitFlag::Help)); + assert_eq!("!🖮" , format!("{}",EventModifierBitFlag::DeviceIndependentFlagsMask)); + // Shift ignored, only flagmask remains + assert_eq!("!🖮" , format!("{}",EventModifierBitFlag::DeviceIndependentFlagsMask | EventModifierBitFlag::RightShift)); + } +}