keyboard_types/
composition.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use alloc::string::String;
5
6/// Describes the state of a composition session.
7#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum CompositionState {
10    /// The [compositionstart] event.
11    ///
12    /// See also [the MDN documentation][mdn].
13    ///
14    /// [compositionstart]: https://w3c.github.io/uievents/#event-type-compositionstart
15    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionstart_event
16    Start,
17    /// The [compositionupdate] event.
18    ///
19    /// See also [the MDN documentation][mdn].
20    ///
21    /// [compositionupdate]: https://w3c.github.io/uievents/#event-type-compositionupdate
22    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionupdate_event
23    Update,
24    /// The [compositionend] event.
25    ///
26    /// In a text editor, in this state the data should be added to the input.
27    ///
28    /// See also [the MDN documentation][mdn].
29    ///
30    /// [compositionend]: https://w3c.github.io/uievents/#event-type-compositionend
31    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionend_event
32    End,
33}
34
35impl CompositionState {
36    /// The [type] name of the corresponding composition event.
37    ///
38    /// This is either `"compositionstart"`, `"compositionupdate"` or `"compositionend"`.
39    ///
40    /// [type]: https://w3c.github.io/uievents/#events-composition-types
41    pub const fn event_type(self) -> &'static str {
42        match self {
43            Self::Start => "compositionstart",
44            Self::Update => "compositionupdate",
45            Self::End => "compositionend",
46        }
47    }
48}
49
50/// Event to expose input methods to program logic.
51///
52/// Provides information about entered sequences from
53/// dead key combinations and IMEs.
54///
55/// A composition session is always started by a [`CompositionState::Start`]
56/// event followed by zero or more [`CompositionState::Update`] events
57/// and terminated by a single [`CompositionState::End`] event.
58#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
59#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
60pub struct CompositionEvent {
61    /// Describes the event kind.
62    pub state: CompositionState,
63    /// Current composition data. May be empty.
64    pub data: String,
65}