Mutable draw_child so no more mutex needed

This commit is contained in:
2025-08-03 09:04:24 -05:00
parent acce51c372
commit 71299046d3
4 changed files with 24 additions and 24 deletions
+6 -6
View File
@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use crate::{ use crate::{
widget::{Element, Widget}, widget::{Element, Widget},
@@ -6,14 +6,14 @@ use crate::{
}; };
pub struct Div { pub struct Div {
children: Mutex<Vec<Arc<Widget>>>, children: Vec<Arc<Widget>>,
size: (u16, u16), size: (u16, u16),
} }
impl Div { impl Div {
pub fn new() -> Self { pub fn new() -> Self {
Div { Div {
children: Mutex::new(Vec::new()), children: Vec::new(),
size: (0, 0), size: (0, 0),
} }
} }
@@ -30,7 +30,7 @@ impl Element for Div {
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);
for elem in self.children.lock().unwrap().iter() { for elem in &self.children {
scope.clear(); scope.clear();
if let Some(style) = elem.get() { if let Some(style) = elem.get() {
scope.set_style(style); scope.set_style(style);
@@ -55,8 +55,8 @@ impl Element for Div {
self.size = (transform.width, transform.height); self.size = (transform.width, transform.height);
} }
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&mut self, element: &Arc<Widget>) {
self.children.lock().unwrap().push(element.clone()); self.children.push(element.clone());
element.inject(|w| w.component(NoRender)); element.inject(|w| w.component(NoRender));
} }
+11 -11
View File
@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use crate::{ use crate::{
widget::{Element, Widget}, widget::{Element, Widget},
@@ -7,20 +7,20 @@ use crate::{
pub struct FlexRow { pub struct FlexRow {
pub gap: u16, pub gap: u16,
children: Mutex<Vec<Arc<Widget>>>, children: Vec<Arc<Widget>>,
size: (u16, u16), size: (u16, u16),
} }
pub struct FlexCol { pub struct FlexCol {
pub gap: u16, pub gap: u16,
children: Mutex<Vec<Arc<Widget>>>, children: Vec<Arc<Widget>>,
size: (u16, u16), size: (u16, u16),
} }
impl FlexRow { impl FlexRow {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
children: Mutex::new(Vec::new()), children: Vec::new(),
size: (0, 0), size: (0, 0),
gap: 0, gap: 0,
} }
@@ -30,7 +30,7 @@ impl FlexRow {
impl FlexCol { impl FlexCol {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
children: Mutex::new(Vec::new()), children: Vec::new(),
size: (0, 0), size: (0, 0),
gap: 0, gap: 0,
} }
@@ -50,7 +50,7 @@ impl Element for FlexRow {
let mut v = 0; let mut v = 0;
for elem in self.children.lock().unwrap().iter() { for elem in &self.children {
scope.clear(); scope.clear();
if let Some(style) = elem.get() { if let Some(style) = elem.get() {
scope.set_style(style); scope.set_style(style);
@@ -79,8 +79,8 @@ impl Element for FlexRow {
self.size = (transform.width, transform.height); self.size = (transform.width, transform.height);
} }
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&mut self, element: &Arc<Widget>) {
self.children.lock().unwrap().push(element.clone()); self.children.push(element.clone());
element.inject(|w| w.component(NoRender)); element.inject(|w| w.component(NoRender));
} }
@@ -106,7 +106,7 @@ impl Element for FlexCol {
let mut v = 0; let mut v = 0;
for elem in self.children.lock().unwrap().iter() { for elem in &self.children {
scope.clear(); scope.clear();
if let Some(style) = elem.get() { if let Some(style) = elem.get() {
scope.set_style(style); scope.set_style(style);
@@ -135,8 +135,8 @@ impl Element for FlexCol {
self.size = (transform.width, transform.height); self.size = (transform.width, transform.height);
} }
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&mut self, element: &Arc<Widget>) {
self.children.lock().unwrap().push(element.clone()); self.children.push(element.clone());
element.inject(|w| w.component(NoRender)); element.inject(|w| w.component(NoRender));
} }
+6 -6
View File
@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use figlet_rs::FIGfont; use figlet_rs::FIGfont;
@@ -10,7 +10,7 @@ use crate::{
pub struct Heading { pub struct Heading {
pub font: FIGfont, pub font: FIGfont,
pub smooth: bool, pub smooth: bool,
children: Mutex<Vec<Arc<Widget>>>, children: Vec<Arc<Widget>>,
} }
impl Heading { impl Heading {
@@ -18,7 +18,7 @@ impl Heading {
Heading { Heading {
font: FIGfont::standard().unwrap(), font: FIGfont::standard().unwrap(),
smooth: false, smooth: false,
children: Mutex::new(Vec::new()), children: Vec::new(),
} }
} }
} }
@@ -26,7 +26,7 @@ impl Heading {
impl Element for Heading { impl Element for Heading {
fn render(&mut self, scope: &mut crate::prelude::RenderScope) { fn render(&mut self, scope: &mut crate::prelude::RenderScope) {
let mut s = String::new(); let mut s = String::new();
for element in self.children.lock().unwrap().iter() { for element in &self.children {
if let Some(e) = element.get_elem().as_any().downcast_ref::<String>() { if let Some(e) = element.get_elem().as_any().downcast_ref::<String>() {
s += e; s += e;
} }
@@ -44,9 +44,9 @@ impl Element for Heading {
} }
} }
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&mut self, element: &Arc<Widget>) {
element.inject(|w| w.component(NoRender)); element.inject(|w| w.component(NoRender));
self.children.lock().unwrap().push(element.clone()); self.children.push(element.clone());
} }
fn as_any(&self) -> &dyn std::any::Any { fn as_any(&self) -> &dyn std::any::Any {
+1 -1
View File
@@ -39,7 +39,7 @@ pub trait Element: Send + Sync {
/// Called to draw child widgets, if any. /// Called to draw child widgets, if any.
#[allow(unused)] #[allow(unused)]
fn draw_child(&self, element: &Arc<Widget>) {} fn draw_child(&mut self, element: &Arc<Widget>) {}
#[allow(unused)] #[allow(unused)]
fn event(&mut self, event: &dyn Event) {} fn event(&mut self, event: &dyn Event) {}