summaryrefslogtreecommitdiff
path: root/src/text.rs
diff options
context:
space:
mode:
authorMax Verevkin <maxxverrr@gmail.com>2022-04-10 11:16:25 +0300
committerMax Verevkin <maxxverrr@gmail.com>2022-04-10 11:23:51 +0300
commitdcc20f7aca51a4ca1f47c15730a1c9e9d8dfdfc7 (patch)
treeeb3e94e385fec28145916ba8f98fe543a32864d0 /src/text.rs
parentf127f935fc47826d2d3e687a4ad2b3449a370b5f (diff)
downloadi3bar-mango-dcc20f7aca51a4ca1f47c15730a1c9e9d8dfdfc7.tar.gz
rounded corners
Diffstat (limited to 'src/text.rs')
-rw-r--r--src/text.rs49
1 files changed, 46 insertions, 3 deletions
diff --git a/src/text.rs b/src/text.rs
index 1af64ec..60ed707 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -2,6 +2,8 @@ use crate::color::Color;
use pango::{EllipsizeMode, FontDescription};
use pangocairo::{cairo, pango};
use serde::Deserialize;
+use std::f64::consts::{FRAC_PI_2, PI, TAU};
+use std::ops::Deref;
#[derive(Clone, Debug, PartialEq)]
pub struct RenderOptions {
@@ -9,6 +11,9 @@ pub struct RenderOptions {
pub bar_height: f64,
pub fg_color: Color,
pub bg_color: Option<Color>,
+ pub r_left: f64,
+ pub r_right: f64,
+ pub overlap: f64,
}
#[derive(Clone, Debug, PartialEq)]
@@ -96,6 +101,14 @@ pub struct ComputedText {
pub height: f64,
}
+impl Deref for ComputedText {
+ type Target = str;
+
+ fn deref(&self) -> &Self::Target {
+ &self.text.text
+ }
+}
+
impl ComputedText {
pub fn render(&self, context: &cairo::Context, options: RenderOptions) {
let text = &self.text;
@@ -108,7 +121,7 @@ impl ComputedText {
}
context.save().unwrap();
- context.translate(options.x_offset - 1.0, 0.0);
+ context.translate(options.x_offset - options.overlap, 0.0);
// Set the width/height on the Pango layout so that it word-wraps/ellipises.
let text_width = self.width - text.attr.padding_left - text.attr.padding_right;
@@ -117,15 +130,24 @@ impl ComputedText {
layout.set_width(text_width as i32 * pango::SCALE);
layout.set_height(text_height as i32 * pango::SCALE);
+ // Draw background
if let Some(bg) = options.bg_color {
bg.apply(context);
- context.rectangle(0.0, 0.0, self.width + 1.0, options.bar_height);
+ rounded_rectangle(
+ context,
+ 0.0,
+ 0.0,
+ self.width + options.overlap,
+ options.bar_height,
+ options.r_left,
+ options.r_right,
+ );
context.fill().unwrap();
}
options.fg_color.apply(context);
context.translate(
- text.attr.padding_left + 1.0,
+ text.attr.padding_left + options.overlap,
(options.bar_height - self.height) * 0.5,
);
show_pango_layout(context, &layout);
@@ -133,6 +155,27 @@ impl ComputedText {
}
}
+fn rounded_rectangle(
+ context: &cairo::Context,
+ x: f64,
+ y: f64,
+ w: f64,
+ h: f64,
+ r_left: f64,
+ r_right: f64,
+) {
+ if r_left > 0.0 || r_right > 0.0 {
+ context.new_sub_path();
+ context.arc(x + r_left, y + r_left, r_left, PI, 3.0 * FRAC_PI_2);
+ context.arc(x + w - r_right, y + r_right, r_right, 3.0 * FRAC_PI_2, TAU);
+ context.arc(x + w - r_right, y + h - r_right, r_right, 0.0, FRAC_PI_2);
+ context.arc(x + r_left, y + h - r_left, r_left, FRAC_PI_2, PI);
+ context.close_path();
+ } else {
+ context.rectangle(x, y, w, h);
+ }
+}
+
pub fn width_of(text: &str, context: &cairo::Context, markup: bool, font: &FontDescription) -> f64 {
let text = Text {
text: text.into(),