resource management

This commit is contained in:
2025-07-07 10:05:23 -05:00
parent 5fabd1a8ab
commit 3d995671cd
7 changed files with 77 additions and 82 deletions
+7 -11
View File
@@ -1,22 +1,18 @@
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use crate::{component, extensions::Extension, widget::Widget};
use crate::{component, extensions::Extension, widget::Widget, Screen};
pub struct IdExtension(Mutex<Vec<Arc<Widget>>>);
pub struct IdExtension(pub Arc<Screen>);
impl Extension for Arc<IdExtension> {
fn init(&mut self, widgets: &Vec<Arc<Widget>>) {
self.0.lock().unwrap().append(&mut widgets.clone());
}
}
impl Extension for Arc<IdExtension> {}
impl IdExtension {
pub fn new() -> Arc<Self> {
Arc::new(Self(Mutex::new(Vec::new())))
pub fn new(screen: Arc<Screen>) -> Arc<Self> {
Arc::new(IdExtension(screen))
}
pub fn get_element(self: &Arc<IdExtension>, id: usize) -> Option<Arc<Widget>> {
for elem in self.0.lock().unwrap().iter() {
for elem in self.0.widgets.lock().unwrap().iter() {
if let Some(current_id) = elem.get::<Id>() {
if current_id.0 == id {
return Some(elem.clone());
+3 -2
View File
@@ -1,7 +1,7 @@
pub mod id;
pub mod resources;
pub mod tick;
pub mod velocity;
pub mod resources;
use std::{
any::{type_name, Any},
@@ -12,10 +12,11 @@ use std::{
use crate::{
render_scope::RenderScope,
widget::{Component, Widget},
Screen,
};
pub trait Extension {
fn init(&mut self, _widgets: &Vec<Arc<Widget>>) {}
fn init(&mut self, _screen: Arc<Screen>) {}
fn render_widget(&mut self, _scope: &mut RenderScope, _widget: &Arc<Widget>) {}
}
+16 -10
View File
@@ -1,16 +1,22 @@
use crate::{event, widget::Component};
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
pub struct Resource<T>(pub T);
#[derive(Clone)]
pub struct VecResource<T>(Arc<Mutex<Box<dyn FnMut(&T) + Send + Sync>>>);
impl<T: Send + Sync + 'static> Component for Resource<T> {
fn as_any(&self) -> &dyn std::any::Any {
self
impl<T: Send + Sync + 'static> VecResource<T> {
pub fn new() -> Self {
Self(Arc::new(Mutex::new(Box::new(|_| {}))))
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
pub fn iterate<F: FnMut(&T) + Send + Sync + 'static>(&self, f: F) {
*self.0.lock().unwrap() = Box::new(f);
}
pub fn push(&self, e: T) {
let i = self.0.clone();
std::thread::spawn(move || {
(i.lock().unwrap())(&e);
});
}
}
event!(ResourceUpdate);
+4 -5
View File
@@ -1,20 +1,19 @@
use std::{fmt::Debug, sync::Arc};
use crate::event;
use crate::extensions::Extension;
use crate::extensions::Handler;
use crate::{extensions::Extension, widget::Widget};
use crate::{event, Screen};
pub struct TickExtension(pub u16);
impl Extension for TickExtension {
fn init(&mut self, widgets: &Vec<Arc<Widget>>) {
fn init(&mut self, screen: Arc<Screen>) {
let rate_dur = 1000 / self.0 as u64;
std::thread::spawn({
let widgets = widgets.clone();
move || {
let mut tick = 0;
loop {
for w in &widgets {
for w in screen.widgets.lock().unwrap().iter() {
if let Some(on_tick) = w.get::<Handler<TickEvent>>() {
on_tick.call(&w, &TickEvent(tick))
}
+3 -3
View File
@@ -5,6 +5,7 @@ use crate::{
extensions::Extension,
style::{Position, Transform},
widget::Widget,
Screen,
};
pub struct VelocityExtension;
@@ -42,13 +43,12 @@ impl VelocityExtension {
}
impl Extension for VelocityExtension {
fn init(&mut self, widgets: &Vec<Arc<Widget>>) {
fn init(&mut self, screen: Arc<Screen>) {
std::thread::spawn({
let widgets = widgets.clone();
move || {
let mut tick = 0;
loop {
for widget in &widgets {
for widget in screen.widgets.lock().unwrap().iter() {
Self::apply_velocity_xy(tick, widget);
}
if tick > 1000 {