multithreading

This commit is contained in:
2025-06-29 22:56:53 +02:00
parent 85bfa0f45f
commit bbaa9d09e1
5 changed files with 35 additions and 18 deletions
+1
View File
@@ -6,5 +6,6 @@ use std::sync::Arc;
use crate::widget::Widget; use crate::widget::Widget;
pub trait Extension { pub trait Extension {
fn init(&self, _widgets: &Vec<Arc<Widget>>) {}
fn render(&self, _widget: &Arc<Widget>) {} fn render(&self, _widget: &Arc<Widget>) {}
} }
+14 -5
View File
@@ -2,13 +2,22 @@ use std::sync::Arc;
use crate::{component, extensions::Extension, widget::Widget}; use crate::{component, extensions::Extension, widget::Widget};
pub struct TickExtension; pub struct TickExtension(pub u16);
impl Extension for TickExtension { impl Extension for TickExtension {
fn render(&self, _widget: &Arc<Widget>) { fn init(&self, widgets: &Vec<Arc<Widget>>) {
if let Some(on_tick) = _widget.get::<OnTick>() { let rate_dur = 1000 / self.0 as u64;
(on_tick.0)(_widget) std::thread::spawn({
} let widgets = widgets.clone();
move || loop {
for w in &widgets {
if let Some(on_tick) = w.get::<OnTick>() {
(on_tick.0)(&w)
}
}
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
}
});
} }
} }
+11 -7
View File
@@ -16,21 +16,21 @@ pub mod utils;
pub mod widget; pub mod widget;
pub struct Screen { pub struct Screen {
pub elements: Vec<Arc<Widget>>, pub widgets: Vec<Arc<Widget>>,
extensions: Vec<Arc<Box<dyn Extension>>>, extensions: Vec<Arc<Box<dyn Extension>>>,
} }
impl Screen { impl Screen {
pub fn new() -> Screen { pub fn new() -> Screen {
Screen { Screen {
elements: Vec::new(), widgets: Vec::new(),
extensions: Vec::new(), extensions: Vec::new(),
} }
} }
pub fn draw<E: Element + 'static>(&mut self, element: E) -> &Arc<Widget> { pub fn draw<E: Element + 'static>(&mut self, element: E) -> &Arc<Widget> {
self.elements.push(Arc::new(Widget::new(Box::new(element)))); self.widgets.push(Arc::new(Widget::new(Box::new(element))));
self.elements.last().unwrap() self.widgets.last().unwrap()
} }
pub fn extension<E: Extension + 'static>(&mut self, ext: E) { pub fn extension<E: Extension + 'static>(&mut self, ext: E) {
@@ -38,15 +38,19 @@ impl Screen {
} }
pub fn run(&mut self) -> std::io::Result<()> { pub fn run(&mut self) -> std::io::Result<()> {
for elem in &mut self.elements { for elem in &mut self.widgets {
elem.component(Transform::new()); elem.component(Transform::new());
} }
for ext in &self.extensions {
ext.init(&self.widgets);
}
utils::hide_cursor()?; utils::hide_cursor()?;
loop { loop {
self.render()?; self.render()?;
std::thread::sleep(std::time::Duration::from_millis(50)); std::thread::sleep(std::time::Duration::from_millis(28));
} }
} }
@@ -54,7 +58,7 @@ impl Screen {
let mut scope = RenderScope::new(); let mut scope = RenderScope::new();
utils::clear()?; utils::clear()?;
for elem in &self.elements { for elem in &self.widgets {
scope.clear(); scope.clear();
if let Some(t) = elem.get() { if let Some(t) = elem.get() {
scope.set_transform(&t); scope.set_transform(&t);
+2 -2
View File
@@ -10,7 +10,7 @@ use osui::{
fn main() { fn main() {
let mut screen = Screen::new(); let mut screen = Screen::new();
screen.extension(VelocityExtension); screen.extension(VelocityExtension);
screen.extension(TickExtension); screen.extension(TickExtension(60));
screen screen
.draw(format!("Hello, World!")) .draw(format!("Hello, World!"))
@@ -20,7 +20,7 @@ fn main() {
if let Some(mut v) = w.get::<Velocity>() { if let Some(mut v) = w.get::<Velocity>() {
if let Some(t) = w.get::<Transform>() { if let Some(t) = w.get::<Transform>() {
if let Position::Const(c) = t.x { if let Position::Const(c) = t.x {
if c == 30 { if c >= 30 {
v.0 = 0; v.0 = 0;
w.set_component(v); w.set_component(v);
} }
+7 -4
View File
@@ -6,21 +6,24 @@ use std::{
use crate::render_scope::RenderScope; use crate::render_scope::RenderScope;
pub trait Element { pub type BoxedElement = Box<dyn Element + Send + Sync>;
pub type BoxedComponent = Box<dyn Component + Send + Sync>;
pub trait Element: Send + Sync {
#[allow(unused)] #[allow(unused)]
fn render(&mut self, scope: &mut RenderScope) {} fn render(&mut self, scope: &mut RenderScope) {}
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any;
} }
pub trait Component { pub trait Component: Send + Sync {
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any;
} }
pub struct Widget( pub struct Widget(
pub Mutex<Box<dyn Element>>, pub Mutex<BoxedElement>,
Mutex<HashMap<TypeId, Box<dyn Component>>>, Mutex<HashMap<TypeId, BoxedComponent>>,
); );
impl Widget { impl Widget {