From 3f57ce62fc24d9762728a0f966f1d01909be5404 Mon Sep 17 00:00:00 2001 From: Mist Date: Sun, 17 Mar 2024 17:12:47 +0800 Subject: [PATCH] [Feat] add more beautiful background themes --- generator/src/components/background.rs | 54 ++++++++++++++----- .../src/components/interface/render_error.rs | 3 ++ generator/src/config.rs | 1 + lua/codesnap/static.lua | 1 + 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/generator/src/components/background.rs b/generator/src/components/background.rs index e7515cb..3c662b0 100644 --- a/generator/src/components/background.rs +++ b/generator/src/components/background.rs @@ -4,25 +4,17 @@ use tiny_skia::{ use super::interface::{ component::{Component, ComponentContext, RenderParams}, - render_error, + render_error::{self, RenderError}, style::{ComponentAlign, ComponentStyle, RawComponentStyle}, }; pub struct Background { children: Vec>, - gradient_stop_points: Vec, } impl Background { pub fn from_children(children: Vec>) -> Background { - Background { - children, - gradient_stop_points: 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(1.0, Color::from_rgba8(255, 175, 123, 255)), - ], - } + Background { children } } } @@ -38,7 +30,7 @@ impl Component for Background { fn draw_self( &self, pixmap: &mut Pixmap, - _context: &ComponentContext, + context: &ComponentContext, _render_params: &RenderParams, _style: &ComponentStyle, ) -> render_error::Result<()> { @@ -50,7 +42,7 @@ impl Component for Background { paint.shader = LinearGradient::new( Point::from_xy(0., 0.), Point::from_xy(w, 0.), - self.gradient_stop_points.clone(), + Background::get_theme(&context.take_snapshot_params.bg_theme)?, SpreadMode::Pad, Transform::identity(), ) @@ -66,3 +58,41 @@ impl Component for Background { Ok(()) } } + +impl Background { + fn get_theme(theme: &str) -> render_error::Result> { + 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)), + ], + "cyan" => 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)), + ], + "rose" => vec![ + GradientStop::new(0.1, Color::from_rgba8(180, 101, 218, 255)), + GradientStop::new(0.33, Color::from_rgba8(207, 108, 201, 255)), + GradientStop::new(0.66, Color::from_rgba8(238, 96, 156, 255)), + GradientStop::new(1., Color::from_rgba8(238, 96, 156, 255)), + ], + "pink" => vec![ + GradientStop::new(0.22, Color::from_rgba8(221, 94, 137, 255)), + GradientStop::new(0.55, Color::from_rgba8(247, 187, 151, 255)), + ], + "yellow" => vec![ + GradientStop::new(0., Color::from_rgba8(85, 239, 196, 255)), + GradientStop::new(0.4, Color::from_rgba8(255, 234, 167, 255)), + ], + "blue" => vec![ + GradientStop::new(0.28, Color::from_rgba8(248, 165, 194, 255)), + GradientStop::new(0.95, Color::from_rgba8(116, 185, 255, 255)), + ], + _ => return Err(RenderError::UnknownBackgroundTheme(theme.to_string())), + }; + + Ok(theme) + } +} diff --git a/generator/src/components/interface/render_error.rs b/generator/src/components/interface/render_error.rs index 3e419d4..b1c6ab6 100644 --- a/generator/src/components/interface/render_error.rs +++ b/generator/src/components/interface/render_error.rs @@ -10,6 +10,9 @@ pub enum RenderError { #[error("Find Highlight theme for {0} failed")] HighlightCodeFailed(String), + + #[error("Unable to parse unknown background theme {0}")] + UnknownBackgroundTheme(String), } impl From for nvim_oxi::Error { diff --git a/generator/src/config.rs b/generator/src/config.rs index b55d50a..e8ac090 100644 --- a/generator/src/config.rs +++ b/generator/src/config.rs @@ -17,6 +17,7 @@ pub struct TakeSnapshotParams { pub themes_folder: String, pub fonts_folder: String, pub theme: String, + pub bg_theme: String, } impl FromObject for TakeSnapshotParams { diff --git a/lua/codesnap/static.lua b/lua/codesnap/static.lua index c625ee5..d0137a0 100644 --- a/lua/codesnap/static.lua +++ b/lua/codesnap/static.lua @@ -7,6 +7,7 @@ return { code_font_family = "CaskaydiaCove Nerd Font", watermark_font_family = "Pacifico", watermark = "CodeSnap.nvim", + bg_theme = "default", }, cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), preview_switch = true,