Skip to content
Snippets Groups Projects
Unverified Commit 2571f642 authored by Ygg01's avatar Ygg01
Browse files

Initial impl

parent f90c0541
No related branches found
No related tags found
No related merge requests found
use crate::{
context_ref::KayakContextRef, styles::Style, Children, Index, OnEvent, Widget, WidgetProps,
};
use crate::{context_ref::KayakContextRef, styles::Style, Children, Index, OnEvent, Widget, WidgetProps, OnLayout};
/// Props used by the [`Fragment`] widget
#[derive(Default, Debug, PartialEq, Clone)]
......@@ -45,6 +43,10 @@ impl WidgetProps for FragmentProps {
None
}
fn get_on_layout(&self) -> Option<OnLayout> {
None
}
fn get_focusable(&self) -> Option<bool> {
Some(false)
}
......
use morphorm::GeometryChanged;
use crate::layout_cache::Rect;
use crate::{Index, KayakContextRef};
/// A layout event context sent to widgets
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Layout {
/// width of the component
pub width: f32,
/// height of the component
pub height: f32,
/// x-coordinates of the component
pub x: f32,
/// y-coordinates of the component
pub y: f32,
/// z-coordinates of the component
pub z_index: f32,
}
impl Layout {
/// Returns the position as a Kayak position type
pub fn pos(&self) -> (f32, f32) {
(self.x, self.y)
}
}
impl From<Layout> for Rect {
fn from(layout: Layout) -> Self {
Rect {
posx: layout.x,
posy: layout.y,
width: layout.width,
height: layout.height,
z_index: layout.z_index,
}
}
}
impl From<Rect> for Layout {
fn from(rect: Rect) -> Self {
Layout {
width: rect.width,
height: rect.height,
x: rect.posx,
y: rect.posy,
z_index: rect.z_index
}
}
}
///
///
///
pub struct LayoutEvent {
pub layout: Layout,
pub flags: GeometryChanged,
pub target: Index,
}
\ No newline at end of file
use crate::{Index, KayakContext};
pub(crate) struct LayoutEventDispatcher;
impl LayoutEventDispatcher {
pub fn dispatch(context: &mut KayakContext) {
todo!()
}
}
\ No newline at end of file
......@@ -26,6 +26,9 @@ pub mod tree;
mod vec;
pub mod widget;
pub mod widget_manager;
mod on_layout;
mod layout;
mod layout_dispatcher;
use std::sync::{Arc, RwLock};
......@@ -37,6 +40,7 @@ pub use context_ref::KayakContextRef;
pub use cursor::PointerEvents;
pub use cursor_icon::CursorIcon;
pub use event::*;
pub use layout::*;
pub use focus_tree::FocusTree;
pub use fragment::{Fragment, FragmentProps};
pub use generational_arena::{Arena, Index};
......@@ -44,6 +48,7 @@ pub use input_event::*;
pub use keyboard::{KeyboardEvent, KeyboardModifiers};
pub use keys::KeyCode;
pub use on_event::OnEvent;
pub use on_layout::OnLayout;
pub use resources::Resources;
pub use tree::{Tree, WidgetTree};
pub use vec::{VecTracker, VecTrackerProps};
......
use std::fmt::{Debug, Formatter};
use std::sync::{Arc, RwLock};
use morphorm::GeometryChanged;
use crate::KayakContextRef;
use crate::layout::{Layout, LayoutEvent};
/// A container for a function that handles layout
///
/// This differs from a standard [`Handler`](crate::Handler) in that it's sent directly
/// from the [`KayakContext`](crate::KayakContext) and gives the [`KayakContextRef`]
/// as a parameter.
#[derive(Clone)]
pub struct OnLayout(
Arc<RwLock<dyn FnMut(&mut KayakContextRef, &mut LayoutEvent) + Send + Sync + 'static>>,
);
impl OnLayout {
/// Create a new layout handler
///
/// The handler should be a closure that takes the following arguments:
/// 1. The current context
/// 2. The LayoutEvent
pub fn new<F: FnMut(&mut KayakContextRef, &mut LayoutEvent) + Send + Sync + 'static>(
f: F,
) -> OnLayout {
OnLayout(Arc::new(RwLock::new(f)))
}
/// Call the layout handler
///
/// Returns true if the handler was successfully invoked.
pub fn try_call(&self, context: &mut KayakContextRef, event: &mut LayoutEvent) -> bool {
if let Ok(mut on_event) = self.0.write() {
on_event(context, event);
true
} else {
false
}
}
}
impl Debug for OnLayout {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OnLayout").finish()
}
}
impl PartialEq for OnLayout {
fn eq(&self, _: &Self) -> bool {
// TODO what goes here
true
}
}
\ No newline at end of file
use crate::{
context_ref::KayakContextRef, styles::Style, Children, Index, OnEvent, Widget, WidgetProps,
};
use crate::{context_ref::KayakContextRef, styles::Style, Children, Index, OnEvent, Widget, WidgetProps, OnLayout};
/// Props used by the [`VecTracker`] widget
#[derive(Default, Debug, PartialEq, Clone)]
......@@ -12,6 +10,7 @@ pub struct VecTrackerProps<T> {
pub styles: Option<Style>,
pub children: Option<Children>,
pub on_event: Option<OnEvent>,
pub on_layout: Option<OnLayout>,
}
/// A widget that renders a `Vec` of widgets
......@@ -40,6 +39,7 @@ impl<T> VecTracker<T> {
styles: None,
children: None,
on_event: None,
on_layout: None,
};
Self {
......@@ -78,6 +78,10 @@ where
self.on_event.clone()
}
fn get_on_layout(&self) -> Option<OnLayout> {
self.on_layout.clone()
}
fn get_focusable(&self) -> Option<bool> {
Some(false)
}
......
......@@ -2,6 +2,7 @@ use as_any::AsAny;
use std::any::Any;
use crate::{context_ref::KayakContextRef, styles::Style, Children, Event, Index, OnEvent};
use crate::on_layout::OnLayout;
/// An internal trait that has a blanket implementation over all implementors of [`Widget`]
///
......@@ -86,6 +87,10 @@ pub trait WidgetProps: std::fmt::Debug + AsAny + Send + Sync {
///
/// Returns `None` if this widget doesn't contain a custom event handler
fn get_on_event(&self) -> Option<OnEvent>;
/// Gets the custom layout handler of this widget
///
/// Returns `None` if this widget doesn't contain a custom event handler
fn get_on_layout(&self) -> Option<OnLayout>;
/// Gets the focusability of this widget
///
/// The meanings of the returned values are:
......@@ -164,6 +169,10 @@ impl WidgetProps for () {
None
}
fn get_on_layout(&self) -> Option<OnLayout> {
None
}
fn get_focusable(&self) -> Option<bool> {
None
}
......
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