Skip to content

Commit cb3d1a5

Browse files
authored
[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
1 parent 08d7588 commit cb3d1a5

File tree

6 files changed

+62
-25
lines changed

6 files changed

+62
-25
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,23 @@ require("codesnap").setup({
324324

325325
![CodeSnap](https://github.com/mistricky/codesnap.nvim/assets/22574136/a600c2e4-4c60-4ec0-b2fc-3b41481048dc)
326326

327+
### Customize background padding
328+
CodeSnap allows you to customize the padding of background using `bg_x_padding`, `bg_y_padding` and `bg_padding`, the default value is:
329+
```lua
330+
require("codesnap").setup({
331+
bg_x_padding = 122,
332+
bg_y_padding = 82,
333+
bg_padding = null
334+
})
335+
```
327336

337+
If you want to hide background, you can set `bg_padding` to `0` in your config:
338+
```lua
339+
require("codesnap").setup({
340+
-- ...
341+
bg_padding = 0
342+
})
343+
```
328344

329345
## Watermark
330346
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:
370386
breadcrumbs_separator = "/",
371387
has_breadcrumbs = false,
372388
has_line_number = false,
373-
min_width = 0
389+
show_workspace = false,
390+
min_width = 0,
391+
bg_x_padding = 122,
392+
bg_y_padding = 82,
374393
}
375394
```
376395

generator/src/components/background.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tiny_skia::{
44

55
use crate::{
66
color::{is_valid_hex_color, RgbaColor},
7-
edges::padding::Padding,
7+
edges::{edge::Edge, padding::Padding},
88
};
99

1010
use super::interface::{
@@ -15,16 +15,33 @@ use super::interface::{
1515

1616
pub struct Background {
1717
children: Vec<Box<dyn Component>>,
18-
has_background: bool,
18+
padding: Padding,
1919
}
2020

2121
impl Background {
22-
pub fn new(has_background: bool, children: Vec<Box<dyn Component>>) -> Background {
23-
Background {
24-
children,
25-
has_background,
22+
pub fn new(padding: Padding, children: Vec<Box<dyn Component>>) -> Background {
23+
Background { children, padding }
24+
}
25+
26+
pub fn parse_background_padding(
27+
horizontal_background_padding: f32,
28+
vertical_background_padding: f32,
29+
background_padding: Option<f32>,
30+
) -> Padding {
31+
match background_padding {
32+
Some(padding) => Padding::from_value(padding),
33+
None => Padding {
34+
top: vertical_background_padding,
35+
bottom: vertical_background_padding,
36+
left: horizontal_background_padding,
37+
right: horizontal_background_padding,
38+
},
2639
}
2740
}
41+
42+
pub fn has_background(padding: &Padding) -> bool {
43+
return padding.horizontal() != 0. || padding.vertical() != 0.;
44+
}
2845
}
2946

3047
impl Component for Background {
@@ -33,22 +50,13 @@ impl Component for Background {
3350
}
3451

3552
fn style(&self) -> RawComponentStyle {
36-
let style = RawComponentStyle::default().align(ComponentAlign::Column);
37-
38-
if self.has_background {
39-
return style.padding(Padding {
40-
top: 82.,
41-
left: 122.,
42-
right: 122.,
43-
bottom: 82.,
44-
});
45-
}
46-
47-
return style;
53+
RawComponentStyle::default()
54+
.align(ComponentAlign::Column)
55+
.padding(self.padding.clone())
4856
}
4957

5058
fn self_render_condition(&self) -> bool {
51-
self.has_background
59+
Self::has_background(&self.padding)
5260
}
5361

5462
fn draw_self(

generator/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ pub struct TakeSnapshotParams {
3030
pub highlight_start_line_number: Option<usize>,
3131
pub highlight_end_line_number: Option<usize>,
3232
pub min_width: Option<f32>,
33-
pub has_background: bool,
33+
pub bg_x_padding: f32,
34+
pub bg_y_padding: f32,
35+
pub bg_padding: Option<f32>,
3436
}
3537

3638
impl FromObject for TakeSnapshotParams {

generator/src/snapshot.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@ use crate::config::TakeSnapshotParams;
1919
// Scale the screenshot to 3 times its size
2020
const SCALE_FACTOR: f32 = 3.;
2121
const LINE_HEIGHT: f32 = 20.;
22+
const VIEW_WATERMARK_PADDING: f32 = 82.;
2223

2324
// The params is come from neovim instance
2425
pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap> {
2526
let context = ComponentContext {
2627
scale_factor: SCALE_FACTOR,
2728
take_snapshot_params: Arc::new(params.clone()),
2829
};
29-
// If background is disabled, should hidden watermark component
30+
let background_padding = Background::parse_background_padding(
31+
params.bg_x_padding,
32+
params.bg_y_padding,
33+
params.bg_padding,
34+
);
35+
36+
// If vertical background padding is less than 82., should hidden watermark component
3037
// If watermark text is equal to "", the watermark component is hidden
31-
let watermark = if params.has_background {
38+
let watermark = if background_padding.bottom >= VIEW_WATERMARK_PADDING {
3239
params.watermark
3340
} else {
3441
"".to_string()
3542
};
3643
let pixmap = Container::from_children(vec![Box::new(Background::new(
37-
params.has_background,
44+
background_padding,
3845
vec![
3946
Box::new(Rect::new(
4047
16.,

lua/codesnap/config.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function config_module.get_config(extension)
4949
theme = "base16-onedark",
5050
file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "",
5151
start_line_number = static.config.has_line_number and start_line_number or nil,
52-
has_background = true,
5352
}, static.config)
5453

5554
config.save_path = parse_save_path(config.save_path)

lua/codesnap/static.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ return {
1313
has_line_number = false,
1414
show_workspace = false,
1515
min_width = 0,
16+
bg_x_padding = 122,
17+
bg_y_padding = 82,
1618
},
1719
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
1820
preview_switch = true,

0 commit comments

Comments
 (0)