size full and content
This commit is contained in:
+11
-6
@@ -8,16 +8,17 @@ use crate::{
|
|||||||
pub struct Div {
|
pub struct Div {
|
||||||
color: u32,
|
color: u32,
|
||||||
children: Mutex<Vec<Arc<Widget>>>,
|
children: Mutex<Vec<Arc<Widget>>>,
|
||||||
|
size: (u16, u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Element for Arc<Div> {
|
impl Element for Div {
|
||||||
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
||||||
let (width, height) = scope.get_size_or_parent();
|
let (width, height) = scope.get_size_or(self.size.0, self.size.1);
|
||||||
scope.draw_rect(width, height, self.color);
|
scope.draw_rect(width, height, self.color);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
||||||
let transform = scope.get_transform().clone();
|
let mut transform = scope.get_transform().clone();
|
||||||
let (w, h) = scope.get_parent_size();
|
let (w, h) = scope.get_parent_size();
|
||||||
scope.set_parent_size(transform.width, transform.height);
|
scope.set_parent_size(transform.width, transform.height);
|
||||||
|
|
||||||
@@ -31,6 +32,8 @@ impl Element for Arc<Div> {
|
|||||||
scope.set_transform(&t);
|
scope.set_transform(&t);
|
||||||
}
|
}
|
||||||
let t = scope.get_transform_mut();
|
let t = scope.get_transform_mut();
|
||||||
|
transform.width = transform.width.max(t.x + t.width);
|
||||||
|
transform.height = transform.height.max(t.y + t.height);
|
||||||
t.x += transform.x;
|
t.x += transform.x;
|
||||||
t.y += transform.y;
|
t.y += transform.y;
|
||||||
|
|
||||||
@@ -39,6 +42,7 @@ impl Element for Arc<Div> {
|
|||||||
scope.clear();
|
scope.clear();
|
||||||
}
|
}
|
||||||
scope.set_parent_size(w, h);
|
scope.set_parent_size(w, h);
|
||||||
|
self.size = (transform.width, transform.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_child(&self, element: &Arc<Widget>) {
|
fn draw_child(&self, element: &Arc<Widget>) {
|
||||||
@@ -56,10 +60,11 @@ impl Element for Arc<Div> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Div {
|
impl Div {
|
||||||
pub fn new(color: u32) -> Arc<Self> {
|
pub fn new(color: u32) -> Self {
|
||||||
Arc::new(Div {
|
Div {
|
||||||
color,
|
color,
|
||||||
children: Mutex::new(Vec::new()),
|
children: Mutex::new(Vec::new()),
|
||||||
})
|
size: (0, 0),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ impl RenderScope {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_transform(&mut self, transform: &Transform) {
|
pub fn set_transform(&mut self, transform: &Transform) {
|
||||||
transform.use_dimensions(&mut self.transform);
|
transform.use_dimensions(self.parent_width, self.parent_height, &mut self.transform);
|
||||||
transform.use_position(self.parent_width, self.parent_height, &mut self.transform);
|
transform.use_position(self.parent_width, self.parent_height, &mut self.transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-10
@@ -17,7 +17,8 @@ pub enum Position {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Dimension {
|
pub enum Dimension {
|
||||||
Auto,
|
Full,
|
||||||
|
Content,
|
||||||
Const(u16),
|
Const(u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,8 +38,8 @@ impl Transform {
|
|||||||
y: Position::Const(0),
|
y: Position::Const(0),
|
||||||
mx: 0,
|
mx: 0,
|
||||||
my: 0,
|
my: 0,
|
||||||
width: Dimension::Auto,
|
width: Dimension::Content,
|
||||||
height: Dimension::Auto,
|
height: Dimension::Content,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,8 +49,8 @@ impl Transform {
|
|||||||
y: Position::Center,
|
y: Position::Center,
|
||||||
mx: 0,
|
mx: 0,
|
||||||
my: 0,
|
my: 0,
|
||||||
width: Dimension::Auto,
|
width: Dimension::Content,
|
||||||
height: Dimension::Auto,
|
height: Dimension::Content,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,9 +76,9 @@ impl Transform {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_dimensions(&self, raw: &mut RawTransform) {
|
pub fn use_dimensions(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) {
|
||||||
self.width.use_dimension(&mut raw.width);
|
self.width.use_dimension(parent_width, &mut raw.width);
|
||||||
self.height.use_dimension(&mut raw.height);
|
self.height.use_dimension(parent_height, &mut raw.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_position(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) {
|
pub fn use_position(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) {
|
||||||
@@ -100,9 +101,10 @@ impl RawTransform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Dimension {
|
impl Dimension {
|
||||||
pub fn use_dimension(&self, r: &mut u16) {
|
pub fn use_dimension(&self, parent: u16, r: &mut u16) {
|
||||||
match self {
|
match self {
|
||||||
Self::Auto => {}
|
Self::Full => *r = parent,
|
||||||
|
Self::Content => {}
|
||||||
Self::Const(n) => *r = *n,
|
Self::Const(n) => *r = *n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user