atspi_common/events/
keyboard.rs

1use crate::{
2	error::AtspiError,
3	events::{Accessible, EventBodyOwned, GenericEvent, HasMatchRule, HasRegistryEventString},
4	Event,
5};
6use zvariant::ObjectPath;
7
8#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash)]
9pub enum KeyboardEvents {
10	/// See: [`ModifiersEvent`].
11	Modifiers(ModifiersEvent),
12}
13
14impl_from_interface_event_enum_for_event!(KeyboardEvents, Event::Keyboard);
15impl_try_from_event_for_user_facing_event_type!(KeyboardEvents, Event::Keyboard);
16
17event_wrapper_test_cases!(KeyboardEvents, ModifiersEvent);
18
19impl HasMatchRule for KeyboardEvents {
20	const MATCH_RULE_STRING: &'static str =
21		"type='signal',interface='org.a11y.atspi.Event.Keyboard'";
22}
23
24#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
25pub struct ModifiersEvent {
26	/// The [`Accessible`] which the event applies to.
27	pub item: crate::events::Accessible,
28	pub previous_modifiers: i32,
29	pub current_modifiers: i32,
30}
31
32impl GenericEvent<'_> for ModifiersEvent {
33	const DBUS_MEMBER: &'static str = "Modifiers";
34	const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Keyboard";
35	const MATCH_RULE_STRING: &'static str =
36		"type='signal',interface='org.a11y.atspi.Event.Keyboard',member='Modifiers'";
37	const REGISTRY_EVENT_STRING: &'static str = "Keyboard:";
38
39	type Body = EventBodyOwned;
40
41	fn build(item: Accessible, body: Self::Body) -> Result<Self, AtspiError> {
42		Ok(Self { item, previous_modifiers: body.detail1, current_modifiers: body.detail2 })
43	}
44	fn sender(&self) -> String {
45		self.item.name.clone()
46	}
47	fn path<'a>(&self) -> ObjectPath<'_> {
48		self.item.path.clone().into()
49	}
50	fn body(&self) -> Self::Body {
51		let copy = self.clone();
52		copy.into()
53	}
54}
55
56#[cfg(feature = "zbus")]
57impl TryFrom<&zbus::Message> for KeyboardEvents {
58	type Error = AtspiError;
59	fn try_from(ev: &zbus::Message) -> Result<Self, Self::Error> {
60		let member = ev
61			.member()
62			.ok_or(AtspiError::MemberMatch("Event without member".into()))?;
63		match member.as_str() {
64			"Modifiers" => Ok(KeyboardEvents::Modifiers(ev.try_into()?)),
65			_ => Err(AtspiError::MemberMatch("No matching member for Keyboard".into())),
66		}
67	}
68}
69
70impl_from_user_facing_event_for_interface_event_enum!(
71	ModifiersEvent,
72	KeyboardEvents,
73	KeyboardEvents::Modifiers
74);
75impl_from_user_facing_type_for_event_enum!(ModifiersEvent, Event::Keyboard);
76impl_try_from_event_for_user_facing_type!(
77	ModifiersEvent,
78	KeyboardEvents::Modifiers,
79	Event::Keyboard
80);
81
82event_test_cases!(ModifiersEvent);
83impl_to_dbus_message!(ModifiersEvent);
84impl_from_dbus_message!(ModifiersEvent);
85impl From<ModifiersEvent> for EventBodyOwned {
86	fn from(event: ModifiersEvent) -> Self {
87		EventBodyOwned {
88			properties: std::collections::HashMap::new(),
89			kind: String::default(),
90			detail1: event.previous_modifiers,
91			detail2: event.current_modifiers,
92			any_data: zvariant::Value::U8(0).into(),
93		}
94	}
95}
96
97impl HasRegistryEventString for KeyboardEvents {
98	const REGISTRY_EVENT_STRING: &'static str = "Keyboard:";
99}