cleanup part2

This commit is contained in:
Daniel Bulant 2023-10-23 19:50:39 +02:00
parent 7cffab2e7e
commit b84a8fba38
6 changed files with 41 additions and 42 deletions

View file

@ -1,6 +1,6 @@
[workspace] [workspace]
package.authors = ["Daniel Bulant"] package.authors = ["Daniel Bulant"]
resolver = "2"
members = [ members = [
"ui", "ui",
"mangades" "mangades"

View file

@ -1,6 +1,6 @@
use std::sync::{RwLock, Arc}; use std::sync::{RwLock, Arc};
use mangui::{self, nodes::{layout::Layout, self, Style, TaffyStyle}, taffy::{self, prelude::Size, style::Dimension}, femtovg::{Paint, Color}, CurrentRenderer, SharedTNode}; use mangui::{self, nodes::{layout::Layout, self, Style, TaffyStyle}, taffy::{self, prelude::Size, style::Dimension}, femtovg::{Paint, Color}, CurrentRenderer, SharedNode};
fn main() { fn main() {
let mut root = Layout::<CurrentRenderer>::new(); let mut root = Layout::<CurrentRenderer>::new();
@ -81,7 +81,7 @@ fn main() {
fill: Paint::color(Color::rgb(0, 0, 255)), fill: Paint::color(Color::rgb(0, 0, 255)),
radius: 0. radius: 0.
}))); })));
let groot: SharedTNode<CurrentRenderer> = Arc::new(RwLock::new(root)); let groot: SharedNode<CurrentRenderer> = Arc::new(RwLock::new(root));
mangui::run_event_loop(groot); mangui::run_event_loop(groot);
} }

View file

@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::ops::Deref; use std::ops::Deref;
use std::sync::{Arc, RwLock, Weak, RwLockReadGuard}; use std::sync::{Arc, RwLock, Weak};
use femtovg::renderer::OpenGl; use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color}; use femtovg::{Canvas, Color};
@ -32,13 +31,12 @@ pub use taffy;
pub use femtovg; pub use femtovg;
pub type CurrentRenderer = OpenGl; pub type CurrentRenderer = OpenGl;
pub type TNode<T> = dyn Node<T>; pub type SharedNode = Arc<RwLock<dyn Node>>;
pub type SharedTNode<T> = Arc<RwLock<TNode<T>>>; type WeakNode = Weak<RwLock<dyn Node>>;
type WeakTNode<T> = Weak<RwLock<TNode<T>>>; type NodePtr = Option<Vec<WeakNode>>;
type TNodePtr<T> = Option<Vec<WeakTNode<T>>>; type NodeLayoutMap = PtrWeakKeyHashMap<Weak<RwLock<dyn Node>>, taffy::node::Node>;
type NodeLayoutMap<T> = PtrWeakKeyHashMap<Weak<RwLock<TNode<T>>>, taffy::node::Node>;
pub fn run_event_loop(root_node: SharedTNode<CurrentRenderer>) -> ! { pub fn run_event_loop(root_node: SharedNode) -> ! {
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let (buffer_context, gl_display, window, surface) = create_window(&event_loop); let (buffer_context, gl_display, window, surface) = create_window(&event_loop);
@ -119,6 +117,7 @@ pub fn run_event_loop(root_node: SharedTNode<CurrentRenderer>) -> ! {
// dbg!(&root); // dbg!(&root);
render(&buffer_context, &surface, &window, &mut context, &root); render(&buffer_context, &surface, &window, &mut context, &root);
}, },
// In the future, window should be created after resuming from suspend (for android support)
_ => {} _ => {}
}) })
} }
@ -165,8 +164,8 @@ fn render(
buffer_context: &PossiblyCurrentContext, buffer_context: &PossiblyCurrentContext,
surface: &Surface<WindowSurface>, surface: &Surface<WindowSurface>,
window: &Window, window: &Window,
context: &mut RenderContext<CurrentRenderer>, context: &mut RenderContext,
root_node: &SharedTNode<CurrentRenderer> root_node: &SharedNode
) { ) {
let size = window.inner_size(); let size = window.inner_size();
context.canvas.reset(); context.canvas.reset();

View file

@ -1,17 +1,17 @@
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use femtovg::Renderer; use femtovg::Renderer;
use crate::nodes::{Node, NodeChildren, Overflow, RenderContext, Style}; use crate::nodes::{Node, NodeChildren, Overflow, Style};
use taffy::style::{Style as TaffyStyle, Dimension}; use taffy::style::{Style as TaffyStyle, Dimension};
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Layout<T> { pub struct Layout {
pub style: Style, pub style: Style,
pub children: NodeChildren<T> pub children: NodeChildren
} }
impl<T: Renderer> Layout<T> { impl Layout {
pub fn new() -> Layout<T> { pub fn new() -> Layout {
Layout { Layout {
style: Style { style: Style {
layout: TaffyStyle::default(), layout: TaffyStyle::default(),
@ -22,7 +22,7 @@ impl<T: Renderer> Layout<T> {
} }
} }
impl<T: Renderer> Debug for Layout<T> { impl Debug for Layout {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Layout") f.debug_struct("Layout")
.field("style", &self.style) .field("style", &self.style)
@ -31,11 +31,11 @@ impl<T: Renderer> Debug for Layout<T> {
} }
} }
impl<T: Renderer> Node<T> for Layout<T> { impl Node for Layout {
fn style(&self) -> &Style { fn style(&self) -> &Style {
&self.style &self.style
} }
fn children(&self) -> Option<&NodeChildren<T>> { fn children(&self) -> Option<&NodeChildren> {
Some(&self.children) Some(&self.children)
} }
fn resize(&mut self, width: f32, height: f32) { fn resize(&mut self, width: f32, height: f32) {

View file

@ -3,26 +3,26 @@ pub mod primitives;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use femtovg::{Canvas, Renderer, Color}; use femtovg::{Canvas, Color};
use taffy::layout::Layout; use taffy::layout::Layout;
pub use taffy::style::Style as TaffyStyle; pub use taffy::style::Style as TaffyStyle;
use taffy::Taffy; use taffy::Taffy;
use crate::{NodeLayoutMap, TNodePtr}; use crate::{NodeLayoutMap, NodePtr, CurrentRenderer};
type SharedTNode<T> = Arc<RwLock<dyn Node<T>>>; type SharedTNode = Arc<RwLock<dyn Node>>;
pub struct RenderContext<T: Renderer> { pub struct RenderContext {
pub canvas: Canvas<T>, pub canvas: Canvas<CurrentRenderer>,
pub node_layout: NodeLayoutMap<T>, pub node_layout: NodeLayoutMap,
pub taffy: Taffy, pub taffy: Taffy,
pub mouse: TNodePtr<T>, pub mouse: NodePtr,
pub keyboard_focus: TNodePtr<T> pub keyboard_focus: NodePtr
} }
impl<T: Renderer> RenderContext<T> { impl RenderContext {
/// Fills a rectangle area with the specified color, using the current transform of the canvas. /// Fills a rectangle area with the specified color, using the current transform of the canvas.
/// Rotation WILL break this, this is mostly for simple scaling and translation. /// Rotation WILL break this, this is mostly for simple scaling and translation.
fn fill_rect(&mut self, x: u32, y: u32, width: u32, height: u32, color: Color) { pub fn fill_rect(&mut self, x: u32, y: u32, width: u32, height: u32, color: Color) {
let transform = self.canvas.transform(); let transform = self.canvas.transform();
let x = transform[0] * x as f32 + transform[2] * y as f32 + transform[4]; let x = transform[0] * x as f32 + transform[2] * y as f32 + transform[4];
let y = transform[1] * x as f32 + transform[3] * y as f32 + transform[5]; let y = transform[1] * x as f32 + transform[3] * y as f32 + transform[5];
@ -50,26 +50,26 @@ pub struct Style {
pub overflow: Overflow pub overflow: Overflow
} }
type NodeChildren<T> = Vec<SharedTNode<T>>; type NodeChildren = Vec<SharedTNode>;
pub trait Node<T: Renderer>: Debug { pub trait Node: Debug {
/// Return style. Usually, you just want self.style. /// Return style. Usually, you just want self.style.
fn style(&self) -> &Style; fn style(&self) -> &Style;
/// Returns the children of the node. If the node has no children, return None (empty Vec also works, None is mainly for nodes without children support). /// Returns the children of the node. If the node has no children, return None (empty Vec also works, None is mainly for nodes without children support).
fn children(&self) -> Option<&NodeChildren<T>>; fn children(&self) -> Option<&NodeChildren>;
/// Render the node, called before rendering it's children /// Render the node, called before rendering it's children
/// Canvas considers 0, 0 to be top left corner (for location after layouting happens) /// Canvas considers 0, 0 to be top left corner (for location after layouting happens)
fn render_pre_children(&self, _context: &mut RenderContext<T>, _layout: Layout) {} fn render_pre_children(&self, _context: &mut RenderContext, _layout: Layout) {}
/// Render the node, called after rendering it's children /// Render the node, called after rendering it's children
/// Canvas considers 0, 0 to be top left corner (for location after layouting happens) /// Canvas considers 0, 0 to be top left corner (for location after layouting happens)
fn render_post_children(&self, _context: &mut RenderContext<T>, _layout: Layout) {} fn render_post_children(&self, _context: &mut RenderContext, _layout: Layout) {}
/// Called when the size of window changes on the root node. Layouts do implement this. /// Called when the size of window changes on the root node. Layouts do implement this.
/// Is an optional function instead of another trait because of missing support for trait upcasting /// Is an optional function instead of another trait because of missing support for trait upcasting
// TODO: When rust supports trait upcasting, make this a trait // TODO: When rust supports trait upcasting, make this a trait
fn resize(&mut self, width: f32, height: f32) {} fn resize(&mut self, _width: f32, _height: f32) {}
} }
pub fn layout_recursively<T: Renderer>(node: &SharedTNode<T>, context: &mut RenderContext<T>) -> taffy::node::Node { pub fn layout_recursively(node: &SharedTNode, context: &mut RenderContext) -> taffy::node::Node {
let taffy_node = context.node_layout.get(node); let taffy_node = context.node_layout.get(node);
let taffy_node = match taffy_node { let taffy_node = match taffy_node {
Some(taffy_node) => taffy_node, Some(taffy_node) => taffy_node,
@ -96,7 +96,7 @@ pub fn layout_recursively<T: Renderer>(node: &SharedTNode<T>, context: &mut Rend
taffy_node taffy_node
} }
pub fn render_recursively<T: Renderer>(node: &SharedTNode<T>, context: &mut RenderContext<T>) { pub fn render_recursively(node: &SharedTNode, context: &mut RenderContext) {
let read_node = node.read().unwrap(); let read_node = node.read().unwrap();
let styles = read_node.style(); let styles = read_node.style();
let taffy_node = context.node_layout.get(node).unwrap(); let taffy_node = context.node_layout.get(node).unwrap();

View file

@ -19,14 +19,14 @@ impl Rectangle {
} }
} }
impl<T: Renderer> Node<T> for Rectangle { impl Node for Rectangle {
fn style(&self) -> &Style { fn style(&self) -> &Style {
&self.style &self.style
} }
fn children(&self) -> Option<&NodeChildren<T>> { fn children(&self) -> Option<&NodeChildren> {
None None
} }
fn render_pre_children(&self, context: &mut RenderContext<T>, layout: Layout) { fn render_pre_children(&self, context: &mut RenderContext, layout: Layout) {
let mut path = Path::new(); let mut path = Path::new();
path.rounded_rect( path.rounded_rect(
0., 0.,