Skip to content

Commit 066dd78

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

File tree

5 files changed

+534
-0
lines changed

5 files changed

+534
-0
lines changed

examples/Cargo.toml

+4
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

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

examples/gio_action_impl/main.rs

+22
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)