Skip to content

Commit ecae6f6

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

File tree

5 files changed

+545
-0
lines changed

5 files changed

+545
-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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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: &'static 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+
if !self.delegate_set_property(id, value, pspec) {
33+
self.derived_set_property(id, value, pspec);
34+
}
35+
}
36+
37+
fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
38+
self.delegate_get_property(id, pspec)
39+
.unwrap_or_else(|| self.derived_property(id, pspec))
40+
}
41+
}
42+
43+
impl ActionImpl for RenamedAction {
44+
fn name(&self) -> glib::GString {
45+
self.obj().new_name()
46+
}
47+
48+
fn parameter_type(&self) -> Option<glib::VariantType> {
49+
self.obj().action().parameter_type()
50+
}
51+
52+
fn state_type(&self) -> Option<glib::VariantType> {
53+
self.obj().action().state_type()
54+
}
55+
56+
fn state_hint(&self) -> Option<glib::Variant> {
57+
self.obj().action().state_hint()
58+
}
59+
60+
fn is_enabled(&self) -> bool {
61+
self.obj().action().is_enabled()
62+
}
63+
64+
fn state(&self) -> Option<glib::Variant> {
65+
self.obj().action().state()
66+
}
67+
68+
fn change_state(&self, value: glib::Variant) {
69+
self.obj().action().change_state(&value);
70+
}
71+
72+
fn activate(&self, parameter: Option<glib::Variant>) {
73+
self.obj().action().activate(parameter.as_ref());
74+
}
75+
}
76+
}
77+
78+
glib::wrapper! {
79+
pub struct RenamedAction(ObjectSubclass<imp::RenamedAction>)
80+
@implements gio::Action;
81+
}
82+
83+
impl RenamedAction {
84+
pub fn new(name: &str, action: &impl IsA<gio::Action>) -> Self {
85+
glib::Object::builder()
86+
.property("new-name", name)
87+
.property("action", action)
88+
.build()
89+
}
90+
}

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)