Skip to content

Commit

Permalink
add custom display format for EventModifierBitFlag, printing key symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenesvk committed Nov 14, 2024
1 parent 6d23040 commit 8e8952f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/appkit/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -250,3 +252,37 @@ 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 ) {write!(f,"ƒ")?;}
if self.contains(EventModifierBitFlag::Numpad ) {write!(f,"⇭")?;}
if self.contains(EventModifierBitFlag::Help ) {write!(f,"ℹ")?;}
fmt::Result::Ok(())
}
}
19 changes: 19 additions & 0 deletions src/appkit/event/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[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));
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));
}
}

0 comments on commit 8e8952f

Please sign in to comment.