mod base;
mod build;
mod contexts;
mod draw;
mod event;
mod layout;
mod rebuild;
use std::any::Any;
pub use base::*;
pub use build::*;
pub use contexts::*;
pub use draw::*;
pub use event::*;
pub use layout::*;
pub use rebuild::*;
use crate::{
event::{Ime, RequestFocus, RequestFocusNext, RequestFocusPrev},
style::{Style, Styles, Theme},
view::{ViewId, ViewState},
window::{Cursor, Window},
};
macro_rules! impl_context {
($ty:ty { $($impl:item)* }) => {
impl $ty {
$($impl)*
}
};
($ty:ty, $($mode:ty),* { $($impl:item)* }) => {
impl_context!($ty { $($impl)* });
impl_context!($($mode),* { $($impl)* });
};
}
impl_context! {BuildCx<'_, '_>, RebuildCx<'_, '_>, EventCx<'_, '_>, LayoutCx<'_, '_>, DrawCx<'_, '_> {
pub fn window(&self) -> &Window {
self.context()
}
pub fn window_mut(&mut self) -> &mut Window {
self.context_mut()
}
pub fn styles(&mut self) -> &mut Styles {
self.context_mut()
}
pub fn style<T: Style + Any>(&mut self) -> &T {
self.styles().style::<T>()
}
pub fn theme(&mut self) -> &Theme {
self.style()
}
pub fn id(&self) -> ViewId {
self.view_state.id()
}
pub fn is_hovered(&self) -> bool {
self.view_state.is_hovered()
}
pub fn is_focused(&self) -> bool {
self.view_state.is_focused()
}
pub fn is_active(&self) -> bool {
self.view_state.is_active()
}
pub fn is_focusable(&self) -> bool {
self.view_state.is_focusable()
}
pub fn has_hovered(&self) -> bool {
self.view_state.has_hovered()
}
pub fn has_focused(&self) -> bool {
self.view_state.has_focused()
}
pub fn has_active(&self) -> bool {
self.view_state.has_active()
}
pub fn contains_property<T: 'static>(&self) -> bool {
self.view_state.contains_property::<T>()
}
pub fn insert_property<T: 'static>(&mut self, item: T) {
self.view_state.insert_property(item);
}
pub fn remove_property<T: 'static>(&mut self) -> Option<T> {
self.view_state.remove_property()
}
pub fn get_property<T: 'static>(&self) -> Option<&T> {
self.view_state.get_property()
}
pub fn get_property_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.view_state.get_property_mut()
}
pub fn property_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
self.view_state.property_or_insert_with(f)
}
pub fn property_or<T: 'static>(&mut self, item: T) -> &mut T {
self.view_state.property_or(item)
}
pub fn property_or_default<T: 'static + Default>(&mut self) -> &mut T {
self.view_state.property_or_default()
}
}}
impl_context! {BuildCx<'_, '_>, RebuildCx<'_, '_>, EventCx<'_, '_> {
pub fn propagate(&mut self, child: &mut ViewState) {
self.view_state.propagate(child);
}
pub fn layout(&mut self) {
self.view_state.request_layout();
}
pub fn draw(&mut self) {
self.view_state.request_draw();
}
pub fn animate(&mut self) {
self.view_state.request_animate();
}
pub fn focus(&mut self) {
if !self.is_focused() {
let cmd = RequestFocus(self.window().id(), self.id());
self.cmd(cmd);
}
}
pub fn focus_next(&mut self) {
let cmd = RequestFocusNext(self.window().id());
self.cmd(cmd);
}
pub fn focus_prev(&mut self) {
let cmd = RequestFocusPrev(self.window().id());
self.cmd(cmd);
}
pub fn set_cursor(&mut self, cursor: Option<Cursor>) {
self.view_state.set_cursor(cursor);
}
pub fn set_ime(&mut self, ime: Option<Ime>) {
self.view_state.set_ime(ime);
}
pub fn set_hovered(&mut self, hovered: bool) -> bool {
let updated = self.is_hovered() != hovered;
self.view_state.set_hovered(hovered);
updated
}
pub fn set_focused(&mut self, focused: bool) -> bool {
let updated = self.is_focused() != focused;
self.view_state.set_focused(focused);
updated
}
pub fn set_active(&mut self, active: bool) -> bool {
let updated = self.is_active() != active;
self.view_state.set_active(active);
updated
}
pub fn set_focusable(&mut self, focusable: bool) -> bool {
let updated = self.is_focusable() != focusable;
self.view_state.set_focusable(focusable);
updated
}
}}