diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bar.rs | 23 | ||||
| -rw-r--r-- | src/tags.rs | 14 | ||||
| -rw-r--r-- | src/text.rs | 109 |
3 files changed, 52 insertions, 94 deletions
@@ -133,8 +133,7 @@ impl Bar { .iter() .enumerate() { - let tag = - compute_tag_label(text.to_string(), ss.config.font.clone(), &cairo_ctx); + let tag = compute_tag_label(text, ss.config.font.clone(), &cairo_ctx); self.tags_btns.push(x_offset, tag.width, id); x_offset += tag.width; self.tags_computed.push(tag); @@ -218,9 +217,9 @@ fn render_blocks( }; let comp_full = |block: &Block, min_width: Option<f64>| { let markup = block.markup.as_deref() == Some("pango"); - text::Text { - text: block.full_text.clone(), - attr: text::Attributes { + text::ComputedText::new( + &block.full_text, + text::Attributes { font: config.font.clone(), padding_left: 0.0, padding_right: 0.0, @@ -228,15 +227,15 @@ fn render_blocks( align: block.align.unwrap_or_default(), markup, }, - } - .compute(context) + context, + ) }; let comp_short = |block: &Block, min_width: Option<f64>| { let markup = block.markup.as_deref() == Some("pango"); block.short_text.as_ref().map(|short_text| { - text::Text { - text: short_text.clone(), - attr: text::Attributes { + text::ComputedText::new( + short_text, + text::Attributes { font: config.font.clone(), padding_left: 0.0, padding_right: 0.0, @@ -244,8 +243,8 @@ fn render_blocks( align: block.align.unwrap_or_default(), markup, }, - } - .compute(context) + context, + ) }) }; diff --git a/src/tags.rs b/src/tags.rs index 8114a74..0c63570 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -1,4 +1,4 @@ -use crate::text::{Attributes, ComputedText, Text}; +use crate::text::{Attributes, ComputedText}; use cairo::Context; use pango::FontDescription; use pangocairo::{cairo, pango}; @@ -28,9 +28,10 @@ impl TagsInfo { } } -pub fn compute_tag_label(label: String, font: FontDescription, context: &Context) -> ComputedText { - let text = Text { - attr: Attributes { +pub fn compute_tag_label(label: &str, font: FontDescription, context: &Context) -> ComputedText { + ComputedText::new( + label, + Attributes { font, padding_left: 25.0, padding_right: 25.0, @@ -38,7 +39,6 @@ pub fn compute_tag_label(label: String, font: FontDescription, context: &Context align: Default::default(), markup: false, }, - text: label, - }; - text.compute(context) + context, + ) } diff --git a/src/text.rs b/src/text.rs index 60ed707..978a9cc 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1,9 +1,8 @@ use crate::color::Color; -use pango::{EllipsizeMode, FontDescription}; +use pango::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 { @@ -40,96 +39,55 @@ impl Default for Align { } } -fn create_pango_layout(cairo_context: &cairo::Context) -> pango::Layout { - pangocairo::create_layout(cairo_context).unwrap() -} - -fn show_pango_layout(cairo_context: &cairo::Context, layout: &pango::Layout) { - pangocairo::show_layout(cairo_context, layout); -} - #[derive(Clone, Debug, PartialEq)] -pub struct Text { +pub struct ComputedText { + pub layout: pango::Layout, pub attr: Attributes, - pub text: String, + pub width: f64, + pub height: f64, } -impl Text { - pub fn compute(mut self, context: &cairo::Context) -> ComputedText { - let (mut width, height) = { - let layout = create_pango_layout(context); - layout.set_font_description(Some(&self.attr.font)); - if self.attr.markup { - layout.set_markup(&self.text); - } else { - layout.set_text(&self.text); - } +impl ComputedText { + pub fn new(text: &str, mut attr: Attributes, context: &cairo::Context) -> Self { + let layout = pangocairo::create_layout(context).unwrap(); + layout.set_font_description(Some(&attr.font)); + if attr.markup { + layout.set_markup(text); + } else { + layout.set_text(text); + } - let (text_width, text_height) = layout.pixel_size(); - let width = f64::from(text_width) + self.attr.padding_right + self.attr.padding_right; - let height = f64::from(text_height); - (width, height) - }; + let (text_width, text_height) = layout.pixel_size(); + let mut width = f64::from(text_width) + attr.padding_right + attr.padding_right; + let height = f64::from(text_height); - if let Some(min_width) = self.attr.min_width { + if let Some(min_width) = attr.min_width { if width < min_width { let d = min_width - width; width = min_width; - match self.attr.align { - Align::Right => self.attr.padding_left += d, - Align::Left => self.attr.padding_right += d, + match attr.align { + Align::Right => attr.padding_left += d, + Align::Left => attr.padding_right += d, Align::Center => { - self.attr.padding_left += d * 0.5; - self.attr.padding_right += d * 0.5; + attr.padding_left += d * 0.5; + attr.padding_right += d * 0.5; } } } } - ComputedText { - text: self, + Self { + layout, + attr, width, height, } } -} -#[derive(Clone, Debug, PartialEq)] -pub struct ComputedText { - pub text: Text, - pub width: f64, - 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; - let layout = create_pango_layout(context); - layout.set_font_description(Some(&text.attr.font)); - if text.attr.markup { - layout.set_markup(&text.text); - } else { - layout.set_text(&text.text); - } - context.save().unwrap(); 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; - let text_height = self.height; - layout.set_ellipsize(EllipsizeMode::End); - 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); @@ -147,10 +105,10 @@ impl ComputedText { options.fg_color.apply(context); context.translate( - text.attr.padding_left + options.overlap, + self.attr.padding_left + options.overlap, (options.bar_height - self.height) * 0.5, ); - show_pango_layout(context, &layout); + pangocairo::show_layout(context, &self.layout); context.restore().unwrap(); } } @@ -177,9 +135,9 @@ fn rounded_rectangle( } pub fn width_of(text: &str, context: &cairo::Context, markup: bool, font: &FontDescription) -> f64 { - let text = Text { - text: text.into(), - attr: Attributes { + ComputedText::new( + text, + Attributes { font: font.clone(), padding_left: 0.0, padding_right: 0.0, @@ -187,6 +145,7 @@ pub fn width_of(text: &str, context: &cairo::Context, markup: bool, font: &FontD align: Default::default(), markup, }, - }; - text.compute(context).width + context, + ) + .width } |
