Skip to content

Commit 88f0bd4

Browse files
authored
[Feat] add new style type Inherit which allows components can inherit (#34)
style from parent component
1 parent 1b50d9d commit 88f0bd4

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

core/src/components/code_block.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use super::interface::component::Component;
1+
use super::interface::{
2+
component::Component,
3+
style::{RawComponentStyle, Size, Style},
4+
};
25

36
pub struct CodeBlock {
47
children: Vec<Box<dyn Component>>,
@@ -8,6 +11,10 @@ impl Component for CodeBlock {
811
fn children(&self) -> &Vec<Box<dyn Component>> {
912
&self.children
1013
}
14+
15+
fn style(&self) -> RawComponentStyle {
16+
Style::default().size(Size::Inherit, Size::Dynamic)
17+
}
1118
}
1219

1320
impl CodeBlock {

core/src/components/container.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Container {
2222
}
2323

2424
pub fn draw_root(&self, context: &ComponentContext) -> Result<Pixmap> {
25-
let style = self.parsed_style();
25+
let style = self.parsed_style(None);
2626
let mut pixmap = Pixmap::new(
2727
(style.width * context.scale_factor) as u32,
2828
(style.height * context.scale_factor) as u32,

core/src/components/highlight_code_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl HighlightCodeBlock {
105105
let rect = Rect::from_xywh(
106106
render_params.x - self.editor_padding.left,
107107
render_params.y + start_y_offset,
108-
parent_style.width + self.editor_padding.horizontal(),
108+
parent_style.width,
109109
// If end_line_number is equal to start_line_number, the height should be line_height
110110
(end_line_number - start_line_number + 1) as f32 * self.line_height,
111111
)

core/src/components/interface/component.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ pub trait Component {
8686
RawComponentStyle::default()
8787
}
8888

89-
fn parse_size(&self, size: Size, dynamic_value: f32) -> f32 {
89+
fn parse_size(&self, size: Size, dynamic_value: f32, inherit_value: Option<f32>) -> f32 {
9090
match size {
9191
Size::Num(num) => num,
9292
Size::Dynamic => dynamic_value,
93+
Size::Inherit => inherit_value.unwrap_or(dynamic_value),
9394
}
9495
}
9596

96-
fn parsed_style(&self) -> Style<f32> {
97+
fn parsed_style(&self, parent_style: Option<&ComponentStyle>) -> Style<f32> {
9798
// If render_condition return false, the whole component shouldn't rendered,
9899
// includes its children
99100
if !self.render_condition() {
@@ -108,7 +109,7 @@ pub trait Component {
108109
RawComponentStyle::default()
109110
};
110111
let (width, height) = self.get_dynamic_wh();
111-
let width = self.parse_size(style.width, width)
112+
let width = self.parse_size(style.width, width, parent_style.map(|s| s.width))
112113
+ style.padding.horizontal()
113114
+ style.margin.horizontal();
114115

@@ -119,7 +120,7 @@ pub trait Component {
119120
} else {
120121
style.min_width
121122
},
122-
height: self.parse_size(style.height, height)
123+
height: self.parse_size(style.height, height, parent_style.map(|s| s.height))
123124
+ style.padding.vertical()
124125
+ style.margin.vertical(),
125126
align: style.align,
@@ -136,7 +137,7 @@ pub trait Component {
136137
parent_style: ComponentStyle,
137138
sibling_style: ComponentStyle,
138139
) -> render_error::Result<RenderParams> {
139-
let style = self.parsed_style();
140+
let style = self.parsed_style(Some(&parent_style));
140141
let render_params = self.initialize(
141142
&component_render_params.parse_into_render_params_with_style(
142143
parent_style.clone(),
@@ -172,7 +173,7 @@ pub trait Component {
172173
style.clone(),
173174
sibling_style,
174175
)?;
175-
sibling_style = child.parsed_style();
176+
sibling_style = child.parsed_style(Some(&style.clone()));
176177
}
177178

178179
Ok(render_params.clone())
@@ -190,13 +191,13 @@ pub trait Component {
190191
match style.align {
191192
// If align is row, width is sum of children width, height is max of children height
192193
ComponentAlign::Row => calc_children_wh(|(w, h), child| {
193-
let style = child.parsed_style();
194+
let style = child.parsed_style(None);
194195

195196
(w + style.width, h.max(style.height))
196197
}),
197198
// If align is column, width is max of children width, height is sum of children height
198199
ComponentAlign::Column => calc_children_wh(|(w, h), child| {
199-
let style = child.parsed_style();
200+
let style = child.parsed_style(None);
200201

201202
(w.max(style.width), h + style.height)
202203
}),

core/src/components/interface/style.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub enum ComponentAlign {
88

99
pub enum Size {
1010
Dynamic,
11+
Inherit,
1112
Num(f32),
1213
}
1314

core/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub struct Breadcrumbs {
132132
#[builder(setter(into, strip_option), default = None)]
133133
pub separator: Option<String>,
134134

135-
#[builder(setter(into, strip_option), default = None)]
135+
#[builder(setter(into, strip_option), default = Some(String::from("CaskaydiaCove Nerd Font")))]
136136
pub font_family: Option<String>,
137137

138138
#[builder(setter(into), default = String::from("#80848b"))]

0 commit comments

Comments
 (0)