[Feat] supports for allows users can custom min_width of snapshots

This commit is contained in:
Mist 2024-05-07 16:35:21 +08:00 committed by The Mist
parent 8f9e6fa5aa
commit 19dd1d4817
6 changed files with 29 additions and 5 deletions

View file

@ -89,11 +89,17 @@ pub trait Component {
fn parsed_style(&self) -> Style<f32> { fn parsed_style(&self) -> Style<f32> {
let style = self.style(); let style = self.style();
let (width, height) = self.get_dynamic_wh(); let (width, height) = self.get_dynamic_wh();
let width = self.parse_size(style.width, width)
+ style.padding.horizontal()
+ style.margin.horizontal();
Style { Style {
width: self.parse_size(style.width, width) min_width: style.min_width,
+ style.padding.horizontal() width: if width > style.min_width {
+ style.margin.horizontal(), width
} else {
style.min_width
},
height: self.parse_size(style.height, height) height: self.parse_size(style.height, height)
+ style.padding.vertical() + style.padding.vertical()
+ style.margin.vertical(), + style.margin.vertical(),

View file

@ -15,6 +15,7 @@ pub enum Size {
pub struct Style<T> { pub struct Style<T> {
pub width: T, pub width: T,
pub height: T, pub height: T,
pub min_width: f32,
pub align: ComponentAlign, pub align: ComponentAlign,
pub padding: Padding, pub padding: Padding,
pub margin: Margin, pub margin: Margin,
@ -26,6 +27,7 @@ pub type ComponentStyle = Style<f32>;
impl Default for RawComponentStyle { impl Default for RawComponentStyle {
fn default() -> Self { fn default() -> Self {
Style { Style {
min_width: 0.,
width: Size::Dynamic, width: Size::Dynamic,
height: Size::Dynamic, height: Size::Dynamic,
align: ComponentAlign::Row, align: ComponentAlign::Row,
@ -38,6 +40,7 @@ impl Default for RawComponentStyle {
impl Default for ComponentStyle { impl Default for ComponentStyle {
fn default() -> Self { fn default() -> Self {
Style { Style {
min_width: 0.,
width: 0., width: 0.,
height: 0., height: 0.,
align: ComponentAlign::Row, align: ComponentAlign::Row,
@ -54,6 +57,12 @@ impl RawComponentStyle {
self 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 { pub fn align(mut self, align: ComponentAlign) -> Self {
self.align = align; self.align = align;
self self

View file

@ -10,6 +10,7 @@ pub const EDITOR_PADDING: f32 = 20.;
pub struct Rect { pub struct Rect {
radius: f32, radius: f32,
min_width: f32,
children: Vec<Box<dyn Component>>, children: Vec<Box<dyn Component>>,
} }
@ -20,6 +21,7 @@ impl Component for Rect {
fn style(&self) -> RawComponentStyle { fn style(&self) -> RawComponentStyle {
Style::default() Style::default()
.min_width(self.min_width)
.align(ComponentAlign::Column) .align(ComponentAlign::Column)
.padding(Padding::from_value(EDITOR_PADDING)) .padding(Padding::from_value(EDITOR_PADDING))
} }
@ -94,7 +96,11 @@ impl Component for Rect {
} }
impl Rect { impl Rect {
pub fn new(radius: f32, children: Vec<Box<dyn Component>>) -> Rect { pub fn new(radius: f32, min_width: Option<f32>, children: Vec<Box<dyn Component>>) -> Rect {
Rect { radius, children } Rect {
radius,
children,
min_width: min_width.unwrap_or(0.),
}
} }
} }

View file

@ -29,6 +29,7 @@ pub struct TakeSnapshotParams {
pub start_line_number: Option<usize>, pub start_line_number: Option<usize>,
pub highlight_start_line_number: Option<usize>, pub highlight_start_line_number: Option<usize>,
pub highlight_end_line_number: Option<usize>, pub highlight_end_line_number: Option<usize>,
pub min_width: Option<f32>,
} }
impl FromObject for TakeSnapshotParams { impl FromObject for TakeSnapshotParams {

View file

@ -29,6 +29,7 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap>
let pixmap = Container::from_children(vec![Box::new(Background::from_children(vec![ let pixmap = Container::from_children(vec![Box::new(Background::from_children(vec![
Box::new(Rect::new( Box::new(Rect::new(
16., 16.,
params.min_width,
vec![ vec![
Box::new(MacTitleBar::from_radius(8., params.mac_window_bar)), Box::new(MacTitleBar::from_radius(8., params.mac_window_bar)),
Box::new(Breadcrumbs::from_path( Box::new(Breadcrumbs::from_path(

View file

@ -11,6 +11,7 @@ return {
breadcrumbs_separator = "/", breadcrumbs_separator = "/",
has_breadcrumbs = false, has_breadcrumbs = false,
has_line_number = false, has_line_number = false,
min_width = 0,
}, },
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
preview_switch = true, preview_switch = true,