[Feat] add more beautiful background themes

This commit is contained in:
Mist 2024-03-17 17:12:47 +08:00
parent 7c38bea715
commit 3f57ce62fc
4 changed files with 47 additions and 12 deletions

View file

@ -4,25 +4,17 @@ use tiny_skia::{
use super::interface::{ use super::interface::{
component::{Component, ComponentContext, RenderParams}, component::{Component, ComponentContext, RenderParams},
render_error, render_error::{self, RenderError},
style::{ComponentAlign, ComponentStyle, RawComponentStyle}, style::{ComponentAlign, ComponentStyle, RawComponentStyle},
}; };
pub struct Background { pub struct Background {
children: Vec<Box<dyn Component>>, children: Vec<Box<dyn Component>>,
gradient_stop_points: Vec<GradientStop>,
} }
impl Background { impl Background {
pub fn from_children(children: Vec<Box<dyn Component>>) -> Background { pub fn from_children(children: Vec<Box<dyn Component>>) -> Background {
Background { Background { children }
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)),
],
}
} }
} }
@ -38,7 +30,7 @@ impl Component for Background {
fn draw_self( fn draw_self(
&self, &self,
pixmap: &mut Pixmap, pixmap: &mut Pixmap,
_context: &ComponentContext, context: &ComponentContext,
_render_params: &RenderParams, _render_params: &RenderParams,
_style: &ComponentStyle, _style: &ComponentStyle,
) -> render_error::Result<()> { ) -> render_error::Result<()> {
@ -50,7 +42,7 @@ impl Component for Background {
paint.shader = LinearGradient::new( paint.shader = LinearGradient::new(
Point::from_xy(0., 0.), Point::from_xy(0., 0.),
Point::from_xy(w, 0.), Point::from_xy(w, 0.),
self.gradient_stop_points.clone(), Background::get_theme(&context.take_snapshot_params.bg_theme)?,
SpreadMode::Pad, SpreadMode::Pad,
Transform::identity(), Transform::identity(),
) )
@ -66,3 +58,41 @@ impl Component for Background {
Ok(()) 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)),
],
"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)
}
}

View file

@ -10,6 +10,9 @@ pub enum RenderError {
#[error("Find Highlight theme for {0} failed")] #[error("Find Highlight theme for {0} failed")]
HighlightCodeFailed(String), HighlightCodeFailed(String),
#[error("Unable to parse unknown background theme {0}")]
UnknownBackgroundTheme(String),
} }
impl From<RenderError> for nvim_oxi::Error { impl From<RenderError> for nvim_oxi::Error {

View file

@ -17,6 +17,7 @@ pub struct TakeSnapshotParams {
pub themes_folder: String, pub themes_folder: String,
pub fonts_folder: String, pub fonts_folder: String,
pub theme: String, pub theme: String,
pub bg_theme: String,
} }
impl FromObject for TakeSnapshotParams { impl FromObject for TakeSnapshotParams {

View file

@ -7,6 +7,7 @@ return {
code_font_family = "CaskaydiaCove Nerd Font", code_font_family = "CaskaydiaCove Nerd Font",
watermark_font_family = "Pacifico", watermark_font_family = "Pacifico",
watermark = "CodeSnap.nvim", watermark = "CodeSnap.nvim",
bg_theme = "default",
}, },
cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))),
preview_switch = true, preview_switch = true,