use std::sync::{mpsc::Sender, Arc, RwLock};
use crate::{
event::{Event, UserEvent},
layout::*,
surface::Surface,
};
pub struct RenderCtx<'render, U, S> {
pub focused: bool,
pub layout: &'render Layout<U, S>,
pub state: &'render S,
}
pub struct UpdateCtx<'update, U, S> {
pub owner: NodeId,
pub bounds: Rect,
pub layout: &'update mut Layout<U, S>,
pub tx: Arc<Sender<UserEvent<U>>>,
pub state: &'update mut S,
}
impl<'render, U, S> RenderCtx<'render, U, S> {
pub fn new(focused: bool, layout: &'render Layout<U, S>, state: &'render S) -> Self {
Self {
focused,
layout,
state,
}
}
}
impl<'update, U, S> UpdateCtx<'update, U, S> {
pub fn new(
owner: NodeId,
bounds: Rect,
layout: &'update mut Layout<U, S>,
tx: Arc<Sender<UserEvent<U>>>,
state: &'update mut S,
) -> Self {
Self {
owner,
bounds,
layout,
tx,
state,
}
}
pub fn with_rect<'u>(&'u mut self, rect: Rect) -> UpdateCtx<'u, U, S> {
UpdateCtx {
owner: self.owner,
bounds: rect,
layout: self.layout,
tx: self.tx.clone(),
state: self.state,
}
}
}
pub trait Widget<U, S> {
fn render(
&self,
cx: &RenderCtx<U, S>,
surface: &mut Surface,
) -> Option<Vec<(Rect, Arc<RwLock<dyn Widget<U, S>>>)>>;
#[allow(unused_variables)]
fn update(&mut self, cx: &mut UpdateCtx<U, S>, event: Event<U>) -> crate::error::Result<()> {
Ok(())
}
fn cursor(&self) -> Option<(Option<usize>, usize, usize)> {
None
}
fn constraint(&self) -> Constraint {
Constraint::Fill
}
}