Skip to main content

flux_tui/window/
event_queue.rs

1use std::sync::{OnceLock, mpsc::*};
2
3use crossterm::event::Event;
4
5use super::*;
6
7// Changed property with the corresponding UID of the [Window]
8pub(super) struct ChangedProperty {
9	pub(super) uid: WindowUID,
10	pub(super) property: WindowProperty,
11}
12
13impl ChangedProperty {
14	fn new(uid: WindowUID, property: WindowProperty) -> Self {
15		Self { property, uid }
16	}
17}
18
19/// All Properties of the [Window] trait that can be evoked
20#[derive(Debug, Copy, Clone, PartialEq, Hash)]
21pub enum WindowProperty {
22	Focus,
23	Children,
24	Alignment,
25	Size,
26	Margin,
27	Border,
28	IsVisible,
29}
30
31pub(super) struct TreeUpdate {
32	pub win: WindowRef,
33	pub depth: u8,
34	pub rect: Rect,
35}
36
37pub(super) struct FocusUpdate {
38	pub win: WindowRef,
39	pub depth: u8,
40}
41
42static SENDER: OnceLock<SyncSender<ChangedProperty>> = OnceLock::new();
43
44/// Handles externally fired events
45pub struct WindowEventQueue {
46	pub(super) recv: Receiver<ChangedProperty>,
47}
48
49impl WindowEventQueue {
50	pub(super) fn initialize() -> Self {
51		let (send, recv) = sync_channel(32);
52		SENDER.set(send).unwrap();
53		Self { recv }
54	}
55
56	/// On the next render call, all internal metadata that correlate to the property will update
57	pub fn provoke_changed_property(uid: WindowUID, property: WindowProperty) {
58		if let Some(sender) = SENDER.get() {
59			let change = ChangedProperty::new(uid, property);
60			sender.send(change).unwrap();
61		}
62	}
63}
64
65#[derive(Debug)]
66/// [Event] wrapper with some additional information used by this library
67pub struct WindowEvent {
68	event: Event,
69	pub handled: bool,
70}
71
72impl WindowEvent {
73	pub(crate) fn new(event: Event) -> Self {
74		Self {
75			event,
76			handled: false,
77		}
78	}
79
80	/// Returns the raw [Event]
81	pub fn raw(&self) -> &Event {
82		&self.event
83	}
84}