Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Add keyval_to/is_upper/lower functions to Key struct
Browse files Browse the repository at this point in the history
Also `From<u32> for Key`
  • Loading branch information
Amjad50 committed Sep 30, 2020
1 parent 44fc03e commit 2038357
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
45 changes: 33 additions & 12 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ use glib::translate::*;
use glib::GString;
use libc::c_uint;

pub fn keyval_name(keyval: u32) -> Option<GString> {
skip_assert_initialized!();
unsafe { from_glib_none(gdk_sys::gdk_keyval_name(keyval as c_uint)) }
}

pub fn keyval_to_unicode(keyval: u32) -> Option<char> {
skip_assert_initialized!();
unsafe { ::std::char::from_u32(gdk_sys::gdk_keyval_to_unicode(keyval)).filter(|x| *x != '\0') }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Key(u32);

Expand All @@ -34,6 +24,13 @@ impl ::std::ops::DerefMut for Key {
}
}

impl ::std::convert::From<u32> for Key {
fn from(value: u32) -> Self {
skip_assert_initialized!();
Key(value)
}
}

impl FromGlib<u32> for Key {
fn from_glib(value: u32) -> Self {
skip_assert_initialized!();
Expand All @@ -51,11 +48,35 @@ impl ToGlib for Key {

impl Key {
pub fn to_unicode(&self) -> Option<char> {
keyval_to_unicode(**self)
skip_assert_initialized!();
unsafe {
::std::char::from_u32(gdk_sys::gdk_keyval_to_unicode(**self)).filter(|x| *x != '\0')
}
}

pub fn name(&self) -> Option<GString> {
keyval_name(**self)
skip_assert_initialized!();
unsafe { from_glib_none(gdk_sys::gdk_keyval_name(**self as c_uint)) }
}

pub fn is_upper(&self) -> bool {
skip_assert_initialized!();
unsafe { from_glib(gdk_sys::gdk_keyval_is_upper(**self)) }
}

pub fn is_lower(&self) -> bool {
skip_assert_initialized!();
unsafe { from_glib(gdk_sys::gdk_keyval_is_lower(**self)) }
}

pub fn to_upper(&self) -> Self {
skip_assert_initialized!();
unsafe { gdk_sys::gdk_keyval_to_upper(**self) }.into()
}

pub fn to_lower(&self) -> Self {
skip_assert_initialized!();
unsafe { gdk_sys::gdk_keyval_to_lower(**self) }.into()
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ pub use window::WindowAttr;
#[allow(non_camel_case_types)]
pub type key = i32;

pub use self::keys::{keyval_name, keyval_to_unicode};

/// The primary button. This is typically the left mouse button, or the right button in a left-handed setup.
pub const BUTTON_PRIMARY: u32 = gdk_sys::GDK_BUTTON_PRIMARY as u32;

Expand Down
12 changes: 4 additions & 8 deletions tests/check_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ fn check_event() {
let mut ev: gdk::EventKey = base_ev.downcast().unwrap();
ev.as_mut().keyval = *gdk::keys::constants::A;

let keyval = gdk::keyval_to_unicode(*ev.get_keyval());
let keyval2 = ev.get_keyval().to_unicode();
let keyval_unicode = ev.get_keyval().to_unicode();

assert_eq!(keyval, Some('A'));
assert_eq!(keyval, keyval2);
assert_eq!(keyval_unicode, Some('A'));

let name = gdk::keyval_name(*ev.get_keyval());
let name2 = ev.get_keyval().name();
let keyval_name = ev.get_keyval().name();

assert_eq!(name, Some("A".into()));
assert_eq!(name, name2);
assert_eq!(keyval_name, Some("A".into()));
}

0 comments on commit 2038357

Please sign in to comment.