Skip to content

Commit 59cbb3a

Browse files
committed
Allow implementing a GAction interface
1 parent 35e15b7 commit 59cbb3a

File tree

5 files changed

+546
-0
lines changed

5 files changed

+546
-0
lines changed

examples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ path = "object_subclass/main.rs"
5151
[[bin]]
5252
name = "virtual_methods"
5353
path = "virtual_methods/main.rs"
54+
55+
[[bin]]
56+
name = "gio_action_impl"
57+
path = "gio_action_impl/main.rs"

examples/gio_action_impl/action.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use gio::{prelude::*, subclass::prelude::*};
2+
use glib;
3+
4+
mod imp {
5+
use super::*;
6+
use std::cell::OnceCell;
7+
8+
#[derive(glib::Properties, Default)]
9+
#[properties(wrapper_type = super::RenamedAction)]
10+
pub struct RenamedAction {
11+
#[property(get, construct_only)]
12+
pub new_name: OnceCell<glib::GString>,
13+
14+
#[property(get, construct_only)]
15+
pub action: OnceCell<gio::Action>,
16+
}
17+
18+
#[glib::object_subclass]
19+
impl ObjectSubclass for RenamedAction {
20+
const NAME: &str = "ExampleRenamedAction";
21+
type Type = super::RenamedAction;
22+
type Interfaces = (gio::Action,);
23+
}
24+
25+
#[glib::derived_properties]
26+
impl ObjectImpl for RenamedAction {
27+
fn properties() -> &'static [glib::ParamSpec] {
28+
Self::derived_properties()
29+
}
30+
31+
fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
32+
let _ = self.delegate_set_property(id, value, pspec) || {
33+
self.derived_set_property(id, value, pspec);
34+
true
35+
};
36+
}
37+
38+
fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
39+
self.delegate_get_property(id, pspec)
40+
.unwrap_or_else(|| self.derived_property(id, pspec))
41+
}
42+
}
43+
44+
impl ActionImpl for RenamedAction {
45+
fn name(&self) -> glib::GString {
46+
self.obj().new_name()
47+
}
48+
49+
fn parameter_type(&self) -> Option<glib::VariantType> {
50+
self.obj().action().parameter_type()
51+
}
52+
53+
fn state_type(&self) -> Option<glib::VariantType> {
54+
self.obj().action().state_type()
55+
}
56+
57+
fn state_hint(&self) -> Option<glib::Variant> {
58+
self.obj().action().state_hint()
59+
}
60+
61+
fn is_enabled(&self) -> bool {
62+
self.obj().action().is_enabled()
63+
}
64+
65+
fn state(&self) -> Option<glib::Variant> {
66+
self.obj().action().state()
67+
}
68+
69+
fn change_state(&self, value: glib::Variant) {
70+
self.obj().action().change_state(&value);
71+
}
72+
73+
fn activate(&self, parameter: Option<glib::Variant>) {
74+
self.obj().action().activate(parameter.as_ref());
75+
}
76+
}
77+
}
78+
79+
glib::wrapper! {
80+
pub struct RenamedAction(ObjectSubclass<imp::RenamedAction>)
81+
@implements gio::Action;
82+
}
83+
84+
impl RenamedAction {
85+
pub fn new(name: &str, action: &impl IsA<gio::Action>) -> Self {
86+
glib::Object::builder()
87+
.property("new-name", name)
88+
.property("action", action)
89+
.build()
90+
}
91+
}

examples/gio_action_impl/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mod action;
2+
3+
use gio::prelude::*;
4+
5+
fn main() {
6+
let action = gio::SimpleAction::new("bark", Some(glib::VariantTy::STRING));
7+
action.connect_activate(|_, p| {
8+
let target = p.unwrap().str().unwrap();
9+
println!("Woof, {}!", target);
10+
});
11+
12+
let renamed_action = action::RenamedAction::new("meow", &action);
13+
14+
let group = gio::SimpleActionGroup::new();
15+
group.add_action(&action);
16+
group.add_action(&renamed_action);
17+
18+
println!("actions = {:?}", group.list_actions());
19+
20+
group.activate_action("bark", Some(&"postman".to_variant()));
21+
group.activate_action("meow", Some(&"milkman".to_variant()));
22+
}

0 commit comments

Comments
 (0)