use workspace

This commit is contained in:
Daniel Bulant 2023-10-23 17:15:24 +02:00
parent 76d1ce07f4
commit a13dc3b114
11 changed files with 1501 additions and 125 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
/target
target
.idea

23
Cargo.lock generated
View file

@ -351,9 +351,9 @@ checksum = "eec1c01eb1de97451ee0d60de7d81cf1e72aabefb021616027f3d1c3ec1c723c"
[[package]]
name = "hashbrown"
version = "0.14.1"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12"
checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156"
[[package]]
name = "image"
@ -470,6 +470,13 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "718e8fae447df0c7e1ba7f5189829e63fd536945c8988d61444c19039f16b670"
[[package]]
name = "mangades"
version = "0.1.0"
dependencies = [
"mangui",
]
[[package]]
name = "mangui"
version = "0.1.0"
@ -515,9 +522,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "mio"
version = "0.8.8"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0"
dependencies = [
"libc",
"log",
@ -858,18 +865,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.49"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.49"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
dependencies = [
"proc-macro2",
"quote",

View file

@ -1,15 +1,7 @@
[package]
name = "mangui"
version = "0.1.0"
edition = "2021"
[workspace]
package.authors = ["Daniel Bulant"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
femtovg = "0.7.1"
glutin = "0.30.10"
raw-window-handle = "0.5.0"
winit = { version = "0.28.7", default-features = false }
glutin-winit = "0.3.0"
taffy = "0.3.16"
weak-table = "0.3.2"
members = [
"ui",
"mangades"
]

9
mangades/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "mangades"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
mangui = { path = "../ui"}

87
mangades/src/main.rs Normal file
View file

@ -0,0 +1,87 @@
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};
fn main() {
let mut root = Layout::<CurrentRenderer>::new();
root.style.layout.display = taffy::style::Display::Flex;
root.style.layout.flex_direction = taffy::style::FlexDirection::Row;
root.children.push(Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(100.),
height: Dimension::Points(100.)
},
..Default::default()
}
},
fill: Paint::color(Color::rgb(255, 0, 0)),
radius: 10.
})));
root.children.push(Arc::new(RwLock::new(Layout {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(100.),
height: Dimension::Points(100.)
},
flex_grow: 1.,
display: taffy::style::Display::Flex,
flex_direction: taffy::style::FlexDirection::Column,
..Default::default()
}
},
children: vec![
Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(50.),
height: Dimension::Points(50.)
},
flex_grow: 1.,
..Default::default()
}
},
fill: Paint::color(Color::rgb(0, 255, 0)),
radius: 5.
})),
Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(50.),
height: Dimension::Points(50.)
},
..Default::default()
}
},
fill: Paint::color(Color::rgb(0, 255, 255)),
radius: 5.
}))
]
})));
root.children.push(Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(50.),
height: Dimension::Points(100.)
},
..Default::default()
}
},
fill: Paint::color(Color::rgb(0, 0, 255)),
radius: 0.
})));
let groot: SharedTNode<CurrentRenderer> = Arc::new(RwLock::new(root));
mangui::run_event_loop(groot);
}

1337
ui/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

15
ui/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "mangui"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
femtovg = "0.7.1"
glutin = "0.30.10"
raw-window-handle = "0.5.0"
winit = { version = "0.28.7", default-features = false }
glutin-winit = "0.3.0"
taffy = "0.3.16"
weak-table = "0.3.2"

View file

@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::num::NonZeroU32;
use std::ops::Deref;
use std::sync::{Arc, RwLock, Weak};
use std::sync::{Arc, RwLock, Weak, RwLockReadGuard};
use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color, Paint};
use femtovg::{Canvas, Color};
use glutin::surface::Surface;
use glutin::{context::PossiblyCurrentContext, display::Display};
use glutin_winit::DisplayBuilder;
@ -22,24 +22,24 @@ use glutin::{
surface::{SurfaceAttributesBuilder, WindowSurface},
};
use taffy::geometry::Size;
use taffy::style::Dimension;
use taffy::style_helpers::TaffyMaxContent;
use taffy::Taffy;
use weak_table::PtrWeakKeyHashMap;
use crate::nodes::{layout_recursively, Node, render_recursively, RenderContext, Style, TaffyStyle};
use crate::nodes::layout::Layout;
use crate::nodes::{layout_recursively, Node, render_recursively, RenderContext};
mod nodes;
pub mod nodes;
pub use taffy;
pub use femtovg;
type TNode<T> = dyn Node<T>;
type SharedTNode<T> = Arc<RwLock<TNode<T>>>;
pub type CurrentRenderer = OpenGl;
pub type TNode<T> = dyn Node<T>;
pub type SharedTNode<T> = Arc<RwLock<TNode<T>>>;
type WeakTNode<T> = Weak<RwLock<TNode<T>>>;
type TNodePtr<T> = Option<Vec<WeakTNode<T>>>;
type NodeLayoutMap<T> = PtrWeakKeyHashMap<Weak<RwLock<TNode<T>>>, taffy::node::Node>;
type LayoutNodeMap<T> = HashMap<taffy::node::Node, Weak<RwLock<TNode<T>>>>;
type CurrentRenderer = OpenGl;
fn main() {
pub fn run_event_loop(root_node: SharedTNode<CurrentRenderer>) -> ! {
let event_loop = EventLoop::new();
let (buffer_context, gl_display, window, surface) = create_window(&event_loop);
@ -50,94 +50,15 @@ fn main() {
let mut taffy = Taffy::new();
let mut taffy_map = NodeLayoutMap::new();
let mut root = Layout::<CurrentRenderer>::new();
root.style.layout.display = taffy::style::Display::Flex;
root.style.layout.flex_direction = taffy::style::FlexDirection::Row;
root.children.push(Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(100.),
height: Dimension::Points(100.)
},
..Default::default()
}
},
fill: Paint::color(Color::rgb(255, 0, 0)),
radius: 10.
})));
root.children.push(Arc::new(RwLock::new(Layout {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(100.),
height: Dimension::Points(100.)
},
flex_grow: 1.,
display: taffy::style::Display::Flex,
flex_direction: taffy::style::FlexDirection::Column,
..Default::default()
}
},
children: vec![
Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(50.),
height: Dimension::Points(50.)
},
flex_grow: 1.,
..Default::default()
}
},
fill: Paint::color(Color::rgb(0, 255, 0)),
radius: 5.
})),
Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(50.),
height: Dimension::Points(50.)
},
..Default::default()
}
},
fill: Paint::color(Color::rgb(0, 255, 255)),
radius: 5.
}))
]
})));
root.children.push(Arc::new(RwLock::new(nodes::primitives::Rectangle {
style: Style {
overflow: nodes::Overflow::Visible,
layout: TaffyStyle {
min_size: Size {
width: Dimension::Points(50.),
height: Dimension::Points(100.)
},
..Default::default()
}
},
fill: Paint::color(Color::rgb(0, 0, 255)),
radius: 0.
})));
let groot: Arc<RwLock<Layout<CurrentRenderer>>> = Arc::new(RwLock::new(root));
{
let clonned = groot.clone();
let clonned = root_node.clone();
let root = clonned.read().unwrap();
let root_style = TNode::<OpenGl>::style(root.deref());
// let root: RwLockReadGuard<'_, dyn Node<CurrentRenderer>> = root;
let root_style = root.deref().style(); // TNode::<CurrentRenderer>::style(root.deref());
let root_layout = root_style.layout.to_owned();
let root_node = taffy.new_leaf(root_layout).unwrap();
let taffy_root_node = taffy.new_leaf(root_layout).unwrap();
taffy_map.insert(groot.clone(), root_node);
taffy_map.insert(root_node.clone(), taffy_root_node);
}
let mut context = RenderContext {
@ -148,6 +69,7 @@ fn main() {
mouse: None,
keyboard_focus: None
};
let root = root_node.clone();
// let mut width: u32 = 0;
// let mut height: u32 = 0;
@ -165,10 +87,11 @@ fn main() {
let width: NonZeroU32 = NonZeroU32::new(size.width).unwrap();
let height: NonZeroU32 = NonZeroU32::new(size.height).unwrap();
surface.resize(&buffer_context, width, height);
let mut groot = groot.write().unwrap();
let mut groot = root_node.write().unwrap();
// let scale_factor = window.scale_factor();
groot.style.layout.size.width = Dimension::Points(size.width as f32);
groot.style.layout.size.height = Dimension::Points(size.height as f32);
// groot.style.layout.size.width = Dimension::Points(size.width as f32);
// groot.style.layout.size.height = Dimension::Points(size.height as f32);
groot.resize(size.width as f32, size.height as f32);
drop(groot);
window.request_redraw();
should_recompute = true;
@ -176,7 +99,6 @@ fn main() {
_ => {}
},
Event::RedrawRequested(_) => {
let root: SharedTNode<CurrentRenderer> = groot.clone();
if should_recompute {
layout_recursively(&root, &mut context);
let src_nodes = context.node_layout.values().map(|v| v.to_owned()).collect::<Vec<_>>();

View file

@ -1,7 +1,7 @@
use std::fmt::{Debug, Formatter};
use femtovg::Renderer;
use crate::nodes::{Node, NodeChildren, Overflow, RenderContext, Style};
use taffy::style::{Style as TaffyStyle};
use taffy::style::{Style as TaffyStyle, Dimension};
#[derive(Clone, Default)]
@ -41,5 +41,8 @@ impl<T: Renderer> Node<T> for Layout<T> {
// fn render_(&self, context: &mut RenderContext<T>, _layout: taffy::layout::Layout, render_children: &dyn Fn(&mut RenderContext<T>)) {
// render_children(context);
// }
fn resize(&mut self, width: f32, height: f32) {
self.style.layout.size.width = Dimension::Points(width);
self.style.layout.size.height = Dimension::Points(height);
}
}

View file

@ -47,7 +47,7 @@ pub enum Overflow {
}
#[derive(Clone, Default, Debug)]
pub struct Style {
pub(crate) layout: TaffyStyle,
pub layout: TaffyStyle,
pub overflow: Overflow
}
@ -64,6 +64,10 @@ pub trait Node<T: Renderer>: Debug {
/// Render the node, called after rendering it's children
/// 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) {}
/// 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
// TODO: When rust supports trait upcasting, make this a trait
fn resize(&mut self, width: f32, height: f32) {}
}
pub fn layout_recursively<T: Renderer>(node: &SharedTNode<T>, context: &mut RenderContext<T>) -> taffy::node::Node {

View file

@ -10,7 +10,7 @@ pub struct Rectangle {
}
impl Rectangle {
pub(crate) fn new() -> Rectangle {
pub fn new() -> Rectangle {
Rectangle {
style: Style::default(),
fill: Paint::color(Color::rgb(0, 0, 0)),