mirror of
https://github.com/mistricky/codesnap.nvim.git
synced 2024-12-27 03:46:29 +00:00
[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
This commit is contained in:
parent
a223fd8829
commit
7be7336420
14 changed files with 140 additions and 94 deletions
|
@ -2,7 +2,10 @@ use tiny_skia::{
|
||||||
Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform,
|
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::{
|
use super::interface::{
|
||||||
component::{Component, ComponentContext, RenderParams},
|
component::{Component, ComponentContext, RenderParams},
|
||||||
|
@ -12,11 +15,15 @@ use super::interface::{
|
||||||
|
|
||||||
pub struct Background {
|
pub struct Background {
|
||||||
children: Vec<Box<dyn Component>>,
|
children: Vec<Box<dyn Component>>,
|
||||||
|
has_background: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Background {
|
impl Background {
|
||||||
pub fn from_children(children: Vec<Box<dyn Component>>) -> Background {
|
pub fn new(has_background: bool, children: Vec<Box<dyn Component>>) -> Background {
|
||||||
Background { children }
|
Background {
|
||||||
|
children,
|
||||||
|
has_background,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +33,22 @@ impl Component for Background {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style(&self) -> RawComponentStyle {
|
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(
|
fn draw_self(
|
||||||
|
|
|
@ -1,9 +1,4 @@
|
||||||
use crate::edges::margin::Margin;
|
use super::interface::component::Component;
|
||||||
|
|
||||||
use super::interface::{
|
|
||||||
component::Component,
|
|
||||||
style::{RawComponentStyle, Style},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct CodeBlock {
|
pub struct CodeBlock {
|
||||||
children: Vec<Box<dyn Component>>,
|
children: Vec<Box<dyn Component>>,
|
||||||
|
@ -13,13 +8,6 @@ impl Component for CodeBlock {
|
||||||
fn children(&self) -> &Vec<Box<dyn Component>> {
|
fn children(&self) -> &Vec<Box<dyn Component>> {
|
||||||
&self.children
|
&self.children
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style(&self) -> RawComponentStyle {
|
|
||||||
Style::default().margin(Margin {
|
|
||||||
top: 10.,
|
|
||||||
..Margin::default()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeBlock {
|
impl CodeBlock {
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use tiny_skia::Pixmap;
|
use tiny_skia::Pixmap;
|
||||||
|
|
||||||
use crate::edges::padding::Padding;
|
|
||||||
|
|
||||||
use super::interface::{
|
use super::interface::{
|
||||||
component::{Component, ComponentContext, ComponentRenderParams},
|
component::{Component, ComponentContext, ComponentRenderParams},
|
||||||
render_error::Result,
|
render_error::Result,
|
||||||
style::{RawComponentStyle, Style},
|
style::Style,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Container {
|
pub struct Container {
|
||||||
|
@ -16,15 +14,6 @@ impl Component for Container {
|
||||||
fn children(&self) -> &Vec<Box<dyn Component>> {
|
fn children(&self) -> &Vec<Box<dyn Component>> {
|
||||||
&self.children
|
&self.children
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style(&self) -> RawComponentStyle {
|
|
||||||
Style::default().padding(Padding {
|
|
||||||
top: 82.,
|
|
||||||
left: 122.,
|
|
||||||
right: 122.,
|
|
||||||
bottom: 82.,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Container {
|
impl Container {
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
use tiny_skia::{Color, FillRule, Paint, PathBuilder, Transform};
|
use tiny_skia::{Color, FillRule, Paint, PathBuilder, Transform};
|
||||||
|
|
||||||
use crate::components::interface::{
|
use crate::{
|
||||||
component::{Component, ComponentContext, RenderParams},
|
components::interface::{
|
||||||
render_error,
|
component::{Component, ComponentContext, RenderParams},
|
||||||
style::{ComponentStyle, RawComponentStyle, Size, Style},
|
render_error,
|
||||||
|
style::{ComponentStyle, RawComponentStyle, Size, Style},
|
||||||
|
},
|
||||||
|
edges::margin::Margin,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct MacTitleBar {
|
pub struct MacTitleBar {
|
||||||
|
@ -20,7 +23,12 @@ impl Component for MacTitleBar {
|
||||||
fn style(&self) -> RawComponentStyle {
|
fn style(&self) -> RawComponentStyle {
|
||||||
let demeter = self.radius * 2.;
|
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 {
|
fn render_condition(&self) -> bool {
|
||||||
|
|
|
@ -60,10 +60,16 @@ pub trait Component {
|
||||||
render_params.clone()
|
render_params.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The render_condition determines whether the component should be rendered or not
|
||||||
fn render_condition(&self) -> bool {
|
fn render_condition(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The difference with render_condition is that self_render_condition still renders childrens
|
||||||
|
fn self_render_condition(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
fn draw_self(
|
fn draw_self(
|
||||||
&self,
|
&self,
|
||||||
_pixmap: &mut Pixmap,
|
_pixmap: &mut Pixmap,
|
||||||
|
@ -87,7 +93,19 @@ pub trait Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parsed_style(&self) -> Style<f32> {
|
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, height) = self.get_dynamic_wh();
|
||||||
let width = self.parse_size(style.width, width)
|
let width = self.parse_size(style.width, width)
|
||||||
+ style.padding.horizontal()
|
+ style.padding.horizontal()
|
||||||
|
@ -121,12 +139,19 @@ pub trait Component {
|
||||||
let render_params = self.initialize(
|
let render_params = self.initialize(
|
||||||
&component_render_params.parse_into_render_params_with_style(
|
&component_render_params.parse_into_render_params_with_style(
|
||||||
parent_style.clone(),
|
parent_style.clone(),
|
||||||
sibling_style,
|
sibling_style.clone(),
|
||||||
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 children = self.children();
|
||||||
let mut sibling_render_params = RenderParams {
|
let mut sibling_render_params = RenderParams {
|
||||||
|
@ -136,10 +161,6 @@ pub trait Component {
|
||||||
let mut sibling_style = ComponentStyle::default();
|
let mut sibling_style = ComponentStyle::default();
|
||||||
|
|
||||||
for child in children {
|
for child in children {
|
||||||
if !child.render_condition() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
sibling_render_params = child.draw(
|
sibling_render_params = child.draw(
|
||||||
pixmap,
|
pixmap,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::edges::{margin::Margin, padding::Padding};
|
use crate::edges::{margin::Margin, padding::Padding};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum ComponentAlign {
|
pub enum ComponentAlign {
|
||||||
Row,
|
Row,
|
||||||
Column,
|
Column,
|
||||||
|
@ -11,7 +11,7 @@ pub enum Size {
|
||||||
Num(f32),
|
Num(f32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Style<T> {
|
pub struct Style<T> {
|
||||||
pub width: T,
|
pub width: T,
|
||||||
pub height: T,
|
pub height: T,
|
||||||
|
|
|
@ -25,28 +25,26 @@ impl Component for Watermark {
|
||||||
) -> render_error::Result<()> {
|
) -> render_error::Result<()> {
|
||||||
let params = &context.take_snapshot_params;
|
let params = &context.take_snapshot_params;
|
||||||
|
|
||||||
if ¶ms.watermark != "" {
|
let attrs = Attrs::new().family(Family::Name(
|
||||||
let attrs = Attrs::new().family(Family::Name(
|
&context.take_snapshot_params.watermark_font_family,
|
||||||
&context.take_snapshot_params.watermark_font_family,
|
));
|
||||||
));
|
|
||||||
|
|
||||||
FontRenderer::new(
|
FontRenderer::new(
|
||||||
20.,
|
20.,
|
||||||
20.,
|
20.,
|
||||||
context.scale_factor,
|
context.scale_factor,
|
||||||
&context.take_snapshot_params.fonts_folder,
|
&context.take_snapshot_params.fonts_folder,
|
||||||
)
|
)
|
||||||
.draw_line(
|
.draw_line(
|
||||||
0.,
|
0.,
|
||||||
render_params.y,
|
render_params.y,
|
||||||
pixmap.width() as f32,
|
pixmap.width() as f32,
|
||||||
pixmap.height() as f32,
|
pixmap.height() as f32,
|
||||||
¶ms.watermark,
|
¶ms.watermark,
|
||||||
attrs,
|
attrs,
|
||||||
Some(Align::Center),
|
Some(Align::Center),
|
||||||
pixmap,
|
pixmap,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -55,6 +53,10 @@ impl Component for Watermark {
|
||||||
&self.children
|
&self.children
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_condition(&self) -> bool {
|
||||||
|
self.value != ""
|
||||||
|
}
|
||||||
|
|
||||||
fn style(&self) -> RawComponentStyle {
|
fn style(&self) -> RawComponentStyle {
|
||||||
let default_style = RawComponentStyle::default();
|
let default_style = RawComponentStyle::default();
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ pub struct TakeSnapshotParams {
|
||||||
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>,
|
pub min_width: Option<f32>,
|
||||||
|
pub has_background: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromObject for TakeSnapshotParams {
|
impl FromObject for TakeSnapshotParams {
|
||||||
|
|
|
@ -5,6 +5,8 @@ use arboard::{Clipboard, ImageData};
|
||||||
|
|
||||||
use nvim_oxi::Result;
|
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<()> {
|
pub fn copy_into_clipboard(config: TakeSnapshotParams) -> Result<()> {
|
||||||
let pixmap = take_snapshot(config.clone())?;
|
let pixmap = take_snapshot(config.clone())?;
|
||||||
let premultplied_colors = pixmap.pixels();
|
let premultplied_colors = pixmap.pixels();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::edge::Edge;
|
use super::edge::Edge;
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default, Debug)]
|
||||||
pub struct Margin {
|
pub struct Margin {
|
||||||
pub left: f32,
|
pub left: f32,
|
||||||
pub right: f32,
|
pub right: f32,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::edge::Edge;
|
use super::edge::Edge;
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default, Debug)]
|
||||||
pub struct Padding {
|
pub struct Padding {
|
||||||
pub left: f32,
|
pub left: f32,
|
||||||
pub right: f32,
|
pub right: f32,
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use crate::{config::TakeSnapshotParams, path::parse_save_path, snapshot::take_snapshot};
|
use crate::{config::TakeSnapshotParams, path::parse_save_path, snapshot::take_snapshot};
|
||||||
use nvim_oxi::{lua::Error::RuntimeError, Error, Result};
|
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<()> {
|
pub fn save_snapshot(config: TakeSnapshotParams) -> Result<()> {
|
||||||
match &config.save_path {
|
match &config.save_path {
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
|
|
|
@ -26,35 +26,45 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap>
|
||||||
scale_factor: SCALE_FACTOR,
|
scale_factor: SCALE_FACTOR,
|
||||||
take_snapshot_params: Arc::new(params.clone()),
|
take_snapshot_params: Arc::new(params.clone()),
|
||||||
};
|
};
|
||||||
let pixmap = Container::from_children(vec![Box::new(Background::from_children(vec![
|
// If background is disabled, should hidden watermark component
|
||||||
Box::new(Rect::new(
|
// If watermark text is equal to "", the watermark component is hidden
|
||||||
16.,
|
let watermark = if params.has_background {
|
||||||
params.min_width,
|
params.watermark
|
||||||
vec![
|
} else {
|
||||||
Box::new(MacTitleBar::from_radius(8., params.mac_window_bar)),
|
"".to_string()
|
||||||
Box::new(Breadcrumbs::from_path(
|
};
|
||||||
params.file_path,
|
let pixmap = Container::from_children(vec![Box::new(Background::new(
|
||||||
15.,
|
params.has_background,
|
||||||
params.breadcrumbs_separator,
|
vec![
|
||||||
params.has_breadcrumbs,
|
Box::new(Rect::new(
|
||||||
)),
|
16.,
|
||||||
Box::new(CodeBlock::from_children(vec![
|
params.min_width,
|
||||||
Box::new(HighlightCodeBlock::from_line_number(
|
vec![
|
||||||
params.highlight_start_line_number,
|
Box::new(MacTitleBar::from_radius(8., params.mac_window_bar)),
|
||||||
params.highlight_end_line_number,
|
Box::new(Breadcrumbs::from_path(
|
||||||
LINE_HEIGHT,
|
params.file_path,
|
||||||
|
15.,
|
||||||
|
params.breadcrumbs_separator,
|
||||||
|
params.has_breadcrumbs,
|
||||||
)),
|
)),
|
||||||
Box::new(LineNumber::new(
|
Box::new(CodeBlock::from_children(vec![
|
||||||
¶ms.code,
|
Box::new(HighlightCodeBlock::from_line_number(
|
||||||
params.start_line_number,
|
params.highlight_start_line_number,
|
||||||
LINE_HEIGHT,
|
params.highlight_end_line_number,
|
||||||
)),
|
LINE_HEIGHT,
|
||||||
Box::new(Code::new(params.code, LINE_HEIGHT, 15.)),
|
)),
|
||||||
])),
|
Box::new(LineNumber::new(
|
||||||
],
|
¶ms.code,
|
||||||
)),
|
params.start_line_number,
|
||||||
Box::new(Watermark::new(params.watermark)),
|
LINE_HEIGHT,
|
||||||
]))])
|
)),
|
||||||
|
Box::new(Code::new(params.code, LINE_HEIGHT, 15.)),
|
||||||
|
])),
|
||||||
|
],
|
||||||
|
)),
|
||||||
|
Box::new(Watermark::new(watermark)),
|
||||||
|
],
|
||||||
|
))])
|
||||||
.draw_root(&context)?;
|
.draw_root(&context)?;
|
||||||
|
|
||||||
Ok(pixmap)
|
Ok(pixmap)
|
||||||
|
|
|
@ -49,6 +49,7 @@ function config_module.get_config(extension)
|
||||||
theme = "base16-onedark",
|
theme = "base16-onedark",
|
||||||
file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "",
|
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,
|
start_line_number = static.config.has_line_number and start_line_number or nil,
|
||||||
|
has_background = true,
|
||||||
}, static.config)
|
}, static.config)
|
||||||
|
|
||||||
config.save_path = parse_save_path(config.save_path)
|
config.save_path = parse_save_path(config.save_path)
|
||||||
|
|
Loading…
Reference in a new issue