From 19dd1d481722234cbdeee7ae06e57987e28ac075 Mon Sep 17 00:00:00 2001 From: Mist Date: Tue, 7 May 2024 16:35:21 +0800 Subject: [PATCH] [Feat] supports for allows users can custom min_width of snapshots --- generator/src/components/interface/component.rs | 12 +++++++++--- generator/src/components/interface/style.rs | 9 +++++++++ generator/src/components/rect.rs | 10 ++++++++-- generator/src/config.rs | 1 + generator/src/snapshot.rs | 1 + lua/codesnap/static.lua | 1 + 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/generator/src/components/interface/component.rs b/generator/src/components/interface/component.rs index 3e64cf5..0275748 100644 --- a/generator/src/components/interface/component.rs +++ b/generator/src/components/interface/component.rs @@ -89,11 +89,17 @@ pub trait Component { fn parsed_style(&self) -> Style { let style = self.style(); let (width, height) = self.get_dynamic_wh(); + let width = self.parse_size(style.width, width) + + style.padding.horizontal() + + style.margin.horizontal(); Style { - width: self.parse_size(style.width, width) - + style.padding.horizontal() - + style.margin.horizontal(), + min_width: style.min_width, + width: if width > style.min_width { + width + } else { + style.min_width + }, height: self.parse_size(style.height, height) + style.padding.vertical() + style.margin.vertical(), diff --git a/generator/src/components/interface/style.rs b/generator/src/components/interface/style.rs index 94a6f09..bdc5fec 100644 --- a/generator/src/components/interface/style.rs +++ b/generator/src/components/interface/style.rs @@ -15,6 +15,7 @@ pub enum Size { pub struct Style { pub width: T, pub height: T, + pub min_width: f32, pub align: ComponentAlign, pub padding: Padding, pub margin: Margin, @@ -26,6 +27,7 @@ pub type ComponentStyle = Style; impl Default for RawComponentStyle { fn default() -> Self { Style { + min_width: 0., width: Size::Dynamic, height: Size::Dynamic, align: ComponentAlign::Row, @@ -38,6 +40,7 @@ impl Default for RawComponentStyle { impl Default for ComponentStyle { fn default() -> Self { Style { + min_width: 0., width: 0., height: 0., align: ComponentAlign::Row, @@ -54,6 +57,12 @@ impl RawComponentStyle { self } + // Only works if the width is calculate dynamically + pub fn min_width(mut self, min_width: f32) -> Self { + self.min_width = min_width; + self + } + pub fn align(mut self, align: ComponentAlign) -> Self { self.align = align; self diff --git a/generator/src/components/rect.rs b/generator/src/components/rect.rs index 6289b77..dd97b2d 100644 --- a/generator/src/components/rect.rs +++ b/generator/src/components/rect.rs @@ -10,6 +10,7 @@ pub const EDITOR_PADDING: f32 = 20.; pub struct Rect { radius: f32, + min_width: f32, children: Vec>, } @@ -20,6 +21,7 @@ impl Component for Rect { fn style(&self) -> RawComponentStyle { Style::default() + .min_width(self.min_width) .align(ComponentAlign::Column) .padding(Padding::from_value(EDITOR_PADDING)) } @@ -94,7 +96,11 @@ impl Component for Rect { } impl Rect { - pub fn new(radius: f32, children: Vec>) -> Rect { - Rect { radius, children } + pub fn new(radius: f32, min_width: Option, children: Vec>) -> Rect { + Rect { + radius, + children, + min_width: min_width.unwrap_or(0.), + } } } diff --git a/generator/src/config.rs b/generator/src/config.rs index 14fd02d..8b6158e 100644 --- a/generator/src/config.rs +++ b/generator/src/config.rs @@ -29,6 +29,7 @@ pub struct TakeSnapshotParams { pub start_line_number: Option, pub highlight_start_line_number: Option, pub highlight_end_line_number: Option, + pub min_width: Option, } impl FromObject for TakeSnapshotParams { diff --git a/generator/src/snapshot.rs b/generator/src/snapshot.rs index 8020296..cbf14bc 100644 --- a/generator/src/snapshot.rs +++ b/generator/src/snapshot.rs @@ -29,6 +29,7 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result 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( diff --git a/lua/codesnap/static.lua b/lua/codesnap/static.lua index 1bec55f..670134a 100644 --- a/lua/codesnap/static.lua +++ b/lua/codesnap/static.lua @@ -11,6 +11,7 @@ return { breadcrumbs_separator = "/", has_breadcrumbs = false, has_line_number = false, + min_width = 0, }, cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), preview_switch = true,