|
| 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 | +} |
0 commit comments