forked from mirror/codesnap.nvim
[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 configmain
parent
a223fd8829
commit
7be7336420
|
@ -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::{
|
||||||
|
components::interface::{
|
||||||
component::{Component, ComponentContext, RenderParams},
|
component::{Component, ComponentContext, RenderParams},
|
||||||
render_error,
|
render_error,
|
||||||
style::{ComponentStyle, RawComponentStyle, Size, Style},
|
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(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 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)?;
|
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,7 +25,6 @@ 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,
|
||||||
));
|
));
|
||||||
|
@ -46,7 +45,6 @@ impl Component for Watermark {
|
||||||
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,7 +26,16 @@ 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
|
||||||
|
// 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(
|
Box::new(Rect::new(
|
||||||
16.,
|
16.,
|
||||||
params.min_width,
|
params.min_width,
|
||||||
|
@ -53,8 +62,9 @@ pub fn take_snapshot(params: TakeSnapshotParams) -> render_error::Result<Pixmap>
|
||||||
])),
|
])),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
Box::new(Watermark::new(params.watermark)),
|
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 New Issue