winctx 0.0.20

A minimal window context for Rust on Windows.
Documentation
//! Types related to events produced by this library.

use crate::{AreaId, Error, ItemId, NotificationId};

/// A mouse button.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
#[repr(u32)]
pub enum MouseButton {
    /// Left mouse button.
    Left = 0x1,
    /// Right mouse button.
    Right = 0x2,
}

/// A collection of mouse buttons.
#[derive(Debug)]
pub struct MouseButtons(u32);

impl MouseButtons {
    /// A set containing only the right mouse button.
    pub(super) const RIGHT: Self = Self(MouseButton::Right as u32);

    /// Copy the buttons collection.
    ///
    /// Note that we don't want to implement [`Copy`] for this type as it would
    /// be too leaky.
    pub(super) const fn copy_data(&self) -> Self {
        Self(self.0)
    }

    /// Create a new empty collection of mouse buttons.
    pub(super) const fn empty() -> Self {
        Self(0)
    }

    /// Create a new collection of mouse buttons.
    pub(super) fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = MouseButton>,
    {
        let mut buttons = 0;

        for button in iter {
            buttons |= button as u32;
        }

        Self(buttons)
    }

    /// Test if the given mouse button is active.
    pub fn test(&self, button: MouseButton) -> bool {
        self.0 & button as u32 != 0
    }
}

/// An event generated by a mouse click.
#[derive(Debug)]
#[non_exhaustive]
pub struct MouseEvent {
    /// Mouse button responsible for the event.
    pub buttons: MouseButtons,
}

/// A clipbaord event.
#[derive(Debug)]
#[non_exhaustive]
pub enum ClipboardEvent {
    /// A bitmap has been copied.
    BitMap(Vec<u8>),
    /// A string has been copied.
    Text(String),
}

/// An event emitted by the event loop.
#[derive(Debug)]
#[non_exhaustive]
pub enum Event {
    /// Window has been shut down.
    Shutdown {},
    /// The menu item identified by [`ItemId`] has been clicked.
    MenuItemClicked {
        /// The item that was clicked.
        item_id: ItemId,
        /// The generated event.
        event: MouseEvent,
    },
    /// An icon has been clicked.
    IconClicked {
        /// The area that was clicked.
        area_id: AreaId,
        /// The generated event.
        event: MouseEvent,
    },
    /// Indicates that the notification with the associated token has been clicked.
    NotificationClicked {
        /// The area the notification belonged to.
        area_id: AreaId,
        /// The identifier of the notification.
        id: NotificationId,
        /// The generated event.
        event: MouseEvent,
    },
    /// The notification associated with the given token either timed out or was dismissed.
    NotificationDismissed {
        /// The area from which the dismissed notification originated.
        area_id: AreaId,
        /// The identifier of the dismissed notification.
        id: NotificationId,
    },
    /// The system clipboard has been modified.
    Clipboard {
        /// The generated clipboard event.
        event: ClipboardEvent,
    },
    /// Data was copied to the current process remotely using
    /// [`Window::copy_data`].
    ///
    /// [`Window::copy_data`]: crate::Window::copy_data
    CopyData {
        /// The type parameter passed when sending the data.
        ty: usize,
        /// The data.
        data: Vec<u8>,
    },
    /// A non-fatal error has been reported.
    Error {
        /// The reported error.
        error: Error,
    },
}