From fa692278977d6c71b45c109eac3ade1e273eeb0e Mon Sep 17 00:00:00 2001 From: Carlos de Paula Date: Wed, 12 Nov 2025 17:36:22 -0300 Subject: [PATCH] Add background rendering functions for panes and overall area --- yazi-fm/src/bg_render.rs | 43 +++++++++++++++++++++++++++++++++++ yazi-fm/src/main.rs | 2 +- yazi-fm/src/root.rs | 49 ++++------------------------------------ 3 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 yazi-fm/src/bg_render.rs diff --git a/yazi-fm/src/bg_render.rs b/yazi-fm/src/bg_render.rs new file mode 100644 index 00000000..8e02392d --- /dev/null +++ b/yazi-fm/src/bg_render.rs @@ -0,0 +1,43 @@ +use ratatui::{buffer::Buffer, layout::Rect, style::Color}; + +/// Apply background color to a pane, skipping borders on all sides +#[inline] +pub fn apply_pane_bg_with_borders(buf: &mut Buffer, pane: Rect, bg_color: Color) { + let start_y = pane.top() + 1; + let end_y = if pane.bottom() > 0 { pane.bottom() - 1 } else { pane.bottom() }; + let start_x = pane.left() + 1; + let end_x = if pane.right() > 0 { pane.right() - 1 } else { pane.right() }; + + if start_y < end_y && start_x < end_x { + for y in start_y..end_y { + for x in start_x..end_x { + buf[(x, y)].set_bg(bg_color); + } + } + } +} + +/// Apply background color to a pane, skipping only top/bottom borders +#[inline] +pub fn apply_pane_bg_no_vertical_borders(buf: &mut Buffer, pane: Rect, bg_color: Color) { + let start_y = pane.top() + 1; + let end_y = if pane.bottom() > 0 { pane.bottom() - 1 } else { pane.bottom() }; + + if start_y < end_y { + for y in start_y..end_y { + for x in pane.left()..pane.right() { + buf[(x, y)].set_bg(bg_color); + } + } + } +} + +/// Apply background color to entire area +#[inline] +pub fn apply_overall_bg(buf: &mut Buffer, area: Rect, bg_color: Color) { + for y in area.top()..area.bottom() { + for x in area.left()..area.right() { + buf[(x, y)].set_bg(bg_color); + } + } +} diff --git a/yazi-fm/src/main.rs b/yazi-fm/src/main.rs index 64ae41e5..de4ddaa4 100644 --- a/yazi-fm/src/main.rs +++ b/yazi-fm/src/main.rs @@ -6,7 +6,7 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; yazi_macro::mod_pub!(app cmp confirm help input mgr notify pick spot tasks which); -yazi_macro::mod_flat!(dispatcher executor logs panic root router signals term); +yazi_macro::mod_flat!(bg_render dispatcher executor logs panic root router signals term); #[tokio::main] async fn main() -> anyhow::Result<()> { diff --git a/yazi-fm/src/root.rs b/yazi-fm/src/root.rs index c2d08189..9f30d8ce 100644 --- a/yazi-fm/src/root.rs +++ b/yazi-fm/src/root.rs @@ -6,7 +6,7 @@ use yazi_config::{THEME, YAZI}; use yazi_core::Core; use yazi_plugin::LUA; -use super::{cmp, confirm, help, input, mgr, pick, spot, tasks, which}; +use super::{bg_render::*, cmp, confirm, help, input, mgr, pick, spot, tasks, which}; pub(super) struct Root<'a> { core: &'a Core, @@ -101,55 +101,21 @@ impl Widget for Root<'_> { let parent_bg = THEME.app.parent_bg(); if !parent_bg.is_empty() { if let Ok(bg_color) = parent_bg.parse::() { - let pane = chunks[0]; - // Skip first and last row, leftmost and rightmost columns - let start_y = pane.top() + 1; - let end_y = if pane.bottom() > 0 { pane.bottom() - 1 } else { pane.bottom() }; - let start_x = pane.left() + 1; - let end_x = if pane.right() > 0 { pane.right() - 1 } else { pane.right() }; - if start_y < end_y && start_x < end_x { - for y in start_y..end_y { - for x in start_x..end_x { - buf[(x, y)].set_bg(bg_color); - } - } - } + apply_pane_bg_with_borders(buf, chunks[0], bg_color); } } let current_bg = THEME.app.current_bg(); if !current_bg.is_empty() { if let Ok(bg_color) = current_bg.parse::() { - let pane = chunks[1]; - // Skip first and last row (no vertical borders for current pane) - let start_y = pane.top() + 1; - let end_y = if pane.bottom() > 0 { pane.bottom() - 1 } else { pane.bottom() }; - if start_y < end_y { - for y in start_y..end_y { - for x in pane.left()..pane.right() { - buf[(x, y)].set_bg(bg_color); - } - } - } + apply_pane_bg_no_vertical_borders(buf, chunks[1], bg_color); } } let preview_bg = THEME.app.preview_bg(); if !preview_bg.is_empty() { if let Ok(bg_color) = preview_bg.parse::() { - let pane = chunks[2]; - // Skip first and last row, leftmost and rightmost columns - let start_y = pane.top() + 1; - let end_y = if pane.bottom() > 0 { pane.bottom() - 1 } else { pane.bottom() }; - let start_x = pane.left() + 1; - let end_x = if pane.right() > 0 { pane.right() - 1 } else { pane.right() }; - if start_y < end_y && start_x < end_x { - for y in start_y..end_y { - for x in start_x..end_x { - buf[(x, y)].set_bg(bg_color); - } - } - } + apply_pane_bg_with_borders(buf, chunks[2], bg_color); } } @@ -158,12 +124,7 @@ impl Widget for Root<'_> { let bg_color_str = THEME.app.bg_color(); if !bg_color_str.is_empty() { if let Ok(bg_color) = bg_color_str.parse::() { - // When using app-wide background, fill everything including borders - for y in area.top()..area.bottom() { - for x in area.left()..area.right() { - buf[(x, y)].set_bg(bg_color); - } - } + apply_overall_bg(buf, area, bg_color); } } }