relm4/binding/
bindings.rs1use gtk::glib;
2
3use crate::binding::Binding;
4
5macro_rules! binding {
6 ($name:ident, $obj_name:literal, $ty:ty, $mod:ident) => {
7 glib::wrapper! {
8 #[doc = "A data binding storing a value of type [`"]
9 #[doc = stringify!($ty)]
10 #[doc = "`]"]
11 pub struct $name(ObjectSubclass<$mod::$name>);
12 }
13
14 impl $name {
15 #[doc = "Create a new [`"]
16 #[doc = stringify!($name)]
17 #[doc = "`]."]
18 pub fn new<T: Into<$ty>>(value: T) -> Self {
19 let this: Self = glib::Object::new();
20 this.set_value(value.into());
21 this
22 }
23 }
24
25 impl Default for $name {
26 fn default() -> Self {
27 glib::Object::new()
28 }
29 }
30
31 impl Binding for $name {
32 type Target = $ty;
33
34 fn get(&self) -> Self::Target {
35 self.value()
36 }
37
38 fn set(&self, value: Self::Target) {
39 self.set_value(value)
40 }
41 }
42
43 #[allow(missing_docs)]
44 mod $mod {
45 use std::cell::RefCell;
46
47 use glib::prelude::*;
48 use glib::{ParamSpec, Properties, Value};
49 use gtk::subclass::prelude::ObjectImpl;
50 use gtk::{
51 glib,
52 subclass::prelude::{DerivedObjectProperties, ObjectSubclass},
53 };
54
55 #[derive(Default, Properties, Debug)]
56 #[properties(wrapper_type = super::$name)]
57 pub struct $name {
59 #[property(get, set)]
60 value: RefCell<$ty>,
62 }
63
64 impl ObjectImpl for $name {
65 fn properties() -> &'static [ParamSpec] {
66 Self::derived_properties()
67 }
68 fn set_property(&self, id: usize, value: &Value, pspec: &ParamSpec) {
69 self.derived_set_property(id, value, pspec)
70 }
71 fn property(&self, id: usize, pspec: &ParamSpec) -> Value {
72 self.derived_property(id, pspec)
73 }
74 }
75
76 #[glib::object_subclass]
77 impl ObjectSubclass for $name {
78 const NAME: &'static str = $obj_name;
79 type Type = super::$name;
80 }
81 }
82 };
83}
84
85binding!(BoolBinding, "BoolBinding", bool, imp_bool);
87
88binding!(U64Binding, "U64Binding", u64, imp_u64);
90binding!(I64Binding, "I64Binding", i64, imp_i64);
91binding!(U32Binding, "U32Binding", u32, imp_u32);
92binding!(I32Binding, "I32Binding", i32, imp_i32);
93binding!(U8Binding, "U8Binding", u8, imp_u8);
94binding!(I8Binding, "I8Binding", i8, imp_i8);
95
96binding!(F64Binding, "F64Binding", f64, imp_f64);
98binding!(F32Binding, "F32Binding", f32, imp_f32);
99
100binding!(StringBinding, "StringBinding", String, imp_string);