This commit is contained in:
2025-01-11 23:42:09 +01:00
parent 4d759e6444
commit d4d4108714
6 changed files with 122 additions and 104 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ pub mod counter_example {
#[component] #[component]
pub fn App() -> Element { pub fn App() -> Element {
let count = State::new(0); let count = use_state(0);
rsx! { rsx! {
@SetStyle(styling()) @SetStyle(styling())
+19 -26
View File
@@ -44,6 +44,7 @@ use std::{
use crossterm::event::Event; use crossterm::event::Event;
use ui::{Style, StyleCore}; use ui::{Style, StyleCore};
use utils::VisibleLength;
pub mod css; pub mod css;
pub mod examples; pub mod examples;
@@ -58,13 +59,16 @@ pub mod prelude {
pub use crate::ui::Number::{Auto, Center}; pub use crate::ui::Number::{Auto, Center};
pub use crate::Instruction::*; pub use crate::Instruction::*;
pub use crate::{self as osui, css, ersx, launch, rsx, ui::*, Handler}; pub use crate::{self as osui, css, ersx, launch, rsx, ui::*, Handler};
pub use crate::{style, Component, Document, Instruction, State}; pub use crate::{style, Component, Document};
pub use crate::{Children, Element, ElementCore, ElementWidget}; pub use crate::{Children, Element, ElementCore, ElementWidget};
pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind}; pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
pub use osui_element::component; pub use osui_element::component;
pub fn sleep(ms: u64) { pub fn sleep(ms: u64) {
std::thread::sleep(std::time::Duration::from_millis(ms)); std::thread::sleep(std::time::Duration::from_millis(ms));
} }
pub fn use_state<'a, T>(v: T) -> crate::State<T> {
crate::State::new(v)
}
} }
////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////
@@ -261,6 +265,7 @@ impl Children {
_ => false, _ => false,
} }
} }
pub fn get_text(&self) -> String { pub fn get_text(&self) -> String {
match self { match self {
Children::Text(text) => text(), Children::Text(text) => text(),
@@ -268,6 +273,7 @@ impl Children {
_ => String::new(), _ => String::new(),
} }
} }
pub fn set_text(&mut self, text: Arc<dyn Fn() -> String>) { pub fn set_text(&mut self, text: Arc<dyn Fn() -> String>) {
match self { match self {
Children::Text(t) => { Children::Text(t) => {
@@ -276,19 +282,11 @@ impl Children {
_ => {} _ => {}
} }
} }
pub fn set_text_force(&mut self, text: Arc<dyn Fn() -> String>) {
match self { pub fn set_static(&mut self, text: String) {
Children::Text(t) => {
*t = text;
}
_ => {
*self = Children::Text(text);
}
}
}
pub fn set_static_force(&mut self, text: String) {
*self = Children::StaticText(text); *self = Children::StaticText(text);
} }
pub fn add_child(&mut self, element: Element) { pub fn add_child(&mut self, element: Element) {
if let Children::Children(children, _) = self { if let Children::Children(children, _) = self {
children.push(element); children.push(element);
@@ -333,6 +331,13 @@ impl Children {
0 0
} }
} }
pub fn index_mut(&mut self) -> Option<&mut usize> {
if let Children::Children(_, c) = self {
Some(c)
} else {
None
}
}
pub fn insert(&mut self, index: usize, element: Element) { pub fn insert(&mut self, index: usize, element: Element) {
if let Children::Children(children, _) = self { if let Children::Children(children, _) = self {
children.insert(index, element); children.insert(index, element);
@@ -362,7 +367,7 @@ impl Writer {
pub fn after_render(&self, width: u16, height: u16) { pub fn after_render(&self, width: u16, height: u16) {
if self.style.outline.1 { if self.style.outline.1 {
let ol = self.style.write_outline("│"); let ol = self.style.write_outline("│");
let hl = "─".repeat(width as usize - 2); let hl = "─".repeat(width as usize);
self.write_abs_outline(&self.style.write_outline(&format!("╭{}╮", hl)), (0, 0)); self.write_abs_outline(&self.style.write_outline(&format!("╭{}╮", hl)), (0, 0));
for i in 1..height { for i in 1..height {
self.write_abs_outline(&ol, (0, i)); self.write_abs_outline(&ol, (0, i));
@@ -373,18 +378,6 @@ impl Writer {
} }
pub fn write(&mut self, s: &str) { pub fn write(&mut self, s: &str) {
// let width = self.get_size().0 as usize;
// let s = s
// .lines()
// .map(|l| {
// if l.len() > width {
// l.chars().take(width).collect::<String>()
// } else {
// l.to_string()
// }
// })
// .collect::<Vec<String>>()
// .join("\n");
self.write_abs(&self.style.write(&s), (0, 0)); self.write_abs(&self.style.write(&s), (0, 0));
} }
@@ -415,7 +408,7 @@ impl Writer {
} else { } else {
l.to_string() l.to_string()
}; };
max_written = max_written.max(truncated.len() as u16); max_written = max_written.max(truncated.visible_len() as u16);
lines_written += 1; lines_written += 1;
truncated truncated
}) })
+7 -13
View File
@@ -5,19 +5,13 @@ fn main() {
} }
#[component] #[component]
pub fn App() { fn App() {
rsx! { let count = use_state(20);
@SetStyle(css! {
div {
outline: true,
width: 33%,
x: Auto,
y: 0 px,
}
})
div { class: "div" } rsx! {
div { class: "div" } %text { "Hello" }
div { class: "div" } button { on_click: fn (_, _, _) @count {
count += 1
}, "World: {count}" }
} }
} }
+19 -8
View File
@@ -32,7 +32,7 @@ pub fn text<'a>(class: &'a str, id: &'a str, children: Children, style: Style) -
#[element] #[element]
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct Div { pub struct Div {
pub instructions: Vec<Instruction<'a>>, pub instructions: Vec<crate::Instruction<'a>>,
pub ghosts: Vec<usize>, pub ghosts: Vec<usize>,
} }
@@ -92,26 +92,37 @@ impl ElementWidget for Div<'_> {
} }
fn initialize(&mut self, document: &mut Document) { fn initialize(&mut self, document: &mut Document) {
let l = self.children.len();
if let Some(i) = self.children.index_mut() {
while self.ghosts.get(*i).is_some() {
if *i < l - 1 {
*i += 1;
} else {
*i = 0;
break;
}
}
}
let mut load_funcs = Vec::new(); let mut load_funcs = Vec::new();
for inst in &self.instructions { for inst in &self.instructions {
match inst { match inst {
Instruction::SetStyle(s) => { crate::Instruction::SetStyle(s) => {
document.css.extend(s.clone().into_iter()); document.css.extend(s.clone().into_iter());
} }
Instruction::SetChild(c) => { crate::Instruction::SetChild(c) => {
self.children.set_index(*c); self.children.set_index(*c);
} }
Instruction::Ghost(idx) => { crate::Instruction::Ghost(idx) => {
self.ghosts.push(*idx); self.ghosts.push(*idx);
} }
Instruction::Load(f) => { crate::Instruction::Load(f) => {
load_funcs.push(f.clone()); load_funcs.push(f.clone());
} }
} }
} }
self.instructions.clear(); self.instructions.clear();
for l in load_funcs { for i in load_funcs {
l.call(self, Event::FocusGained, document) i.call(self, Event::FocusGained, document)
} }
} }
} }
@@ -122,7 +133,7 @@ pub fn div<'a>(
id: &'a str, id: &'a str,
children: Children, children: Children,
style: Style, style: Style,
instructions: Vec<Instruction<'a>>, instructions: Vec<crate::Instruction<'a>>,
ghosts: Vec<usize>, ghosts: Vec<usize>,
) -> Box<Div<'a>> { ) -> Box<Div<'a>> {
Box::new(Div { Box::new(Div {
-4
View File
@@ -322,11 +322,7 @@ impl Number {
} }
pub fn as_size(&self, written: u16, frame_size: u16, outline: bool) -> u16 { pub fn as_size(&self, written: u16, frame_size: u16, outline: bool) -> u16 {
if *self == Number::Auto { if *self == Number::Auto {
if written > 0 {
written - 1
} else {
written written
}
} else if outline { } else if outline {
self.as_size_raw(frame_size) - 2 self.as_size_raw(frame_size) - 2
} else { } else {
+34 -10
View File
@@ -34,7 +34,10 @@ impl<'a> Frame<'a> {
} }
} }
if style_element.visible.1 { if !style_element.visible.1 {
return;
}
let y = style_element.y.1.as_position( let y = style_element.y.1.as_position(
style_element.outline.1, style_element.outline.1,
&{ &{
@@ -46,12 +49,13 @@ impl<'a> Frame<'a> {
}, },
self.writer.size.1, self.writer.size.1,
); );
if let Some(y_) = self.used.get(y as usize) { if let Some(y_) = self.used.get(y as usize) {
let x = let x = style_element
style_element
.x .x
.1 .1
.as_position(style_element.outline.1, y_, self.writer.size.0); .as_position(style_element.outline.1, y_, self.writer.size.0);
let mut writer = crate::Writer { let mut writer = crate::Writer {
focused, focused,
style: style_element.clone(), style: style_element.clone(),
@@ -62,11 +66,7 @@ impl<'a> Frame<'a> {
written: (0, 0), written: (0, 0),
}; };
element.render(&mut writer); element.render(&mut writer);
let (mut width, mut height) = writer.get_size_root(); let (width, height) = writer.get_size_root();
if style_element.outline.1 {
width += 2;
height += 2;
}
{ {
let (mut x, mut y) = (x, y); let (mut x, mut y) = (x, y);
if style_element.outline.1 { if style_element.outline.1 {
@@ -84,14 +84,13 @@ impl<'a> Frame<'a> {
if style_element.x.1 != crate::ui::Number::Auto { if style_element.x.1 != crate::ui::Number::Auto {
*o += x; *o += x;
} }
*o += width + 1; *o += width;
} }
} }
} }
writer.after_render(width, height); writer.after_render(width, height);
} }
} }
}
} }
pub fn clear() { pub fn clear() {
@@ -112,3 +111,28 @@ pub fn show_cursor() {
pub fn flush() { pub fn flush() {
stdout().flush().unwrap(); stdout().flush().unwrap();
} }
pub trait VisibleLength {
fn visible_len(&self) -> usize;
}
impl VisibleLength for String {
fn visible_len(&self) -> usize {
let mut length = 0;
let mut in_escape = false;
for c in self.chars() {
if c == '\x1b' {
in_escape = true;
} else if in_escape {
if c.is_ascii_alphabetic() {
in_escape = false;
}
} else {
length += 1;
}
}
length
}
}