handlers and ids

This commit is contained in:
2025-07-05 06:28:50 +02:00
parent 921bfa27c1
commit 661756eccd
8 changed files with 112 additions and 39 deletions
+31
View File
@@ -0,0 +1,31 @@
use std::sync::Arc;
use crate::widget::{Element, Widget};
#[derive(Default)]
pub struct Div {
pub children: Vec<Arc<Widget>>,
}
impl Element for Div {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
impl Div {
pub fn new() -> Self {
Div {
children: Vec::new(),
}
}
pub fn draw<E: Element + 'static>(&mut self, element: E) -> &Arc<Widget> {
self.children.push(Arc::new(Widget::new(Box::new(element))));
self.children.last().unwrap()
}
}
+2
View File
@@ -1,3 +1,5 @@
pub mod div;
use crate::Element;
impl Element for String {
+31
View File
@@ -0,0 +1,31 @@
use std::sync::{Arc, Mutex};
use crate::{component, extensions::Extension, widget::Widget};
pub struct IdExtension(Mutex<Vec<Arc<Widget>>>);
impl Extension for Arc<IdExtension> {
fn init(&mut self, widgets: &Vec<Arc<Widget>>) {
self.0.lock().unwrap().append(&mut widgets.clone());
}
}
impl IdExtension {
pub fn new() -> Arc<Self> {
Arc::new(Self(Mutex::new(Vec::new())))
}
pub fn get_element(self: &Arc<IdExtension>, id: usize) -> Option<Arc<Widget>> {
for elem in self.0.lock().unwrap().iter() {
if let Some(current_id) = elem.get::<Id>() {
if current_id.0 == id {
return Some(elem.clone());
}
}
}
None
}
}
component!(Id(pub usize));
+27 -4
View File
@@ -1,11 +1,34 @@
pub mod id;
pub mod tick;
pub mod velocity;
use std::sync::Arc;
use std::{
fmt::Debug,
sync::{Arc, Mutex},
};
use crate::widget::Widget;
use crate::{render_scope::RenderScope, widget::Widget};
pub trait Extension {
fn init(&self, _widgets: &Vec<Arc<Widget>>) {}
fn render(&self, _widget: &Arc<Widget>) {}
fn init(&mut self, _widgets: &Vec<Arc<Widget>>) {}
fn render_widget(&mut self, _scope: &mut RenderScope, _widget: &Arc<Widget>) {}
}
#[derive(Clone)]
pub struct Handler(pub Arc<Mutex<dyn FnMut(&Arc<Widget>) + Send + Sync>>);
impl Handler {
pub fn new<F: FnMut(&Arc<Widget>) + Send + Sync + 'static>(f: F) -> Handler {
Handler(Arc::new(Mutex::new(f)))
}
pub fn call(&self, w: &Arc<Widget>) {
(self.0.lock().unwrap())(w)
}
}
impl Debug for Handler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Handler")
}
}
+5 -4
View File
@@ -1,18 +1,19 @@
use std::sync::Arc;
use std::{fmt::Debug, sync::Arc};
use crate::extensions::Handler;
use crate::{component, extensions::Extension, widget::Widget};
pub struct TickExtension(pub u16);
impl Extension for TickExtension {
fn init(&self, widgets: &Vec<Arc<Widget>>) {
fn init(&mut self, widgets: &Vec<Arc<Widget>>) {
let rate_dur = 1000 / self.0 as u64;
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)
on_tick.0.call(&w)
}
}
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
@@ -21,4 +22,4 @@ impl Extension for TickExtension {
}
}
component!(OnTick(pub fn(&Arc<Widget>)));
component!(OnTick(pub Handler));
+1 -1
View File
@@ -42,7 +42,7 @@ impl VelocityExtension {
}
impl Extension for VelocityExtension {
fn init(&self, widgets: &Vec<Arc<Widget>>) {
fn init(&mut self, widgets: &Vec<Arc<Widget>>) {
std::thread::spawn({
let widgets = widgets.clone();
move || {
+8 -12
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use crate::{
extensions::Extension,
@@ -17,7 +17,7 @@ pub mod widget;
pub struct Screen {
pub widgets: Vec<Arc<Widget>>,
extensions: Vec<Arc<Box<dyn Extension>>>,
extensions: Vec<Arc<Mutex<Box<dyn Extension>>>>,
}
impl Screen {
@@ -34,7 +34,7 @@ impl Screen {
}
pub fn extension<E: Extension + 'static>(&mut self, ext: E) {
self.extensions.push(Arc::new(Box::new(ext)));
self.extensions.push(Arc::new(Mutex::new(Box::new(ext))));
}
pub fn run(&mut self) -> std::io::Result<()> {
@@ -43,7 +43,7 @@ impl Screen {
}
for ext in &self.extensions {
ext.init(&self.widgets);
ext.lock().unwrap().init(&self.widgets);
}
utils::hide_cursor()?;
@@ -64,7 +64,10 @@ impl Screen {
scope.set_transform(&t);
}
self.render_extension(elem.clone()).unwrap();
for ext in &self.extensions {
ext.lock().unwrap().render_widget(&mut scope, elem);
}
elem.0.lock().unwrap().render(&mut scope);
if let Some(t) = elem.get() {
@@ -74,11 +77,4 @@ impl Screen {
}
Ok(())
}
pub fn render_extension(&self, wi: Arc<Widget>) -> std::io::Result<()> {
for ext in &self.extensions {
ext.render(&wi);
}
Ok(())
}
}
+7 -18
View File
@@ -1,33 +1,22 @@
use osui::{
extensions::{
id::{Id, IdExtension},
tick::{OnTick, TickExtension},
velocity::{Velocity, VelocityExtension},
Handler,
},
style::{Position, Transform},
Screen,
};
fn main() {
let mut screen = Screen::new();
screen.extension(VelocityExtension);
screen.extension(TickExtension(60));
let document = IdExtension::new();
screen.extension(document.clone());
screen.extension(TickExtension(10));
screen
.draw(format!("Hello, World!"))
.component(Transform::new())
.component(Velocity(100, 0))
.component(OnTick(|w| {
if let Some(mut v) = w.get::<Velocity>() {
if let Some(t) = w.get::<Transform>() {
if let Position::Const(c) = t.x {
if c >= 30 {
v.0 = 0;
w.set_component(v);
}
}
}
}
}));
.component(OnTick(Handler::new(move |_| {})))
.component(Id(69));
screen.run().unwrap();
}