flux-tui 0.5.0

Fast and lightweight Terminal UI drawing library
Documentation
use std::sync::{OnceLock, mpsc::*};

use crossterm::event::Event;

use super::*;

// Changed property with the corresponding UID of the [Window]
pub(super) struct ChangedProperty {
	pub(super) uid: WindowUID,
	pub(super) property: WindowProperty,
}

impl ChangedProperty {
	fn new(uid: WindowUID, property: WindowProperty) -> Self {
		Self { property, uid }
	}
}

/// All Properties of the [Window] trait that can be evoked
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
pub enum WindowProperty {
	Focus,
	Children,
	Alignment,
	Size,
	Margin,
	Border,
	IsVisible,
}

pub(super) struct TreeUpdate {
	pub win: WindowRef,
	pub depth: u8,
	pub rect: Rect,
}

pub(super) struct FocusUpdate {
	pub win: WindowRef,
	pub depth: u8,
}

static SENDER: OnceLock<SyncSender<ChangedProperty>> = OnceLock::new();

/// Handles externally fired events
pub struct WindowEventQueue {
	pub(super) recv: Receiver<ChangedProperty>,
}

impl WindowEventQueue {
	pub(super) fn initialize() -> Self {
		let (send, recv) = sync_channel(32);
		SENDER.set(send).unwrap();
		Self { recv }
	}

	/// On the next render call, all internal metadata that correlate to the property will update
	pub fn provoke_changed_property(uid: WindowUID, property: WindowProperty) {
		if let Some(sender) = SENDER.get() {
			let change = ChangedProperty::new(uid, property);
			sender.send(change).unwrap();
		}
	}
}

#[derive(Debug)]
/// [Event] wrapper with some additional information used by this library
pub struct WindowEvent {
	event: Event,
	pub handled: bool,
}

impl WindowEvent {
	pub(crate) fn new(event: Event) -> Self {
		Self {
			event,
			handled: false,
		}
	}

	/// Returns the raw [Event]
	pub fn raw(&self) -> &Event {
		&self.event
	}
}