zwp_input_method_service/
lib.rs

1//! This crate provides an easy to use interface for the zwp_input_method_v2 protocol.
2//! It allows a wayland client to serve as an input method for other wayland-clients. This could be used for virtual keyboards
3//!
4#[cfg(feature = "debug")]
5#[macro_use]
6extern crate log;
7
8use std::sync::{Arc, Mutex};
9use wayland_client::{protocol::wl_seat::WlSeat, Main};
10use wayland_protocols::misc::zwp_input_method_v2::client::zwp_input_method_manager_v2::ZwpInputMethodManagerV2;
11
12mod traits;
13pub use traits::*;
14
15use arc_input_method::*;
16mod arc_input_method;
17
18#[derive(Debug, Clone)]
19/// Error when sending a request to the wayland-client
20pub enum SubmitError {
21    /// Input method was not activ
22    NotActive,
23}
24
25#[derive(Clone, Debug)]
26/// Manages the pending state and the current state of the input method.
27pub struct IMService<T: 'static + IMVisibility + HintPurpose, D: 'static + ReceiveSurroundingText> {
28    im_service_arc: Arc<Mutex<IMServiceArc<T, D>>>, // provides an easy to use interface by hiding the Arc<Mutex<>>
29}
30
31impl<T: IMVisibility + HintPurpose, D: ReceiveSurroundingText> InputMethod<T, D>
32    for IMService<T, D>
33{
34    fn new(
35        seat: &WlSeat,
36        im_manager: Main<ZwpInputMethodManagerV2>,
37        ui_connector: T,
38        content_connector: D,
39    ) -> Self {
40        let im_service_arc = IMServiceArc::new(seat, im_manager, ui_connector, content_connector);
41        IMService { im_service_arc }
42    }
43
44    fn commit_string(&self, text: String) -> Result<(), SubmitError> {
45        self.im_service_arc.lock().unwrap().commit_string(text)
46    }
47
48    fn delete_surrounding_text(&self, before: usize, after: usize) -> Result<(), SubmitError> {
49        self.im_service_arc
50            .lock()
51            .unwrap()
52            .delete_surrounding_text(before, after)
53    }
54
55    fn commit(&self) -> Result<(), SubmitError> {
56        self.im_service_arc.lock().unwrap().commit()
57    }
58
59    fn is_active(&self) -> bool {
60        self.im_service_arc.lock().unwrap().is_active()
61    }
62
63    fn get_surrounding_text(&self) -> (String, String) {
64        self.im_service_arc.lock().unwrap().get_surrounding_text()
65    }
66}