1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{Event, Toplevel, ToplevelSize};
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::IsA;
use std::boxed::Box as Box_;
use std::mem::transmute;

pub trait ToplevelExtManual {
    /// Requests that the `self` inhibit the system shortcuts.
    ///
    /// This is asking the desktop environment/windowing system to let all
    /// keyboard events reach the surface, as long as it is focused, instead
    /// of triggering system actions.
    ///
    /// If granted, the rerouting remains active until the default shortcuts
    /// processing is restored with [``ToplevelExt::restore_system_shortcuts()``][crate::prelude::`ToplevelExt::restore_system_shortcuts()`],
    /// or the request is revoked by the desktop environment, windowing system
    /// or the user.
    ///
    /// A typical use case for this API is remote desktop or virtual machine
    /// viewers which need to inhibit the default system keyboard shortcuts
    /// so that the remote session or virtual host gets those instead of the
    /// local environment.
    ///
    /// The windowing system or desktop environment may ask the user to grant
    /// or deny the request or even choose to ignore the request entirely.
    ///
    /// The caller can be notified whenever the request is granted or revoked
    /// by listening to the `property::Toplevel::shortcuts-inhibited` property.
    /// ## `event`
    /// the [`Event`][crate::Event] that is triggering the inhibit
    ///  request, or [`None`] if none is available
    #[doc(alias = "gdk_toplevel_inhibit_system_shortcuts")]
    fn inhibit_system_shortcuts<P: AsRef<Event>>(&self, event: Option<&P>);

    /// Asks the windowing system to show the window menu.
    ///
    /// The window menu is the menu shown when right-clicking the titlebar
    /// on traditional windows managed by the window manager. This is useful
    /// for windows using client-side decorations, activating it with a
    /// right-click on the window decorations.
    /// ## `event`
    /// a [`Event`][crate::Event] to show the menu for
    ///
    /// # Returns
    ///
    /// [`true`] if the window menu was shown and [`false`] otherwise.
    #[doc(alias = "gdk_toplevel_show_window_menu")]
    fn show_window_menu<P: AsRef<Event>>(&self, event: &P) -> bool;
    /// Emitted when the size for the surface needs to be computed, when
    /// it is present.
    ///
    /// It will normally be emitted during or after [``ToplevelExt::present()``][crate::prelude::`ToplevelExt::present()`],
    /// depending on the configuration received by the windowing system.
    /// It may also be emitted at any other point in time, in response
    /// to the windowing system spontaneously changing the configuration.
    ///
    /// It is the responsibility of the toplevel user to handle this signal
    /// and compute the desired size of the toplevel, given the information
    /// passed via the [`ToplevelSize`][crate::ToplevelSize] object. Failing to do so
    /// will result in an arbitrary size being used as a result.
    ///
    /// # Returns
    ///
    ///
    /// ## `size`
    /// a [`ToplevelSize`][crate::ToplevelSize]
    fn connect_compute_size<F: Fn(&Toplevel, &mut ToplevelSize) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<Toplevel>> ToplevelExtManual for O {
    fn connect_compute_size<F: Fn(&Toplevel, &mut ToplevelSize) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn compute_size_trampoline<
            F: Fn(&Toplevel, &mut ToplevelSize) + 'static,
        >(
            this: *mut ffi::GdkToplevel,
            size: *mut ffi::GdkToplevelSize,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &mut *(size as *mut ToplevelSize))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"compute-size\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    compute_size_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn inhibit_system_shortcuts<P: AsRef<Event>>(&self, event: Option<&P>) {
        unsafe {
            ffi::gdk_toplevel_inhibit_system_shortcuts(
                self.as_ref().to_glib_none().0,
                event.map(|e| e.as_ref()).to_glib_none().0,
            );
        }
    }

    fn show_window_menu<P: AsRef<Event>>(&self, event: &P) -> bool {
        unsafe {
            from_glib(ffi::gdk_toplevel_show_window_menu(
                self.as_ref().to_glib_none().0,
                event.as_ref().to_glib_none().0,
            ))
        }
    }
}