Component params
This commit is contained in:
+14
-8
@@ -17,14 +17,7 @@ pub fn main() {
|
|||||||
|
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
let res = engine
|
let res = engine
|
||||||
.run(move |cx| {
|
.run(BenchmarkApp(items))
|
||||||
rsx! {
|
|
||||||
for _ in (0..items) {
|
|
||||||
"Hello, world!"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.view(cx.clone())
|
|
||||||
})
|
|
||||||
.expect("Failed to run engine");
|
.expect("Failed to run engine");
|
||||||
|
|
||||||
tx.send((items, res)).expect("Failed to send result");
|
tx.send((items, res)).expect("Failed to send result");
|
||||||
@@ -46,5 +39,18 @@ pub fn main() {
|
|||||||
for (items, bench) in results {
|
for (items, bench) in results {
|
||||||
println!("Results for {items} items:\n{bench}");
|
println!("Results for {items} items:\n{bench}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BenchmarkApp(pub usize);
|
||||||
|
|
||||||
|
impl ComponentImpl for BenchmarkApp {
|
||||||
|
fn call(&self, cx: &Arc<Context>) -> View {
|
||||||
|
let items = self.0.clone();
|
||||||
|
rsx! {
|
||||||
|
for _ in (0..items) {
|
||||||
|
"Hello, world!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.view(cx.clone())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,16 @@ use osui::prelude::*;
|
|||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let engine = Console::new();
|
let engine = Console::new();
|
||||||
engine.run(app).expect("Failed to run engine");
|
engine.run(App).expect("Failed to run engine");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
struct App;
|
||||||
rsx! {
|
|
||||||
"Hello, world!"
|
impl ComponentImpl for App {
|
||||||
|
fn call(&self, cx: &Arc<Context>) -> View {
|
||||||
|
rsx! {
|
||||||
|
"Hello, world!"
|
||||||
|
}
|
||||||
|
.view(cx.clone())
|
||||||
}
|
}
|
||||||
.view(cx.clone())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use osui::prelude::*;
|
|||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let engine = Benchmark::new(Console::new());
|
let engine = Benchmark::new(Console::new());
|
||||||
let benchmark_result = engine.run(app).expect("Failed to run engine");
|
let benchmark_result = engine.run(App).expect("Failed to run engine");
|
||||||
println!("Avg: {} μs", benchmark_result.average);
|
println!("Avg: {} μs", benchmark_result.average);
|
||||||
println!("Min: {} μs", benchmark_result.min);
|
println!("Min: {} μs", benchmark_result.min);
|
||||||
println!("Max: {} μs", benchmark_result.max);
|
println!("Max: {} μs", benchmark_result.max);
|
||||||
@@ -12,9 +12,13 @@ pub fn main() {
|
|||||||
println!("Tot Render: {} μs", benchmark_result.total_render);
|
println!("Tot Render: {} μs", benchmark_result.total_render);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
struct App;
|
||||||
rsx! {
|
|
||||||
"Hello, world!"
|
impl ComponentImpl for App {
|
||||||
|
fn call(&self, cx: &Arc<Context>) -> View {
|
||||||
|
rsx! {
|
||||||
|
"Hello, world!"
|
||||||
|
}
|
||||||
|
.view(cx.clone())
|
||||||
}
|
}
|
||||||
.view(cx.clone())
|
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-6
@@ -11,9 +11,13 @@ use crate::{
|
|||||||
View, ViewWrapper,
|
View, ViewWrapper,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> View + Send + Sync>;
|
pub type Component = Arc<dyn ComponentImpl>;
|
||||||
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sync>>;
|
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sync>>;
|
||||||
|
|
||||||
|
pub trait ComponentImpl: Send + Sync {
|
||||||
|
fn call(&self, cx: &Arc<Context>) -> View;
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Scope {
|
pub struct Scope {
|
||||||
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
||||||
executor: Arc<dyn CommandExecutor>,
|
executor: Arc<dyn CommandExecutor>,
|
||||||
@@ -28,7 +32,7 @@ pub struct Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
pub fn new<F: Fn(&Arc<Self>) -> View + Send + Sync + 'static>(
|
pub fn new<F: ComponentImpl + 'static>(
|
||||||
component: F,
|
component: F,
|
||||||
executor: Arc<dyn CommandExecutor>,
|
executor: Arc<dyn CommandExecutor>,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
@@ -48,7 +52,7 @@ impl Context {
|
|||||||
move || {
|
move || {
|
||||||
s.event_handlers.lock().unwrap().clear();
|
s.event_handlers.lock().unwrap().clear();
|
||||||
let c = s.component.lock().unwrap().clone();
|
let c = s.component.lock().unwrap().clone();
|
||||||
*s.view.lock().unwrap() = (c)(&s);
|
*s.view.lock().unwrap() = c.call(&s);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -62,7 +66,7 @@ impl Context {
|
|||||||
move || {
|
move || {
|
||||||
s.event_handlers.lock().unwrap().clear();
|
s.event_handlers.lock().unwrap().clear();
|
||||||
let c = s.component.lock().unwrap().clone();
|
let c = s.component.lock().unwrap().clone();
|
||||||
*s.view.lock().unwrap() = (c)(&s);
|
*s.view.lock().unwrap() = c.call(&s);
|
||||||
*done.lock().unwrap() = true;
|
*done.lock().unwrap() = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -194,7 +198,7 @@ impl Scope {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
pub fn child<F: ComponentImpl + 'static>(
|
||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
child: F,
|
child: F,
|
||||||
view_wrapper: Option<ViewWrapper>,
|
view_wrapper: Option<ViewWrapper>,
|
||||||
@@ -207,10 +211,25 @@ impl Scope {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn view(self: &Arc<Self>, view: View) {
|
pub fn view(self: &Arc<Self>, view: View) {
|
||||||
let ctx = Context::new(move |_| view.clone(), self.executor.clone());
|
let ctx = Context::new(view, self.executor.clone());
|
||||||
|
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
self.children.lock().unwrap().push((ctx, None));
|
self.children.lock().unwrap().push((ctx, None));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ComponentImpl for View {
|
||||||
|
fn call(&self, _: &Arc<Context>) -> View {
|
||||||
|
self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> ComponentImpl for F
|
||||||
|
where
|
||||||
|
F: Fn(&Arc<Context>) -> View + Send + Sync,
|
||||||
|
{
|
||||||
|
fn call(&self, ctx: &Arc<Context>) -> View {
|
||||||
|
self(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,11 @@ use std::{io::stdout, sync::Arc, time::Instant};
|
|||||||
|
|
||||||
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||||
|
|
||||||
use crate::{prelude::Context, render::Area, DrawContext, View};
|
use crate::{
|
||||||
|
prelude::{ComponentImpl, Context},
|
||||||
|
render::Area,
|
||||||
|
DrawContext, View,
|
||||||
|
};
|
||||||
|
|
||||||
use super::Engine;
|
use super::Engine;
|
||||||
|
|
||||||
@@ -24,10 +28,7 @@ impl<T: Engine> Benchmark<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Engine> Engine<BenchmarkResult> for Benchmark<T> {
|
impl<T: Engine> Engine<BenchmarkResult> for Benchmark<T> {
|
||||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
fn run<F: ComponentImpl + 'static>(&self, component: F) -> crate::Result<BenchmarkResult> {
|
||||||
&self,
|
|
||||||
component: F,
|
|
||||||
) -> crate::Result<BenchmarkResult> {
|
|
||||||
let mut times: Vec<u128> = Vec::new();
|
let mut times: Vec<u128> = Vec::new();
|
||||||
let cx = self.init(component);
|
let cx = self.init(component);
|
||||||
|
|
||||||
@@ -59,10 +60,7 @@ impl<T: Engine> Engine<BenchmarkResult> for Benchmark<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
fn init<F: ComponentImpl + 'static>(&self, component: F) -> Arc<Context> {
|
||||||
&self,
|
|
||||||
component: F,
|
|
||||||
) -> Arc<Context> {
|
|
||||||
self.0.init(component)
|
self.0.init(component)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
engine::{commands, CommandExecutor},
|
engine::{commands, CommandExecutor},
|
||||||
prelude::Context,
|
prelude::{ComponentImpl, Context},
|
||||||
render::Area,
|
render::Area,
|
||||||
DrawContext, View,
|
DrawContext, View,
|
||||||
};
|
};
|
||||||
@@ -79,10 +79,7 @@ impl Engine for Console {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
fn init<C: ComponentImpl + 'static>(&self, component: C) -> Arc<Context> {
|
||||||
&self,
|
|
||||||
component: F,
|
|
||||||
) -> Arc<Context> {
|
|
||||||
let cx = Context::new(component, self.executor.clone());
|
let cx = Context::new(component, self.executor.clone());
|
||||||
cx.refresh();
|
cx.refresh();
|
||||||
|
|
||||||
@@ -102,10 +99,7 @@ impl Engine for Console {
|
|||||||
self.executor.clone()
|
self.executor.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
fn run<F: ComponentImpl + 'static>(&self, component: F) -> crate::Result<()> {
|
||||||
&self,
|
|
||||||
component: F,
|
|
||||||
) -> crate::Result<()> {
|
|
||||||
let cx = self.init(component);
|
let cx = self.init(component);
|
||||||
|
|
||||||
while self.executor.is_running() {
|
while self.executor.is_running() {
|
||||||
|
|||||||
+7
-10
@@ -7,18 +7,15 @@ pub use console::*;
|
|||||||
|
|
||||||
use std::{any::Any, sync::Arc};
|
use std::{any::Any, sync::Arc};
|
||||||
|
|
||||||
use crate::{prelude::Context, render::Area, DrawContext, View};
|
use crate::{
|
||||||
|
prelude::{ComponentImpl, Context},
|
||||||
|
render::Area,
|
||||||
|
DrawContext, View,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait Engine<Output = ()> {
|
pub trait Engine<Output = ()> {
|
||||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
fn run<C: ComponentImpl + 'static>(&self, component: C) -> crate::Result<Output>;
|
||||||
&self,
|
fn init<C: ComponentImpl + 'static>(&self, component: C) -> Arc<Context>;
|
||||||
component: F,
|
|
||||||
) -> crate::Result<Output>;
|
|
||||||
|
|
||||||
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
|
||||||
&self,
|
|
||||||
component: F,
|
|
||||||
) -> Arc<Context>;
|
|
||||||
fn render(&self, cx: &Arc<Context>);
|
fn render(&self, cx: &Arc<Context>);
|
||||||
fn render_delay(&self) {
|
fn render_delay(&self) {
|
||||||
std::thread::sleep(std::time::Duration::from_millis(16))
|
std::thread::sleep(std::time::Duration::from_millis(16))
|
||||||
|
|||||||
Reference in New Issue
Block a user