some fixes
This commit is contained in:
+28
-14
@@ -2,9 +2,9 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
create_frame,
|
create_frame,
|
||||||
key::KeyKind,
|
key::{self, KeyKind},
|
||||||
utils::{closest_component, render_to_frame},
|
utils::{closest_component, render_to_frame},
|
||||||
Component, UpdateRequest, UpdateResponse,
|
Component, UpdateResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn div() -> Component {
|
pub fn div() -> Component {
|
||||||
@@ -17,8 +17,8 @@ pub fn div() -> Component {
|
|||||||
(KeyKind::Right, String::from("right")),
|
(KeyKind::Right, String::from("right")),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
fn update(this: &mut Component, req: UpdateRequest) -> UpdateResponse {
|
fn update(this: &mut Component, k: key::Key) -> UpdateResponse {
|
||||||
if let Some(v) = this.binds.get(&req.key.kind) {
|
if let Some(v) = this.binds.get(&k.kind) {
|
||||||
match v.as_str() {
|
match v.as_str() {
|
||||||
"up" => {
|
"up" => {
|
||||||
this.active_child = closest_component(
|
this.active_child = closest_component(
|
||||||
@@ -54,13 +54,13 @@ pub fn div() -> Component {
|
|||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(child) = this.get_active_child() {
|
if let Some(child) = this.get_active_child() {
|
||||||
return (child.update)(child, req);
|
return (child.update)(child, k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Some(child) = this.get_active_child() {
|
if let Some(child) = this.get_active_child() {
|
||||||
return (child.update)(child, req);
|
return (child.update)(child, k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,15 +75,22 @@ pub fn div() -> Component {
|
|||||||
frame.join("\n")
|
frame.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tick(this: &mut Component, i: usize) {
|
||||||
|
if let Some(child) = this.get_active_child() {
|
||||||
|
return (child.tick)(child, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
component.update = update;
|
component.update = update;
|
||||||
component.render = render;
|
component.render = render;
|
||||||
|
component.tick = tick;
|
||||||
component
|
component
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn text() -> Component {
|
pub fn text() -> Component {
|
||||||
let mut component = Component::new();
|
let mut component = Component::new();
|
||||||
|
|
||||||
fn update(_: &mut Component, _: UpdateRequest) -> UpdateResponse {
|
fn update(_: &mut Component, _: key::Key) -> UpdateResponse {
|
||||||
UpdateResponse::None
|
UpdateResponse::None
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,27 +106,34 @@ pub fn text() -> Component {
|
|||||||
pub fn button() -> Component {
|
pub fn button() -> Component {
|
||||||
let mut component = Component::new();
|
let mut component = Component::new();
|
||||||
|
|
||||||
fn update(this: &mut Component, req: UpdateRequest) -> UpdateResponse {
|
fn update(this: &mut Component, k: key::Key) -> UpdateResponse {
|
||||||
if req.key.kind == KeyKind::Enter {
|
if k.kind == KeyKind::Enter {
|
||||||
if this.toggle {
|
if this.toggle {
|
||||||
this.clicked = !this.clicked;
|
this.clicked = !this.clicked;
|
||||||
(this.on_click)(this);
|
(this.on_click)(this);
|
||||||
} else {
|
} else {
|
||||||
this.clicked = true;
|
return UpdateResponse::Tick(vec![1, 140]);
|
||||||
(this.on_click)(this);
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(300));
|
|
||||||
this.clicked = false;
|
|
||||||
(this.on_click)(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UpdateResponse::None
|
UpdateResponse::None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tick(this: &mut Component, i: usize) {
|
||||||
|
if i == 0 {
|
||||||
|
this.clicked = true;
|
||||||
|
(this.on_click)(this);
|
||||||
|
} else if i == 1 {
|
||||||
|
this.clicked = false;
|
||||||
|
(this.on_click)(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn render(s: &mut Component) -> String {
|
fn render(s: &mut Component) -> String {
|
||||||
s.expr.clone()
|
s.expr.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
component.update = update;
|
component.update = update;
|
||||||
component.render = render;
|
component.render = render;
|
||||||
|
component.tick = tick;
|
||||||
component
|
component
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-13
@@ -11,17 +11,14 @@ pub enum UpdateResponse {
|
|||||||
Exit,
|
Exit,
|
||||||
Done,
|
Done,
|
||||||
None,
|
None,
|
||||||
}
|
Tick(Vec<u32>),
|
||||||
|
|
||||||
pub struct UpdateRequest<'a> {
|
|
||||||
pub key: key::Key,
|
|
||||||
pub screen: &'a App,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Component {
|
pub struct Component {
|
||||||
pub render: fn(&mut Component) -> String,
|
pub render: fn(&mut Component) -> String,
|
||||||
pub update: fn(&mut Component, UpdateRequest) -> UpdateResponse,
|
pub update: fn(&mut Component, key::Key) -> UpdateResponse,
|
||||||
|
pub tick: fn(&mut Component, usize),
|
||||||
pub on_click: fn(&mut Component),
|
pub on_click: fn(&mut Component),
|
||||||
pub x: usize,
|
pub x: usize,
|
||||||
pub y: usize,
|
pub y: usize,
|
||||||
@@ -41,6 +38,7 @@ impl Component {
|
|||||||
Component {
|
Component {
|
||||||
render: |_| String::new(),
|
render: |_| String::new(),
|
||||||
update: |_, _| UpdateResponse::None,
|
update: |_, _| UpdateResponse::None,
|
||||||
|
tick: |_, _| {},
|
||||||
on_click: |_| {},
|
on_click: |_| {},
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
@@ -102,19 +100,20 @@ impl App {
|
|||||||
crossterm::terminal::enable_raw_mode().unwrap();
|
crossterm::terminal::enable_raw_mode().unwrap();
|
||||||
loop {
|
loop {
|
||||||
self.render();
|
self.render();
|
||||||
match (self.component.update)(
|
match (self.component.update)(&mut self.component, key::read_key()) {
|
||||||
&mut self.component,
|
|
||||||
UpdateRequest {
|
|
||||||
key: key::read_key(),
|
|
||||||
screen: self, // error: cannot borrow `*self` as immutable because it is also borrowed as mutable (IMPORTANT)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
UpdateResponse::Exit => {
|
UpdateResponse::Exit => {
|
||||||
crossterm::terminal::disable_raw_mode().unwrap();
|
crossterm::terminal::disable_raw_mode().unwrap();
|
||||||
utils::clear();
|
utils::clear();
|
||||||
utils::show_cursor();
|
utils::show_cursor();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
UpdateResponse::Tick(ticks) => {
|
||||||
|
for (i, n) in ticks.into_iter().enumerate() {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(n as u64));
|
||||||
|
(self.component.tick)(&mut self.component, i);
|
||||||
|
self.render();
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user