Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add has_background config to control visibility of background component #110

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions generator/src/components/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -12,11 +15,15 @@ use super::interface::{

pub struct Background {
children: Vec<Box<dyn Component>>,
has_background: bool,
}

impl Background {
pub fn from_children(children: Vec<Box<dyn Component>>) -> Background {
Background { children }
pub fn new(has_background: bool, children: Vec<Box<dyn Component>>) -> Background {
Background {
children,
has_background,
}
}
}

Expand All @@ -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(
Expand Down
14 changes: 1 addition & 13 deletions generator/src/components/code_block.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn Component>>,
Expand All @@ -13,13 +8,6 @@ impl Component for CodeBlock {
fn children(&self) -> &Vec<Box<dyn Component>> {
&self.children
}

fn style(&self) -> RawComponentStyle {
Style::default().margin(Margin {
top: 10.,
..Margin::default()
})
}
}

impl CodeBlock {
Expand Down
13 changes: 1 addition & 12 deletions generator/src/components/container.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,15 +14,6 @@ impl Component for Container {
fn children(&self) -> &Vec<Box<dyn Component>> {
&self.children
}

fn style(&self) -> RawComponentStyle {
Style::default().padding(Padding {
top: 82.,
left: 122.,
right: 122.,
bottom: 82.,
})
}
}

impl Container {
Expand Down
18 changes: 13 additions & 5 deletions generator/src/components/editor/mac_title_bar.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down
35 changes: 28 additions & 7 deletions generator/src/components/interface/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -87,7 +93,19 @@ pub trait Component {
}

fn parsed_style(&self) -> Style<f32> {
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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions generator/src/components/interface/style.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::edges::{margin::Margin, padding::Padding};

#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum ComponentAlign {
Row,
Column,
Expand All @@ -11,7 +11,7 @@ pub enum Size {
Num(f32),
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Style<T> {
pub width: T,
pub height: T,
Expand Down
44 changes: 23 additions & 21 deletions generator/src/components/watermark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@ impl Component for Watermark {
) -> render_error::Result<()> {
let params = &context.take_snapshot_params;

if &params.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,
&params.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,
&params.watermark,
attrs,
Some(Align::Center),
pixmap,
);

Ok(())
}
Expand All @@ -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();

Expand Down
1 change: 1 addition & 0 deletions generator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct TakeSnapshotParams {
pub highlight_start_line_number: Option<usize>,
pub highlight_end_line_number: Option<usize>,
pub min_width: Option<f32>,
pub has_background: bool,
}

impl FromObject for TakeSnapshotParams {
Expand Down
2 changes: 2 additions & 0 deletions generator/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion generator/src/edges/margin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::edge::Edge;

#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct Margin {
pub left: f32,
pub right: f32,
Expand Down
2 changes: 1 addition & 1 deletion generator/src/edges/padding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::edge::Edge;

#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct Padding {
pub left: f32,
pub right: f32,
Expand Down
2 changes: 2 additions & 0 deletions generator/src/save.rs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
Loading
Loading