zwp_input_method_service/
traits.rs

1use super::SubmitError;
2use wayland_client::{protocol::wl_seat::WlSeat, Main};
3use wayland_protocols::misc::zwp_input_method_v2::client::zwp_input_method_manager_v2::ZwpInputMethodManagerV2;
4use wayland_protocols::unstable::text_input::v3::client::zwp_text_input_v3::{
5    ContentHint, ContentPurpose,
6};
7
8/// All input methods must be able to handle these functions
9/// This helps write test cases, because they can be generic
10pub trait InputMethod<T: IMVisibility + HintPurpose, D: ReceiveSurroundingText> {
11    /// Create a new InputMethod. The connectors must implement the traits IMVisibility and HintPurpose
12    fn new(
13        seat: &WlSeat,
14        im_manager: Main<ZwpInputMethodManagerV2>,
15        ui_connector: T,
16        content_connector: D,
17    ) -> Self;
18
19    /// Sends a 'commit_string' request to the wayland-server
20    ///
21    /// INPUTS:
22    ///
23    /// text -> Text that will be committed
24    fn commit_string(&self, text: String) -> Result<(), SubmitError>;
25
26    /// Sends a 'delete_surrounding_text' request to the wayland server
27    ///
28    /// INPUTS:
29    ///
30    /// before -> number of chars to delete from the surrounding_text going left from the cursor
31    ///
32    /// after  -> number of chars to delete from the surrounding_text going right from the cursor
33    fn delete_surrounding_text(&self, before: usize, after: usize) -> Result<(), SubmitError>;
34
35    /// Sends a 'commit' request to the wayland server
36    ///
37    /// This makes the pending changes permanent
38    fn commit(&self) -> Result<(), SubmitError>;
39
40    /// Returns if the input method is currently active
41    fn is_active(&self) -> bool;
42
43    /// Returns a tuple of the current strings left and right of the cursor
44    fn get_surrounding_text(&self) -> (String, String);
45}
46
47/// Trait to get notified when the input method should be active or deactivated
48///
49/// If the user clicks for example on a text field, the method activate_im() is called
50pub trait IMVisibility {
51    fn activate_im(&self);
52    fn deactivate_im(&self);
53}
54
55/// Trait to get notified when the text surrounding the cursor changes
56pub trait ReceiveSurroundingText {
57    fn text_changed(&self, string_left_of_cursor: String, string_right_of_cursor: String);
58}
59
60/// Trait to get notified when the hint or the purpose of the content changes
61pub trait HintPurpose {
62    fn set_hint_purpose(&self, content_hint: ContentHint, content_purpose: ContentPurpose);
63}