kbd_global/
binding_guard.rs1use std::fmt;
11
12use kbd::binding::BindingId;
13
14use crate::Error;
15use crate::engine::Command;
16use crate::engine::CommandSender;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19enum HandleState {
20 Active,
21 Released,
22}
23
24pub struct BindingGuard {
26 id: BindingId,
27 commands: CommandSender,
28 state: HandleState,
29}
30
31impl fmt::Debug for BindingGuard {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 f.debug_struct("BindingGuard")
34 .field("id", &self.id)
35 .field("state", &self.state)
36 .finish_non_exhaustive()
37 }
38}
39
40impl BindingGuard {
41 pub(crate) fn new(id: BindingId, commands: CommandSender) -> Self {
42 Self {
43 id,
44 commands,
45 state: HandleState::Active,
46 }
47 }
48
49 #[must_use]
51 pub const fn binding_id(&self) -> BindingId {
52 self.id
53 }
54
55 pub fn unregister(mut self) -> Result<(), Error> {
65 self.unregister_inner()
66 }
67
68 fn unregister_inner(&mut self) -> Result<(), Error> {
69 match self.state {
70 HandleState::Active => {
71 self.state = HandleState::Released;
72 self.commands.send(Command::Unregister { id: self.id })
73 }
74 HandleState::Released => Ok(()),
75 }
76 }
77}
78
79impl Drop for BindingGuard {
80 fn drop(&mut self) {
81 let _ = self.unregister_inner();
82 }
83}