use std::convert::Infallible;
use crate::{
data_widget::compose_child_as_data_widget, impl_compose_child_with_focus_for_listener,
impl_listener, impl_query_self_only, prelude::*,
};
#[derive(Declare)]
pub struct CharsListener {
#[declare(builtin, convert=custom)]
on_chars: MutRefItemSubject<'static, CharsEvent, Infallible>,
}
#[derive(Debug)]
pub struct CharsEvent {
pub chars: String,
pub common: EventCommon,
}
impl_listener!(
CharsListener,
CharsListenerDeclarer,
on_chars,
CharsEvent,
char_stream
);
impl_compose_child_with_focus_for_listener!(CharsListener);
impl std::borrow::Borrow<EventCommon> for CharsEvent {
#[inline]
fn borrow(&self) -> &EventCommon { &self.common }
}
impl std::borrow::BorrowMut<EventCommon> for CharsEvent {
#[inline]
fn borrow_mut(&mut self) -> &mut EventCommon { &mut self.common }
}
impl std::ops::Deref for CharsEvent {
type Target = EventCommon;
#[inline]
fn deref(&self) -> &Self::Target { &self.common }
}
impl std::ops::DerefMut for CharsEvent {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.common }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helper::*;
use std::{cell::RefCell, rc::Rc};
use winit::event::WindowEvent;
#[test]
fn smoke() {
let receive = Rc::new(RefCell::new("".to_string()));
let c_receive = receive.clone();
let widget = widget! {
MockBox {
size: ZERO_SIZE,
auto_focus: true,
on_chars: move |event| c_receive.borrow_mut().push_str(&event.chars)
}
};
let mut wnd = TestWindow::new(widget);
let test_text_case = "Hello 世界!";
wnd.draw_frame();
#[allow(deprecated)]
test_text_case
.chars()
.for_each(|c| wnd.processes_native_event(WindowEvent::ReceivedCharacter(c)));
assert_eq!(&*receive.borrow(), test_text_case);
}
}