Skip to content
Snippets Groups Projects
Commit 0af32b00 authored by MrGVSV's avatar MrGVSV
Browse files

Extracted ChangeEvent and made it generic

parent ea84d912
No related branches found
No related tags found
No related merge requests found
...@@ -3,13 +3,14 @@ use bevy::{ ...@@ -3,13 +3,14 @@ use bevy::{
window::WindowDescriptor, window::WindowDescriptor,
DefaultPlugins, DefaultPlugins,
}; };
use kayak_core::OnChange;
use kayak_ui::bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle}; use kayak_ui::bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle};
use kayak_ui::core::{ use kayak_ui::core::{
render, rsx, render, rsx,
styles::{Style, StyleProp, Units}, styles::{Style, StyleProp, Units},
widget, Bound, Index, MutableBound, widget, Bound, Index, MutableBound,
}; };
use kayak_widgets::{App, OnChange, TextBox, Window}; use kayak_widgets::{App, TextBox, Window};
#[widget] #[widget]
fn TextBoxExample(context: &mut KayakContext) { fn TextBoxExample(context: &mut KayakContext) {
......
use std::sync::{Arc, RwLock};
use crate::{Index, KeyCode}; use crate::{Index, KeyCode};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
...@@ -28,3 +29,39 @@ pub enum EventType { ...@@ -28,3 +29,39 @@ pub enum EventType {
CharInput { c: char }, CharInput { c: char },
KeyboardInput { key: KeyCode }, KeyboardInput { key: KeyCode },
} }
/// An event denoting a change of some type
#[derive(Debug, Clone, PartialEq)]
pub struct ChangeEvent<T> {
pub value: T,
}
/// A handler struct for a [ChangeEvent].
///
/// ## Example
/// ```rust
/// let handler = OnChange::new(move |event| {
/// let value = event.value;
/// // Do something...
/// });
/// ```
#[derive(Clone)]
pub struct OnChange<T>(pub Arc<RwLock<dyn FnMut(ChangeEvent<T>) + Send + Sync + 'static>>);
impl<T> OnChange<T> {
pub fn new<F: FnMut(ChangeEvent<T>) + Send + Sync + 'static>(f: F) -> Self {
Self(Arc::new(RwLock::new(f)))
}
}
impl<T> PartialEq for OnChange<T> {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl<T> std::fmt::Debug for OnChange<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("OnChange").finish()
}
}
\ No newline at end of file
use std::sync::{Arc, RwLock};
use kayak_ui::core::{ use kayak_ui::core::{
Bound, ChangeEvent, Color, EventType, MutableBound, OnChange, OnEvent,
render_command::RenderCommand, render_command::RenderCommand,
rsx, rsx,
styles::{Style, StyleProp, Units}, styles::{Style, StyleProp, Units},
widget, Bound, Color, EventType, MutableBound, OnEvent, widget
}; };
use std::sync::{Arc, RwLock};
use crate::{Background, Clip, Text}; use crate::{Background, Clip, Text};
#[derive(Debug, Clone, PartialEq)]
pub struct ChangeEvent {
pub value: String,
}
#[derive(Clone)]
pub struct OnChange(pub Arc<RwLock<dyn FnMut(ChangeEvent) + Send + Sync + 'static>>);
impl OnChange {
pub fn new<F: FnMut(ChangeEvent) + Send + Sync + 'static>(f: F) -> OnChange {
OnChange(Arc::new(RwLock::new(f)))
}
}
impl PartialEq for OnChange {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl std::fmt::Debug for OnChange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("OnChange").finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Focus(pub bool); pub struct Focus(pub bool);
#[widget(focusable)] #[widget(focusable)]
pub fn TextBox(value: String, on_change: Option<OnChange>) { pub fn TextBox(value: String, on_change: Option<OnChange<String>>) {
*styles = Some(Style { *styles = Some(Style {
render_command: StyleProp::Value(RenderCommand::Layout), render_command: StyleProp::Value(RenderCommand::Layout),
..styles.clone().unwrap_or_default() ..styles.clone().unwrap_or_default()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment