This commit is contained in:
sxyazi 2023-10-23 13:13:03 +08:00
parent 45081eeaec
commit 03bc86f46f
No known key found for this signature in database
3 changed files with 25 additions and 29 deletions

View file

@ -1,4 +1,4 @@
use std::{cmp::max, path::PathBuf, process::Stdio}; use std::{path::PathBuf, process::Stdio};
use anyhow::Result; use anyhow::Result;
use ratatui::prelude::Rect; use ratatui::prelude::Rect;
@ -33,17 +33,6 @@ impl Ueberzug {
Ok(tx) Ok(tx)
} }
fn adjust_rect(mut rect: Rect) -> Rect {
let cfg = &PREVIEW.ueberzug;
rect.x = max((rect.x as f64 / cfg.scale_down_factor + cfg.x_offset) as i32, 0) as u16;
rect.y = max((rect.y as f64 / cfg.scale_down_factor + cfg.y_offset) as i32, 0) as u16;
rect.width =
max((rect.width as f64 / cfg.scale_down_factor + cfg.width_offset) as i32, 0) as u16;
rect.height =
max((rect.height as f64 / cfg.scale_down_factor + cfg.height_offset) as i32, 0) as u16;
return rect;
}
fn create_demon(adaptor: Adaptor) -> Result<Child> { fn create_demon(adaptor: Adaptor) -> Result<Child> {
Ok( Ok(
Command::new("ueberzug") Command::new("ueberzug")
@ -55,12 +44,24 @@ impl Ueberzug {
) )
} }
fn adjust_rect(mut rect: Rect) -> Rect {
let scale = PREVIEW.ueberzug_scale;
let (x, y, w, h) = PREVIEW.ueberzug_offset;
rect.x = 0f64.max(rect.x as f64 * scale + x) as u16;
rect.y = 0f64.max(rect.y as f64 * scale + y) as u16;
rect.width = 0f64.max(rect.width as f64 * scale + w) as u16;
rect.height = 0f64.max(rect.height as f64 * scale + h) as u16;
rect
}
async fn send_command(child: &mut Child, cmd: Option<(PathBuf, Rect)>) -> Result<()> { async fn send_command(child: &mut Child, cmd: Option<(PathBuf, Rect)>) -> Result<()> {
let stdin = child.stdin.as_mut().unwrap(); let stdin = child.stdin.as_mut().unwrap();
if let Some((path, tmp_rect)) = cmd { if let Some((path, tmp_rect)) = cmd {
debug!("ueberzug rect before adjustment: {:?}", tmp_rect); debug!("ueberzug rect before adjustment: {:?}", tmp_rect);
let rect = Self::adjust_rect(tmp_rect); let rect = Self::adjust_rect(tmp_rect);
debug!("ueberzug rect after adjustment: {:?}", rect); debug!("ueberzug rect after adjustment: {:?}", rect);
let s = format!( let s = format!(
r#"{{"action":"add","identifier":"yazi","x":{},"y":{},"max_width":{},"max_height":{},"path":"{}"}}{}"#, r#"{{"action":"add","identifier":"yazi","x":{},"y":{},"max_width":{},"max_height":{},"path":"{}"}}{}"#,
rect.x, rect.x,

View file

@ -9,20 +9,12 @@ show_hidden = false
show_symlink = true show_symlink = true
[preview] [preview]
tab_size = 2 tab_size = 2
max_width = 600 max_width = 600
max_height = 900 max_height = 900
cache_dir = "" cache_dir = ""
ueberzug_scale = 1
[preview.ueberzug] ueberzug_offset = [ 0, 0, 0, 0 ]
# Scale down the coordinate and size of image preview that is incorrectly scaled
# up by ueberzugpp in Wayland (tested under Hyprland). Example usage: if your
# monitor has a 2.0 scale factor, you should pass 2.0 here.
scale_down_factor = 1.0
x_offset = 0.0
y_offset = 0.0
width_offset = 0.0
height_offset = 0.0
[opener] [opener]
edit = [ edit = [

View file

@ -23,7 +23,8 @@ pub struct Preview {
pub cache_dir: PathBuf, pub cache_dir: PathBuf,
pub ueberzug: UeberzugPreview, pub ueberzug_scale: f64,
pub ueberzug_offset: (f64, f64, f64, f64),
} }
impl Default for Preview { impl Default for Preview {
@ -40,7 +41,8 @@ impl Default for Preview {
cache_dir: Option<String>, cache_dir: Option<String>,
ueberzug: UeberzugPreview, ueberzug_scale: f64,
ueberzug_offset: (f64, f64, f64, f64),
} }
let preview = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().preview; let preview = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().preview;
@ -55,7 +57,8 @@ impl Default for Preview {
cache_dir, cache_dir,
ueberzug: preview.ueberzug, ueberzug_scale: preview.ueberzug_scale,
ueberzug_offset: preview.ueberzug_offset,
} }
} }
} }