gio/auto/simple_action.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Action, ffi};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// A `GSimpleAction` is the obvious simple implementation of the
16 /// [`Action`][crate::Action] interface. This is the easiest way to create an action for
17 /// purposes of adding it to a [`SimpleActionGroup`][crate::SimpleActionGroup].
18 ///
19 /// ## Properties
20 ///
21 ///
22 /// #### `enabled`
23 /// If @action is currently enabled.
24 ///
25 /// If the action is disabled then calls to g_action_activate() and
26 /// g_action_change_state() have no effect.
27 ///
28 /// Readable | Writeable
29 ///
30 ///
31 /// #### `name`
32 /// The name of the action. This is mostly meaningful for identifying
33 /// the action once it has been added to a #GSimpleActionGroup.
34 ///
35 /// Readable | Writeable | Construct Only
36 ///
37 ///
38 /// #### `parameter-type`
39 /// The type of the parameter that must be given when activating the
40 /// action.
41 ///
42 /// Readable | Writeable | Construct Only
43 ///
44 ///
45 /// #### `state`
46 /// The state of the action, or [`None`] if the action is stateless.
47 ///
48 /// Readable | Writeable | Construct
49 ///
50 ///
51 /// #### `state-type`
52 /// The #GVariantType of the state that the action has, or [`None`] if the
53 /// action is stateless.
54 ///
55 /// Readable
56 /// <details><summary><h4>Action</h4></summary>
57 ///
58 ///
59 /// #### `enabled`
60 /// If @action is currently enabled.
61 ///
62 /// If the action is disabled then calls to [`ActionExt::activate()`][crate::prelude::ActionExt::activate()] and
63 /// [`ActionExt::change_state()`][crate::prelude::ActionExt::change_state()] have no effect.
64 ///
65 /// Readable
66 ///
67 ///
68 /// #### `name`
69 /// The name of the action. This is mostly meaningful for identifying
70 /// the action once it has been added to a [type@Gio.ActionGroup]. It is immutable.
71 ///
72 /// Readable
73 ///
74 ///
75 /// #### `parameter-type`
76 /// The type of the parameter that must be given when activating the
77 /// action. This is immutable, and may be `NULL` if no parameter is needed when
78 /// activating the action.
79 ///
80 /// Readable
81 ///
82 ///
83 /// #### `state`
84 /// The state of the action, or `NULL` if the action is stateless.
85 ///
86 /// Readable
87 ///
88 ///
89 /// #### `state-type`
90 /// The [type@GLib.VariantType] of the state that the action has, or `NULL` if the
91 /// action is stateless. This is immutable.
92 ///
93 /// Readable
94 /// </details>
95 ///
96 /// ## Signals
97 ///
98 ///
99 /// #### `activate`
100 /// Indicates that the action was just activated.
101 ///
102 /// @parameter will always be of the expected type, i.e. the parameter type
103 /// specified when the action was created. If an incorrect type is given when
104 /// activating the action, this signal is not emitted.
105 ///
106 /// Since GLib 2.40, if no handler is connected to this signal then the
107 /// default behaviour for boolean-stated actions with a [`None`] parameter
108 /// type is to toggle them via the #GSimpleAction::change-state signal.
109 /// For stateful actions where the state type is equal to the parameter
110 /// type, the default is to forward them directly to
111 /// #GSimpleAction::change-state. This should allow almost all users
112 /// of #GSimpleAction to connect only one handler or the other.
113 ///
114 ///
115 ///
116 ///
117 /// #### `change-state`
118 /// Indicates that the action just received a request to change its
119 /// state.
120 ///
121 /// @value will always be of the correct state type, i.e. the type of the
122 /// initial state passed to g_simple_action_new_stateful(). If an incorrect
123 /// type is given when requesting to change the state, this signal is not
124 /// emitted.
125 ///
126 /// If no handler is connected to this signal then the default
127 /// behaviour is to call g_simple_action_set_state() to set the state
128 /// to the requested value. If you connect a signal handler then no
129 /// default action is taken. If the state should change then you must
130 /// call g_simple_action_set_state() from the handler.
131 ///
132 /// An example of a 'change-state' handler:
133 ///
134 ///
135 /// **⚠️ The following code is in C ⚠️**
136 ///
137 /// ```C
138 /// static void
139 /// change_volume_state (GSimpleAction *action,
140 /// GVariant *value,
141 /// gpointer user_data)
142 /// {
143 /// gint requested;
144 ///
145 /// requested = g_variant_get_int32 (value);
146 ///
147 /// // Volume only goes from 0 to 10
148 /// if (0 <= requested && requested <= 10)
149 /// g_simple_action_set_state (action, value);
150 /// }
151 /// ```
152 ///
153 /// The handler need not set the state to the requested value.
154 /// It could set it to any value at all, or take some other action.
155 ///
156 ///
157 ///
158 /// # Implements
159 ///
160 /// [`trait@glib::ObjectExt`], [`ActionExt`][trait@crate::prelude::ActionExt]
161 #[doc(alias = "GSimpleAction")]
162 pub struct SimpleAction(Object<ffi::GSimpleAction>) @implements Action;
163
164 match fn {
165 type_ => || ffi::g_simple_action_get_type(),
166 }
167}
168
169impl SimpleAction {
170 /// Creates a new action.
171 ///
172 /// The created action is stateless. See g_simple_action_new_stateful() to create
173 /// an action that has state.
174 /// ## `name`
175 /// the name of the action
176 /// ## `parameter_type`
177 /// the type of parameter that will be passed to
178 /// handlers for the #GSimpleAction::activate signal, or [`None`] for no parameter
179 ///
180 /// # Returns
181 ///
182 /// a new #GSimpleAction
183 #[doc(alias = "g_simple_action_new")]
184 pub fn new(name: &str, parameter_type: Option<&glib::VariantTy>) -> SimpleAction {
185 unsafe {
186 from_glib_full(ffi::g_simple_action_new(
187 name.to_glib_none().0,
188 parameter_type.to_glib_none().0,
189 ))
190 }
191 }
192
193 /// Creates a new stateful action.
194 ///
195 /// All future state values must have the same #GVariantType as the initial
196 /// @state.
197 ///
198 /// If the @state #GVariant is floating, it is consumed.
199 /// ## `name`
200 /// the name of the action
201 /// ## `parameter_type`
202 /// the type of the parameter that will be passed to
203 /// handlers for the #GSimpleAction::activate signal, or [`None`] for no parameter
204 /// ## `state`
205 /// the initial state of the action
206 ///
207 /// # Returns
208 ///
209 /// a new #GSimpleAction
210 #[doc(alias = "g_simple_action_new_stateful")]
211 pub fn new_stateful(
212 name: &str,
213 parameter_type: Option<&glib::VariantTy>,
214 state: &glib::Variant,
215 ) -> SimpleAction {
216 unsafe {
217 from_glib_full(ffi::g_simple_action_new_stateful(
218 name.to_glib_none().0,
219 parameter_type.to_glib_none().0,
220 state.to_glib_none().0,
221 ))
222 }
223 }
224
225 /// Sets the action as enabled or not.
226 ///
227 /// An action must be enabled in order to be activated or in order to
228 /// have its state changed from outside callers.
229 ///
230 /// This should only be called by the implementor of the action. Users
231 /// of the action should not attempt to modify its enabled flag.
232 /// ## `enabled`
233 /// whether the action is enabled
234 #[doc(alias = "g_simple_action_set_enabled")]
235 #[doc(alias = "enabled")]
236 pub fn set_enabled(&self, enabled: bool) {
237 unsafe {
238 ffi::g_simple_action_set_enabled(self.to_glib_none().0, enabled.into_glib());
239 }
240 }
241
242 /// Sets the state of the action.
243 ///
244 /// This directly updates the 'state' property to the given value.
245 ///
246 /// This should only be called by the implementor of the action. Users
247 /// of the action should not attempt to directly modify the 'state'
248 /// property. Instead, they should call g_action_change_state() to
249 /// request the change.
250 ///
251 /// If the @value GVariant is floating, it is consumed.
252 /// ## `value`
253 /// the new #GVariant for the state
254 #[doc(alias = "g_simple_action_set_state")]
255 #[doc(alias = "state")]
256 pub fn set_state(&self, value: &glib::Variant) {
257 unsafe {
258 ffi::g_simple_action_set_state(self.to_glib_none().0, value.to_glib_none().0);
259 }
260 }
261
262 /// Sets the state hint for the action.
263 ///
264 /// See g_action_get_state_hint() for more information about
265 /// action state hints.
266 /// ## `state_hint`
267 /// a #GVariant representing the state hint
268 #[doc(alias = "g_simple_action_set_state_hint")]
269 pub fn set_state_hint(&self, state_hint: Option<&glib::Variant>) {
270 unsafe {
271 ffi::g_simple_action_set_state_hint(self.to_glib_none().0, state_hint.to_glib_none().0);
272 }
273 }
274
275 /// Indicates that the action was just activated.
276 ///
277 /// @parameter will always be of the expected type, i.e. the parameter type
278 /// specified when the action was created. If an incorrect type is given when
279 /// activating the action, this signal is not emitted.
280 ///
281 /// Since GLib 2.40, if no handler is connected to this signal then the
282 /// default behaviour for boolean-stated actions with a [`None`] parameter
283 /// type is to toggle them via the #GSimpleAction::change-state signal.
284 /// For stateful actions where the state type is equal to the parameter
285 /// type, the default is to forward them directly to
286 /// #GSimpleAction::change-state. This should allow almost all users
287 /// of #GSimpleAction to connect only one handler or the other.
288 /// ## `parameter`
289 /// the parameter to the activation, or [`None`] if it has
290 /// no parameter
291 #[doc(alias = "activate")]
292 pub fn connect_activate<F: Fn(&Self, Option<&glib::Variant>) + 'static>(
293 &self,
294 f: F,
295 ) -> SignalHandlerId {
296 unsafe extern "C" fn activate_trampoline<
297 F: Fn(&SimpleAction, Option<&glib::Variant>) + 'static,
298 >(
299 this: *mut ffi::GSimpleAction,
300 parameter: *mut glib::ffi::GVariant,
301 f: glib::ffi::gpointer,
302 ) {
303 unsafe {
304 let f: &F = &*(f as *const F);
305 f(
306 &from_glib_borrow(this),
307 Option::<glib::Variant>::from_glib_borrow(parameter)
308 .as_ref()
309 .as_ref(),
310 )
311 }
312 }
313 unsafe {
314 let f: Box_<F> = Box_::new(f);
315 connect_raw(
316 self.as_ptr() as *mut _,
317 c"activate".as_ptr() as *const _,
318 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
319 activate_trampoline::<F> as *const (),
320 )),
321 Box_::into_raw(f),
322 )
323 }
324 }
325
326 /// Indicates that the action just received a request to change its
327 /// state.
328 ///
329 /// @value will always be of the correct state type, i.e. the type of the
330 /// initial state passed to g_simple_action_new_stateful(). If an incorrect
331 /// type is given when requesting to change the state, this signal is not
332 /// emitted.
333 ///
334 /// If no handler is connected to this signal then the default
335 /// behaviour is to call g_simple_action_set_state() to set the state
336 /// to the requested value. If you connect a signal handler then no
337 /// default action is taken. If the state should change then you must
338 /// call g_simple_action_set_state() from the handler.
339 ///
340 /// An example of a 'change-state' handler:
341 ///
342 ///
343 /// **⚠️ The following code is in C ⚠️**
344 ///
345 /// ```C
346 /// static void
347 /// change_volume_state (GSimpleAction *action,
348 /// GVariant *value,
349 /// gpointer user_data)
350 /// {
351 /// gint requested;
352 ///
353 /// requested = g_variant_get_int32 (value);
354 ///
355 /// // Volume only goes from 0 to 10
356 /// if (0 <= requested && requested <= 10)
357 /// g_simple_action_set_state (action, value);
358 /// }
359 /// ```
360 ///
361 /// The handler need not set the state to the requested value.
362 /// It could set it to any value at all, or take some other action.
363 /// ## `value`
364 /// the requested value for the state
365 #[doc(alias = "change-state")]
366 pub fn connect_change_state<F: Fn(&Self, Option<&glib::Variant>) + 'static>(
367 &self,
368 f: F,
369 ) -> SignalHandlerId {
370 unsafe extern "C" fn change_state_trampoline<
371 F: Fn(&SimpleAction, Option<&glib::Variant>) + 'static,
372 >(
373 this: *mut ffi::GSimpleAction,
374 value: *mut glib::ffi::GVariant,
375 f: glib::ffi::gpointer,
376 ) {
377 unsafe {
378 let f: &F = &*(f as *const F);
379 f(
380 &from_glib_borrow(this),
381 Option::<glib::Variant>::from_glib_borrow(value)
382 .as_ref()
383 .as_ref(),
384 )
385 }
386 }
387 unsafe {
388 let f: Box_<F> = Box_::new(f);
389 connect_raw(
390 self.as_ptr() as *mut _,
391 c"change-state".as_ptr() as *const _,
392 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
393 change_state_trampoline::<F> as *const (),
394 )),
395 Box_::into_raw(f),
396 )
397 }
398 }
399}