This commit is contained in:
2024-10-23 02:49:56 +02:00
parent 5dbc5378d9
commit ac39556c24
5 changed files with 129 additions and 53 deletions
+97 -22
View File
@@ -21,42 +21,35 @@ pub fn div() -> Component {
if let Some(v) = this.binds.get(&k.kind) { if let Some(v) = this.binds.get(&k.kind) {
match v.as_str() { match v.as_str() {
"up" => { "up" => {
this.set_child(closest_component( this.child =
&this.children, closest_component(&this.children, this.child, crate::utils::Direction::Up);
this.child,
crate::utils::Direction::Up,
));
} }
"down" => { "down" => {
this.set_child(closest_component( this.child = closest_component(
&this.children, &this.children,
this.child, this.child,
crate::utils::Direction::Down, crate::utils::Direction::Down,
)); );
} }
"left" => { "left" => {
this.set_child(closest_component( this.child = closest_component(
&this.children, &this.children,
this.child, this.child,
crate::utils::Direction::Left, crate::utils::Direction::Left,
)); );
} }
"right" => { "right" => {
this.set_child(closest_component( this.child = closest_component(
&this.children, &this.children,
this.child, this.child,
crate::utils::Direction::Right, crate::utils::Direction::Right,
)); );
} }
_ => { _ => {}
if let Some(child) = this.get_child() {
return (child.update)(child, k);
}
}
} }
} else { } else {
if let Some(child) = this.get_child() { if let Some(child) = this.get_child() {
@@ -69,9 +62,15 @@ pub fn div() -> Component {
fn render(this: &mut Component) -> String { fn render(this: &mut Component) -> String {
let mut frame: Vec<String> = create_frame!(this.width, this.height); let mut frame: Vec<String> = create_frame!(this.width, this.height);
for (i, c) in &mut this.children.iter_mut().enumerate() { for (i, child) in &mut this.children.iter_mut().enumerate() {
c.style.is_active = this.style.is_active && i == this.child; if child.width == 0 {
render_to_frame(&mut frame, c); child.width = this.width;
}
if child.height == 0 {
child.height = this.height;
}
child.style.is_active = this.style.is_active && i == this.child;
render_to_frame(&mut frame, child);
} }
frame.join("\n") frame.join("\n")
} }
@@ -107,11 +106,11 @@ pub fn text() -> Component {
pub fn button() -> Component { pub fn button() -> Component {
let mut component = Component::new(); let mut component = Component::new();
component.style.clicked_fg = crate::style::Color::Red; component.binds = HashMap::from([(key::KeyKind::Enter, String::from("click"))]);
component.style.hover_fg = crate::style::Color::Green;
fn update(this: &mut Component, k: key::Key) -> UpdateResponse { fn update(this: &mut Component, k: key::Key) -> UpdateResponse {
if k.kind == KeyKind::Enter { if let Some(v) = this.binds.get(&k.kind) {
if v == "click" {
if this.toggle { if this.toggle {
this.clicked = !this.clicked; this.clicked = !this.clicked;
(this.on_click)(this); (this.on_click)(this);
@@ -119,6 +118,7 @@ pub fn button() -> Component {
return UpdateResponse::Tick(vec![1, 100]); return UpdateResponse::Tick(vec![1, 100]);
} }
} }
}
UpdateResponse::None UpdateResponse::None
} }
@@ -144,3 +144,78 @@ pub fn button() -> Component {
component.tick = tick; component.tick = tick;
component component
} }
pub fn tab() -> Component {
let mut component = Component::new();
component.binds = HashMap::from([
(key::KeyKind::Tab, String::from("next")),
(key::KeyKind::ShiftTab, String::from("previous")),
]);
component.style.clicked_fg = crate::style::Color::Red;
fn update(this: &mut Component, k: key::Key) -> UpdateResponse {
if let Some(v) = this.binds.get(&k.kind) {
match v.as_str() {
"next" => {
if this.child + 1 < this.children.len() {
this.child += 1;
} else {
this.child = 0;
}
}
"previous" => {
if this.child == 0 {
this.child = this.children.len() - 1;
} else {
this.child -= 1;
}
}
_ => {}
}
} else {
if let Some(child) = this.get_child() {
return (child.update)(child, k);
} else {
}
}
UpdateResponse::None
}
fn render(this: &mut Component) -> String {
let mut frame: Vec<String> = create_frame!(this.width, this.height - 1);
let w = this.width;
let h = this.height;
if let Some(child) = this.get_child() {
if child.width == 0 {
child.width = w;
}
if child.height == 0 {
child.height = h;
}
child.style.is_active = true;
render_to_frame(&mut frame, child);
}
let mut v: String = " ".repeat((this.width / 2) - this.children.len());
for (i, _) in (&this.children).into_iter().enumerate() {
if i == this.child {
v += this.style.write_clicked("*").as_str()
} else {
v += this.style.write("*").as_str()
}
}
format!("{}\n{}", v, frame.join("\n"))
}
fn tick(this: &mut Component, i: usize) {
if let Some(child) = this.get_child() {
return (child.tick)(child, i);
}
}
component.update = update;
component.tick = tick;
component.render = render;
component
}
+9 -3
View File
@@ -3,6 +3,8 @@ use std::io::{self, Read};
#[derive(Debug, Clone, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum KeyKind { pub enum KeyKind {
Enter, Enter,
Tab,
ShiftTab,
Escape, Escape,
Up, Up,
Down, Down,
@@ -19,8 +21,12 @@ pub struct Key {
impl Key { impl Key {
pub fn new(k: String) -> Key { pub fn new(k: String) -> Key {
let kind = match k.as_str() { Key {
raw: k.clone(),
kind: match k.as_str() {
"\r" => KeyKind::Enter, "\r" => KeyKind::Enter,
"\t" => KeyKind::Tab,
"\x1b[Z" => KeyKind::ShiftTab,
"\x1b" => KeyKind::Escape, "\x1b" => KeyKind::Escape,
"\x1b[A" => KeyKind::Up, "\x1b[A" => KeyKind::Up,
"\x1b[B" => KeyKind::Down, "\x1b[B" => KeyKind::Down,
@@ -28,8 +34,8 @@ impl Key {
"\x1b[D" => KeyKind::Left, "\x1b[D" => KeyKind::Left,
_ => KeyKind::Char(k.clone()), _ => KeyKind::Char(k.clone()),
}; },
Key { raw: k, kind } }
} }
} }
-12
View File
@@ -51,18 +51,6 @@ impl Component {
pub fn get_child(&mut self) -> Option<&mut Component> { pub fn get_child(&mut self) -> Option<&mut Component> {
self.children.get_mut(self.child) self.children.get_mut(self.child)
} }
pub fn set_child(&mut self, idx: usize) {
if let Some(child) = self.children.get_mut(idx) {
self.child = idx;
if child.width == 0 {
child.width = self.width;
}
if child.height == 0 {
child.height = self.height;
}
}
}
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
+6 -1
View File
@@ -16,9 +16,14 @@ fn onclick(c: &mut Component) {
fn app_elem() -> Component { fn app_elem() -> Component {
oml!( oml!(
tab {
div {
text("Hello, World!";)
}
div { div {
button("click me"; toggle = false, on_click = onclick) button("click me"; toggle = false, on_click = onclick)
button("click me"; toggle = false, y = 2, on_click = onclick) }
} }
) )
} }
+3 -1
View File
@@ -64,7 +64,9 @@ fn merge_line(frame_: &str, line_: &str, x: usize) -> String {
if let Some(v) = lm.get(&(i - x)) { if let Some(v) = lm.get(&(i - x)) {
res.push_str(v); res.push_str(v);
} }
if line[i - x] != '\n' { if line[i - x] == '\n' {
res.push(frame[i]);
} else {
res.push(line[i - x]); res.push(line[i - x]);
} }
} else { } else {