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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::env;
use crate::logging;
use glib::prelude::ObjectExt;
use crate::logging::Warn;
pub mod c {
use super::*;
use gio;
use gtk;
use gtk_sys;
use gtk::prelude::CssProviderExt;
use glib::translate::ToGlibPtr;
#[no_mangle]
pub extern "C"
fn squeek_load_style() -> *const gtk_sys::GtkCssProvider {
unsafe { gtk::set_initialized() };
#[cfg(feature = "glib_v0_14")]
let theme = gtk::Settings::default();
#[cfg(not(feature = "glib_v0_14"))]
let theme = gtk::Settings::get_default();
let theme = theme.map(|settings| get_theme_name(&settings));
let css_name = path_from_theme(theme);
let resource_name = if gio::resources_get_info(
&css_name,
gio::ResourceLookupFlags::NONE
).is_ok() {
css_name
} else {
path_from_theme(None)
};
let provider = gtk::CssProvider::new();
provider.load_from_resource(&resource_name);
provider.to_glib_full()
}
}
const DEFAULT_THEME_NAME: &str = "";
struct GtkTheme {
name: String,
variant: Option<String>,
}
fn get_theme_name(settings: >k::Settings) -> GtkTheme {
let env_theme = env::var("GTK_THEME")
.map(|theme| {
let mut parts = theme.splitn(2, ":");
GtkTheme {
name: parts.next().unwrap().into(),
variant: parts.next().map(String::from)
}
})
.map_err(|e| {
match &e {
env::VarError::NotPresent => {},
e => log_print!(
logging::Level::Surprise,
"GTK_THEME variable invalid: {}", e,
),
};
e
}).ok();
#[cfg(feature = "glib_v0_14")]
let prop = |s: >k::Settings, name| s.property(name);
#[cfg(not(feature = "glib_v0_14"))]
let prop = |s: >k::Settings, name| s.get_property(name);
#[cfg(feature = "glib_v0_14")]
fn check<T, E: std::fmt::Display>(v: Result<T, E>) -> Option<T> {
v.or_print(logging::Problem::Surprise, "Key not of expected type")
}
#[cfg(not(feature = "glib_v0_14"))]
fn check<T>(v: Option<T>) -> Option<T> { v }
match env_theme {
Some(theme) => theme,
None => GtkTheme {
name: {
prop(settings, "gtk-theme-name")
.or_print(logging::Problem::Surprise, "No theme name")
.and_then(|value| check(value.get::<String>()))
.unwrap_or(DEFAULT_THEME_NAME.into())
},
variant: {
prop(settings, "gtk-application-prefer-dark-theme")
.or_print(logging::Problem::Surprise, "No settings key")
.and_then(|value| check(value.get::<bool>()))
.and_then(|dark_preferred| match dark_preferred {
true => Some("dark".into()),
false => None,
})
},
},
}
}
fn path_from_theme(theme: Option<GtkTheme>) -> String {
format!(
"/sm/puri/squeekboard/style{}.css",
match theme {
Some(GtkTheme { name, variant: Some(variant) }) => {
format!("-{}:{}", name, variant)
},
Some(GtkTheme { name, variant: None }) => format!("-{}", name),
None => "".into(),
}
)
}