From 7be73364206101c13c15128aa0d66d2b4300788b Mon Sep 17 00:00:00 2001 From: The Mist Date: Wed, 3 Jul 2024 04:35:41 -0400 Subject: [PATCH] [Feat] add `has_background` config to control visibility of background component (#110) * [Feat] add has_background config for control visiblity of background component * [Feat] add default has_background config --- generator/src/components/background.rs | 30 +++++++-- generator/src/components/code_block.rs | 14 +--- generator/src/components/container.rs | 13 +--- .../src/components/editor/mac_title_bar.rs | 18 +++-- .../src/components/interface/component.rs | 35 ++++++++-- generator/src/components/interface/style.rs | 4 +- generator/src/components/watermark.rs | 44 +++++++------ generator/src/config.rs | 1 + generator/src/copy.rs | 2 + generator/src/edges/margin.rs | 2 +- generator/src/edges/padding.rs | 2 +- generator/src/save.rs | 2 + generator/src/snapshot.rs | 66 +++++++++++-------- lua/codesnap/config.lua | 1 + 14 files changed, 140 insertions(+), 94 deletions(-) diff --git a/generator/src/components/background.rs b/generator/src/components/background.rs index 6fa69e2..06dc1c8 100644 --- a/generator/src/components/background.rs +++ b/generator/src/components/background.rs @@ -2,7 +2,10 @@ use tiny_skia::{ Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform, }; -use crate::color::{is_valid_hex_color, RgbaColor}; +use crate::{ + color::{is_valid_hex_color, RgbaColor}, + edges::padding::Padding, +}; use super::interface::{ component::{Component, ComponentContext, RenderParams}, @@ -12,11 +15,15 @@ use super::interface::{ pub struct Background { children: Vec>, + has_background: bool, } impl Background { - pub fn from_children(children: Vec>) -> Background { - Background { children } + pub fn new(has_background: bool, children: Vec>) -> Background { + Background { + children, + has_background, + } } } @@ -26,7 +33,22 @@ impl Component for Background { } fn style(&self) -> RawComponentStyle { - RawComponentStyle::default().align(ComponentAlign::Column) + 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; + } + + fn self_render_condition(&self) -> bool { + self.has_background } fn draw_self( diff --git a/generator/src/components/code_block.rs b/generator/src/components/code_block.rs index d103ba8..41d27be 100644 --- a/generator/src/components/code_block.rs +++ b/generator/src/components/code_block.rs @@ -1,9 +1,4 @@ -use crate::edges::margin::Margin; - -use super::interface::{ - component::Component, - style::{RawComponentStyle, Style}, -}; +use super::interface::component::Component; pub struct CodeBlock { children: Vec>, @@ -13,13 +8,6 @@ impl Component for CodeBlock { fn children(&self) -> &Vec> { &self.children } - - fn style(&self) -> RawComponentStyle { - Style::default().margin(Margin { - top: 10., - ..Margin::default() - }) - } } impl CodeBlock { diff --git a/generator/src/components/container.rs b/generator/src/components/container.rs index cbb5608..afe595d 100644 --- a/generator/src/components/container.rs +++ b/generator/src/components/container.rs @@ -1,11 +1,9 @@ use tiny_skia::Pixmap; -use crate::edges::padding::Padding; - use super::interface::{ component::{Component, ComponentContext, ComponentRenderParams}, render_error::Result, - style::{RawComponentStyle, Style}, + style::Style, }; pub struct Container { @@ -16,15 +14,6 @@ impl Component for Container { fn children(&self) -> &Vec> { &self.children } - - fn style(&self) -> RawComponentStyle { - Style::default().padding(Padding { - top: 82., - left: 122., - right: 122., - bottom: 82., - }) - } } impl Container { diff --git a/generator/src/components/editor/mac_title_bar.rs b/generator/src/components/editor/mac_title_bar.rs index e112ad3..eae4557 100644 --- a/generator/src/components/editor/mac_title_bar.rs +++ b/generator/src/components/editor/mac_title_bar.rs @@ -1,9 +1,12 @@ use tiny_skia::{Color, FillRule, Paint, PathBuilder, Transform}; -use crate::components::interface::{ - component::{Component, ComponentContext, RenderParams}, - render_error, - style::{ComponentStyle, RawComponentStyle, Size, Style}, +use crate::{ + components::interface::{ + component::{Component, ComponentContext, RenderParams}, + render_error, + style::{ComponentStyle, RawComponentStyle, Size, Style}, + }, + edges::margin::Margin, }; pub struct MacTitleBar { @@ -20,7 +23,12 @@ impl Component for MacTitleBar { fn style(&self) -> RawComponentStyle { let demeter = self.radius * 2.; - Style::default().size(Size::Num(demeter + 2. * 25.), Size::Num(demeter)) + Style::default() + .size(Size::Num(demeter + 2. * 25.), Size::Num(demeter)) + .margin(Margin { + bottom: 10., + ..Margin::default() + }) } fn render_condition(&self) -> bool { diff --git a/generator/src/components/interface/component.rs b/generator/src/components/interface/component.rs index 0275748..85c1638 100644 --- a/generator/src/components/interface/component.rs +++ b/generator/src/components/interface/component.rs @@ -60,10 +60,16 @@ pub trait Component { render_params.clone() } + // The render_condition determines whether the component should be rendered or not fn render_condition(&self) -> bool { true } + // The difference with render_condition is that self_render_condition still renders childrens + fn self_render_condition(&self) -> bool { + true + } + fn draw_self( &self, _pixmap: &mut Pixmap, @@ -87,7 +93,19 @@ pub trait Component { } fn parsed_style(&self) -> Style { - let style = self.style(); + // If render_condition return false, the whole component shouldn't rendered, + // includes its children + if !self.render_condition() { + return ComponentStyle::default(); + } + + // If self_render_condition return false, the component shouldn't rendered, + // so the corresponding style should be cleared + let style = if self.self_render_condition() { + self.style() + } else { + RawComponentStyle::default() + }; let (width, height) = self.get_dynamic_wh(); let width = self.parse_size(style.width, width) + style.padding.horizontal() @@ -121,12 +139,19 @@ pub trait Component { let render_params = self.initialize( &component_render_params.parse_into_render_params_with_style( parent_style.clone(), - sibling_style, + sibling_style.clone(), style.clone(), ), ); - self.draw_self(pixmap, context, &render_params, &style, &parent_style)?; + // Render nothing on paint if render_condition return false + if !self.render_condition() { + return Ok(render_params.clone()); + } + + if self.self_render_condition() { + self.draw_self(pixmap, context, &render_params, &style, &parent_style)?; + } let children = self.children(); let mut sibling_render_params = RenderParams { @@ -136,10 +161,6 @@ pub trait Component { let mut sibling_style = ComponentStyle::default(); for child in children { - if !child.render_condition() { - continue; - } - sibling_render_params = child.draw( pixmap, context, diff --git a/generator/src/components/interface/style.rs b/generator/src/components/interface/style.rs index bdc5fec..76ff31f 100644 --- a/generator/src/components/interface/style.rs +++ b/generator/src/components/interface/style.rs @@ -1,6 +1,6 @@ use crate::edges::{margin::Margin, padding::Padding}; -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum ComponentAlign { Row, Column, @@ -11,7 +11,7 @@ pub enum Size { Num(f32), } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Style { pub width: T, pub height: T, diff --git a/generator/src/components/watermark.rs b/generator/src/components/watermark.rs index 7cb6f8a..03500ac 100644 --- a/generator/src/components/watermark.rs +++ b/generator/src/components/watermark.rs @@ -25,28 +25,26 @@ impl Component for Watermark { ) -> render_error::Result<()> { let params = &context.take_snapshot_params; - if ¶ms.watermark != "" { - let attrs = Attrs::new().family(Family::Name( - &context.take_snapshot_params.watermark_font_family, - )); + let attrs = Attrs::new().family(Family::Name( + &context.take_snapshot_params.watermark_font_family, + )); - FontRenderer::new( - 20., - 20., - context.scale_factor, - &context.take_snapshot_params.fonts_folder, - ) - .draw_line( - 0., - render_params.y, - pixmap.width() as f32, - pixmap.height() as f32, - ¶ms.watermark, - attrs, - Some(Align::Center), - pixmap, - ); - } + FontRenderer::new( + 20., + 20., + context.scale_factor, + &context.take_snapshot_params.fonts_folder, + ) + .draw_line( + 0., + render_params.y, + pixmap.width() as f32, + pixmap.height() as f32, + ¶ms.watermark, + attrs, + Some(Align::Center), + pixmap, + ); Ok(()) } @@ -55,6 +53,10 @@ impl Component for Watermark { &self.children } + fn render_condition(&self) -> bool { + self.value != "" + } + fn style(&self) -> RawComponentStyle { let default_style = RawComponentStyle::default(); diff --git a/generator/src/config.rs b/generator/src/config.rs index 8b6158e..56d48e6 100644 --- a/generator/src/config.rs +++ b/generator/src/config.rs @@ -30,6 +30,7 @@ pub struct TakeSnapshotParams { pub highlight_start_line_number: Option, pub highlight_end_line_number: Option, pub min_width: Option, + pub has_background: bool, } impl FromObject for TakeSnapshotParams { diff --git a/generator/src/copy.rs b/generator/src/copy.rs index 6f5a388..e98b039 100644 --- a/generator/src/copy.rs +++ b/generator/src/copy.rs @@ -5,6 +5,8 @@ use arboard::{Clipboard, ImageData}; use nvim_oxi::Result; +// The function will be called as FFI on Lua side +#[allow(dead_code)] pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> { let pixmap = take_snapshot(config.clone())?; let premultplied_colors = pixmap.pixels(); diff --git a/generator/src/edges/margin.rs b/generator/src/edges/margin.rs index d428666..305ef65 100644 --- a/generator/src/edges/margin.rs +++ b/generator/src/edges/margin.rs @@ -1,6 +1,6 @@ use super::edge::Edge; -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] pub struct Margin { pub left: f32, pub right: f32, diff --git a/generator/src/edges/padding.rs b/generator/src/edges/padding.rs index e64254b..94c135f 100644 --- a/generator/src/edges/padding.rs +++ b/generator/src/edges/padding.rs @@ -1,6 +1,6 @@ use super::edge::Edge; -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] pub struct Padding { pub left: f32, pub right: f32, diff --git a/generator/src/save.rs b/generator/src/save.rs index 9ccabdc..a455c6b 100644 --- a/generator/src/save.rs +++ b/generator/src/save.rs @@ -1,6 +1,8 @@ use crate::{config::TakeSnapshotParams, path::parse_save_path, snapshot::take_snapshot}; use nvim_oxi::{lua::Error::RuntimeError, Error, Result}; +// The function will be called as FFI on Lua side +#[allow(dead_code)] pub fn save_snapshot(config: TakeSnapshotParams) -> Result<()> { match &config.save_path { Some(path) => { diff --git a/generator/src/snapshot.rs b/generator/src/snapshot.rs index cbf14bc..50d2fef 100644 --- a/generator/src/snapshot.rs +++ b/generator/src/snapshot.rs @@ -26,35 +26,45 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result scale_factor: SCALE_FACTOR, take_snapshot_params: Arc::new(params.clone()), }; - let pixmap = Container::from_children(vec![Box::new(Background::from_children(vec![ - Box::new(Rect::new( - 16., - params.min_width, - vec![ - Box::new(MacTitleBar::from_radius(8., params.mac_window_bar)), - Box::new(Breadcrumbs::from_path( - params.file_path, - 15., - params.breadcrumbs_separator, - params.has_breadcrumbs, - )), - Box::new(CodeBlock::from_children(vec![ - Box::new(HighlightCodeBlock::from_line_number( - params.highlight_start_line_number, - params.highlight_end_line_number, - LINE_HEIGHT, + // If background is disabled, should hidden watermark component + // If watermark text is equal to "", the watermark component is hidden + let watermark = if params.has_background { + params.watermark + } else { + "".to_string() + }; + let pixmap = Container::from_children(vec![Box::new(Background::new( + params.has_background, + vec![ + Box::new(Rect::new( + 16., + params.min_width, + vec![ + Box::new(MacTitleBar::from_radius(8., params.mac_window_bar)), + Box::new(Breadcrumbs::from_path( + params.file_path, + 15., + params.breadcrumbs_separator, + params.has_breadcrumbs, )), - Box::new(LineNumber::new( - ¶ms.code, - params.start_line_number, - LINE_HEIGHT, - )), - Box::new(Code::new(params.code, LINE_HEIGHT, 15.)), - ])), - ], - )), - Box::new(Watermark::new(params.watermark)), - ]))]) + Box::new(CodeBlock::from_children(vec![ + Box::new(HighlightCodeBlock::from_line_number( + params.highlight_start_line_number, + params.highlight_end_line_number, + LINE_HEIGHT, + )), + Box::new(LineNumber::new( + ¶ms.code, + params.start_line_number, + LINE_HEIGHT, + )), + Box::new(Code::new(params.code, LINE_HEIGHT, 15.)), + ])), + ], + )), + Box::new(Watermark::new(watermark)), + ], + ))]) .draw_root(&context)?; Ok(pixmap) diff --git a/lua/codesnap/config.lua b/lua/codesnap/config.lua index a91121b..da32e37 100644 --- a/lua/codesnap/config.lua +++ b/lua/codesnap/config.lua @@ -49,6 +49,7 @@ 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)