perfomance changes

This commit is contained in:
2024-11-22 11:29:00 +01:00
parent cc0ea4a9f5
commit 7f1437e779
4 changed files with 18 additions and 9 deletions
+11 -6
View File
@@ -64,12 +64,7 @@ extern "C" fn render_loop(ptr: *mut c_void) -> bool {
} }
let args = unsafe { &mut *(ptr as *mut LArgs) }; let args = unsafe { &mut *(ptr as *mut LArgs) };
while args.running { while args.running {
let (width, height) = get_term_size(); sleep(1000);
let mut frame: Vec<String> = create_frame(width, height);
render_to_frame(true, width, &mut frame, args.element);
clear();
print!("{}", frame.join(""));
flush();
} }
false false
} }
@@ -92,12 +87,14 @@ extern "C" fn event_loop(ptr: *mut c_void) -> *const c_void {
crossterm::event::Event::FocusGained, crossterm::event::Event::FocusGained,
&document, &document,
); );
document.render();
while args.running { while args.running {
if ptr.is_null() { if ptr.is_null() {
break; break;
} }
event(args.element, crossterm::event::read().unwrap(), &document); event(args.element, crossterm::event::read().unwrap(), &document);
document.render();
} }
null() as *const c_void null() as *const c_void
} }
@@ -124,6 +121,14 @@ extern "C" fn cmd_loop(ptr: *mut c_void) -> *const c_void {
}) })
.unwrap(); .unwrap();
} }
Command::Render => {
let (width, height) = get_term_size();
let mut frame: Vec<String> = create_frame(width, height);
render_to_frame(true, width, &mut frame, args.element);
clear();
print!("{}", frame.join(""));
flush();
}
} }
} }
null() null()
+4
View File
@@ -216,6 +216,7 @@ pub fn convert<T>(widget: &mut Box<dyn ElementWidget>) -> &mut Box<T> {
pub enum Command { pub enum Command {
Exit, Exit,
Render,
GetElementById(String), GetElementById(String),
} }
@@ -240,4 +241,7 @@ impl Document {
None None
} }
} }
pub fn render(&self) {
self.cmd_sender.send(Command::Render).unwrap();
}
} }
+1 -3
View File
@@ -9,13 +9,11 @@ fn main() {
fn app() -> Element { fn app() -> Element {
rsx! { styling: Some(styles()), rsx! { styling: Some(styles()),
button { class: "btn", on_click: Handler::new(|_, _, document| { button { class: "btn", on_click: Handler::new(|_, _, document| {
if let Some(txt) = document.get_element_by_id::<Text>("txt") { if let Some(txt) = document.get_element_by_id::<Text>("txt") {
txt.children.set_text("edited"); txt.children.set_text("edited");
} }
}), "Hello, World!" } }), "Hello, World!" }
text { id: "txt", "Some text!" } text { id: "txt", y: 2, "Some text!" }
} }
} }
+2
View File
@@ -159,8 +159,10 @@ impl ElementWidget for Button<'_> {
match event { match event {
Event::Key(key) => { Event::Key(key) => {
if key.code == KeyCode::Enter { if key.code == KeyCode::Enter {
document.render();
self.state = "clicked"; self.state = "clicked";
run_handler!(self.on_click(event, document)); run_handler!(self.on_click(event, document));
document.render();
sleep(100); sleep(100);
self.state = ""; self.state = "";
} }