From 2860b1b8b143bf2dce740d92e98bd682c55e3828 Mon Sep 17 00:00:00 2001 From: rabite Date: Thu, 14 Feb 2019 11:57:52 +0100 Subject: async_widget2 --- src/preview.rs | 67 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'src/preview.rs') diff --git a/src/preview.rs b/src/preview.rs index 859c8eb..662529e 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -2,13 +2,13 @@ use std::io::Write; use std::sync::Mutex; use std::sync::Arc; -use crate::coordinates::{Coordinates, Position, Size}; +use crate::coordinates::{Coordinates}; use crate::files::{File, Files, Kind}; use crate::listview::ListView; use crate::textview::TextView; use crate::widget::Widget; -use crate::async_widget::AsyncPlug; - +//use crate::async_widget::AsyncPlug; +use crate::async_widget::AsyncPlug2; lazy_static! { static ref PIDS: Arc>> = { Arc::new(Mutex::new(vec![])) }; @@ -30,36 +30,43 @@ fn is_current(file: &File) -> bool { } } +enum WillBe { + Is(T), + Becoming, + Wont(Box) +} + #[derive(PartialEq)] pub struct AsyncPreviewer { pub file: Option, pub buffer: String, pub coordinates: Coordinates, - pub async_plug: AsyncPlug + pub async_plug: AsyncPlug2> } impl AsyncPreviewer { pub fn new() -> AsyncPreviewer { - let textview = crate::textview::TextView { - lines: vec![], - buffer: "".to_string(), - coordinates: Coordinates::new(), - }; - let textview = Box::new(textview); - + let closure = Box::new(|| { + Box::new(crate::textview::TextView { + lines: vec![], + buffer: "".to_string(), + coordinates: Coordinates::new() + }) as Box + }); + AsyncPreviewer { file: None, buffer: String::new(), coordinates: Coordinates::new(), - async_plug: AsyncPlug::new(textview) + async_plug: AsyncPlug2::new_from_closure(closure), } } pub fn set_file(&mut self, file: &File) { let coordinates = self.coordinates.clone(); let file = file.clone(); let redraw = crate::term::reset() + &self.get_redraw_empty_list(0); - let pids = PIDS.clone(); - kill_procs(); + //let pids = PIDS.clone(); + //kill_procs(); self.async_plug.replace_widget(Box::new(move || { kill_procs(); @@ -75,7 +82,7 @@ impl AsyncPreviewer { file_list.refresh(); //if !is_current(&file) { return } file_list.animate_slide_up(); - return Box::new(file_list) as Box; + return Box::new(file_list) } Err(err) => { @@ -85,7 +92,7 @@ impl AsyncPreviewer { buffer: "".to_string(), coordinates: Coordinates::new(), }; - return Box::new(textview) as Box; + return Box::new(textview) }, } _ => { @@ -99,7 +106,7 @@ impl AsyncPreviewer { textview.refresh(); //if !is_current(&file) { return } textview.animate_slide_up(); - return Box::new(textview); + return Box::new(textview) } else { let process = std::process::Command::new("scope.sh") @@ -135,7 +142,7 @@ impl AsyncPreviewer { textview.set_coordinates(&coordinates); textview.refresh(); textview.animate_slide_up(); - return Box::new(textview); + return Box::new(textview) } }, None => {} } @@ -149,7 +156,7 @@ impl AsyncPreviewer { buffer: "".to_string(), coordinates: Coordinates::new(), }; - return Box::new(textview); + return Box::new(textview) } } }})) @@ -184,3 +191,25 @@ impl Widget for AsyncPreviewer { "".to_string() } } + +impl Widget for Box where T: Widget + ?Sized { + fn get_coordinates(&self) -> &Coordinates { + (**self).get_coordinates() + } + fn set_coordinates(&mut self, coordinates: &Coordinates) { + if (**self).get_coordinates() == coordinates { + return; + } + (**self).set_coordinates(&coordinates); + (**self).refresh(); + } + fn render_header(&self) -> String { + (**self).render_header() + } + fn refresh(&mut self) { + (**self).refresh() + } + fn get_drawlist(&self) -> String { + (**self).get_drawlist() + } +} -- cgit v1.2.3