1#![allow(deprecated)]
5
6use crate::{ffi, CssSection, StyleProvider};
7#[cfg(feature = "v4_20")]
8#[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
9use crate::{InterfaceColorScheme, InterfaceContrast};
10use glib::{
11 object::ObjectType as _,
12 prelude::*,
13 signal::{connect_raw, SignalHandlerId},
14 translate::*,
15};
16use std::boxed::Box as Box_;
17
18glib::wrapper! {
19 #[doc(alias = "GtkCssProvider")]
20 pub struct CssProvider(Object<ffi::GtkCssProvider, ffi::GtkCssProviderClass>) @implements StyleProvider;
21
22 match fn {
23 type_ => || ffi::gtk_css_provider_get_type(),
24 }
25}
26
27impl CssProvider {
28 #[doc(alias = "gtk_css_provider_new")]
29 pub fn new() -> CssProvider {
30 assert_initialized_main_thread!();
31 unsafe { from_glib_full(ffi::gtk_css_provider_new()) }
32 }
33
34 pub fn builder() -> CssProviderBuilder {
39 CssProviderBuilder::new()
40 }
41
42 #[cfg(feature = "v4_12")]
43 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
44 #[doc(alias = "gtk_css_provider_load_from_bytes")]
45 pub fn load_from_bytes(&self, data: &glib::Bytes) {
46 unsafe {
47 ffi::gtk_css_provider_load_from_bytes(self.to_glib_none().0, data.to_glib_none().0);
48 }
49 }
50
51 #[cfg_attr(feature = "v4_12", deprecated = "Since 4.12")]
52 #[allow(deprecated)]
53 #[doc(alias = "gtk_css_provider_load_from_data")]
54 pub fn load_from_data(&self, data: &str) {
55 let length = data.len() as _;
56 unsafe {
57 ffi::gtk_css_provider_load_from_data(
58 self.to_glib_none().0,
59 data.to_glib_none().0,
60 length,
61 );
62 }
63 }
64
65 #[doc(alias = "gtk_css_provider_load_from_file")]
66 pub fn load_from_file(&self, file: &impl IsA<gio::File>) {
67 unsafe {
68 ffi::gtk_css_provider_load_from_file(
69 self.to_glib_none().0,
70 file.as_ref().to_glib_none().0,
71 );
72 }
73 }
74
75 #[doc(alias = "gtk_css_provider_load_from_path")]
76 pub fn load_from_path(&self, path: impl AsRef<std::path::Path>) {
77 unsafe {
78 ffi::gtk_css_provider_load_from_path(
79 self.to_glib_none().0,
80 path.as_ref().to_glib_none().0,
81 );
82 }
83 }
84
85 #[doc(alias = "gtk_css_provider_load_from_resource")]
86 pub fn load_from_resource(&self, resource_path: &str) {
87 unsafe {
88 ffi::gtk_css_provider_load_from_resource(
89 self.to_glib_none().0,
90 resource_path.to_glib_none().0,
91 );
92 }
93 }
94
95 #[cfg(feature = "v4_12")]
96 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
97 #[doc(alias = "gtk_css_provider_load_from_string")]
98 pub fn load_from_string(&self, string: &str) {
99 unsafe {
100 ffi::gtk_css_provider_load_from_string(self.to_glib_none().0, string.to_glib_none().0);
101 }
102 }
103
104 #[cfg_attr(feature = "v4_20", deprecated = "Since 4.20")]
105 #[allow(deprecated)]
106 #[doc(alias = "gtk_css_provider_load_named")]
107 pub fn load_named(&self, name: &str, variant: Option<&str>) {
108 unsafe {
109 ffi::gtk_css_provider_load_named(
110 self.to_glib_none().0,
111 name.to_glib_none().0,
112 variant.to_glib_none().0,
113 );
114 }
115 }
116
117 #[doc(alias = "gtk_css_provider_to_string")]
118 #[doc(alias = "to_string")]
119 pub fn to_str(&self) -> glib::GString {
120 unsafe { from_glib_full(ffi::gtk_css_provider_to_string(self.to_glib_none().0)) }
121 }
122
123 #[cfg(feature = "v4_20")]
124 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
125 #[doc(alias = "prefers-color-scheme")]
126 pub fn prefers_color_scheme(&self) -> InterfaceColorScheme {
127 ObjectExt::property(self, "prefers-color-scheme")
128 }
129
130 #[cfg(feature = "v4_20")]
131 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
132 #[doc(alias = "prefers-color-scheme")]
133 pub fn set_prefers_color_scheme(&self, prefers_color_scheme: InterfaceColorScheme) {
134 ObjectExt::set_property(self, "prefers-color-scheme", prefers_color_scheme)
135 }
136
137 #[cfg(feature = "v4_20")]
138 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
139 #[doc(alias = "prefers-contrast")]
140 pub fn prefers_contrast(&self) -> InterfaceContrast {
141 ObjectExt::property(self, "prefers-contrast")
142 }
143
144 #[cfg(feature = "v4_20")]
145 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
146 #[doc(alias = "prefers-contrast")]
147 pub fn set_prefers_contrast(&self, prefers_contrast: InterfaceContrast) {
148 ObjectExt::set_property(self, "prefers-contrast", prefers_contrast)
149 }
150
151 #[doc(alias = "parsing-error")]
152 pub fn connect_parsing_error<F: Fn(&Self, &CssSection, &glib::Error) + 'static>(
153 &self,
154 f: F,
155 ) -> SignalHandlerId {
156 unsafe extern "C" fn parsing_error_trampoline<
157 F: Fn(&CssProvider, &CssSection, &glib::Error) + 'static,
158 >(
159 this: *mut ffi::GtkCssProvider,
160 section: *mut ffi::GtkCssSection,
161 error: *mut glib::ffi::GError,
162 f: glib::ffi::gpointer,
163 ) {
164 let f: &F = &*(f as *const F);
165 f(
166 &from_glib_borrow(this),
167 &from_glib_borrow(section),
168 &from_glib_borrow(error),
169 )
170 }
171 unsafe {
172 let f: Box_<F> = Box_::new(f);
173 connect_raw(
174 self.as_ptr() as *mut _,
175 c"parsing-error".as_ptr() as *const _,
176 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
177 parsing_error_trampoline::<F> as *const (),
178 )),
179 Box_::into_raw(f),
180 )
181 }
182 }
183
184 #[cfg(feature = "v4_20")]
185 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
186 #[doc(alias = "prefers-color-scheme")]
187 pub fn connect_prefers_color_scheme_notify<F: Fn(&Self) + 'static>(
188 &self,
189 f: F,
190 ) -> SignalHandlerId {
191 unsafe extern "C" fn notify_prefers_color_scheme_trampoline<
192 F: Fn(&CssProvider) + 'static,
193 >(
194 this: *mut ffi::GtkCssProvider,
195 _param_spec: glib::ffi::gpointer,
196 f: glib::ffi::gpointer,
197 ) {
198 let f: &F = &*(f as *const F);
199 f(&from_glib_borrow(this))
200 }
201 unsafe {
202 let f: Box_<F> = Box_::new(f);
203 connect_raw(
204 self.as_ptr() as *mut _,
205 c"notify::prefers-color-scheme".as_ptr() as *const _,
206 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
207 notify_prefers_color_scheme_trampoline::<F> as *const (),
208 )),
209 Box_::into_raw(f),
210 )
211 }
212 }
213
214 #[cfg(feature = "v4_20")]
215 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
216 #[doc(alias = "prefers-contrast")]
217 pub fn connect_prefers_contrast_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
218 unsafe extern "C" fn notify_prefers_contrast_trampoline<F: Fn(&CssProvider) + 'static>(
219 this: *mut ffi::GtkCssProvider,
220 _param_spec: glib::ffi::gpointer,
221 f: glib::ffi::gpointer,
222 ) {
223 let f: &F = &*(f as *const F);
224 f(&from_glib_borrow(this))
225 }
226 unsafe {
227 let f: Box_<F> = Box_::new(f);
228 connect_raw(
229 self.as_ptr() as *mut _,
230 c"notify::prefers-contrast".as_ptr() as *const _,
231 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
232 notify_prefers_contrast_trampoline::<F> as *const (),
233 )),
234 Box_::into_raw(f),
235 )
236 }
237 }
238}
239
240impl Default for CssProvider {
241 fn default() -> Self {
242 Self::new()
243 }
244}
245
246impl std::fmt::Display for CssProvider {
247 #[inline]
248 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
249 f.write_str(&self.to_str())
250 }
251}
252
253#[must_use = "The builder must be built to be used"]
258pub struct CssProviderBuilder {
259 builder: glib::object::ObjectBuilder<'static, CssProvider>,
260}
261
262impl CssProviderBuilder {
263 fn new() -> Self {
264 Self {
265 builder: glib::object::Object::builder(),
266 }
267 }
268
269 #[cfg(feature = "v4_20")]
270 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
271 pub fn prefers_color_scheme(self, prefers_color_scheme: InterfaceColorScheme) -> Self {
272 Self {
273 builder: self
274 .builder
275 .property("prefers-color-scheme", prefers_color_scheme),
276 }
277 }
278
279 #[cfg(feature = "v4_20")]
280 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
281 pub fn prefers_contrast(self, prefers_contrast: InterfaceContrast) -> Self {
282 Self {
283 builder: self.builder.property("prefers-contrast", prefers_contrast),
284 }
285 }
286
287 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
290 pub fn build(self) -> CssProvider {
291 assert_initialized_main_thread!();
292 self.builder.build()
293 }
294}