Compare commits
143
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
506ccae049 | ||
|
|
86e3f4e684 | ||
|
|
89b460697f | ||
|
|
bd53e6450c | ||
|
|
083812458b | ||
|
|
4364177538 | ||
|
|
98598bec15 | ||
|
|
eb702dce45 | ||
|
|
229353f5f0 | ||
|
|
04a69b138a | ||
|
|
993a790da7 | ||
|
|
ecc1172737 | ||
|
|
a9ecc8da8b | ||
|
|
f6a4bbd8ac | ||
|
|
4afe1c24c2 | ||
|
|
b50ca3263b | ||
|
|
6b3e9239c9 | ||
|
|
470512a542 | ||
|
|
6e481862ec | ||
|
|
05a390b10f | ||
|
|
926405a863 | ||
|
|
9953e4a0bc | ||
|
|
8f0712d4f8 | ||
|
|
9d55dacde2 | ||
|
|
abe37686d8 | ||
|
|
02f3aea4e3 | ||
|
|
33370287b2 | ||
|
|
0f12a371e5 | ||
|
|
1f9bf48bb4 | ||
|
|
49bb99fedb | ||
|
|
979af999e4 | ||
|
|
0515087f0d | ||
|
|
2a62e53ed5 | ||
|
|
c2b9bb3aa1 | ||
|
|
0a026c4ee7 | ||
|
|
15ff801cb0 | ||
|
|
538cc8731f | ||
|
|
b326fc48ed | ||
|
|
8740bbee89 | ||
|
|
22d02e7d1a | ||
|
|
0cefbaa223 | ||
|
|
b57fffe9b7 | ||
|
|
4d2f0d7b9d | ||
|
|
ff351caee0 | ||
|
|
e4110976cd | ||
|
|
1fbff7a0bc | ||
|
|
6e8ca5535e | ||
|
|
e031f9bcef | ||
|
|
c6d11315d6 | ||
|
|
68d00be96c | ||
|
|
f5d1b42db4 | ||
|
|
63ed7b8da4 | ||
|
|
98b2a4e667 | ||
|
|
e4b905d085 | ||
|
|
1d6ce1506e | ||
|
|
0e3c1ccafe | ||
|
|
dbd16956dd | ||
|
|
c91a473263 | ||
|
|
6424ad3390 | ||
|
|
23edb6c917 | ||
|
|
72e8df007e | ||
|
|
a2350f10f5 | ||
|
|
2d41f3b50b | ||
|
|
5cb6d341af | ||
|
|
f41a4936f9 | ||
|
|
dbb8a0e01a | ||
|
|
fd8e466830 | ||
|
|
29432a939b | ||
|
|
9d4b247b2a | ||
|
|
916818f915 | ||
|
|
5cf59f751d | ||
|
|
c460e073a7 | ||
|
|
f51182b2b1 | ||
|
|
6100023a2a | ||
|
|
3d9aa35964 | ||
|
|
9a03f241d8 | ||
|
|
fde8ad17d6 | ||
|
|
e0cf149790 | ||
|
|
1e590597f3 | ||
|
|
935f18178e | ||
|
|
20a9d2b216 | ||
|
|
3520369e35 | ||
|
|
c8ebe36d78 | ||
|
|
d3861909e7 | ||
|
|
1bdbc447ab | ||
|
|
e0059585ce | ||
|
|
b363c81a24 | ||
|
|
2fae5c71f8 | ||
|
|
9868f02c33 | ||
|
|
896dfb0a36 | ||
|
|
3a5286a54f | ||
|
|
ca4ee54e9d | ||
|
|
62e0c1eac1 | ||
|
|
48397c06fa | ||
|
|
14602f95c8 | ||
|
|
fbd84f98d3 | ||
|
|
db1de03fbb | ||
|
|
6569a9eb20 | ||
|
|
2c7d3cc80f | ||
|
|
2383a7b42a | ||
|
|
d59c3d7911 | ||
|
|
1aa78bc1a4 | ||
|
|
f8f924ce25 | ||
|
|
ea9afb0e64 | ||
|
|
71299046d3 | ||
|
|
acce51c372 | ||
|
|
ba04a82980 | ||
|
|
ec3576e6db | ||
|
|
e0ae338e15 | ||
|
|
d2757ad7b0 | ||
|
|
de039fac96 | ||
|
|
2d8755b13c | ||
|
|
d9d107bd80 | ||
|
|
35dcc4d22c | ||
|
|
34b5799644 | ||
|
|
911968a1cb | ||
|
|
d00fbb6565 | ||
|
|
2892176a76 | ||
|
|
36e06732d9 | ||
|
|
3a4c3505ac | ||
|
|
0ccf03de3b | ||
|
|
48849c7cc2 | ||
|
|
2b0d85549f | ||
|
|
598ab5b54d | ||
|
|
095aa14b82 | ||
|
|
d1008a69c9 | ||
|
|
a30afdeb28 | ||
|
|
1debb6a576 | ||
|
|
fc0406e412 | ||
|
|
27580e773a | ||
|
|
a1499064ac | ||
|
|
c2563358b6 | ||
|
|
b45ef950a1 | ||
|
|
063c90d986 | ||
|
|
da1ecb1f21 | ||
|
|
75827260cc | ||
|
|
9a91fb3af0 | ||
|
|
bbff192788 | ||
|
|
73e2c214ee | ||
|
|
45db1fb774 | ||
|
|
af5428a2f3 | ||
|
|
b227f19a9c | ||
|
|
ae16c9a69e |
@@ -0,0 +1,5 @@
|
|||||||
|
debug
|
||||||
|
target
|
||||||
|
github
|
||||||
|
Cargo.lock
|
||||||
|
.DS_Store
|
||||||
+4
-4
@@ -1,16 +1,15 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "osui"
|
name = "osui"
|
||||||
version = "0.0.9-alpha.2"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
description = "A TUI library for advanced uis"
|
description = "A TUI library for advanced uis"
|
||||||
repository = "https://github.com/osui-rs/osui"
|
repository = "https://github.com/osui-rs/osui"
|
||||||
homepage = "https://osui.netlify.app"
|
homepage = "https://osui.netlify.app"
|
||||||
documentation = "https://osui.netlify.app/docs/"
|
documentation = "https://osui.netlify.app/docs/"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
no_rsx = []
|
rsx = []
|
||||||
no_elem = []
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "osui"
|
name = "osui"
|
||||||
@@ -25,6 +24,7 @@ cc = "1.0"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm = "0.28.1"
|
crossterm = "0.28.1"
|
||||||
|
figlet-rs = "0.1.5"
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 1
|
opt-level = 1
|
||||||
|
|||||||
@@ -21,14 +21,15 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use osui::{style::Transform, Screen};
|
use osui::prelude::*;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let screen = Screen::new();
|
let screen = Screen::new();
|
||||||
|
|
||||||
screen
|
rsx! {
|
||||||
.draw(format!("Hello, World"))
|
"Hello, World"
|
||||||
.component(Transform::center());
|
}
|
||||||
|
.draw(&screen);
|
||||||
|
|
||||||
screen.run()
|
screen.run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use osui::{prelude::*, rsx};
|
||||||
|
|
||||||
|
pub struct Count(pub State<u16>);
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let console = Console::new();
|
||||||
|
console.run(app);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn app(cx: &Arc<Context>) -> View {
|
||||||
|
let count = use_state(0);
|
||||||
|
let mount = use_mount_manual();
|
||||||
|
|
||||||
|
// Sync state with children
|
||||||
|
use_sync_effect(cx, &count, |v| Count(v.clone()), &[&mount, &count]);
|
||||||
|
|
||||||
|
use_effect(
|
||||||
|
{
|
||||||
|
let count = count.clone();
|
||||||
|
move || loop {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||||
|
let mut count = count.get();
|
||||||
|
if *count > 5 {
|
||||||
|
*count = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&[&mount],
|
||||||
|
);
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
// Static scope
|
||||||
|
my_component (ctx, view) {
|
||||||
|
let area = ctx.allocate(5, 0, 10, 10);
|
||||||
|
ctx.draw_view(area, view);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dynamic scope
|
||||||
|
if %count (*count.get() > 5) {
|
||||||
|
my_component (ctx, view) {
|
||||||
|
let area = ctx.allocate(18, 0, 10, 10);
|
||||||
|
ctx.draw_view(area, view);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dynamic scope
|
||||||
|
for %count (i in 0..count.get_dl()) {
|
||||||
|
"{i}" @Point { x: 0, y: i };
|
||||||
|
}
|
||||||
|
|
||||||
|
!mount mount
|
||||||
|
}
|
||||||
|
.view(cx.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn my_component(cx: &Arc<Context>) -> View {
|
||||||
|
// Sync state with parent
|
||||||
|
let count = use_sync_state(cx, 0, |Count(v)| v.get_dl());
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
"Count: {count}"
|
||||||
|
}
|
||||||
|
.view(cx.clone())
|
||||||
|
}
|
||||||
@@ -0,0 +1,199 @@
|
|||||||
|
use std::{
|
||||||
|
any::{Any, TypeId},
|
||||||
|
collections::HashMap,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
render::DrawContext,
|
||||||
|
state::{use_effect, HookDependency},
|
||||||
|
View, ViewWrapper,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type Component = Arc<dyn Fn(&Arc<Context>) -> View + Send + Sync>;
|
||||||
|
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sync>>;
|
||||||
|
|
||||||
|
pub struct Scope {
|
||||||
|
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Context {
|
||||||
|
component: Mutex<Component>,
|
||||||
|
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
||||||
|
view: Mutex<View>,
|
||||||
|
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Context {
|
||||||
|
pub fn new<F: Fn(&Arc<Self>) -> View + Send + Sync + 'static>(component: F) -> Arc<Self> {
|
||||||
|
Arc::new(Self {
|
||||||
|
component: Mutex::new(Arc::new(component)),
|
||||||
|
event_handlers: Mutex::new(HashMap::new()),
|
||||||
|
view: Mutex::new(Arc::new(|_| {})),
|
||||||
|
scopes: Mutex::new(Vec::new()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refresh(self: &Arc<Self>) {
|
||||||
|
let s = self.clone();
|
||||||
|
|
||||||
|
std::thread::spawn({
|
||||||
|
move || {
|
||||||
|
s.event_handlers.lock().unwrap().clear();
|
||||||
|
let c = s.component.lock().unwrap().clone();
|
||||||
|
*s.view.lock().unwrap() = (c)(&s);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refresh_atomic(self: &Arc<Self>) -> Arc<Mutex<bool>> {
|
||||||
|
let s = self.clone();
|
||||||
|
let done = Arc::new(Mutex::new(false));
|
||||||
|
|
||||||
|
std::thread::spawn({
|
||||||
|
let done = done.clone();
|
||||||
|
move || {
|
||||||
|
s.event_handlers.lock().unwrap().clear();
|
||||||
|
let c = s.component.lock().unwrap().clone();
|
||||||
|
*s.view.lock().unwrap() = (c)(&s);
|
||||||
|
*done.lock().unwrap() = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_view(self: &Arc<Self>) -> View {
|
||||||
|
self.view.lock().unwrap().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_event<T: Any + 'static, F: Fn(&Arc<Self>, &T) + Send + Sync + 'static>(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
handler: F,
|
||||||
|
) {
|
||||||
|
self.event_handlers
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.entry(TypeId::of::<T>())
|
||||||
|
.or_insert_with(|| Vec::new())
|
||||||
|
.push(Arc::new(Mutex::new(
|
||||||
|
move |ctx: &Arc<Self>, event: &dyn Any| {
|
||||||
|
if let Some(e) = event.downcast_ref::<T>() {
|
||||||
|
(handler)(ctx, e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn emit_event<E: Any + 'static>(self: &Arc<Self>, event: &E) {
|
||||||
|
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
|
||||||
|
for i in v {
|
||||||
|
(i.lock().unwrap())(self, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for scope in self.scopes.lock().unwrap().iter() {
|
||||||
|
for (child, _) in scope.children.lock().unwrap().iter() {
|
||||||
|
child.emit_event(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn emit_event_threaded<E: Any + Send + Sync + Clone + 'static>(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
event: &E,
|
||||||
|
) {
|
||||||
|
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
|
||||||
|
for i in v {
|
||||||
|
let i = i.clone();
|
||||||
|
let event = event.clone();
|
||||||
|
let s = self.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
(i.lock().unwrap())(&s, &event);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for scope in self.scopes.lock().unwrap().iter() {
|
||||||
|
for (child, _) in scope.children.lock().unwrap().iter() {
|
||||||
|
child.emit_event_threaded(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scope(self: &Arc<Self>) -> Arc<Scope> {
|
||||||
|
let scope = Arc::new(Scope {
|
||||||
|
children: Mutex::new(Vec::new()),
|
||||||
|
});
|
||||||
|
self.scopes.lock().unwrap().push(scope.clone());
|
||||||
|
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dyn_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
drawer: F,
|
||||||
|
dependencies: &[&dyn HookDependency],
|
||||||
|
) -> Arc<Scope> {
|
||||||
|
let scope = Arc::new(Scope {
|
||||||
|
children: Mutex::new(Vec::new()),
|
||||||
|
});
|
||||||
|
self.scopes.lock().unwrap().push(scope.clone());
|
||||||
|
|
||||||
|
drawer(&scope);
|
||||||
|
|
||||||
|
use_effect(
|
||||||
|
{
|
||||||
|
let scope = scope.clone();
|
||||||
|
move || {
|
||||||
|
drawer(&scope);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
dependencies,
|
||||||
|
);
|
||||||
|
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext) {
|
||||||
|
for scope in self.scopes.lock().unwrap().iter() {
|
||||||
|
for (child, view_wrapper) in scope.children.lock().unwrap().iter() {
|
||||||
|
let view = child.get_view();
|
||||||
|
|
||||||
|
if let Some(view_wrapper) = view_wrapper {
|
||||||
|
view_wrapper(ctx, view)
|
||||||
|
} else {
|
||||||
|
ctx.draw_view(ctx.area.clone(), view);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scope {
|
||||||
|
pub fn new() -> Arc<Self> {
|
||||||
|
Arc::new(Self {
|
||||||
|
children: Mutex::new(Vec::new()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
child: F,
|
||||||
|
view_wrapper: Option<ViewWrapper>,
|
||||||
|
) {
|
||||||
|
let ctx = Context::new(child);
|
||||||
|
|
||||||
|
ctx.refresh();
|
||||||
|
|
||||||
|
self.children.lock().unwrap().push((ctx, view_wrapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn view(self: &Arc<Self>, view: View) {
|
||||||
|
let ctx = Context::new(move |_| view.clone());
|
||||||
|
|
||||||
|
ctx.refresh();
|
||||||
|
|
||||||
|
self.children.lock().unwrap().push((ctx, None));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
widget::{Element, Widget},
|
|
||||||
NoRender,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Div {
|
|
||||||
color: u32,
|
|
||||||
children: Mutex<Vec<Arc<Widget>>>,
|
|
||||||
size: (u16, u16),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Element for Div {
|
|
||||||
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
let (width, height) = scope.get_size_or(self.size.0, self.size.1);
|
|
||||||
scope.draw_rect(width, height, self.color);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
let mut transform = scope.get_transform().clone();
|
|
||||||
let (w, h) = scope.get_parent_size();
|
|
||||||
scope.set_parent_size(transform.width, transform.height);
|
|
||||||
|
|
||||||
for elem in self.children.lock().unwrap().iter() {
|
|
||||||
scope.clear();
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
elem.0.lock().unwrap().render(scope);
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
let t = scope.get_transform_mut();
|
|
||||||
transform.width = transform.width.max(t.x + t.width);
|
|
||||||
transform.height = transform.height.max(t.y + t.height);
|
|
||||||
t.x += transform.x;
|
|
||||||
t.y += transform.y;
|
|
||||||
|
|
||||||
scope.draw();
|
|
||||||
elem.0.lock().unwrap().after_render(scope);
|
|
||||||
scope.clear();
|
|
||||||
}
|
|
||||||
scope.set_parent_size(w, h);
|
|
||||||
self.size = (transform.width, transform.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_child(&self, element: &Arc<Widget>) {
|
|
||||||
self.children.lock().unwrap().push(element.clone());
|
|
||||||
element.inject(|w| w.component(NoRender));
|
|
||||||
}
|
|
||||||
|
|
||||||
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(color: u32) -> Self {
|
|
||||||
Div {
|
|
||||||
color,
|
|
||||||
children: Mutex::new(Vec::new()),
|
|
||||||
size: (0, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use crate::{widget::{Element, Widget}, NoRender};
|
|
||||||
|
|
||||||
pub struct FlexRow {
|
|
||||||
color: u32,
|
|
||||||
gap: u16,
|
|
||||||
children: Mutex<Vec<Arc<Widget>>>,
|
|
||||||
size: (u16, u16),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FlexCol {
|
|
||||||
color: u32,
|
|
||||||
gap: u16,
|
|
||||||
children: Mutex<Vec<Arc<Widget>>>,
|
|
||||||
size: (u16, u16),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Element for FlexRow {
|
|
||||||
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
let (width, height) = scope.get_size_or(self.size.0, self.size.1);
|
|
||||||
scope.draw_rect(width, height, self.color);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
let mut transform = scope.get_transform().clone();
|
|
||||||
let (w, h) = scope.get_parent_size();
|
|
||||||
scope.set_parent_size(transform.width, transform.height);
|
|
||||||
|
|
||||||
let mut v = 0;
|
|
||||||
|
|
||||||
for elem in self.children.lock().unwrap().iter() {
|
|
||||||
scope.clear();
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
elem.0.lock().unwrap().render(scope);
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
let t = scope.get_transform_mut();
|
|
||||||
transform.width = transform.width.max(t.width);
|
|
||||||
transform.height = transform.height.max(v + t.height);
|
|
||||||
t.x += transform.x;
|
|
||||||
t.y += transform.y + v;
|
|
||||||
v += t.height + self.gap;
|
|
||||||
|
|
||||||
scope.draw();
|
|
||||||
|
|
||||||
elem.0.lock().unwrap().after_render(scope);
|
|
||||||
}
|
|
||||||
scope.set_parent_size(w, h);
|
|
||||||
self.size = (transform.width, transform.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_child(&self, element: &Arc<Widget>) {
|
|
||||||
self.children.lock().unwrap().push(element.clone());
|
|
||||||
element.inject(|w| w.component(NoRender));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FlexRow {
|
|
||||||
pub fn new(color: u32, gap: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
children: Mutex::new(Vec::new()),
|
|
||||||
size: (0, 0),
|
|
||||||
color,
|
|
||||||
gap,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Element for FlexCol {
|
|
||||||
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
let (width, height) = scope.get_size_or(self.size.0, self.size.1);
|
|
||||||
scope.draw_rect(width, height, self.color);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
let mut transform = scope.get_transform().clone();
|
|
||||||
let (w, h) = scope.get_parent_size();
|
|
||||||
scope.set_parent_size(transform.width, transform.height);
|
|
||||||
|
|
||||||
let mut v = 0;
|
|
||||||
|
|
||||||
for elem in self.children.lock().unwrap().iter() {
|
|
||||||
scope.clear();
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
elem.0.lock().unwrap().render(scope);
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
let t = scope.get_transform_mut();
|
|
||||||
transform.width = transform.width.max(v + t.width);
|
|
||||||
transform.height = transform.height.max(t.height);
|
|
||||||
t.x += transform.x + v;
|
|
||||||
t.y += transform.y;
|
|
||||||
v += t.width + self.gap;
|
|
||||||
|
|
||||||
scope.draw();
|
|
||||||
|
|
||||||
elem.0.lock().unwrap().after_render(scope);
|
|
||||||
}
|
|
||||||
scope.set_parent_size(w, h);
|
|
||||||
self.size = (transform.width, transform.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_child(&self, element: &Arc<Widget>) {
|
|
||||||
self.children.lock().unwrap().push(element.clone());
|
|
||||||
element.inject(|w| w.component(NoRender));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FlexCol {
|
|
||||||
pub fn new(color: u32, gap: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
children: Mutex::new(Vec::new()),
|
|
||||||
size: (0, 0),
|
|
||||||
color,
|
|
||||||
gap,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
pub mod div;
|
|
||||||
pub mod flex;
|
|
||||||
pub mod shape;
|
|
||||||
|
|
||||||
pub use div::*;
|
|
||||||
pub use flex::*;
|
|
||||||
pub use shape::*;
|
|
||||||
|
|
||||||
use crate::Element;
|
|
||||||
|
|
||||||
impl Element for String {
|
|
||||||
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
scope.draw_text(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Element for (String, u32) {
|
|
||||||
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
|
|
||||||
scope.draw_text_colored(&self.0, self.1);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
use crate::widget::Element;
|
|
||||||
|
|
||||||
pub struct RoundedOutline;
|
|
||||||
|
|
||||||
impl Element for RoundedOutline {
|
|
||||||
fn render(&mut self, scope: &mut crate::prelude::RenderScope) {
|
|
||||||
let (w, h) = scope.get_size_or_parent();
|
|
||||||
if w > 2 && h > 2 {
|
|
||||||
let d = "─".repeat(w as usize - 2);
|
|
||||||
scope.draw_text(&format!(
|
|
||||||
"╭{d}╮{}\n╰{d}╯",
|
|
||||||
format!("\n│{}│", " ".repeat(w as usize - 2)).repeat(h as usize - 2),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RoundedOutline {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Outline;
|
|
||||||
|
|
||||||
impl Element for Outline {
|
|
||||||
fn render(&mut self, scope: &mut crate::prelude::RenderScope) {
|
|
||||||
let (w, h) = scope.get_size_or_parent();
|
|
||||||
if w > 2 && h > 2 {
|
|
||||||
let d = "─".repeat(w as usize - 2);
|
|
||||||
scope.draw_text(&format!(
|
|
||||||
"┌{d}┐{}\n└{d}┘",
|
|
||||||
format!("\n│{}│", " ".repeat(w as usize - 2)).repeat(h as usize - 2),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Outline {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
use std::{
|
||||||
|
io::{stdout, Write},
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||||
|
|
||||||
|
use crate::{prelude::Context, render::Area, DrawContext, View};
|
||||||
|
|
||||||
|
pub trait Engine {
|
||||||
|
fn render_view(&self, area: &Area, view: &View) -> DrawContext;
|
||||||
|
fn draw_context(&self, ctx: &DrawContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Console {
|
||||||
|
threads: Mutex<Vec<Arc<dyn Fn(Arc<Context>) + Send + Sync>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Console {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
threads: Mutex::new(Vec::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn thread<F: Fn(Arc<Context>) + Send + Sync + 'static>(&self, run: F) {
|
||||||
|
self.threads.lock().unwrap().push(Arc::new(run));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) {
|
||||||
|
let cx = Context::new(component);
|
||||||
|
cx.refresh();
|
||||||
|
|
||||||
|
for thread in self.threads.lock().unwrap().iter() {
|
||||||
|
let thread = thread.clone();
|
||||||
|
|
||||||
|
std::thread::spawn({
|
||||||
|
let cx = cx.clone();
|
||||||
|
move || thread(cx)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let (width, height) = crossterm::terminal::size().unwrap();
|
||||||
|
|
||||||
|
execute!(stdout(), Clear(crossterm::terminal::ClearType::Purge)).unwrap();
|
||||||
|
execute!(stdout(), Clear(crossterm::terminal::ClearType::All)).unwrap();
|
||||||
|
|
||||||
|
self.draw_context(&self.render_view(
|
||||||
|
&Area {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
},
|
||||||
|
&cx.get_view(),
|
||||||
|
));
|
||||||
|
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(16));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Engine for Console {
|
||||||
|
fn render_view(&self, area: &Area, view: &View) -> DrawContext {
|
||||||
|
let mut context = DrawContext::new(area.clone());
|
||||||
|
view(&mut context);
|
||||||
|
context
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_context(&self, ctx: &DrawContext) {
|
||||||
|
for inst in &ctx.drawing {
|
||||||
|
match inst {
|
||||||
|
crate::render::DrawInstruction::Text(point, text) => {
|
||||||
|
let (x, y) = (ctx.area.x + point.x, ctx.area.y + point.y);
|
||||||
|
execute!(stdout(), MoveTo(x, y),).unwrap();
|
||||||
|
print!("{text}");
|
||||||
|
stdout().flush().unwrap();
|
||||||
|
}
|
||||||
|
crate::render::DrawInstruction::Child(_point, child) => self.draw_context(child),
|
||||||
|
crate::render::DrawInstruction::View(area, view) => {
|
||||||
|
self.draw_context(&self.render_view(area, view))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::{component, extensions::Extension, widget::Widget, Screen};
|
|
||||||
|
|
||||||
pub struct IdExtension(pub Arc<Screen>);
|
|
||||||
|
|
||||||
impl Extension for Arc<IdExtension> {}
|
|
||||||
|
|
||||||
impl IdExtension {
|
|
||||||
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.widgets.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));
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
pub mod id;
|
|
||||||
pub mod resources;
|
|
||||||
pub mod tick;
|
|
||||||
pub mod velocity;
|
|
||||||
|
|
||||||
pub use id::*;
|
|
||||||
pub use resources::*;
|
|
||||||
pub use tick::*;
|
|
||||||
pub use velocity::*;
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
any::{type_name, Any},
|
|
||||||
fmt::Debug,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
render_scope::RenderScope,
|
|
||||||
widget::{Component, Widget},
|
|
||||||
Screen,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub trait Extension {
|
|
||||||
fn init(&mut self, _screen: Arc<Screen>) {}
|
|
||||||
fn render_widget(&mut self, _scope: &mut RenderScope, _widget: &Arc<Widget>) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Event: Send + Sync {
|
|
||||||
fn as_any(&self) -> &dyn Any;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Handler<E: Event>(Arc<Mutex<dyn FnMut(&Arc<Widget>, &E) + Send + Sync>>);
|
|
||||||
|
|
||||||
impl<E: Event + 'static> Component for Handler<E> {
|
|
||||||
fn as_any(&self) -> &dyn Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: Event + 'static> Handler<E> {
|
|
||||||
pub fn new<F: FnMut(&Arc<Widget>, &E) + Send + Sync + 'static>(f: F) -> Handler<E> {
|
|
||||||
Handler(Arc::new(Mutex::new(f)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn call(&self, w: &Arc<Widget>, e: &E) {
|
|
||||||
(self.0.lock().unwrap())(w, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: Event> Debug for Handler<E> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "Handler({})", type_name::<E>())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct VecResource<T>(Arc<Mutex<Box<dyn FnMut(&T) + Send + Sync>>>);
|
|
||||||
|
|
||||||
impl<T: Send + Sync + 'static> VecResource<T> {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self(Arc::new(Mutex::new(Box::new(|_| {}))))
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
use std::{fmt::Debug, sync::Arc};
|
|
||||||
|
|
||||||
use crate::extensions::Extension;
|
|
||||||
use crate::extensions::Handler;
|
|
||||||
use crate::{event, Screen};
|
|
||||||
|
|
||||||
pub struct TickExtension(pub u16);
|
|
||||||
|
|
||||||
impl Extension for TickExtension {
|
|
||||||
fn init(&mut self, screen: Arc<Screen>) {
|
|
||||||
let rate_dur = 1000 / self.0 as u64;
|
|
||||||
std::thread::spawn({
|
|
||||||
move || {
|
|
||||||
let mut tick = 0;
|
|
||||||
loop {
|
|
||||||
for w in screen.widgets.lock().unwrap().iter() {
|
|
||||||
if let Some(on_tick) = w.get::<Handler<TickEvent>>() {
|
|
||||||
on_tick.call(&w, &TickEvent(tick))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tick += 1;
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
event!(TickEvent(pub u32));
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
component,
|
|
||||||
extensions::Extension,
|
|
||||||
style::{Position, Transform},
|
|
||||||
widget::Widget,
|
|
||||||
Screen,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct VelocityExtension;
|
|
||||||
|
|
||||||
impl VelocityExtension {
|
|
||||||
fn apply_velocity(ticks: u16, velocity: i32, x: &mut u16) {
|
|
||||||
if velocity.abs() != 0 && ticks as i32 % (1000 / velocity.abs()) == 0 {
|
|
||||||
if velocity > 0 {
|
|
||||||
*x += 1;
|
|
||||||
} else if velocity < 0 {
|
|
||||||
*x -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VelocityExtension {
|
|
||||||
fn apply_velocity_xy(ticks: u16, widget: &Arc<Widget>) {
|
|
||||||
if let Some(velocity) = widget.get::<Velocity>() {
|
|
||||||
if let Some(mut t) = widget.get::<Transform>() {
|
|
||||||
match &mut t.x {
|
|
||||||
Position::Const(x) => Self::apply_velocity(ticks, velocity.0, x),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
match &mut t.y {
|
|
||||||
Position::Const(y) => Self::apply_velocity(ticks, velocity.1, y),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
widget.set_component(t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Extension for VelocityExtension {
|
|
||||||
fn init(&mut self, screen: Arc<Screen>) {
|
|
||||||
std::thread::spawn({
|
|
||||||
move || {
|
|
||||||
let mut tick = 0;
|
|
||||||
loop {
|
|
||||||
for widget in screen.widgets.lock().unwrap().iter() {
|
|
||||||
Self::apply_velocity_xy(tick, widget);
|
|
||||||
}
|
|
||||||
if tick > 1000 {
|
|
||||||
tick = 0;
|
|
||||||
} else {
|
|
||||||
tick += 1;
|
|
||||||
}
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
component!(Velocity(pub i32, pub i32));
|
|
||||||
+39
-39
@@ -1,58 +1,58 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
state::DependencyHandler,
|
component::{Context, Scope},
|
||||||
widget::{Widget, WidgetLoad},
|
state::HookDependency,
|
||||||
Screen,
|
View,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub enum RsxElement {
|
pub enum RsxScope {
|
||||||
Element(
|
Static(Arc<Scope>),
|
||||||
Box<dyn FnMut() -> WidgetLoad + Send + Sync>,
|
Dynamic(
|
||||||
Vec<Box<dyn DependencyHandler>>,
|
Arc<dyn Fn(&Arc<Scope>) + Send + Sync>,
|
||||||
Rsx,
|
Vec<Box<dyn HookDependency>>,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Rsx(pub Vec<RsxElement>);
|
pub struct Rsx(Vec<RsxScope>);
|
||||||
|
|
||||||
impl Rsx {
|
impl Rsx {
|
||||||
pub fn draw(self, screen: &Arc<Screen>) {
|
pub fn new() -> Self {
|
||||||
self.draw_parent(screen, None);
|
Self(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_parent(self, screen: &Arc<Screen>, parent: Option<Arc<Widget>>) {
|
pub fn static_scope(&mut self, scope: Arc<Scope>) {
|
||||||
for rsx_elem in self.0 {
|
self.0.push(RsxScope::Static(scope));
|
||||||
match rsx_elem {
|
}
|
||||||
RsxElement::Element(f, dep, child) => {
|
|
||||||
let w = if let Some(parent) = &parent {
|
|
||||||
let w = screen.draw_box(f);
|
|
||||||
parent.0.lock().unwrap().draw_child(&w);
|
|
||||||
w
|
|
||||||
} else {
|
|
||||||
screen.draw_box(f)
|
|
||||||
};
|
|
||||||
|
|
||||||
for d in dep {
|
pub fn dynamic_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(
|
||||||
w.dependency_box(d);
|
&mut self,
|
||||||
}
|
drawer: F,
|
||||||
child.draw_parent(screen, Some(w.clone()));
|
dependencies: Vec<Box<dyn HookDependency>>,
|
||||||
|
) {
|
||||||
|
self.0
|
||||||
|
.push(RsxScope::Dynamic(Arc::new(drawer), dependencies));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn view(&self, context: Arc<Context>) -> View {
|
||||||
|
for scope in &self.0 {
|
||||||
|
match scope {
|
||||||
|
RsxScope::Static(scope) => context.scopes.lock().unwrap().push(scope.clone()),
|
||||||
|
RsxScope::Dynamic(drawer, dependencies) => {
|
||||||
|
let drawer = drawer.clone();
|
||||||
|
context.dyn_scope(
|
||||||
|
move |c| drawer(c),
|
||||||
|
&dependencies.iter().map(|d| d.as_ref()).collect::<Vec<_>>(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_element<F: FnMut() -> WidgetLoad + Send + Sync + 'static>(
|
let context = context.clone();
|
||||||
&mut self,
|
Arc::new({
|
||||||
load: F,
|
move |ctx| {
|
||||||
dependencies: Vec<Box<dyn DependencyHandler>>,
|
context.draw_children(ctx);
|
||||||
children: Rsx,
|
}
|
||||||
) {
|
})
|
||||||
self.0
|
|
||||||
.push(RsxElement::Element(Box::new(load), dependencies, children));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expand(&mut self, other: &mut Rsx) {
|
|
||||||
self.0.append(&mut other.0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-123
@@ -1,132 +1,21 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::render::DrawContext;
|
||||||
extensions::{Extension, Handler},
|
|
||||||
render_scope::RenderScope,
|
|
||||||
widget::{Element, Widget, WidgetLoad},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod elements;
|
pub mod component;
|
||||||
pub mod extensions;
|
pub mod engine;
|
||||||
pub mod frontend;
|
pub mod frontend;
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
pub mod render_scope;
|
pub mod render;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod style;
|
|
||||||
pub mod utils;
|
|
||||||
pub mod widget;
|
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::{
|
pub use crate::component::*;
|
||||||
elements::*, extensions::*, frontend::*, render_scope::*, state::*, style::*, utils::*,
|
pub use crate::engine::*;
|
||||||
widget::*, *,
|
pub use crate::render::*;
|
||||||
};
|
pub use crate::state::*;
|
||||||
|
pub use crate::View;
|
||||||
pub use crate::style::{Dimension::{Full, Content}, Position::{Center, End}};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Screen {
|
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
|
||||||
pub widgets: Mutex<Vec<Arc<Widget>>>,
|
pub type ViewWrapper = Arc<dyn Fn(&mut DrawContext, View) + Send + Sync>;
|
||||||
extensions: Mutex<Vec<Arc<Mutex<Box<dyn Extension + Send + Sync>>>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
event!(RenderWrapperEvent(*mut RenderScope));
|
|
||||||
component!(NoRender);
|
|
||||||
|
|
||||||
impl RenderWrapperEvent {
|
|
||||||
pub fn get_scope(&self) -> &mut RenderScope {
|
|
||||||
unsafe { &mut *self.0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl Send for RenderWrapperEvent {}
|
|
||||||
unsafe impl Sync for RenderWrapperEvent {}
|
|
||||||
|
|
||||||
impl Screen {
|
|
||||||
pub fn new() -> Arc<Self> {
|
|
||||||
Arc::new(Self {
|
|
||||||
widgets: Mutex::new(Vec::new()),
|
|
||||||
extensions: Mutex::new(Vec::new()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn draw<F: FnMut() -> WidgetLoad + 'static + Send + Sync>(
|
|
||||||
self: &Arc<Self>,
|
|
||||||
element: F,
|
|
||||||
) -> Arc<Widget> {
|
|
||||||
let w = Arc::new(Widget::new(Box::new(element)));
|
|
||||||
self.widgets.lock().unwrap().push(w.clone());
|
|
||||||
w
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn draw_box(
|
|
||||||
self: &Arc<Self>,
|
|
||||||
element: Box<dyn FnMut() -> WidgetLoad + Send + Sync>,
|
|
||||||
) -> Arc<Widget> {
|
|
||||||
let w = Arc::new(Widget::new(Box::new(element)));
|
|
||||||
self.widgets.lock().unwrap().push(w.clone());
|
|
||||||
w
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extension<E: Extension + Send + Sync + 'static>(self: &Arc<Self>, ext: E) {
|
|
||||||
self.extensions
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.push(Arc::new(Mutex::new(Box::new(ext))));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(self: &Arc<Self>) -> std::io::Result<()> {
|
|
||||||
for ext in self.extensions.lock().unwrap().iter() {
|
|
||||||
ext.lock().unwrap().init(self.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::hide_cursor()?;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
self.render()?;
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(28));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render(self: &Arc<Self>) -> std::io::Result<()> {
|
|
||||||
let mut scope = RenderScope::new();
|
|
||||||
let (w, h) = crossterm::terminal::size().unwrap();
|
|
||||||
scope.set_parent_size(w, h);
|
|
||||||
|
|
||||||
utils::clear()?;
|
|
||||||
for elem in self.widgets.lock().unwrap().iter() {
|
|
||||||
if let Some(NoRender) = elem.get() {
|
|
||||||
for ext in self.extensions.lock().unwrap().iter() {
|
|
||||||
ext.lock().unwrap().render_widget(&mut scope, elem);
|
|
||||||
}
|
|
||||||
elem.auto_refresh();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(wrapper) = elem.get::<Handler<RenderWrapperEvent>>() {
|
|
||||||
wrapper.call(elem, &RenderWrapperEvent(&mut scope));
|
|
||||||
} else {
|
|
||||||
scope.clear();
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
|
|
||||||
for ext in self.extensions.lock().unwrap().iter() {
|
|
||||||
ext.lock().unwrap().render_widget(&mut scope, elem);
|
|
||||||
}
|
|
||||||
|
|
||||||
elem.0.lock().unwrap().render(&mut scope);
|
|
||||||
|
|
||||||
if let Some(t) = elem.get() {
|
|
||||||
scope.set_transform(&t);
|
|
||||||
}
|
|
||||||
scope.draw();
|
|
||||||
|
|
||||||
elem.0.lock().unwrap().after_render(&mut scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
elem.auto_refresh();
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+148
-158
@@ -1,175 +1,165 @@
|
|||||||
#[macro_export]
|
|
||||||
macro_rules! event {
|
|
||||||
($name:ident) => {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct $name;
|
|
||||||
|
|
||||||
impl $crate::extensions::Event for $name {
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
($name:ident {$($inner:tt)*}) => {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct $name {
|
|
||||||
$($inner)*
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $crate::extensions::Event for $name {
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
($name:ident ($($inner:tt)*)) => {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct $name ($($inner)*);
|
|
||||||
|
|
||||||
impl $crate::extensions::Event for $name {
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! component {
|
|
||||||
($name:ident) => {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct $name;
|
|
||||||
|
|
||||||
impl $crate::widget::Component for $name {
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
($name:ident {$($inner:tt)*}) => {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct $name {
|
|
||||||
$($inner)*
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $crate::widget::Component for $name {
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
($name:ident ($($inner:tt)*)) => {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct $name ($($inner)*);
|
|
||||||
|
|
||||||
impl $crate::widget::Component for $name {
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Macro to create an event handler closure that calls a method on `self`
|
|
||||||
///
|
|
||||||
/// This macro automates the pattern of capturing `self` as a raw pointer,
|
|
||||||
/// then passing an event to a method on `self` inside an unsafe closure.
|
|
||||||
///
|
|
||||||
/// # Parameters
|
|
||||||
/// - `$self_ty`: The self type.
|
|
||||||
/// - `$self`: The instance variable (usually `self`) to call the method on.
|
|
||||||
/// - `$events`: The event source which has an `.on` method to register the handler.
|
|
||||||
/// - `$method`: The method name on `self` to call when an event occurs.
|
|
||||||
///
|
|
||||||
/// # Usage
|
|
||||||
/// ```rust
|
|
||||||
/// event_handler!(self, events, on_keypress);
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// This expands roughly to:
|
|
||||||
/// ```rust
|
|
||||||
/// let self_ref = self as *mut Self;
|
|
||||||
/// events.on(move |event| unsafe { (*self_ref).on_keypress(event) });
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
/// This macro uses `unsafe` code because it dereferences a raw pointer inside the closure.
|
|
||||||
/// Ensure that the `self` reference lives at least as long as the closure to avoid undefined behavior.
|
|
||||||
///
|
|
||||||
/// # Why use raw pointers here?
|
|
||||||
/// Often, event handlers require `'static` closures, but `self` is a stack reference.
|
|
||||||
/// Capturing `self` directly is not possible, so this workaround uses a raw pointer.
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! event_handler {
|
|
||||||
($self_ty:ty, $self:ident, $events:ident, $method:ident) => {{
|
|
||||||
let self_ref = $self as *mut $self_ty;
|
|
||||||
$events.on(move |es, e| unsafe { (*self_ref).$method(es, e) });
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! transform {
|
|
||||||
($($f:ident: $v:expr),* $(,)?) => {{
|
|
||||||
let mut t = Transform::new();
|
|
||||||
$(t.$f = $v.into();)*
|
|
||||||
t
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! rsx {
|
macro_rules! rsx {
|
||||||
($($inner:tt)*) => {{
|
() => {
|
||||||
let mut r = $crate::frontend::Rsx(Vec::new());
|
$crate::frontend::Rsx::new()
|
||||||
|
};
|
||||||
$crate::rsx_inner! { r, $($inner)* };
|
|
||||||
|
|
||||||
|
($($rsx:tt)+) => {{
|
||||||
|
let mut r = $crate::frontend::Rsx::new();
|
||||||
|
{
|
||||||
|
$crate::rsx_scope!(r, $($rsx)+);
|
||||||
|
}
|
||||||
r
|
r
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! rsx_inner {
|
macro_rules! rsx_scope {
|
||||||
($r:expr, $(%$dep:ident)* $(@$comp:expr;)* $s:literal $($rest:tt)*) => {
|
($rsx:expr, for $(%$($dep:ident $(as $dp:pat,)?),+)? ($p:pat in $v:expr) {$($inner:tt)*} $($rest:tt)*) => {
|
||||||
$r.create_element({ $(let $dep = $dep.clone();)* move || {
|
{
|
||||||
$crate::widget::WidgetLoad::new(format!($s)) $(.component($comp))*
|
$($($crate::rsx_dep!($dep);)+)?
|
||||||
} }, vec![$(Box::new($dep.clone())),*], $crate::frontend::Rsx(Vec::new()));
|
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
$rsx.dynamic_scope({
|
||||||
|
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
|
||||||
|
move |scope| {
|
||||||
|
scope.children.lock().unwrap().clear();
|
||||||
|
for $p in $v {
|
||||||
|
$crate::rsx_child!(scope, $($inner)*);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, vec![$($(Box::new($dep)),+)?]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
};
|
};
|
||||||
|
|
||||||
($r:expr, $(%$dep:ident)* $(@$comp:expr;)* $name:path { $($inner:tt)* } ($($e:expr),*) $($rest:tt)*) => {
|
($rsx:expr, if $(%$($dep:ident $(as $dp:pat,)?),+)? ($st:expr) {$($inner:tt)*} $($rest:tt)*) => {
|
||||||
$r.create_element({ $(let $dep = $dep.clone();)* move || {
|
{
|
||||||
$crate::widget::WidgetLoad::new(<$name>::new($($e),*)) $(.component($comp))*
|
$($($crate::rsx_dep!($dep);)+)?
|
||||||
} }, vec![$(Box::new($dep.clone())),*], $crate::rsx!{ $($inner)* });
|
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
$rsx.dynamic_scope({
|
||||||
|
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
|
||||||
|
move |scope| {
|
||||||
|
if $st {
|
||||||
|
if scope.children.lock().unwrap().len() == 0 {
|
||||||
|
$crate::rsx_child!(scope, $($inner)*);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
scope.children.lock().unwrap().clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, vec![$($(Box::new($dep)),+)?]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
};
|
};
|
||||||
|
|
||||||
($r:expr, $(%$dep:ident)* $(@$comp:expr;)* $name:path { $($inner:tt)* } $($rest:tt)*) => {
|
($rsx:expr, $(%$($dep:ident $(as $dp:pat,)?),+)? $text:literal @$d:expr; $($rest:tt)*) => {
|
||||||
$r.create_element({ $(let $dep = $dep.clone();)* move || {
|
{
|
||||||
$crate::widget::WidgetLoad::new(<$name>::new()) $(.component($comp))*
|
let scope = $crate::component::Scope::new();
|
||||||
} }, vec![$(Box::new($dep.clone())),*], $crate::rsx!{ $($inner)* });
|
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
|
||||||
|
|
||||||
|
$crate::rsx_child!(scope, $text @$d;);
|
||||||
|
|
||||||
|
$rsx.static_scope(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
};
|
};
|
||||||
|
|
||||||
($r:expr, $expand:ident => ($($inner:tt)*) $($rest:tt)*) => {
|
($rsx:expr, $(%$($dep:ident $(as $dp:pat,)?),+)? $text:literal $($rest:tt)*) => {
|
||||||
$r.expand(&mut $expand($($inner)*));
|
{
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
let scope = $crate::component::Scope::new();
|
||||||
|
|
||||||
|
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
|
||||||
|
|
||||||
|
$crate::rsx_child!(scope, $text);
|
||||||
|
|
||||||
|
$rsx.static_scope(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
};
|
};
|
||||||
|
|
||||||
($r:expr,) => {};
|
($rsx:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
let scope = $crate::component::Scope::new();
|
||||||
|
|
||||||
|
$crate::rsx_child!(scope, $component ($ctx, $view) $body);
|
||||||
|
|
||||||
|
$rsx.static_scope(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($rsx:expr, $component:ident $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
let scope = $crate::component::Scope::new();
|
||||||
|
|
||||||
|
$crate::rsx_child!(scope, $component);
|
||||||
|
|
||||||
|
$rsx.static_scope(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($rsx:expr, !mount $mount:ident $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
$mount.mount();
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_scope!($rsx, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($rsx:expr,) => {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! rsx_child {
|
||||||
|
($scope:expr, $text:literal @$d:expr; $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
$scope.view(Arc::new(move |ctx| ctx.draw_text($d, &format!($text))));
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_child!($scope, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($scope:expr, $text:literal $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
$scope.view(Arc::new(move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!($text))));
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_child!($scope, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($scope:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
$scope.child($component, Some(Arc::new(|$ctx, $view| $body)));
|
||||||
|
}
|
||||||
|
|
||||||
|
$crate::rsx_child!($scope, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($scope:expr, $component:ident $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
$scope.child($component, None);
|
||||||
|
}
|
||||||
|
$crate::rsx_child!($scope, $($rest)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
($rsx:expr,) => {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! rsx_dep {
|
||||||
|
($dep:ident as $dp:pat) => {
|
||||||
|
let $dp = $dep.clone();
|
||||||
|
};
|
||||||
|
|
||||||
|
($dep:ident) => {
|
||||||
|
let $dep = $dep.clone();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
-28
@@ -1,28 +0,0 @@
|
|||||||
use osui::prelude::*;
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
|
||||||
let screen = Screen::new();
|
|
||||||
|
|
||||||
app().draw(&screen);
|
|
||||||
|
|
||||||
screen.run()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn app() -> Rsx {
|
|
||||||
rsx! {
|
|
||||||
@transform! {
|
|
||||||
width: Full,
|
|
||||||
height: Full,
|
|
||||||
};
|
|
||||||
Div {
|
|
||||||
@transform! {
|
|
||||||
width: Full,
|
|
||||||
height: Full,
|
|
||||||
};
|
|
||||||
RoundedOutline {}
|
|
||||||
|
|
||||||
@Transform::center();
|
|
||||||
"Welcome To OSUI!"
|
|
||||||
} (0x000000)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
use crate::View;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum DrawInstruction {
|
||||||
|
Text(Point, String),
|
||||||
|
View(Area, View),
|
||||||
|
Child(Point, DrawContext),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Size {
|
||||||
|
pub width: u16,
|
||||||
|
pub height: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Point {
|
||||||
|
pub x: u16,
|
||||||
|
pub y: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Area {
|
||||||
|
pub x: u16,
|
||||||
|
pub y: u16,
|
||||||
|
pub width: u16,
|
||||||
|
pub height: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct DrawContext {
|
||||||
|
pub area: Area,
|
||||||
|
pub allocated: Area,
|
||||||
|
pub drawing: Vec<DrawInstruction>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DrawContext {
|
||||||
|
pub fn new(area: Area) -> Self {
|
||||||
|
Self {
|
||||||
|
area,
|
||||||
|
allocated: Area {
|
||||||
|
x: u16::MAX,
|
||||||
|
y: u16::MAX,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
},
|
||||||
|
drawing: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn allocate(&mut self, x: u16, y: u16, width: u16, height: u16) -> Area {
|
||||||
|
self.allocated.x = self.allocated.x.min(x);
|
||||||
|
self.allocated.y = self.allocated.y.min(y);
|
||||||
|
self.allocated.width = self.allocated.width.max(width);
|
||||||
|
self.allocated.height = self.allocated.height.max(height);
|
||||||
|
|
||||||
|
Area {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&mut self, inst: DrawInstruction) {
|
||||||
|
self.drawing.push(inst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_text(&mut self, point: Point, text: &str) {
|
||||||
|
self.drawing
|
||||||
|
.push(DrawInstruction::Text(point, text.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_view(&mut self, area: Area, view: View) {
|
||||||
|
self.drawing.push(DrawInstruction::View(area, view));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
use std::fmt::Debug;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
style::{RawTransform, Transform},
|
|
||||||
utils::{self, hex_ansi_bg},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
enum RenderMethod {
|
|
||||||
Text(String),
|
|
||||||
TextColored(String, u32),
|
|
||||||
Rectangle(u16, u16, u32),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct RenderScope {
|
|
||||||
transform: RawTransform,
|
|
||||||
render_stack: Vec<RenderMethod>,
|
|
||||||
parent_width: u16,
|
|
||||||
parent_height: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RenderScope {
|
|
||||||
pub fn new() -> RenderScope {
|
|
||||||
RenderScope {
|
|
||||||
transform: RawTransform::new(),
|
|
||||||
render_stack: Vec::new(),
|
|
||||||
parent_width: 0,
|
|
||||||
parent_height: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_transform_raw(&mut self, transform: RawTransform) {
|
|
||||||
self.transform = transform;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_transform(&mut self, transform: &Transform) {
|
|
||||||
transform.use_dimensions(self.parent_width, self.parent_height, &mut self.transform);
|
|
||||||
transform.use_position(self.parent_width, self.parent_height, &mut self.transform);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn draw_text(&mut self, text: &str) {
|
|
||||||
self.render_stack.push(RenderMethod::Text(text.to_string()));
|
|
||||||
let (w, h) = utils::str_size(text);
|
|
||||||
self.transform.width = self.transform.width.max(w);
|
|
||||||
self.transform.height = self.transform.height.max(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn draw_text_colored(&mut self, text: &str, color: u32) {
|
|
||||||
self.render_stack
|
|
||||||
.push(RenderMethod::TextColored(text.to_string(), color));
|
|
||||||
let (w, h) = utils::str_size(text);
|
|
||||||
self.transform.width = self.transform.width.max(w);
|
|
||||||
self.transform.height = self.transform.height.max(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn draw_rect(&mut self, width: u16, height: u16, color: u32) {
|
|
||||||
self.render_stack
|
|
||||||
.push(RenderMethod::Rectangle(width, height, color));
|
|
||||||
self.transform.width = self.transform.width.max(width);
|
|
||||||
self.transform.height = self.transform.height.max(height);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn draw(&self) {
|
|
||||||
for m in &self.render_stack {
|
|
||||||
match m {
|
|
||||||
RenderMethod::Text(t) => {
|
|
||||||
utils::print(self.transform.x, self.transform.y, t);
|
|
||||||
}
|
|
||||||
RenderMethod::TextColored(t, c) => utils::print(
|
|
||||||
self.transform.x,
|
|
||||||
self.transform.y,
|
|
||||||
&(utils::hex_ansi(*c) + t),
|
|
||||||
),
|
|
||||||
RenderMethod::Rectangle(width, height, color) => {
|
|
||||||
utils::print_liner(
|
|
||||||
self.transform.x,
|
|
||||||
self.transform.y,
|
|
||||||
&hex_ansi_bg(*color),
|
|
||||||
&std::iter::repeat(" ".repeat(*width as usize))
|
|
||||||
.take(*height as usize)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
|
||||||
self.render_stack.clear();
|
|
||||||
self.transform.width = 0;
|
|
||||||
self.transform.height = 0;
|
|
||||||
self.transform.x = 0;
|
|
||||||
self.transform.y = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_size(&self) -> (u16, u16) {
|
|
||||||
(self.transform.width, self.transform.height)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_size_or(&self, width: u16, height: u16) -> (u16, u16) {
|
|
||||||
(
|
|
||||||
if self.transform.width == 0 {
|
|
||||||
width
|
|
||||||
} else {
|
|
||||||
self.transform.width
|
|
||||||
},
|
|
||||||
if self.transform.height == 0 {
|
|
||||||
height
|
|
||||||
} else {
|
|
||||||
self.transform.height
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_size_or_parent(&self) -> (u16, u16) {
|
|
||||||
(
|
|
||||||
if self.transform.width == 0 {
|
|
||||||
self.parent_width
|
|
||||||
} else {
|
|
||||||
self.transform.width
|
|
||||||
},
|
|
||||||
if self.transform.height == 0 {
|
|
||||||
self.parent_height
|
|
||||||
} else {
|
|
||||||
self.transform.height
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_parent_size(&self) -> (u16, u16) {
|
|
||||||
(self.parent_width, self.parent_height)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_parent_size(&mut self, width: u16, height: u16) {
|
|
||||||
self.parent_width = width;
|
|
||||||
self.parent_height = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_transform_mut(&mut self) -> &mut RawTransform {
|
|
||||||
&mut self.transform
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_transform(&self) -> &RawTransform {
|
|
||||||
&self.transform
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for RenderScope {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "RenderScope")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+160
-44
@@ -1,97 +1,213 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
any::Any,
|
||||||
fmt::{Debug, Display, Formatter, Result as FmtResult},
|
fmt::{Debug, Display, Formatter, Result as FmtResult},
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
use crate::prelude::Context;
|
||||||
pub struct State<T> {
|
|
||||||
inner: Arc<Mutex<Inner<T>>>,
|
#[derive(Clone)]
|
||||||
}
|
pub struct HookEffect(Arc<Mutex<dyn FnMut() + Send + Sync>>);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Inner<T> {
|
pub struct State<T> {
|
||||||
value: T,
|
value: Arc<Mutex<T>>,
|
||||||
dependencies: usize,
|
dependents: Arc<Mutex<Vec<HookEffect>>>,
|
||||||
changed: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Inner<'a, T> {
|
||||||
|
value: MutexGuard<'a, T>,
|
||||||
|
dependents: Arc<Mutex<Vec<HookEffect>>>,
|
||||||
|
updated: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Mount(Arc<Mutex<bool>>, Arc<Mutex<Vec<HookEffect>>>);
|
||||||
|
|
||||||
pub fn use_state<T>(v: T) -> State<T> {
|
pub fn use_state<T>(v: T) -> State<T> {
|
||||||
State {
|
State {
|
||||||
inner: Arc::new(Mutex::new(Inner {
|
value: Arc::new(Mutex::new(v)),
|
||||||
value: v,
|
dependents: Arc::new(Mutex::new(Vec::new())),
|
||||||
dependencies: 0,
|
|
||||||
changed: 0,
|
|
||||||
})),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> State<T> {
|
impl<T: Clone> State<T> {
|
||||||
/// Gets the cloned value, recommended for preventing deadlocks
|
/// Gets the cloned value, recommended for preventing deadlocks
|
||||||
pub fn get_dl(&self) -> T {
|
pub fn get_dl(&self) -> T {
|
||||||
self.inner.lock().unwrap().value.clone()
|
self.value.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> State<T> {
|
impl<T> State<T> {
|
||||||
/// Gets a lock on the state for read/write access.
|
/// Gets a lock on the state for read/write access.
|
||||||
pub fn get(&self) -> MutexGuard<'_, Inner<T>> {
|
pub fn get(&self) -> Inner<'_, T> {
|
||||||
self.inner.lock().unwrap()
|
Inner {
|
||||||
|
value: self.value.lock().unwrap(),
|
||||||
|
dependents: self.dependents.clone(),
|
||||||
|
updated: false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the value and marks it as changed.
|
/// Sets the value and marks it as changed.
|
||||||
pub fn set(&self, v: T) {
|
pub fn set(&self, v: T) {
|
||||||
let mut inner = self.inner.lock().unwrap();
|
*self.value.lock().unwrap() = v;
|
||||||
inner.value = v;
|
self.update();
|
||||||
inner.changed = inner.dependencies;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Marks the state as updated.
|
|
||||||
pub fn update(&self) {
|
pub fn update(&self) {
|
||||||
let mut inner = self.inner.lock().unwrap();
|
for d in self.dependents.lock().unwrap().iter() {
|
||||||
inner.changed = inner.dependencies;
|
d.call();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Debug + Send + Sync> DependencyHandler for State<T> {
|
|
||||||
fn check(&self) -> bool {
|
|
||||||
let mut inner = self.inner.lock().unwrap();
|
|
||||||
let i = inner.changed > 0;
|
|
||||||
if i {
|
|
||||||
inner.changed -= 1;
|
|
||||||
}
|
}
|
||||||
i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add(&self) {
|
pub fn clone(&self) -> Self {
|
||||||
let mut inner = self.inner.lock().unwrap();
|
Self {
|
||||||
inner.dependencies += 1;
|
dependents: self.dependents.clone(),
|
||||||
|
value: self.value.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Display> Display for State<T> {
|
impl<T: Display> Display for State<T> {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||||
let inner = self.inner.lock().unwrap();
|
write!(f, "{}", self.value.lock().unwrap())
|
||||||
write!(f, "{}", inner.value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Deref for Inner<T> {
|
impl Debug for HookEffect {
|
||||||
type Target = T;
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||||
|
write!(f, "HookEffect")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HookEffect {
|
||||||
|
pub fn new<F: Fn() + Send + Sync + 'static>(f: F) -> Self {
|
||||||
|
Self(Arc::new(Mutex::new(f)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call(&self) {
|
||||||
|
(self.0.lock().unwrap())()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for Inner<'_, T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if self.updated {
|
||||||
|
for d in self.dependents.lock().unwrap().iter() {
|
||||||
|
d.call();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for Inner<'_, T> {
|
||||||
|
type Target = T;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.value
|
&self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DerefMut for Inner<T> {
|
impl<T> DerefMut for Inner<'_, T> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
self.changed = self.dependencies;
|
self.updated = true;
|
||||||
&mut self.value
|
&mut self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait DependencyHandler: std::fmt::Debug + Send + Sync {
|
pub trait HookDependency {
|
||||||
fn add(&self);
|
fn on_update(&self, hook: HookEffect);
|
||||||
fn check(&self) -> bool;
|
}
|
||||||
|
|
||||||
|
impl<T> HookDependency for State<T> {
|
||||||
|
fn on_update(&self, hook: HookEffect) {
|
||||||
|
self.dependents.lock().unwrap().push(hook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HookDependency for Mount {
|
||||||
|
fn on_update(&self, hook: HookEffect) {
|
||||||
|
if *self.0.lock().unwrap() {
|
||||||
|
hook.call();
|
||||||
|
} else {
|
||||||
|
self.1.lock().unwrap().push(hook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mount {
|
||||||
|
pub fn mount(&self) {
|
||||||
|
*self.0.lock().unwrap() = true;
|
||||||
|
for hook_effect in self.1.lock().unwrap().iter() {
|
||||||
|
hook_effect.call();
|
||||||
|
}
|
||||||
|
self.1.lock().unwrap().clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_effect<F: FnMut() + Send + Sync + 'static>(f: F, dependencies: &[&dyn HookDependency]) {
|
||||||
|
let f = Arc::new(Mutex::new(f));
|
||||||
|
let hook = HookEffect(Arc::new(Mutex::new({
|
||||||
|
let f = f.clone();
|
||||||
|
move || {
|
||||||
|
let f = f.clone();
|
||||||
|
std::thread::spawn(move || (f.lock().unwrap())());
|
||||||
|
}
|
||||||
|
})));
|
||||||
|
|
||||||
|
for d in dependencies {
|
||||||
|
d.on_update(hook.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_mount() -> Mount {
|
||||||
|
Mount(Arc::new(Mutex::new(true)), Arc::new(Mutex::new(Vec::new())))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_mount_manual() -> Mount {
|
||||||
|
Mount(
|
||||||
|
Arc::new(Mutex::new(false)),
|
||||||
|
Arc::new(Mutex::new(Vec::new())),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_sync_state<
|
||||||
|
T: Send + Sync + 'static,
|
||||||
|
E: Any + 'static,
|
||||||
|
D: Fn(&E) -> T + Send + Sync + 'static,
|
||||||
|
>(
|
||||||
|
cx: &Arc<Context>,
|
||||||
|
v: T,
|
||||||
|
decoder: D,
|
||||||
|
) -> State<T> {
|
||||||
|
let state = use_state(v);
|
||||||
|
|
||||||
|
cx.on_event({
|
||||||
|
let state = state.clone();
|
||||||
|
move |_, v: &E| state.set(decoder(v))
|
||||||
|
});
|
||||||
|
|
||||||
|
state
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_sync_effect<
|
||||||
|
T: Send + Sync + 'static,
|
||||||
|
Ev: 'static,
|
||||||
|
E: Fn(&State<T>) -> Ev + Send + Sync + 'static,
|
||||||
|
>(
|
||||||
|
cx: &Arc<Context>,
|
||||||
|
state: &State<T>,
|
||||||
|
encoder: E,
|
||||||
|
deps: &[&dyn HookDependency],
|
||||||
|
) {
|
||||||
|
use_effect(
|
||||||
|
{
|
||||||
|
let state = state.clone();
|
||||||
|
let cx = cx.clone();
|
||||||
|
move || {
|
||||||
|
cx.emit_event(&encoder(&state));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
deps,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
-153
@@ -1,153 +0,0 @@
|
|||||||
use crate::component;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct RawTransform {
|
|
||||||
pub x: u16,
|
|
||||||
pub y: u16,
|
|
||||||
pub width: u16,
|
|
||||||
pub height: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Position {
|
|
||||||
Const(u16),
|
|
||||||
Center,
|
|
||||||
End,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Dimension {
|
|
||||||
Full,
|
|
||||||
Content,
|
|
||||||
Const(u16),
|
|
||||||
}
|
|
||||||
|
|
||||||
component!(Transform {
|
|
||||||
pub x: Position,
|
|
||||||
pub y: Position,
|
|
||||||
pub mx: i32,
|
|
||||||
pub my: i32,
|
|
||||||
pub width: Dimension,
|
|
||||||
pub height: Dimension,
|
|
||||||
});
|
|
||||||
|
|
||||||
impl Transform {
|
|
||||||
pub fn new() -> Transform {
|
|
||||||
Transform {
|
|
||||||
x: Position::Const(0),
|
|
||||||
y: Position::Const(0),
|
|
||||||
mx: 0,
|
|
||||||
my: 0,
|
|
||||||
width: Dimension::Content,
|
|
||||||
height: Dimension::Content,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn center() -> Transform {
|
|
||||||
Transform {
|
|
||||||
x: Position::Center,
|
|
||||||
y: Position::Center,
|
|
||||||
mx: 0,
|
|
||||||
my: 0,
|
|
||||||
width: Dimension::Content,
|
|
||||||
height: Dimension::Content,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bottom(mut self) -> Self {
|
|
||||||
self.y = Position::End;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn right(mut self) -> Self {
|
|
||||||
self.x = Position::End;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn margin(mut self, x: i32, y: i32) -> Self {
|
|
||||||
self.mx = x;
|
|
||||||
self.my = y;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dimensions(mut self, width: u16, height: u16) -> Self {
|
|
||||||
self.width = Dimension::Const(width);
|
|
||||||
self.height = Dimension::Const(height);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn use_dimensions(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) {
|
|
||||||
self.width.use_dimension(parent_width, &mut raw.width);
|
|
||||||
self.height.use_dimension(parent_height, &mut raw.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn use_position(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) {
|
|
||||||
self.x
|
|
||||||
.use_position(raw.width, parent_width, self.mx, &mut raw.x);
|
|
||||||
self.y
|
|
||||||
.use_position(raw.height, parent_height, self.my, &mut raw.y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RawTransform {
|
|
||||||
pub fn new() -> RawTransform {
|
|
||||||
RawTransform {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 0,
|
|
||||||
height: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Dimension {
|
|
||||||
pub fn use_dimension(&self, parent: u16, r: &mut u16) {
|
|
||||||
match self {
|
|
||||||
Self::Full => *r = parent,
|
|
||||||
Self::Content => {}
|
|
||||||
Self::Const(n) => *r = *n,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Position {
|
|
||||||
pub fn use_position(&self, size: u16, parent: u16, m: i32, r: &mut u16) {
|
|
||||||
let base = match self {
|
|
||||||
Self::Center => {
|
|
||||||
if parent < size {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
(parent - size) / 2
|
|
||||||
}
|
|
||||||
Self::Const(n) => *n,
|
|
||||||
Self::End => {
|
|
||||||
if parent < size {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
parent - size
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let adjusted = if m >= 0 {
|
|
||||||
base.checked_add(m as u16)
|
|
||||||
} else {
|
|
||||||
base.checked_sub((-m) as u16)
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(val) = adjusted {
|
|
||||||
*r = val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<u16> for Position {
|
|
||||||
fn from(value: u16) -> Self {
|
|
||||||
Self::Const(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<u16> for Dimension {
|
|
||||||
fn from(value: u16) -> Self {
|
|
||||||
Self::Const(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-122
@@ -1,122 +0,0 @@
|
|||||||
//! The `utils` module provides utility functions for terminal manipulation
|
|
||||||
//! and string operations, designed to enhance terminal-based applications.
|
|
||||||
|
|
||||||
use std::io::{self, Write};
|
|
||||||
|
|
||||||
/// Clears the terminal screen and moves the cursor to the top-left corner.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A `io::Result<()>` indicating whether the operation succeeded.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
/// ```
|
|
||||||
/// utils::clear().unwrap();
|
|
||||||
/// ```
|
|
||||||
pub fn clear() -> io::Result<()> {
|
|
||||||
print!("\x1B[2J\x1B[H");
|
|
||||||
std::io::stdout().flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Hides the terminal cursor.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A `io::Result<()>` indicating whether the operation succeeded.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
/// ```
|
|
||||||
/// utils::hide_cursor().unwrap();
|
|
||||||
/// ```
|
|
||||||
pub fn hide_cursor() -> io::Result<()> {
|
|
||||||
print!("\x1b[?25l");
|
|
||||||
std::io::stdout().flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shows the terminal cursor.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A `io::Result<()>` indicating whether the operation succeeded.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
/// ```
|
|
||||||
/// utils::show_cursor().unwrap();
|
|
||||||
/// ```
|
|
||||||
pub fn show_cursor() -> io::Result<()> {
|
|
||||||
print!("\x1B[?25h");
|
|
||||||
std::io::stdout().flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Flushes the terminal's stdout buffer.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A `io::Result<()>` indicating whether the operation succeeded.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
/// ```
|
|
||||||
/// utils::flush().unwrap();
|
|
||||||
/// ```
|
|
||||||
pub fn flush() -> io::Result<()> {
|
|
||||||
std::io::stdout().flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculates the width and height of a string when rendered in a terminal.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
/// - `s`: The input string.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A tuple `(u16, u16)` where:
|
|
||||||
/// - The first value is the maximum width of the string in characters.
|
|
||||||
/// - The second value is the height of the string in lines.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
/// ```
|
|
||||||
/// let (width, height) = utils::str_size("Hello\nWorld!");
|
|
||||||
/// assert_eq!((5, 2), (width, height));
|
|
||||||
/// ```
|
|
||||||
pub fn str_size(s: &str) -> (u16, u16) {
|
|
||||||
let mut height = 1;
|
|
||||||
let mut max_width = 0;
|
|
||||||
let mut current_width = 0;
|
|
||||||
|
|
||||||
for b in s.bytes() {
|
|
||||||
if b == b'\n' {
|
|
||||||
height += 1;
|
|
||||||
max_width = max_width.max(current_width);
|
|
||||||
current_width = 0;
|
|
||||||
} else {
|
|
||||||
current_width += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
max_width = max_width.max(current_width);
|
|
||||||
|
|
||||||
(max_width, height)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hex_ansi(hex: u32) -> String {
|
|
||||||
let r = ((hex >> 16) & 0xFF) as u8;
|
|
||||||
let g = ((hex >> 8) & 0xFF) as u8;
|
|
||||||
let b = (hex & 0xFF) as u8;
|
|
||||||
format!("\x1b[38;2;{r};{g};{b}m")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hex_ansi_bg(hex: u32) -> String {
|
|
||||||
let r = ((hex >> 16) & 0xFF) as u8;
|
|
||||||
let g = ((hex >> 8) & 0xFF) as u8;
|
|
||||||
let b = (hex & 0xFF) as u8;
|
|
||||||
format!("\x1b[48;2;{r};{g};{b}m")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn print(x: u16, y: u16, text: &str) {
|
|
||||||
for (i, line) in text.lines().enumerate() {
|
|
||||||
print!("\x1b[{};{}H{line}\x1b[0m", y + i as u16 + 1, x + 1);
|
|
||||||
flush().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn print_liner(x: u16, y: u16, liner: &str, text: &str) {
|
|
||||||
for (i, line) in text.lines().enumerate() {
|
|
||||||
print!("\x1b[{};{}H{liner}{line}\x1b[0m", y + i as u16 + 1, x + 1);
|
|
||||||
flush().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-140
@@ -1,140 +0,0 @@
|
|||||||
use std::{
|
|
||||||
any::{Any, TypeId},
|
|
||||||
collections::HashMap,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{render_scope::RenderScope, state::DependencyHandler};
|
|
||||||
|
|
||||||
pub type BoxedElement = Box<dyn Element + Send + Sync>;
|
|
||||||
pub type BoxedComponent = Box<dyn Component + Send + Sync>;
|
|
||||||
|
|
||||||
pub trait Element: Send + Sync {
|
|
||||||
#[allow(unused)]
|
|
||||||
fn render(&mut self, scope: &mut RenderScope) {}
|
|
||||||
#[allow(unused)]
|
|
||||||
fn after_render(&mut self, scope: &mut RenderScope) {}
|
|
||||||
#[allow(unused)]
|
|
||||||
fn draw_child(&self, element: &Arc<Widget>) {}
|
|
||||||
fn as_any(&self) -> &dyn Any;
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Component: Send + Sync {
|
|
||||||
fn as_any(&self) -> &dyn Any;
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Widget(
|
|
||||||
pub Mutex<BoxedElement>,
|
|
||||||
Mutex<HashMap<TypeId, BoxedComponent>>,
|
|
||||||
Mutex<Box<dyn FnMut() -> WidgetLoad + Send + Sync>>,
|
|
||||||
Mutex<Vec<Box<dyn DependencyHandler>>>,
|
|
||||||
Mutex<Option<Box<dyn FnMut(WidgetLoad) -> WidgetLoad + Send + Sync>>>,
|
|
||||||
);
|
|
||||||
|
|
||||||
impl Widget {
|
|
||||||
pub fn new<F: FnMut() -> WidgetLoad + 'static + Send + Sync>(mut e: F) -> Self {
|
|
||||||
let wl = e();
|
|
||||||
Self(
|
|
||||||
Mutex::new(wl.0),
|
|
||||||
Mutex::new(wl.1),
|
|
||||||
Mutex::new(Box::new(e)),
|
|
||||||
Mutex::new(Vec::new()),
|
|
||||||
Mutex::new(None),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn inject<F: FnMut(WidgetLoad) -> WidgetLoad + 'static + Send + Sync>(
|
|
||||||
self: &Arc<Self>,
|
|
||||||
f: F,
|
|
||||||
) {
|
|
||||||
{
|
|
||||||
*self.4.lock().unwrap() = Some(Box::new(f));
|
|
||||||
}
|
|
||||||
self.refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn refresh(self: &Arc<Self>) {
|
|
||||||
let w = (self.2.lock().unwrap())();
|
|
||||||
match &mut *self.4.lock().unwrap() {
|
|
||||||
Some(inject) => {
|
|
||||||
let w = inject(w);
|
|
||||||
*self.0.lock().unwrap() = w.0;
|
|
||||||
*self.1.lock().unwrap() = w.1;
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
*self.0.lock().unwrap() = w.0;
|
|
||||||
*self.1.lock().unwrap() = w.1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn auto_refresh(self: &Arc<Self>) {
|
|
||||||
for d in self.3.lock().unwrap().iter() {
|
|
||||||
if d.check() {
|
|
||||||
self.refresh();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dependency<D: DependencyHandler + 'static>(self: &Arc<Self>, d: D) -> &Arc<Self> {
|
|
||||||
d.add();
|
|
||||||
self.3.lock().unwrap().push(Box::new(d));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dependency_box(self: &Arc<Self>, d: Box<dyn DependencyHandler>) -> &Arc<Self> {
|
|
||||||
d.add();
|
|
||||||
self.3.lock().unwrap().push(d);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn component<C: Component + 'static>(self: &Arc<Self>, c: C) -> &Arc<Self> {
|
|
||||||
self.1
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.entry(c.type_id())
|
|
||||||
.or_insert_with(|| Box::new(c));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_component<C: Component + 'static>(self: &Arc<Self>, c: C) -> &Arc<Self> {
|
|
||||||
self.1.lock().unwrap().insert(c.type_id(), Box::new(c));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get<C: Component + 'static + Clone>(&self) -> Option<C> {
|
|
||||||
self.1
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.get(&TypeId::of::<C>())
|
|
||||||
.and_then(|c| c.as_any().downcast_ref::<C>())
|
|
||||||
.map(|c| c.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WidgetLoad(BoxedElement, HashMap<TypeId, BoxedComponent>);
|
|
||||||
|
|
||||||
impl WidgetLoad {
|
|
||||||
pub fn new<E: Element + 'static>(e: E) -> Self {
|
|
||||||
Self(Box::new(e), HashMap::new())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn component<C: Component + 'static>(mut self, c: C) -> Self {
|
|
||||||
self.1.entry(c.type_id()).or_insert_with(|| Box::new(c));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_component<C: Component + 'static>(mut self, c: C) -> Self {
|
|
||||||
self.1.insert(c.type_id(), Box::new(c));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get<C: Component + 'static + Clone>(&self) -> Option<C> {
|
|
||||||
self.1
|
|
||||||
.get(&TypeId::of::<C>())
|
|
||||||
.and_then(|c| c.as_any().downcast_ref::<C>())
|
|
||||||
.map(|c| c.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user