From 7b3a79b1b03823b94b4de106f93553d3331968da Mon Sep 17 00:00:00 2001 From: shadowcat Date: Sun, 7 Dec 2025 14:45:54 -0500 Subject: [PATCH] clean up layout dir --- TODO | 1 + src/layout/data.rs | 27 ---------------------- src/layout/mod.rs | 11 +++++---- src/layout/num.rs | 29 ++++++++++++++++++++++++ src/layout/orientation/mod.rs | 3 +-- src/layout/{id.rs => widget_ref.rs} | 0 src/util/borrow.rs | 35 ----------------------------- src/util/mod.rs | 4 ++-- src/util/refcount.rs | 2 ++ src/{layout => util}/vec2.rs | 32 ++------------------------ 10 files changed, 42 insertions(+), 102 deletions(-) delete mode 100644 src/layout/data.rs rename src/layout/{id.rs => widget_ref.rs} (100%) delete mode 100644 src/util/borrow.rs rename src/{layout => util}/vec2.rs (77%) diff --git a/TODO b/TODO index c4f9356..9e68faf 100644 --- a/TODO +++ b/TODO @@ -16,6 +16,7 @@ scaling field could be best solution so redrawing stuff isn't needed & you can specify both as user WidgetRef or smth instead of Id + fyi this is not the same as what was just implemented enum that's either an Id or an actual concrete instance of W painter takes them in instead of (or in addition to) id then type wrapper widgets to contain them diff --git a/src/layout/data.rs b/src/layout/data.rs deleted file mode 100644 index 3c85abc..0000000 --- a/src/layout/data.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::any::{Any, TypeId}; - -use crate::util::{HashMap, Id}; - -#[derive(Default)] -pub struct UiData { - map: HashMap>, -} - -impl UiData { - pub fn get(&self) -> Option<&T> { - self.map - .get(&TypeId::of::()) - .map(|d| d.downcast_ref().unwrap()) - } - pub fn get_mut(&mut self) -> Option<&mut T> { - self.map - .get_mut(&TypeId::of::()) - .map(|d| d.downcast_mut().unwrap()) - } - - pub fn emit_remove(&mut self, id: &Id) { - for (tid, f) in &mut self.on_remove { - let data = self.map.get_mut(tid).unwrap().downcast_ref().unwrap(); - } - } -} diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 0e13b5c..9665692 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -1,6 +1,6 @@ +mod attr; mod color; mod event; -mod id; mod layer; mod module; mod num; @@ -9,14 +9,13 @@ mod painter; mod text; mod texture; mod ui; -mod attr; -mod vec2; mod widget; +mod widget_ref; mod widgets; +pub use attr::*; pub use color::*; pub use event::*; -pub use id::*; pub use layer::*; pub use module::*; pub use num::*; @@ -25,9 +24,9 @@ pub use painter::*; pub use text::*; pub use texture::*; pub use ui::*; -pub use attr::*; -pub use vec2::*; pub use widget::*; +pub use widget_ref::*; pub use widgets::*; +pub use crate::util::Vec2; pub type UiColor = Color; diff --git a/src/layout/num.rs b/src/layout/num.rs index c46b013..ef238c0 100644 --- a/src/layout/num.rs +++ b/src/layout/num.rs @@ -1,3 +1,7 @@ +use std::marker::Destruct; + +use crate::util::Vec2; + pub const trait UiNum { fn to_f32(self) -> f32; } @@ -19,3 +23,28 @@ impl const UiNum for i32 { self as f32 } } + +impl const From<(T, U)> for Vec2 +where + (T, U): const Destruct, +{ + fn from((x, y): (T, U)) -> Self { + Self { + x: x.to_f32(), + y: y.to_f32(), + } + } +} + +impl const From for Vec2 { + fn from(v: T) -> Self { + Self { + x: v.to_f32(), + y: v.to_f32(), + } + } +} + +pub const fn vec2(x: impl const UiNum, y: impl const UiNum) -> Vec2 { + Vec2::new(x.to_f32(), y.to_f32()) +} diff --git a/src/layout/orientation/mod.rs b/src/layout/orientation/mod.rs index f6d2963..8140bc0 100644 --- a/src/layout/orientation/mod.rs +++ b/src/layout/orientation/mod.rs @@ -3,8 +3,7 @@ mod axis; mod len; mod pos; -use super::vec2::*; - +use super::Vec2; pub use align::*; pub use axis::*; pub use len::*; diff --git a/src/layout/id.rs b/src/layout/widget_ref.rs similarity index 100% rename from src/layout/id.rs rename to src/layout/widget_ref.rs diff --git a/src/util/borrow.rs b/src/util/borrow.rs deleted file mode 100644 index bc8e51a..0000000 --- a/src/util/borrow.rs +++ /dev/null @@ -1,35 +0,0 @@ -use std::ops::{Deref, DerefMut}; - -pub struct DynBorrower<'a, T: ?Sized> { - data: &'a mut T, - borrowed: &'a mut bool, -} - -impl<'a, T: ?Sized> DynBorrower<'a, T> { - pub fn new(data: &'a mut T, borrowed: &'a mut bool) -> Self { - if *borrowed { - panic!("tried to mutably borrow the same thing twice"); - } - Self { data, borrowed } - } -} - -impl Drop for DynBorrower<'_, T> { - fn drop(&mut self) { - *self.borrowed = false; - } -} - -impl Deref for DynBorrower<'_, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - self.data - } -} - -impl DerefMut for DynBorrower<'_, T> { - fn deref_mut(&mut self) -> &mut Self::Target { - self.data - } -} diff --git a/src/util/mod.rs b/src/util/mod.rs index e014fd6..01215d7 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,16 +1,16 @@ mod arena; -mod borrow; mod change; mod id; mod math; mod refcount; +mod vec2; pub(crate) use arena::*; -pub(crate) use borrow::*; pub use change::*; pub(crate) use id::*; pub(crate) use math::*; pub(crate) use refcount::*; +pub use vec2::*; pub type HashMap = fxhash::FxHashMap; pub type HashSet = fxhash::FxHashSet; diff --git a/src/util/refcount.rs b/src/util/refcount.rs index ee8efb1..9d0f97c 100644 --- a/src/util/refcount.rs +++ b/src/util/refcount.rs @@ -10,6 +10,7 @@ impl RefCounter { pub fn new() -> Self { Self(Arc::new(0.into())) } + #[allow(unused)] pub fn refs(&self) -> u32 { self.0.load(Ordering::Acquire) } @@ -17,6 +18,7 @@ impl RefCounter { let refs = self.0.fetch_sub(1, Ordering::Release); refs == 0 } + #[allow(unused)] pub fn quiet_clone(&self) -> Self { Self(self.0.clone()) } diff --git a/src/layout/vec2.rs b/src/util/vec2.rs similarity index 77% rename from src/layout/vec2.rs rename to src/util/vec2.rs index 47e8785..7609861 100644 --- a/src/layout/vec2.rs +++ b/src/util/vec2.rs @@ -1,8 +1,5 @@ -use crate::{ - layout::UiNum, - util::{DivOr, impl_op}, -}; -use std::{hash::Hash, marker::Destruct, ops::*}; +use crate::util::{DivOr, impl_op}; +use std::{hash::Hash, ops::*}; #[repr(C)] #[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)] @@ -20,10 +17,6 @@ impl Hash for Vec2 { } } -pub const fn vec2(x: impl const UiNum, y: impl const UiNum) -> Vec2 { - Vec2::new(x.to_f32(), y.to_f32()) -} - impl Vec2 { pub const ZERO: Self = Self::new(0.0, 0.0); pub const ONE: Self = Self::new(1.0, 1.0); @@ -68,15 +61,6 @@ impl Vec2 { } } -impl const From for Vec2 { - fn from(v: T) -> Self { - Self { - x: v.to_f32(), - y: v.to_f32(), - } - } -} - // this version looks kinda cool... is it more readable? more annoying to copy and change though impl_op!(impl Add for Vec2: add x y); impl_op!(Vec2 Sub sub; x y); @@ -102,18 +86,6 @@ impl Neg for Vec2 { } } -impl const From<(T, U)> for Vec2 -where - (T, U): const Destruct, -{ - fn from((x, y): (T, U)) -> Self { - Self { - x: x.to_f32(), - y: y.to_f32(), - } - } -} - impl std::fmt::Debug for Vec2 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "({}, {})", self.x, self.y)