use serde_json::Value;
use crate::ibus::IBusModifierState;
const MS_S: &'static str = "S";
const MS_K: &'static str = "K";
const MS_C: &'static str = "C";
const MS_T: &'static str = "T";
#[derive(Debug, Clone, PartialEq)]
pub enum Ms {
S(MsS),
K(MsK),
C(MsC),
T(MsT),
}
impl ToString for Ms {
fn to_string(&self) -> String {
match self {
Ms::S(m) => m.to_string(),
Ms::K(m) => m.to_string(),
Ms::C(m) => m.to_string(),
Ms::T(m) => m.to_string(),
}
}
}
trait MsToString {
fn value(&self) -> Value;
fn name(&self) -> &'static str;
fn to_string(&self) -> String {
format!("{} {}", self.name().to_string(), self.value().to_string())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MsS(pub String);
impl MsToString for MsS {
fn name(&self) -> &'static str {
MS_S
}
fn value(&self) -> Value {
Value::from(self.0.as_str())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MsK(pub Vec<u32>);
impl MsK {
pub fn new(keyval: u32, keycode: u32, state: u32) -> Self {
let kd = if IBusModifierState::new_with_raw_value(state).is_keydown() {
1
} else {
0
};
Self(vec![keyval, keycode, state, kd])
}
}
impl MsToString for MsK {
fn name(&self) -> &'static str {
MS_K
}
fn value(&self) -> Value {
Value::from(self.0.clone())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MsC(pub Vec<i32>);
impl MsC {
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Self {
Self(vec![x, y, w, h])
}
}
impl MsToString for MsC {
fn name(&self) -> &'static str {
MS_C
}
fn value(&self) -> Value {
Value::from(self.0.clone())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MsT(pub String);
impl MsToString for MsT {
fn name(&self) -> &'static str {
MS_T
}
fn value(&self) -> Value {
Value::from(self.0.as_str())
}
}