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

[Feat] support create snapshot from JSON config #3

Merged
merged 4 commits into from
Oct 27, 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
122 changes: 120 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
workspace = { members = ["cli", "core"] }
[workspace]
resolver = "2"

members = ["cli", "core"]
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ derive_builder = "0.20.2"
chrono = "0.4.38"
base64 = "0.22.1"
once_cell = "1.20.2"
serde_json = "1.0.132"
65 changes: 4 additions & 61 deletions core/src/components/background.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use tiny_skia::{
Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform,
};
use tiny_skia::{LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform};

use crate::{
edges::{edge::Edge, padding::Padding},
utils::color::RgbaColor,
utils::{color::RgbaColor, helpers::convert_vecs},
};

use super::interface::{
component::{Component, ComponentContext, RenderParams},
render_error::{self, RenderError},
render_error::{self},
style::{ComponentAlign, ComponentStyle, RawComponentStyle},
};

Expand Down Expand Up @@ -71,29 +69,14 @@ impl Component for Background {
paint.shader = LinearGradient::new(
Point::from_xy(start.x, start.y),
Point::from_xy(end.x, end.y),
gradient_background.stops.clone(),
convert_vecs(gradient_background.stops.clone()),
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
}
};

// if is_valid_hex_color(&params.background) {
// let rgba_color: RgbaColor = params.background.as_str().into();
//
// paint.set_color(rgba_color.into());
// } else {
// paint.shader = LinearGradient::new(
// Point::from_xy(0., 0.),
// Point::from_xy(w, 0.),
// Background::get_theme(&params.background)?,
// SpreadMode::Pad,
// Transform::identity(),
// )
// .unwrap();
// }

pixmap.fill_rect(
Rect::from_xywh(0., 0., w, h).unwrap(),
&paint,
Expand All @@ -104,43 +87,3 @@ impl Component for Background {
Ok(())
}
}

impl Background {
fn get_theme(theme: &str) -> render_error::Result<Vec<GradientStop>> {
let theme = match theme {
"default" => vec![
GradientStop::new(0.0, Color::from_rgba8(58, 28, 113, 255)),
GradientStop::new(0.5, Color::from_rgba8(215, 109, 119, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 175, 123, 255)),
],
"sea" => vec![
GradientStop::new(0.0, Color::from_rgba8(31, 162, 255, 255)),
GradientStop::new(0.4, Color::from_rgba8(18, 216, 250, 255)),
GradientStop::new(0.95, Color::from_rgba8(166, 255, 203, 255)),
],
"grape" => vec![
GradientStop::new(0.28, Color::from_rgba8(103, 90, 247, 255)),
GradientStop::new(0.95, Color::from_rgba8(189, 101, 250, 255)),
],
"peach" => vec![
GradientStop::new(0.22, Color::from_rgba8(221, 94, 137, 255)),
GradientStop::new(0.95, Color::from_rgba8(247, 187, 151, 255)),
],
"summer" => vec![
GradientStop::new(0.28, Color::from_rgba8(248, 165, 194, 255)),
GradientStop::new(0.95, Color::from_rgba8(116, 185, 255, 255)),
],
"bamboo" => vec![
GradientStop::new(0.22, Color::from_rgba8(107, 203, 165, 255)),
GradientStop::new(0.95, Color::from_rgba8(202, 244, 194, 255)),
],
"dusk" => vec![
GradientStop::new(0.22, Color::from_rgba8(255, 98, 110, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 190, 113, 255)),
],
_ => return Err(RenderError::UnknownBackgroundTheme(theme.to_string())),
};

Ok(theme)
}
}
74 changes: 45 additions & 29 deletions core/src/components/breadcrumbs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use cosmic_text::{Attrs, Color, Family};
use std::path::MAIN_SEPARATOR_STR;

use cosmic_text::{Attrs, Family};
use regex::Regex;

use crate::{
config, edges::margin::Margin, utils::code::calc_wh_with_min_width, utils::text::FontRenderer,
config,
edges::margin::Margin,
utils::text::FontRenderer,
utils::{code::calc_wh_with_min_width, color::RgbaColor},
};

use super::interface::{
Expand All @@ -23,6 +28,10 @@ impl Component for Breadcrumbs {
&self.children
}

fn render_condition(&self) -> bool {
self.config.is_some()
}

fn style(&self) -> RawComponentStyle {
let style = RawComponentStyle::default();

Expand All @@ -47,27 +56,34 @@ impl Component for Breadcrumbs {
style: &super::interface::style::ComponentStyle,
_parent_style: &ComponentStyle,
) -> super::interface::render_error::Result<()> {
if self.config.is_some() {
if let Some(ref path) = self.path {
let attrs = Attrs::new()
.color(Color::rgb(128, 132, 139))
.family(Family::Name(&context.take_snapshot_params.code.font_family));

FontRenderer::new(
12.,
LINE_HEIGHT,
context.scale_factor,
context.take_snapshot_params.fonts_folder.clone(),
)
.draw_text(
render_params.x,
render_params.y,
style.width,
LINE_HEIGHT,
vec![(path, attrs)],
pixmap,
);
}
let config = self.config.clone().unwrap();

if let Some(ref path) = self.path {
let path = config
.separator
.and_then(|separator| Some(parse_separator(path, &separator)))
.unwrap_or(path.clone());
let color: RgbaColor = config.color.as_str().into();
let attrs = Attrs::new().color(color.into());
let attrs = match config.font_family {
Some(ref font_family) => attrs.family(Family::Name(font_family)),
None => attrs,
};

FontRenderer::new(
12.,
LINE_HEIGHT,
context.scale_factor,
context.take_snapshot_params.fonts_folder.clone(),
)
.draw_text(
render_params.x,
render_params.y,
style.width,
LINE_HEIGHT,
vec![(&path, attrs)],
pixmap,
);
}

Ok(())
Expand All @@ -85,11 +101,11 @@ impl Breadcrumbs {
config,
}
}
}

pub fn parse_separator(&self, path: &str, separator: &str) -> String {
Regex::new("/")
.unwrap()
.replace_all(path, separator)
.to_string()
}
fn parse_separator(path_str: &str, separator: &str) -> String {
Regex::new(MAIN_SEPARATOR_STR)
.unwrap()
.replace_all(path_str, separator)
.to_string()
}
Loading
Loading