bar_config/event.rs
1//! Data models used for sending events to the config.
2//!
3//! This module provides everything necessary for sending events from the bar frontend to the
4//! configuration. Implementing all of these events is necessary to make sure that all components
5//! will function properly.
6//!
7//! # Examples
8//!
9//! Here's a minimal example for sending events to the config:
10//! ```
11//! use bar_config::event::{Event, Point};
12//! use bar_config::Bar;
13//! use std::io::Cursor;
14//!
15//! let config_file = Cursor::new(String::from(
16//! "height: 30\n\
17//! monitors:\n\
18//! - { name: \"DVI-1\" }"
19//! ));
20//!
21//! let mut bar = Bar::load(config_file).unwrap();
22//! bar.notify(Event::MouseMotion(Point { x: 0, y: 0 }));
23//! ```
24
25use crate::components::ComponentID;
26
27/// Event which can be transmitted to the components.
28///
29/// This event needs to be created from the frontend and can then be sent to the bar using
30/// the [`Bar::notify`] method. Every component has the choice to use an event or ignore it.
31///
32/// [`Bar::notify`]: struct.Bar.html#method.notify
33#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
34pub enum Event {
35 /// Mouse button action anywhere on the screen.
36 ///
37 /// This event notifies all components that the user has clicked anywhere on the screen.
38 /// It is required that the component knows about its position to act upon this event.
39 /// To let a component know about its current position, the [`PositionChange`] event
40 /// can be used.
41 ///
42 /// [`PositionChange`]: enum.Event.html#variant.PositionChange
43 Click(MouseButton, MouseButtonState, Point),
44
45 /// Update mouse position.
46 ///
47 /// This event notifies all components about the current position of the mouse on the screen.
48 /// It is required that the component knows about its position to act upon this event.
49 /// To let a component know about its current position, the [`PositionChange`] event
50 /// can be used.
51 ///
52 /// [`PositionChange`]: enum.Event.html#variant.PositionChange
53 MouseMotion(Point),
54
55 /// Update the position of a component.
56 ///
57 /// This event is used to make a component aware of its position on the screen. This is
58 /// required to react upon other events which are position dependent.
59 PositionChange(ComponentPosition),
60}
61
62/// Button on the mouse.
63///
64/// This is required for the [`Click`] event.
65///
66/// [`Click`]: enum.Event.html#variant.Click
67#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
68pub enum MouseButton {
69 Left,
70 Center,
71 Right,
72 WheelUp,
73 WheelDown,
74}
75
76/// Mouse button states.
77///
78/// This is required for the [`Click`] event.
79///
80/// [`Click`]: enum.Event.html#variant.Click
81#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
82pub enum MouseButtonState {
83 Pressed,
84 Released,
85}
86
87/// Exact position of a component on the screen.
88///
89/// This must be used in the [`PositionChange`] event which notifies components about their position
90/// on the screen.
91///
92/// [`PositionChange`]: enum.Event.html#variant.PositionChange
93#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
94pub struct ComponentPosition {
95 pub comp_id: ComponentID,
96 pub min_x: usize,
97 pub max_x: usize,
98 pub min_y: usize,
99 pub max_y: usize,
100}
101
102/// Point on the screen.
103#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd)]
104pub struct Point {
105 pub x: u32,
106 pub y: u32,
107}