Skip to content

Commit 7be7336

Browse files
authored
[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
1 parent a223fd8 commit 7be7336

File tree

14 files changed

+140
-94
lines changed

14 files changed

+140
-94
lines changed

generator/src/components/background.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use tiny_skia::{
22
Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform,
33
};
44

5-
use crate::color::{is_valid_hex_color, RgbaColor};
5+
use crate::{
6+
color::{is_valid_hex_color, RgbaColor},
7+
edges::padding::Padding,
8+
};
69

710
use super::interface::{
811
component::{Component, ComponentContext, RenderParams},
@@ -12,11 +15,15 @@ use super::interface::{
1215

1316
pub struct Background {
1417
children: Vec<Box<dyn Component>>,
18+
has_background: bool,
1519
}
1620

1721
impl Background {
18-
pub fn from_children(children: Vec<Box<dyn Component>>) -> Background {
19-
Background { children }
22+
pub fn new(has_background: bool, children: Vec<Box<dyn Component>>) -> Background {
23+
Background {
24+
children,
25+
has_background,
26+
}
2027
}
2128
}
2229

@@ -26,7 +33,22 @@ impl Component for Background {
2633
}
2734

2835
fn style(&self) -> RawComponentStyle {
29-
RawComponentStyle::default().align(ComponentAlign::Column)
36+
let style = RawComponentStyle::default().align(ComponentAlign::Column);
37+
38+
if self.has_background {
39+
return style.padding(Padding {
40+
top: 82.,
41+
left: 122.,
42+
right: 122.,
43+
bottom: 82.,
44+
});
45+
}
46+
47+
return style;
48+
}
49+
50+
fn self_render_condition(&self) -> bool {
51+
self.has_background
3052
}
3153

3254
fn draw_self(

generator/src/components/code_block.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use crate::edges::margin::Margin;
2-
3-
use super::interface::{
4-
component::Component,
5-
style::{RawComponentStyle, Style},
6-
};
1+
use super::interface::component::Component;
72

83
pub struct CodeBlock {
94
children: Vec<Box<dyn Component>>,
@@ -13,13 +8,6 @@ impl Component for CodeBlock {
138
fn children(&self) -> &Vec<Box<dyn Component>> {
149
&self.children
1510
}
16-
17-
fn style(&self) -> RawComponentStyle {
18-
Style::default().margin(Margin {
19-
top: 10.,
20-
..Margin::default()
21-
})
22-
}
2311
}
2412

2513
impl CodeBlock {

generator/src/components/container.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use tiny_skia::Pixmap;
22

3-
use crate::edges::padding::Padding;
4-
53
use super::interface::{
64
component::{Component, ComponentContext, ComponentRenderParams},
75
render_error::Result,
8-
style::{RawComponentStyle, Style},
6+
style::Style,
97
};
108

119
pub struct Container {
@@ -16,15 +14,6 @@ impl Component for Container {
1614
fn children(&self) -> &Vec<Box<dyn Component>> {
1715
&self.children
1816
}
19-
20-
fn style(&self) -> RawComponentStyle {
21-
Style::default().padding(Padding {
22-
top: 82.,
23-
left: 122.,
24-
right: 122.,
25-
bottom: 82.,
26-
})
27-
}
2817
}
2918

3019
impl Container {

generator/src/components/editor/mac_title_bar.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use tiny_skia::{Color, FillRule, Paint, PathBuilder, Transform};
22

3-
use crate::components::interface::{
4-
component::{Component, ComponentContext, RenderParams},
5-
render_error,
6-
style::{ComponentStyle, RawComponentStyle, Size, Style},
3+
use crate::{
4+
components::interface::{
5+
component::{Component, ComponentContext, RenderParams},
6+
render_error,
7+
style::{ComponentStyle, RawComponentStyle, Size, Style},
8+
},
9+
edges::margin::Margin,
710
};
811

912
pub struct MacTitleBar {
@@ -20,7 +23,12 @@ impl Component for MacTitleBar {
2023
fn style(&self) -> RawComponentStyle {
2124
let demeter = self.radius * 2.;
2225

23-
Style::default().size(Size::Num(demeter + 2. * 25.), Size::Num(demeter))
26+
Style::default()
27+
.size(Size::Num(demeter + 2. * 25.), Size::Num(demeter))
28+
.margin(Margin {
29+
bottom: 10.,
30+
..Margin::default()
31+
})
2432
}
2533

2634
fn render_condition(&self) -> bool {

generator/src/components/interface/component.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ pub trait Component {
6060
render_params.clone()
6161
}
6262

63+
// The render_condition determines whether the component should be rendered or not
6364
fn render_condition(&self) -> bool {
6465
true
6566
}
6667

68+
// The difference with render_condition is that self_render_condition still renders childrens
69+
fn self_render_condition(&self) -> bool {
70+
true
71+
}
72+
6773
fn draw_self(
6874
&self,
6975
_pixmap: &mut Pixmap,
@@ -87,7 +93,19 @@ pub trait Component {
8793
}
8894

8995
fn parsed_style(&self) -> Style<f32> {
90-
let style = self.style();
96+
// If render_condition return false, the whole component shouldn't rendered,
97+
// includes its children
98+
if !self.render_condition() {
99+
return ComponentStyle::default();
100+
}
101+
102+
// If self_render_condition return false, the component shouldn't rendered,
103+
// so the corresponding style should be cleared
104+
let style = if self.self_render_condition() {
105+
self.style()
106+
} else {
107+
RawComponentStyle::default()
108+
};
91109
let (width, height) = self.get_dynamic_wh();
92110
let width = self.parse_size(style.width, width)
93111
+ style.padding.horizontal()
@@ -121,12 +139,19 @@ pub trait Component {
121139
let render_params = self.initialize(
122140
&component_render_params.parse_into_render_params_with_style(
123141
parent_style.clone(),
124-
sibling_style,
142+
sibling_style.clone(),
125143
style.clone(),
126144
),
127145
);
128146

129-
self.draw_self(pixmap, context, &render_params, &style, &parent_style)?;
147+
// Render nothing on paint if render_condition return false
148+
if !self.render_condition() {
149+
return Ok(render_params.clone());
150+
}
151+
152+
if self.self_render_condition() {
153+
self.draw_self(pixmap, context, &render_params, &style, &parent_style)?;
154+
}
130155

131156
let children = self.children();
132157
let mut sibling_render_params = RenderParams {
@@ -136,10 +161,6 @@ pub trait Component {
136161
let mut sibling_style = ComponentStyle::default();
137162

138163
for child in children {
139-
if !child.render_condition() {
140-
continue;
141-
}
142-
143164
sibling_render_params = child.draw(
144165
pixmap,
145166
context,

generator/src/components/interface/style.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::edges::{margin::Margin, padding::Padding};
22

3-
#[derive(Clone)]
3+
#[derive(Clone, Debug)]
44
pub enum ComponentAlign {
55
Row,
66
Column,
@@ -11,7 +11,7 @@ pub enum Size {
1111
Num(f32),
1212
}
1313

14-
#[derive(Clone)]
14+
#[derive(Clone, Debug)]
1515
pub struct Style<T> {
1616
pub width: T,
1717
pub height: T,

generator/src/components/watermark.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,26 @@ impl Component for Watermark {
2525
) -> render_error::Result<()> {
2626
let params = &context.take_snapshot_params;
2727

28-
if &params.watermark != "" {
29-
let attrs = Attrs::new().family(Family::Name(
30-
&context.take_snapshot_params.watermark_font_family,
31-
));
28+
let attrs = Attrs::new().family(Family::Name(
29+
&context.take_snapshot_params.watermark_font_family,
30+
));
3231

33-
FontRenderer::new(
34-
20.,
35-
20.,
36-
context.scale_factor,
37-
&context.take_snapshot_params.fonts_folder,
38-
)
39-
.draw_line(
40-
0.,
41-
render_params.y,
42-
pixmap.width() as f32,
43-
pixmap.height() as f32,
44-
&params.watermark,
45-
attrs,
46-
Some(Align::Center),
47-
pixmap,
48-
);
49-
}
32+
FontRenderer::new(
33+
20.,
34+
20.,
35+
context.scale_factor,
36+
&context.take_snapshot_params.fonts_folder,
37+
)
38+
.draw_line(
39+
0.,
40+
render_params.y,
41+
pixmap.width() as f32,
42+
pixmap.height() as f32,
43+
&params.watermark,
44+
attrs,
45+
Some(Align::Center),
46+
pixmap,
47+
);
5048

5149
Ok(())
5250
}
@@ -55,6 +53,10 @@ impl Component for Watermark {
5553
&self.children
5654
}
5755

56+
fn render_condition(&self) -> bool {
57+
self.value != ""
58+
}
59+
5860
fn style(&self) -> RawComponentStyle {
5961
let default_style = RawComponentStyle::default();
6062

generator/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct TakeSnapshotParams {
3030
pub highlight_start_line_number: Option<usize>,
3131
pub highlight_end_line_number: Option<usize>,
3232
pub min_width: Option<f32>,
33+
pub has_background: bool,
3334
}
3435

3536
impl FromObject for TakeSnapshotParams {

generator/src/copy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use arboard::{Clipboard, ImageData};
55

66
use nvim_oxi::Result;
77

8+
// The function will be called as FFI on Lua side
9+
#[allow(dead_code)]
810
pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> {
911
let pixmap = take_snapshot(config.clone())?;
1012
let premultplied_colors = pixmap.pixels();

generator/src/edges/margin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::edge::Edge;
22

3-
#[derive(Clone, Default)]
3+
#[derive(Clone, Default, Debug)]
44
pub struct Margin {
55
pub left: f32,
66
pub right: f32,

generator/src/edges/padding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::edge::Edge;
22

3-
#[derive(Clone, Default)]
3+
#[derive(Clone, Default, Debug)]
44
pub struct Padding {
55
pub left: f32,
66
pub right: f32,

generator/src/save.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{config::TakeSnapshotParams, path::parse_save_path, snapshot::take_snapshot};
22
use nvim_oxi::{lua::Error::RuntimeError, Error, Result};
33

4+
// The function will be called as FFI on Lua side
5+
#[allow(dead_code)]
46
pub fn save_snapshot(config: TakeSnapshotParams) -> Result<()> {
57
match &config.save_path {
68
Some(path) => {

0 commit comments

Comments
 (0)