Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
```

Expand Down
46 changes: 27 additions & 19 deletions generator/src/components/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -15,16 +15,33 @@ 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.;
}
}

impl Component for Background {
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion generator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions generator/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,29 @@ 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> {
let context = ComponentContext {
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.,
Expand Down
1 change: 0 additions & 1 deletion lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lua/codesnap/static.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading