Skip to content

Commit

Permalink
Merge pull request #141 from KuabeM/background-border
Browse files Browse the repository at this point in the history
Add background border
  • Loading branch information
l4l authored Feb 16, 2023
2 parents 4bdf2e8 + f6467e8 commit 36dec6b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const DEFAULT_BG_COLOR: Color = Color::from_rgba(0x27, 0x28, 0x22, 0xee);
const DEFAULT_INPUT_BG_COLOR: Color = Color::from_rgba(0x75, 0x71, 0x5e, 0xc0);
const DEFAULT_SELECTED_FONT_COLOR: Color = Color::from_rgba(0xa6, 0xe2, 0x2e, 0xff);

const DEFAULT_BG_BORDER_COLOR: Color = Color::from_rgba(0x13, 0x14, 0x11, 0xff);
const DEFAULT_BG_BORDER_WIDTH: f32 = 2.0;

mod params;

#[derive(Defaults, Deserialize)]
Expand All @@ -34,6 +37,8 @@ pub struct Config {
font: Option<String>,
font_size: Option<u16>,
bg_color: Option<Color>,
bg_border_color: Option<Color>,
bg_border_width: Option<f32>,
font_color: Option<Color>,
#[def = "Radius::all(0.0)"]
corner_radius: Radius,
Expand Down
7 changes: 7 additions & 0 deletions src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ impl<'a> From<&'a Config> for ListParams {

impl<'a> From<&'a Config> for BgParams {
fn from(config: &'a Config) -> BgParams {
let border = match (config.bg_border_color, config.bg_border_width) {
(None, None) => None,
(Some(c), Some(w)) => Some((c, w)),
(Some(c), None) => Some((c, DEFAULT_BG_BORDER_WIDTH)),
(None, Some(w)) => Some((DEFAULT_BG_BORDER_COLOR, w)),
};
BgParams {
width: config.width,
height: config.height,
radius: config.corner_radius.clone(),
color: config.bg_color.unwrap_or(DEFAULT_BG_COLOR),
border,
}
}
}
Expand Down
45 changes: 43 additions & 2 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::f32::consts;

use oneshot::Sender;
pub use raqote::Point;
use raqote::{DrawOptions, PathBuilder, Source};
use raqote::{DrawOptions, Path, PathBuilder, Source, StrokeStyle};

pub use background::Params as BgParams;
pub use input_text::Params as InputTextParams;
Expand Down Expand Up @@ -76,11 +76,20 @@ where
pub struct RoundedRect {
radius: Radius,
color: Color,
border: Option<Border>,
}

impl RoundedRect {
fn new(radius: Radius, color: Color) -> Self {
Self { radius, color }
Self {
radius,
color,
border: None,
}
}

fn with_border(self, border: Option<Border>) -> Self {
Self { border, ..self }
}
}

Expand Down Expand Up @@ -131,13 +140,45 @@ impl Drawable for RoundedRect {
consts::FRAC_PI_2,
consts::FRAC_PI_2,
);
pb.line_to(x, y + top_left);
let path = pb.finish();

dt.fill(
&path,
&Source::Solid(self.color.as_source()),
&DrawOptions::new(),
);

if let Some(b) = self.border {
b.add_stroke(dt, &path);
}

space
}
}

pub struct Border {
border_color: Color,
border_width: f32,
}

impl Border {
pub fn new(border_color: Color, border_width: f32) -> Self {
Self {
border_color,
border_width,
}
}

fn add_stroke(self, dt: &mut DrawTarget<'_>, path: &Path) {
dt.stroke(
path,
&Source::Solid(self.border_color.as_source()),
&StrokeStyle {
width: self.border_width,
..Default::default()
},
&DrawOptions::new(),
);
}
}
15 changes: 10 additions & 5 deletions src/draw/background.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use raqote::{Point, SolidSource};

use super::{DrawTarget, Drawable, RoundedRect, Space};
use super::{Border, DrawTarget, Drawable, RoundedRect, Space};
use crate::{style::Radius, Color};

pub struct Params {
pub width: u32,
pub height: u32,
pub color: Color,
pub radius: Radius,
pub border: Option<(Color, f32)>,
}

pub struct Background {
Expand All @@ -18,10 +19,14 @@ impl Background {
pub fn new(params: &Params) -> Self {
let color = params.color;
let radius = params.radius.clone();

Self {
rect: RoundedRect::new(radius, color),
}
let border = if let Some((c, w)) = params.border {
Some(Border::new(c, w))
} else {
None
};
let rect = RoundedRect::new(radius, color).with_border(border);

Self { rect }
}
}

Expand Down

0 comments on commit 36dec6b

Please sign in to comment.