diff --git a/yazi-adaptor/Cargo.toml b/yazi-adaptor/Cargo.toml index 5e6b9c28..3058d196 100644 --- a/yazi-adaptor/Cargo.toml +++ b/yazi-adaptor/Cargo.toml @@ -21,4 +21,4 @@ ratatui = "^0" tokio = { version = "^1", features = [ "parking_lot", "io-util", "process" ] } # Logging -tracing = "^0" +tracing = {version = "^0", features = ["max_level_debug", "release_max_level_warn"]} diff --git a/yazi-adaptor/src/ueberzug.rs b/yazi-adaptor/src/ueberzug.rs index 27d891b6..ee092e8f 100644 --- a/yazi-adaptor/src/ueberzug.rs +++ b/yazi-adaptor/src/ueberzug.rs @@ -1,8 +1,15 @@ +use std::cmp::max; use std::{path::PathBuf, process::Stdio}; +use tracing::debug; use anyhow::Result; use ratatui::prelude::Rect; -use tokio::{io::AsyncWriteExt, process::{Child, Command}, sync::mpsc::{self, UnboundedSender}}; +use tokio::{ + io::AsyncWriteExt, + process::{Child, Command}, + sync::mpsc::{self, UnboundedSender}, +}; +use yazi_config::PREVIEW; use crate::Adaptor; @@ -31,6 +38,19 @@ impl Ueberzug { 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 { Ok( Command::new("ueberzug") @@ -44,7 +64,10 @@ impl Ueberzug { async fn send_command(child: &mut Child, cmd: Option<(PathBuf, Rect)>) -> Result<()> { let stdin = child.stdin.as_mut().unwrap(); - if let Some((path, rect)) = cmd { + if let Some((path, tmp_rect)) = cmd { + debug!("ueberzug rect before adjustment: {:?}", tmp_rect); + let rect = Self::adjust_rect(tmp_rect); + debug!("ueberzug rect after adjust adjustment: {:?}", rect); let s = format!( r#"{{"action":"add","identifier":"yazi","x":{},"y":{},"max_width":{},"max_height":{},"path":"{}"}}{}"#, rect.x, diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 0fa22775..cab1b80b 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -14,6 +14,16 @@ max_width = 600 max_height = 900 cache_dir = "" +[preview.ueberzug] +# 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] edit = [ { exec = '$EDITOR "$@"', block = true, for = "unix" }, diff --git a/yazi-config/src/preview/preview.rs b/yazi-config/src/preview/preview.rs index 4484f1b0..4cb00a3a 100644 --- a/yazi-config/src/preview/preview.rs +++ b/yazi-config/src/preview/preview.rs @@ -1,4 +1,7 @@ -use std::{path::{Path, PathBuf}, time::{self, SystemTime}}; +use std::{ + path::{Path, PathBuf}, + time::{self, SystemTime}, +}; use md5::{Digest, Md5}; use serde::Deserialize; @@ -6,13 +9,24 @@ use yazi_shared::expand_path; use crate::{xdg::Xdg, MERGED_YAZI}; +#[derive(Debug, Deserialize)] +pub struct UeberzugPreview { + pub scale_down_factor: f64, + pub x_offset: f64, + pub y_offset: f64, + pub width_offset: f64, + pub height_offset: f64, +} + #[derive(Debug)] pub struct Preview { - pub tab_size: u32, - pub max_width: u32, + pub tab_size: u32, + pub max_width: u32, pub max_height: u32, pub cache_dir: PathBuf, + + pub ueberzug: UeberzugPreview, } impl Default for Preview { @@ -23,11 +37,13 @@ impl Default for Preview { } #[derive(Deserialize)] struct Shadow { - tab_size: u32, - max_width: u32, + tab_size: u32, + max_width: u32, max_height: u32, cache_dir: Option, + + ueberzug: UeberzugPreview, } let preview = toml::from_str::(&MERGED_YAZI).unwrap().preview; @@ -41,6 +57,8 @@ impl Default for Preview { max_height: preview.max_height, cache_dir, + + ueberzug: preview.ueberzug, } } }