diff --git a/README.md b/README.md index d565a30..5dd8dc6 100644 --- a/README.md +++ b/README.md @@ -324,7 +324,23 @@ require("codesnap").setup({ ![CodeSnap](https://github.com/mistricky/codesnap.nvim/assets/22574136/a600c2e4-4c60-4ec0-b2fc-3b41481048dc) +### Customize background padding +CodeSnap allows you to customize the padding of background using `bg_x_padding`, `bg_y_padding` and `bg_padding`, the default value is: +```lua +require("codesnap").setup({ + bg_x_padding = 122, + bg_y_padding = 82, + bg_padding = null +}) +``` +If you want to hide background, you can set `bg_padding` to `0` in your config: +```lua +require("codesnap").setup({ + -- ... + bg_padding = 0 +}) +``` ## Watermark Watermark is something that makes screenshots more personalized, but if you don't like watermark just set it as an empty string to hide it. @@ -370,7 +386,10 @@ There is a default config: breadcrumbs_separator = "/", has_breadcrumbs = false, has_line_number = false, - min_width = 0 + show_workspace = false, + min_width = 0, + bg_x_padding = 122, + bg_y_padding = 82, } ``` diff --git a/generator/src/components/background.rs b/generator/src/components/background.rs index 06dc1c8..a0b1822 100644 --- a/generator/src/components/background.rs +++ b/generator/src/components/background.rs @@ -4,7 +4,7 @@ use tiny_skia::{ use crate::{ color::{is_valid_hex_color, RgbaColor}, - edges::padding::Padding, + edges::{edge::Edge, padding::Padding}, }; use super::interface::{ @@ -15,16 +15,33 @@ use super::interface::{ pub struct Background { children: Vec>, - has_background: bool, + padding: Padding, } impl Background { - pub fn new(has_background: bool, children: Vec>) -> Background { - Background { - children, - has_background, + pub fn new(padding: Padding, children: Vec>) -> Background { + Background { children, padding } + } + + pub fn parse_background_padding( + horizontal_background_padding: f32, + vertical_background_padding: f32, + background_padding: Option, + ) -> Padding { + match background_padding { + Some(padding) => Padding::from_value(padding), + None => Padding { + top: vertical_background_padding, + bottom: vertical_background_padding, + left: horizontal_background_padding, + right: horizontal_background_padding, + }, } } + + pub fn has_background(padding: &Padding) -> bool { + return padding.horizontal() != 0. || padding.vertical() != 0.; + } } impl Component for Background { @@ -33,22 +50,13 @@ impl Component for Background { } fn style(&self) -> RawComponentStyle { - let style = RawComponentStyle::default().align(ComponentAlign::Column); - - if self.has_background { - return style.padding(Padding { - top: 82., - left: 122., - right: 122., - bottom: 82., - }); - } - - return style; + RawComponentStyle::default() + .align(ComponentAlign::Column) + .padding(self.padding.clone()) } fn self_render_condition(&self) -> bool { - self.has_background + Self::has_background(&self.padding) } fn draw_self( diff --git a/generator/src/config.rs b/generator/src/config.rs index 56d48e6..1e347d0 100644 --- a/generator/src/config.rs +++ b/generator/src/config.rs @@ -30,7 +30,9 @@ pub struct TakeSnapshotParams { pub highlight_start_line_number: Option, pub highlight_end_line_number: Option, pub min_width: Option, - pub has_background: bool, + pub bg_x_padding: f32, + pub bg_y_padding: f32, + pub bg_padding: Option, } impl FromObject for TakeSnapshotParams { diff --git a/generator/src/snapshot.rs b/generator/src/snapshot.rs index 50d2fef..04402d5 100644 --- a/generator/src/snapshot.rs +++ b/generator/src/snapshot.rs @@ -19,6 +19,7 @@ use crate::config::TakeSnapshotParams; // Scale the screenshot to 3 times its size const SCALE_FACTOR: f32 = 3.; const LINE_HEIGHT: f32 = 20.; +const VIEW_WATERMARK_PADDING: f32 = 82.; // The params is come from neovim instance pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result { @@ -26,15 +27,21 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result scale_factor: SCALE_FACTOR, take_snapshot_params: Arc::new(params.clone()), }; - // If background is disabled, should hidden watermark component + let background_padding = Background::parse_background_padding( + params.bg_x_padding, + params.bg_y_padding, + params.bg_padding, + ); + + // If vertical background padding is less than 82., should hidden watermark component // If watermark text is equal to "", the watermark component is hidden - let watermark = if params.has_background { + let watermark = if background_padding.bottom >= VIEW_WATERMARK_PADDING { params.watermark } else { "".to_string() }; let pixmap = Container::from_children(vec![Box::new(Background::new( - params.has_background, + background_padding, vec![ Box::new(Rect::new( 16., diff --git a/lua/codesnap/config.lua b/lua/codesnap/config.lua index da32e37..a91121b 100644 --- a/lua/codesnap/config.lua +++ b/lua/codesnap/config.lua @@ -49,7 +49,6 @@ function config_module.get_config(extension) theme = "base16-onedark", file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "", start_line_number = static.config.has_line_number and start_line_number or nil, - has_background = true, }, static.config) config.save_path = parse_save_path(config.save_path) diff --git a/lua/codesnap/static.lua b/lua/codesnap/static.lua index 4ae9869..bd86374 100644 --- a/lua/codesnap/static.lua +++ b/lua/codesnap/static.lua @@ -13,6 +13,8 @@ return { has_line_number = false, show_workspace = false, min_width = 0, + bg_x_padding = 122, + bg_y_padding = 82, }, cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), preview_switch = true,