Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ViewData for internal view owned state #176

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
StyleClassRef, StyleProp, StyleSelector, StyleSelectors, ZIndex,
},
unit::PxPct,
view::{paint_bg, paint_border, paint_outline, View},
view::{paint_bg, paint_border, paint_outline, View, ViewData},
};

/// Control whether an event will continue propagating or whether it should stop.
Expand Down Expand Up @@ -107,7 +107,6 @@ pub struct ViewState {
pub(crate) view_style_props: ViewStyleProps,
pub(crate) animation: Option<Animation>,
pub(crate) base_style: Option<Style>,
pub(crate) style: Style,
pub(crate) class: Option<StyleClassRef>,
pub(crate) dragging_style: Option<Style>,
pub(crate) combined_style: Style,
Expand All @@ -134,7 +133,6 @@ impl ViewState {
has_style_selectors: StyleSelectors::default(),
animation: None,
base_style: None,
style: Style::new(),
class: None,
combined_style: Style::new(),
taffy_style: taffy::style::Style::DEFAULT,
Expand All @@ -151,8 +149,10 @@ impl ViewState {
}

/// Returns `true` if a new frame is requested.
#[allow(clippy::too_many_arguments)]
pub(crate) fn compute_style(
&mut self,
view_data: &mut ViewData,
view_style: Option<Style>,
interact_state: InteractionState,
screen_size_bp: ScreenSizeBp,
Expand All @@ -173,7 +173,7 @@ impl ViewState {
}
computed_style = computed_style
.apply_classes_from_context(classes, context)
.apply(self.style.clone());
.apply(view_data.style.clone());

'anim: {
if let Some(animation) = self.animation.as_mut() {
Expand Down Expand Up @@ -424,6 +424,7 @@ impl AppState {
pub(crate) fn compute_style(
&mut self,
id: Id,
view_data: &mut ViewData,
view_style: Option<Style>,
view_class: Option<StyleClassRef>,
classes: &[StyleClassRef],
Expand All @@ -433,6 +434,7 @@ impl AppState {
let screen_size_bp = self.screen_size_bp;
let view_state = self.view_state(id);
view_state.compute_style(
view_data,
view_style,
interact_state,
screen_size_bp,
Expand Down Expand Up @@ -1206,9 +1208,14 @@ impl<'a> StyleCx<'a> {
});
}

let mut new_frame =
self.app_state
.compute_style(id, view_style, view_class, classes, &self.current);
let mut new_frame = self.app_state.compute_style(
id,
view.view_data_mut(),
view_style,
view_class,
classes,
&self.current,
);

let style = self.app_state_mut().get_computed_style(id).clone();
self.direct = style;
Expand Down
57 changes: 53 additions & 4 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ use taffy::prelude::Node;
use crate::{
context::{AppState, EventCx, LayoutCx, PaintCx, StyleCx, UpdateCx, ViewStyleProps},
event::Event,
id::Id,
id::{Id, ID_PATHS},
style::{BoxShadowProp, Style, StyleClassRef},
EventPropagation,
};

pub trait View {
fn id(&self) -> Id;
fn view_data(&self) -> &ViewData;
fn view_data_mut(&mut self) -> &mut ViewData;

/// This method walks over children and must be implemented if the view has any children.
/// It should return children back to front and should stop if `_for_each` returns `true`.
Expand All @@ -115,6 +116,10 @@ pub trait View {
) {
}

fn id(&self) -> Id {
self.view_data().id()
}

fn view_style(&self) -> Option<Style> {
None
}
Expand Down Expand Up @@ -235,6 +240,42 @@ pub trait View {
}
}

pub struct ViewData {
pub(crate) id: Id,
pub(crate) style: Style,
}

impl ViewData {
pub fn new(id: Id) -> Self {
Self {
id,
style: Style::new(),
}
}
pub fn id(&self) -> Id {
self.id
}
}

pub(crate) fn update_data(id: Id, root: &mut dyn View, f: impl FnOnce(&mut ViewData)) {
pub(crate) fn update_inner(id_path: &[Id], view: &mut dyn View, f: impl FnOnce(&mut ViewData)) {
let id = id_path[0];
let id_path = &id_path[1..];
if id == view.id() {
if id_path.is_empty() {
f(view.view_data_mut());
} else if let Some(child) = view.child_mut(id_path[0]) {
update_inner(id_path, child, f);
}
}
}

let id_path = ID_PATHS.with(|paths| paths.borrow().get(&id).cloned());
if let Some(id_path) = id_path {
update_inner(id_path.dispatch(), root, f)
}
}

/// Computes the layout of the view's children, if any.
pub fn default_compute_layout<V: View + ?Sized>(view: &mut V, cx: &mut LayoutCx) -> Option<Rect> {
let mut layout_rect: Option<Rect> = None;
Expand Down Expand Up @@ -585,8 +626,12 @@ pub(crate) fn view_debug_tree(root_view: &dyn View) {
}

impl View for Box<dyn View> {
fn id(&self) -> Id {
(**self).id()
fn view_data(&self) -> &ViewData {
(**self).view_data()
}

fn view_data_mut(&mut self) -> &mut ViewData {
(**self).view_data_mut()
}

fn for_each_child<'a>(&'a self, for_each: &mut dyn FnMut(&'a dyn View) -> bool) {
Expand All @@ -604,6 +649,10 @@ impl View for Box<dyn View> {
(**self).for_each_child_rev_mut(for_each)
}

fn id(&self) -> Id {
(**self).id()
}

fn view_style(&self) -> Option<Style> {
(**self).view_style()
}
Expand Down
21 changes: 14 additions & 7 deletions src/views/clip.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
use kurbo::Size;

use crate::{id::Id, view::View};
use crate::{
id::Id,
view::{View, ViewData},
};

pub struct Clip {
id: Id,
data: ViewData,
child: Box<dyn View>,
}

pub fn clip<V: View + 'static>(child: V) -> Clip {
Clip {
id: Id::next(),
data: ViewData::new(Id::next()),
child: Box::new(child),
}
}

impl View for Clip {
fn id(&self) -> Id {
self.id
fn view_data(&self) -> &ViewData {
&self.data
}

fn view_data_mut(&mut self) -> &mut ViewData {
&mut self.data
}

fn for_each_child<'a>(&'a self, for_each: &mut dyn FnMut(&'a dyn View) -> bool) {
Expand All @@ -40,10 +47,10 @@ impl View for Clip {

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
cx.save();
let style = cx.get_builtin_style(self.id);
let style = cx.get_builtin_style(self.id());
let border_radius = style.border_radius();
let size = cx
.get_layout(self.id)
.get_layout(self.id())
.map(|layout| Size::new(layout.size.width as f64, layout.size.height as f64))
.unwrap_or_default();

Expand Down
17 changes: 12 additions & 5 deletions src/views/container.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{id::Id, view::View};
use crate::{
id::Id,
view::{View, ViewData},
};

/// A simple wrapper around another View. See [`container`]
pub struct Container {
id: Id,
data: ViewData,
child: Box<dyn View>,
}

Expand All @@ -12,14 +15,18 @@ pub struct Container {
/// set of styles completely separate from the View that is being wrapped.
pub fn container<V: View + 'static>(child: V) -> Container {
Container {
id: Id::next(),
data: ViewData::new(Id::next()),
child: Box::new(child),
}
}

impl View for Container {
fn id(&self) -> Id {
self.id
fn view_data(&self) -> &ViewData {
&self.data
}

fn view_data_mut(&mut self) -> &mut ViewData {
&mut self.data
}

fn for_each_child<'a>(&'a self, for_each: &mut dyn FnMut(&'a dyn View) -> bool) {
Expand Down
17 changes: 12 additions & 5 deletions src/views/container_box.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{id::Id, view::View};
use crate::{
id::Id,
view::{View, ViewData},
};

/// A wrapper around any type that implements View. See [`container_box`]
pub struct ContainerBox {
id: Id,
data: ViewData,
child: Box<dyn View>,
}

Expand Down Expand Up @@ -43,14 +46,18 @@ pub struct ContainerBox {
/// ```
pub fn container_box(child: impl View + 'static) -> ContainerBox {
ContainerBox {
id: Id::next(),
data: ViewData::new(Id::next()),
child: Box::new(child),
}
}

impl View for ContainerBox {
fn id(&self) -> Id {
self.id
fn view_data(&self) -> &ViewData {
&self.data
}

fn view_data_mut(&mut self) -> &mut ViewData {
&mut self.data
}

fn for_each_child<'a>(&'a self, for_each: &mut dyn FnMut(&'a dyn View) -> bool) {
Expand Down
13 changes: 7 additions & 6 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use floem_reactive::create_effect;
use floem_reactive::{create_effect, create_updater};
use kurbo::{Point, Rect};

use crate::{
Expand Down Expand Up @@ -34,12 +34,13 @@ pub trait Decorators: View + Sized {
/// ```
/// If you are returning from a function that produces a view, you may want
/// to use `base_style` for the returned [`View`] instead.
fn style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
fn style(mut self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style(style);
});
let style = create_updater(
move || style(Style::new()),
move |style| id.update_style(style),
);
self.view_data_mut().style = style;
self
}

Expand Down
17 changes: 12 additions & 5 deletions src/views/drag_resize_window_area.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use winit::window::ResizeDirection;

use crate::{
action::drag_resize_window, event::EventListener, id::Id, style::CursorStyle, view::View,
action::drag_resize_window,
event::EventListener,
id::Id,
style::CursorStyle,
view::{View, ViewData},
};

use super::Decorators;

pub struct DragResizeWindowArea {
id: Id,
data: ViewData,
child: Box<dyn View>,
}

Expand All @@ -17,7 +21,7 @@ pub fn drag_resize_window_area<V: View + 'static>(
) -> DragResizeWindowArea {
let id = Id::next();
DragResizeWindowArea {
id,
data: ViewData::new(id),
child: Box::new(child),
}
.on_event_stop(EventListener::PointerDown, move |_| {
Expand All @@ -39,10 +43,13 @@ pub fn drag_resize_window_area<V: View + 'static>(
}

impl View for DragResizeWindowArea {
fn id(&self) -> Id {
self.id
fn view_data(&self) -> &ViewData {
&self.data
}

fn view_data_mut(&mut self) -> &mut ViewData {
&mut self.data
}
fn for_each_child<'a>(&'a self, for_each: &mut dyn FnMut(&'a dyn View) -> bool) {
for_each(&self.child);
}
Expand Down
Loading