Skip to content

Commit 59cb4d2

Browse files
authored
Merge pull request #870 from pbor/master
ActionEntry: take proper types instead of strings
2 parents 8a9d3be + 42c72aa commit 59cb4d2

File tree

4 files changed

+49
-45
lines changed

4 files changed

+49
-45
lines changed

gio/Gir.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,21 @@ status = "generate"
11351135
name = "state"
11361136
#value glib::VariantTy
11371137
ignore = true
1138+
[[object.function]]
1139+
name = "new_stateful"
1140+
[[object.function.parameter]]
1141+
name = "state"
1142+
move = true
1143+
[[object.function]]
1144+
name = "set_state"
1145+
[[object.function.parameter]]
1146+
name = "value"
1147+
move = true
1148+
[[object.function]]
1149+
name = "set_state_hint"
1150+
[[object.function.parameter]]
1151+
name = "state_hint"
1152+
move = true
11381153

11391154
[[object]]
11401155
name = "Gio.SimpleIOStream"

gio/src/action_entry.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use glib::{prelude::*, Variant};
3+
use glib::{prelude::*, Variant, VariantTy, VariantType};
44

55
use crate::{ActionMap, SimpleAction};
66

@@ -10,8 +10,8 @@ where
1010
O: IsA<ActionMap>,
1111
{
1212
name: String,
13-
parameter_type: Option<String>,
14-
state: Option<String>,
13+
parameter_type: Option<VariantType>,
14+
state: Option<Variant>,
1515
pub(crate) activate: Option<Box<dyn Fn(&O, &SimpleAction, Option<&Variant>) + 'static>>,
1616
pub(crate) change_state: Option<Box<dyn Fn(&O, &SimpleAction, Option<&Variant>) + 'static>>,
1717
}
@@ -24,12 +24,12 @@ where
2424
&self.name
2525
}
2626

27-
pub fn parameter_type(&self) -> Option<&str> {
27+
pub fn parameter_type(&self) -> Option<&VariantTy> {
2828
self.parameter_type.as_deref()
2929
}
3030

31-
pub fn state(&self) -> Option<&str> {
32-
self.state.as_deref()
31+
pub fn state(&self) -> Option<&Variant> {
32+
self.state.as_ref()
3333
}
3434

3535
pub fn builder(name: &str) -> ActionEntryBuilder<O> {
@@ -69,13 +69,13 @@ where
6969
})
7070
}
7171

72-
pub fn parameter_type(mut self, parameter_type: &str) -> Self {
73-
self.0.parameter_type = Some(parameter_type.to_owned());
72+
pub fn parameter_type(mut self, parameter_type: Option<&VariantTy>) -> Self {
73+
self.0.parameter_type = parameter_type.map(|vt| vt.to_owned());
7474
self
7575
}
7676

77-
pub fn state(mut self, state: &str) -> Self {
78-
self.0.state = Some(state.to_owned());
77+
pub fn state(mut self, state: Variant) -> Self {
78+
self.0.state = Some(state);
7979
self
8080
}
8181

@@ -109,12 +109,20 @@ mod tests {
109109
fn action_entry() {
110110
let app = crate::Application::new(None, Default::default());
111111

112-
let close = ActionEntry::builder("close")
113-
.activate(move |_app, _, _| {
114-
//Do something
115-
})
116-
.build();
117-
app.add_action_entries(vec![close]).unwrap();
112+
app.add_action_entries(vec![
113+
ActionEntry::builder("close")
114+
.activate(move |_app, _, _| {
115+
//Do something
116+
})
117+
.build(),
118+
ActionEntry::builder("enable")
119+
.state(true.to_variant())
120+
.change_state(move |_app, _, _| {
121+
//Do something
122+
})
123+
.build(),
124+
]);
118125
assert!(app.lookup_action("close").is_some());
126+
assert!(app.lookup_action("enable").is_some());
119127
}
120128
}

gio/src/action_map.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,18 @@ use crate::{prelude::*, ActionEntry, ActionMap, SimpleAction};
66

77
pub trait ActionMapExtManual {
88
#[doc(alias = "g_action_map_add_action_entries")]
9-
fn add_action_entries(
10-
&self,
11-
entries: impl IntoIterator<Item = ActionEntry<Self>>,
12-
) -> Result<(), glib::BoolError>
9+
fn add_action_entries(&self, entries: impl IntoIterator<Item = ActionEntry<Self>>)
1310
where
1411
Self: IsA<ActionMap>;
1512
}
1613

1714
impl<O: IsA<ActionMap>> ActionMapExtManual for O {
18-
fn add_action_entries(
19-
&self,
20-
entries: impl IntoIterator<Item = ActionEntry<Self>>,
21-
) -> Result<(), glib::BoolError> {
15+
fn add_action_entries(&self, entries: impl IntoIterator<Item = ActionEntry<Self>>) {
2216
for entry in entries.into_iter() {
23-
let parameter_type = if let Some(param_type) = entry.parameter_type() {
24-
Some(glib::VariantType::new(param_type)?)
25-
} else {
26-
None
27-
};
2817
let action = if let Some(state) = entry.state() {
29-
let state = glib::Variant::parse(None, state).map_err(|e| {
30-
glib::bool_error!(
31-
"Invalid state passed to gio::ActionEntry {} {}",
32-
entry.name(),
33-
e
34-
)
35-
})?;
36-
SimpleAction::new_stateful(entry.name(), parameter_type.as_deref(), &state)
18+
SimpleAction::new_stateful(entry.name(), entry.parameter_type(), state.clone())
3719
} else {
38-
SimpleAction::new(entry.name(), parameter_type.as_deref())
20+
SimpleAction::new(entry.name(), entry.parameter_type())
3921
};
4022
let action_map = self.as_ref();
4123
if let Some(callback) = entry.activate {
@@ -52,6 +34,5 @@ impl<O: IsA<ActionMap>> ActionMapExtManual for O {
5234
}
5335
self.as_ref().add_action(&action);
5436
}
55-
Ok(())
5637
}
5738
}

gio/src/auto/simple_action.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ impl SimpleAction {
3434
pub fn new_stateful(
3535
name: &str,
3636
parameter_type: Option<&glib::VariantTy>,
37-
state: &glib::Variant,
37+
state: glib::Variant,
3838
) -> SimpleAction {
3939
unsafe {
4040
from_glib_full(ffi::g_simple_action_new_stateful(
4141
name.to_glib_none().0,
4242
parameter_type.to_glib_none().0,
43-
state.to_glib_none().0,
43+
state.into_glib_ptr(),
4444
))
4545
}
4646
}
@@ -53,16 +53,16 @@ impl SimpleAction {
5353
}
5454

5555
#[doc(alias = "g_simple_action_set_state")]
56-
pub fn set_state(&self, value: &glib::Variant) {
56+
pub fn set_state(&self, value: glib::Variant) {
5757
unsafe {
58-
ffi::g_simple_action_set_state(self.to_glib_none().0, value.to_glib_none().0);
58+
ffi::g_simple_action_set_state(self.to_glib_none().0, value.into_glib_ptr());
5959
}
6060
}
6161

6262
#[doc(alias = "g_simple_action_set_state_hint")]
63-
pub fn set_state_hint(&self, state_hint: Option<&glib::Variant>) {
63+
pub fn set_state_hint(&self, state_hint: Option<glib::Variant>) {
6464
unsafe {
65-
ffi::g_simple_action_set_state_hint(self.to_glib_none().0, state_hint.to_glib_none().0);
65+
ffi::g_simple_action_set_state_hint(self.to_glib_none().0, state_hint.into_glib_ptr());
6666
}
6767
}
6868

0 commit comments

Comments
 (0)