1
0
Fork 0

[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
main
The Mist 2024-07-03 04:35:41 -04:00 committed by GitHub
parent a223fd8829
commit 7be7336420
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 140 additions and 94 deletions

View File

@ -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},
@ -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,
}
}
}
@ -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(

View File

@ -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>>,
@ -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 {

View File

@ -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 {
@ -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 {

View File

@ -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 {
@ -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 {

View File

@ -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,
@ -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()
@ -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 {
@ -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,

View File

@ -1,6 +1,6 @@
use crate::edges::{margin::Margin, padding::Padding};
#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum ComponentAlign {
Row,
Column,
@ -11,7 +11,7 @@ pub enum Size {
Num(f32),
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Style<T> {
pub width: T,
pub height: T,

View File

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

View File

@ -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 {

View File

@ -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();

View File

@ -1,6 +1,6 @@
use super::edge::Edge;
#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct Margin {
pub left: f32,
pub right: f32,

View File

@ -1,6 +1,6 @@
use super::edge::Edge;
#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct Padding {
pub left: f32,
pub right: f32,

View File

@ -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) => {

View File

@ -26,35 +26,45 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap>
scale_factor: SCALE_FACTOR,
take_snapshot_params: Arc::new(params.clone()),
};
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(
params.file_path,
15.,
params.breadcrumbs_separator,
params.has_breadcrumbs,
)),
Box::new(CodeBlock::from_children(vec![
Box::new(HighlightCodeBlock::from_line_number(
params.highlight_start_line_number,
params.highlight_end_line_number,
LINE_HEIGHT,
// If background is disabled, should hidden watermark component
// If watermark text is equal to "", the watermark component is hidden
let watermark = if params.has_background {
params.watermark
} else {
"".to_string()
};
let pixmap = Container::from_children(vec![Box::new(Background::new(
params.has_background,
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(
params.file_path,
15.,
params.breadcrumbs_separator,
params.has_breadcrumbs,
)),
Box::new(LineNumber::new(
&params.code,
params.start_line_number,
LINE_HEIGHT,
)),
Box::new(Code::new(params.code, LINE_HEIGHT, 15.)),
])),
],
)),
Box::new(Watermark::new(params.watermark)),
]))])
Box::new(CodeBlock::from_children(vec![
Box::new(HighlightCodeBlock::from_line_number(
params.highlight_start_line_number,
params.highlight_end_line_number,
LINE_HEIGHT,
)),
Box::new(LineNumber::new(
&params.code,
params.start_line_number,
LINE_HEIGHT,
)),
Box::new(Code::new(params.code, LINE_HEIGHT, 15.)),
])),
],
)),
Box::new(Watermark::new(watermark)),
],
))])
.draw_root(&context)?;
Ok(pixmap)

View File

@ -49,6 +49,7 @@ function config_module.get_config(extension)
theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "",
start_line_number = static.config.has_line_number and start_line_number or nil,
has_background = true,
}, static.config)
config.save_path = parse_save_path(config.save_path)