[Feat] add bg_x_padding, bg_y_padding and bg_padding to allows users can customize background padding of snapshot (#112)

* [Feat] add config allow users can customize background padding

* [Update] rename config items which related background config

* [Update] add config to allow users customize the padding of background
This commit is contained in:
The Mist 2024-07-04 01:50:06 -04:00 committed by GitHub
parent 08d7588ea0
commit cb3d1a536e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 62 additions and 25 deletions

View file

@ -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,
}
```

View file

@ -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,15 +15,32 @@ use super::interface::{
pub struct Background {
children: Vec<Box<dyn Component>>,
has_background: bool,
padding: Padding,
}
impl Background {
pub fn new(has_background: bool, children: Vec<Box<dyn Component>>) -> Background {
Background {
children,
has_background,
pub fn new(padding: Padding, children: Vec<Box<dyn Component>>) -> Background {
Background { children, padding }
}
pub fn parse_background_padding(
horizontal_background_padding: f32,
vertical_background_padding: f32,
background_padding: Option<f32>,
) -> 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.;
}
}
@ -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(

View file

@ -30,7 +30,9 @@ pub struct TakeSnapshotParams {
pub highlight_start_line_number: Option<usize>,
pub highlight_end_line_number: Option<usize>,
pub min_width: Option<f32>,
pub has_background: bool,
pub bg_x_padding: f32,
pub bg_y_padding: f32,
pub bg_padding: Option<f32>,
}
impl FromObject for TakeSnapshotParams {

View file

@ -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<Pixmap> {
@ -26,15 +27,21 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap>
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.,

View file

@ -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)

View file

@ -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,