use parking_lot::RwLock;
use std::sync::Arc;
type EventCallback<T> = Box<dyn Fn(&T) + Send + Sync>;
pub struct EventHandler<T> {
handlers: Arc<RwLock<Vec<EventCallback<T>>>>,
}
impl<T> std::fmt::Debug for EventHandler<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EventHandler")
.field("handlers", &format!("{} handler(s)", self.handlers.read().len()))
.finish()
}
}
impl<T> EventHandler<T> {
pub fn new() -> Self {
Self {
handlers: Arc::new(RwLock::new(Vec::new())),
}
}
pub fn subscribe<F>(&self, handler: F)
where
F: Fn(&T) + Send + Sync + 'static,
{
self.handlers.write().push(Box::new(handler));
}
pub fn invoke(&self, args: &T) {
for handler in self.handlers.read().iter() {
handler(args);
}
}
pub fn subscriber_count(&self) -> usize {
self.handlers.read().len()
}
pub fn clear(&self) {
self.handlers.write().clear();
}
}
impl<T> Clone for EventHandler<T> {
fn clone(&self) -> Self {
Self {
handlers: self.handlers.clone(),
}
}
}
impl<T> Default for EventHandler<T> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct RoutedEventArgs {
pub handled: bool,
pub source: Option<String>,
}
impl RoutedEventArgs {
pub fn new() -> Self {
Self {
handled: false,
source: None,
}
}
pub fn with_source(source: impl Into<String>) -> Self {
Self {
handled: false,
source: Some(source.into()),
}
}
}
impl Default for RoutedEventArgs {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct ClickEventArgs {
pub routed: RoutedEventArgs,
}
impl ClickEventArgs {
pub fn new() -> Self {
Self {
routed: RoutedEventArgs::new(),
}
}
}
impl Default for ClickEventArgs {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct TextChangedEventArgs {
pub routed: RoutedEventArgs,
pub text: String,
}
impl TextChangedEventArgs {
pub fn new(text: impl Into<String>) -> Self {
Self {
routed: RoutedEventArgs::new(),
text: text.into(),
}
}
}
#[derive(Debug, Clone)]
pub struct SelectionChangedEventArgs {
pub routed: RoutedEventArgs,
pub selected_index: i32,
}
impl SelectionChangedEventArgs {
pub fn new(selected_index: i32) -> Self {
Self {
routed: RoutedEventArgs::new(),
selected_index,
}
}
}
#[derive(Debug, Clone)]
pub struct ValueChangedEventArgs<T> {
pub routed: RoutedEventArgs,
pub old_value: T,
pub new_value: T,
}
impl<T> ValueChangedEventArgs<T> {
pub fn new(old_value: T, new_value: T) -> Self {
Self {
routed: RoutedEventArgs::new(),
old_value,
new_value,
}
}
}
#[derive(Debug, Clone)]
pub struct CheckedEventArgs {
pub routed: RoutedEventArgs,
pub is_checked: bool,
}
impl CheckedEventArgs {
pub fn new(is_checked: bool) -> Self {
Self {
routed: RoutedEventArgs::new(),
is_checked,
}
}
}
#[derive(Debug, Clone)]
pub struct KeyEventArgs {
pub routed: RoutedEventArgs,
pub key_code: u32,
pub is_down: bool,
}
impl KeyEventArgs {
pub fn new(key_code: u32, is_down: bool) -> Self {
Self {
routed: RoutedEventArgs::new(),
key_code,
is_down,
}
}
}
#[derive(Debug, Clone)]
pub struct MouseEventArgs {
pub routed: RoutedEventArgs,
pub x: i32,
pub y: i32,
}
impl MouseEventArgs {
pub fn new(x: i32, y: i32) -> Self {
Self {
routed: RoutedEventArgs::new(),
x,
y,
}
}
}
#[derive(Debug, Clone)]
pub struct FocusEventArgs {
pub routed: RoutedEventArgs,
pub got_focus: bool,
}
impl FocusEventArgs {
pub fn new(got_focus: bool) -> Self {
Self {
routed: RoutedEventArgs::new(),
got_focus,
}
}
}