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
//! Data models used for sending events to the config.
//!
//! This module provides everything necessary for sending events from the bar frontend to the
//! configuration. Implementing all of these events is necessary to make sure that all components
//! will function properly.
//!
//! # Examples
//!
//! Here's a minimal example for sending events to the config:
//! ```
//! use bar_config::event::{Event, Point};
//! use bar_config::Bar;
//! use std::io::Cursor;
//!
//! let config_file = Cursor::new(String::from(
//!     "height: 30\n\
//!      monitors:\n\
//!       - { name: \"DVI-1\" }"
//! ));
//!
//! let mut bar = Bar::load(config_file).unwrap();
//! bar.notify(Event::MouseMotion(Point { x: 0, y: 0 }));
//! ```

use crate::components::ComponentID;

/// Event which can be transmitted to the components.
///
/// This event needs to be created from the frontend and can then be sent to the bar using
/// the [`Bar::notify`] method. Every component has the choice to use an event or ignore it.
///
/// [`Bar::notify`]: struct.Bar.html#method.notify
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
pub enum Event {
    /// Mouse button action anywhere on the screen.
    ///
    /// This event notifies all components that the user has clicked anywhere on the screen.
    /// It is required that the component knows about its position to act upon this event.
    /// To let a component know about its current position, the [`PositionChange`] event
    /// can be used.
    ///
    /// [`PositionChange`]: enum.Event.html#variant.PositionChange
    Click(MouseButton, MouseButtonState, Point),

    /// Update mouse position.
    ///
    /// This event notifies all components about the current position of the mouse on the screen.
    /// It is required that the component knows about its position to act upon this event.
    /// To let a component know about its current position, the [`PositionChange`] event
    /// can be used.
    ///
    /// [`PositionChange`]: enum.Event.html#variant.PositionChange
    MouseMotion(Point),

    /// Update the position of a component.
    ///
    /// This event is used to make a component aware of its position on the screen. This is
    /// required to react upon other events which are position dependent.
    PositionChange(ComponentPosition),
}

/// Button on the mouse.
///
/// This is required for the [`Click`] event.
///
/// [`Click`]: enum.Event.html#variant.Click
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
pub enum MouseButton {
    Left,
    Center,
    Right,
    WheelUp,
    WheelDown,
}

/// Mouse button states.
///
/// This is required for the [`Click`] event.
///
/// [`Click`]: enum.Event.html#variant.Click
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
pub enum MouseButtonState {
    Pressed,
    Released,
}

/// Exact position of a component on the screen.
///
/// This must be used in the [`PositionChange`] event which notifies components about their position
/// on the screen.
///
/// [`PositionChange`]: enum.Event.html#variant.PositionChange
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
pub struct ComponentPosition {
    pub comp_id: ComponentID,
    pub min_x: usize,
    pub max_x: usize,
    pub min_y: usize,
    pub max_y: usize,
}

/// Point on the screen.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
pub struct Point {
    pub x: u32,
    pub y: u32,
}