diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-09-10 21:03:03 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-09-10 21:03:03 -0400 |
commit | 1908ff94b7e5f981a4254ca697056a299b2e6b51 (patch) | |
tree | 61e39acebe6b3213b7a3dea8abe2924aaf18b276 | |
parent | b44da4d3c6a4b2764f563ce392afeb7fe9632efc (diff) |
fix preview width = 0 issue
-rw-r--r-- | src/preview/preview_default.rs | 14 | ||||
-rw-r--r-- | src/preview/preview_file.rs | 122 | ||||
-rw-r--r-- | src/run.rs | 6 |
3 files changed, 85 insertions, 57 deletions
diff --git a/src/preview/preview_default.rs b/src/preview/preview_default.rs index 002f5c0..7004449 100644 --- a/src/preview/preview_default.rs +++ b/src/preview/preview_default.rs @@ -3,10 +3,16 @@ use std::path; use crate::context::AppContext; use crate::fs::JoshutoMetadata; use crate::preview::{preview_dir, preview_file}; +use crate::ui::AppBackend; use super::preview_file::PreviewFileState; -pub fn load_preview_path(context: &mut AppContext, p: path::PathBuf, metadata: JoshutoMetadata) { +pub fn load_preview_path( + context: &mut AppContext, + backend: &mut AppBackend, + p: path::PathBuf, + metadata: JoshutoMetadata, +) { let preview_options = context.config_ref().preview_options_ref(); if metadata.is_dir() { let tab = context.tab_context_ref().curr_tab_ref(); @@ -38,13 +44,13 @@ pub fn load_preview_path(context: &mut AppContext, p: path::PathBuf, metadata: J .unwrap_or(true); if need_to_load { - preview_file::Background::preview_path_with_script(context, p); + preview_file::Background::preview_path_with_script(context, backend, p); } } else { } } -pub fn load_preview(context: &mut AppContext) { +pub fn load_preview(context: &mut AppContext, backend: &mut AppBackend) { let mut load_list = Vec::with_capacity(2); let curr_tab = context.tab_context_ref().curr_tab_ref(); @@ -63,6 +69,6 @@ pub fn load_preview(context: &mut AppContext) { } for (path, metadata) in load_list { - load_preview_path(context, path, metadata); + load_preview_path(context, backend, path, metadata); } } diff --git a/src/preview/preview_file.rs b/src/preview/preview_file.rs index 25a9580..05f389e 100644 --- a/src/preview/preview_file.rs +++ b/src/preview/preview_file.rs @@ -3,8 +3,11 @@ use std::process::{Command, Output}; use std::thread; use std::time; +use tui::layout::Rect; + use crate::context::AppContext; use crate::event::AppEvent; +use crate::ui::{views, AppBackend}; pub enum PreviewFileState { Loading, @@ -38,64 +41,83 @@ impl std::convert::From<Output> for FilePreview { pub struct Background {} impl Background { - pub fn preview_path_with_script(context: &mut AppContext, path: path::PathBuf) { + pub fn preview_path_with_script( + context: &mut AppContext, + backend: &mut AppBackend, + path: path::PathBuf, + ) { let preview_options = context.config_ref().preview_options_ref(); if let Some(script) = preview_options.preview_script.as_ref() { - let ui_context = context.ui_context_ref(); - if ui_context.layout.is_empty() { - return; - } - let layout_rect = &ui_context.layout[ui_context.layout.len() - 1]; + if let Ok(area) = backend.terminal_ref().size() { + let area = Rect { + y: area.top() + 1, + height: area.height - 2, + ..area + }; - let preview_width = layout_rect.width; - let preview_height = layout_rect.height; - let preview_x_coord = layout_rect.x; - let preview_y_coord = layout_rect.y; + let config = context.config_ref(); + let display_options = config.display_options_ref(); + let constraints = &display_options.default_layout; + let layout = if display_options.show_borders() { + views::calculate_layout_with_borders(area, constraints) + } else { + views::calculate_layout(area, constraints) + }; + let layout_rect = layout[2]; + let preview_width = layout_rect.width; + let preview_height = layout_rect.height; + let preview_x_coord = layout_rect.x; + let preview_y_coord = layout_rect.y; - let image_cache = 0; + if preview_width == 0 || preview_height == 0 { + return; + } - let script = script.clone(); - let event_tx = context.clone_event_tx(); - context - .preview_context_mut() - .previews_mut() - .insert(path.clone(), PreviewFileState::Loading); + let image_cache = 0; - let _ = thread::spawn(move || { - let file_full_path = path.as_path(); + let script = script.clone(); + let event_tx = context.clone_event_tx(); + context + .preview_context_mut() + .previews_mut() + .insert(path.clone(), PreviewFileState::Loading); - let res = Command::new(script) - .arg("--path") - .arg(file_full_path) - .arg("--preview-width") - .arg(preview_width.to_string()) - .arg("--preview-height") - .arg(preview_height.to_string()) - .arg("--x-coord") - .arg(preview_x_coord.to_string()) - .arg("--y-coord") - .arg(preview_y_coord.to_string()) - .arg("--image-cache") - .arg(image_cache.to_string()) - .output(); - match res { - Ok(output) => { - let preview = FilePreview::from(output); - let res = AppEvent::PreviewFile { - path, - res: Box::new(Ok(preview)), - }; - let _ = event_tx.send(res); - } - Err(e) => { - let res = AppEvent::PreviewFile { - path, - res: Box::new(Err(e)), - }; - let _ = event_tx.send(res); + let _ = thread::spawn(move || { + let file_full_path = path.as_path(); + + let res = Command::new(script) + .arg("--path") + .arg(file_full_path) + .arg("--preview-width") + .arg(preview_width.to_string()) + .arg("--preview-height") + .arg(preview_height.to_string()) + .arg("--x-coord") + .arg(preview_x_coord.to_string()) + .arg("--y-coord") + .arg(preview_y_coord.to_string()) + .arg("--image-cache") + .arg(image_cache.to_string()) + .output(); + match res { + Ok(output) => { + let preview = FilePreview::from(output); + let res = AppEvent::PreviewFile { + path, + res: Box::new(Ok(preview)), + }; + let _ = event_tx.send(res); + } + Err(e) => { + let res = AppEvent::PreviewFile { + path, + res: Box::new(Err(e)), + }; + let _ = event_tx.send(res); + } } - } - }); + }); + } } } } @@ -39,7 +39,7 @@ pub fn run_loop( context.tab_context_mut().insert_tab(id, tab); // trigger a preview of child - preview_default::load_preview(context); + preview_default::load_preview(context, backend); } while context.quit == QuitAction::DoNot { @@ -65,7 +65,7 @@ pub fn run_loop( match event { AppEvent::Termion(Event::Mouse(event)) => { process_event::process_mouse(event, context, backend, &keymap_t); - preview_default::load_preview(context); + preview_default::load_preview(context, backend); } AppEvent::Termion(key) => { if context.message_queue_ref().current_message().is_some() { @@ -99,7 +99,7 @@ pub fn run_loop( } }, } - preview_default::load_preview(context); + preview_default::load_preview(context, backend); context.flush_event(); } event => process_event::process_noninteractive(event, context), |