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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
use std::time::Duration;
use glutin::ModifiersState;
use serde::{Deserialize, Deserializer};
use crate::config::bindings::{CommandWrapper, ModsWrapper};
use crate::config::failure_default;
#[serde(default)]
#[derive(Default, Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct Mouse {
#[serde(deserialize_with = "failure_default")]
pub double_click: ClickHandler,
#[serde(deserialize_with = "failure_default")]
pub triple_click: ClickHandler,
#[serde(deserialize_with = "failure_default")]
pub hide_when_typing: bool,
#[serde(deserialize_with = "failure_default")]
pub url: Url,
}
#[serde(default)]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct Url {
// Program for opening links
#[serde(deserialize_with = "deserialize_launcher")]
pub launcher: Option<CommandWrapper>,
// Modifier used to open links
#[serde(deserialize_with = "failure_default")]
modifiers: ModsWrapper,
}
impl Url {
pub fn mods(&self) -> ModifiersState {
self.modifiers.into_inner()
}
}
fn deserialize_launcher<'a, D>(
deserializer: D,
) -> ::std::result::Result<Option<CommandWrapper>, D::Error>
where
D: Deserializer<'a>,
{
let default = Url::default().launcher;
// Deserialize to generic value
let val = serde_yaml::Value::deserialize(deserializer)?;
// Accept `None` to disable the launcher
if val.as_str().filter(|v| v.to_lowercase() == "none").is_some() {
return Ok(None);
}
match <Option<CommandWrapper>>::deserialize(val) {
Ok(launcher) => Ok(launcher),
Err(err) => {
error!("Problem with config: {}; using {}", err, default.clone().unwrap().program());
Ok(default)
},
}
}
impl Default for Url {
fn default() -> Url {
Url {
#[cfg(not(any(target_os = "macos", windows)))]
launcher: Some(CommandWrapper::Just(String::from("xdg-open"))),
#[cfg(target_os = "macos")]
launcher: Some(CommandWrapper::Just(String::from("open"))),
#[cfg(windows)]
launcher: Some(CommandWrapper::Just(String::from("explorer"))),
modifiers: Default::default(),
}
}
}
#[serde(default)]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct ClickHandler {
#[serde(deserialize_with = "deserialize_duration_ms")]
pub threshold: Duration,
}
impl Default for ClickHandler {
fn default() -> Self {
ClickHandler { threshold: default_threshold_ms() }
}
}
fn default_threshold_ms() -> Duration {
Duration::from_millis(300)
}
fn deserialize_duration_ms<'a, D>(deserializer: D) -> ::std::result::Result<Duration, D::Error>
where
D: Deserializer<'a>,
{
let value = serde_yaml::Value::deserialize(deserializer)?;
match u64::deserialize(value) {
Ok(threshold_ms) => Ok(Duration::from_millis(threshold_ms)),
Err(err) => {
error!("Problem with config: {}; using default value", err);
Ok(default_threshold_ms())
},
}
}
|