1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Plugin to work with clipboard and various system related functions.
//! It handles flutter/platform type message.

use crate::{FlutterEngineInner};
use super::{Plugin, PlatformMessage, PluginRegistry};
use serde_json::Value;
use channel::{ Channel, JsonMethodChannel };
use codec::MethodCallResult;
use std::sync::Arc;

pub struct PlatformPlugin {
    channel: JsonMethodChannel,
}

impl PlatformPlugin {
    pub fn new() -> Self {
        PlatformPlugin {
            channel: JsonMethodChannel::new("flutter/platform")
        }
    }
}

impl Plugin for PlatformPlugin {
    fn init_channel(&self, registry: &PluginRegistry) -> &str {
        self.channel.init(registry);
        return self.channel.get_name();
    }
    fn handle(&mut self, msg: &PlatformMessage, _engine: Arc<FlutterEngineInner>, window: &mut glfw::Window) {
        let decoded = self.channel.decode_method_call(msg);
        match decoded.method.as_str() {
            "SystemChrome.setApplicationSwitcherDescription" => {
                // label and primaryColor
                window.set_title(decoded.args.as_object().unwrap().get("label").unwrap().as_str().unwrap());
            },
            "Clipboard.setData" => {
                if let Value::Object(v) = &decoded.args {
                    if let Some(v) = &v.get("text") {
                        if let Value::String(text) = v {
                            window.set_clipboard_string(text);
                        }
                    }
                }
            },
            "Clipboard.getData" => (
                if let Value::String(mime) = &decoded.args {
                    match mime.as_str() {
                        "text/plain" => {
                            self.channel.send_method_call_response(
                                msg.response_handle,
                                MethodCallResult::Ok(json!({
                                    "text": window.get_clipboard_string(),
                                })),
                            );
                        },
                        _ => {
                            error!("Dont know how to handle {} clipboard message", mime.as_str());
                            ()
                        },
                    }
                }
            ),
            _ => (),
        }
    }
}