From 2d77969c7719e72237ec365c3db9e35c3e178631 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Tue, 9 Aug 2022 22:19:56 +0300 Subject: refactoring --- src/tags.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/tags.rs') 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, + ) } -- cgit v1.3.1 From b154262dcbf0d1a5686921f24eb0b0c41ed95f29 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Thu, 25 Aug 2022 09:18:56 +0300 Subject: feat: add hide_inactive_tags option --- src/bar.rs | 27 ++++++++++++++++----------- src/config.rs | 7 +++++++ src/river_protocols/status.rs | 8 ++++++++ src/river_protocols/status/dispatch.rs | 8 +++++++- src/state.rs | 16 ++++++++++++++++ src/tags.rs | 6 +++++- 6 files changed, 59 insertions(+), 13 deletions(-) (limited to 'src/tags.rs') diff --git a/src/bar.rs b/src/bar.rs index ffc4d7c..18ad54c 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -127,24 +127,28 @@ impl Bar { let mut offset_left = 0.0; if self.has_tags_provider() { if self.tags_computed.is_empty() { - let mut x_offset = 0.0; // TODO make configurable - for (id, text) in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] - .iter() - .enumerate() - { - 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); + for text in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] { + self.tags_computed.push(compute_tag_label( + text, + ss.config.font.clone(), + &cairo_ctx, + )); } } + self.tags_btns.clear(); for (i, label) in self.tags_computed.iter().enumerate() { let state = self.tags_info.get_state(i); let (bg, fg) = match state { - TagState::Focused => (ss.config.tag_focused_bg, ss.config.tag_focused_fg), - TagState::Inactive => (ss.config.tag_bg, ss.config.tag_fg), TagState::Urgent => (ss.config.tag_urgent_bg, ss.config.tag_urgent_fg), + TagState::Focused => (ss.config.tag_focused_bg, ss.config.tag_focused_fg), + TagState::Active => (ss.config.tag_bg, ss.config.tag_fg), + TagState::Inactive => { + if ss.config.hide_inactive_tags { + continue; + } + (ss.config.tag_inactive_bg, ss.config.tag_inactive_fg) + } }; label.render( &cairo_ctx, @@ -167,6 +171,7 @@ impl Bar { overlap: 0.0, }, ); + self.tags_btns.push(offset_left, label.width, i); offset_left += label.width; } } diff --git a/src/config.rs b/src/config.rs index bd7b91c..9ae2395 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,6 +20,8 @@ pub struct Config { pub tag_focused_bg: Color, pub tag_urgent_fg: Color, pub tag_urgent_bg: Color, + pub tag_inactive_fg: Color, + pub tag_inactive_bg: Color, // font and size pub font: Font, pub height: u32, @@ -29,6 +31,8 @@ pub struct Config { pub blocks_overlap: f64, // command pub command: Option, + // misc + pub hide_inactive_tags: bool, } impl Default for Config { @@ -44,6 +48,8 @@ impl Default for Config { tag_focused_bg: Color::from_rgba_hex(0x689d68ff), tag_urgent_fg: Color::from_rgba_hex(0x282828ff), tag_urgent_bg: Color::from_rgba_hex(0xcc241dff), + tag_inactive_fg: Color::from_rgba_hex(0xd79921ff), + tag_inactive_bg: Color::from_rgba_hex(0x282828ff), font: Font::new("monospace 10"), height: 24, separator_width: 2.0, @@ -51,6 +57,7 @@ impl Default for Config { blocks_r: 0.0, blocks_overlap: 0.0, command: None, + hide_inactive_tags: false, } } } diff --git a/src/river_protocols/status.rs b/src/river_protocols/status.rs index 50c85e0..f19ac5f 100644 --- a/src/river_protocols/status.rs +++ b/src/river_protocols/status.rs @@ -96,6 +96,14 @@ pub trait RiverStatusHandler: Sized { output_status: &RiverOutputStatus, urgent: u32, ); + + fn views_tags_updated( + &mut self, + conn: &Connection, + qh: &QueueHandle, + output_status: &RiverOutputStatus, + tags: Vec, + ); } #[derive(Debug, Clone)] diff --git a/src/river_protocols/status/dispatch.rs b/src/river_protocols/status/dispatch.rs index d38037d..65d6e0c 100644 --- a/src/river_protocols/status/dispatch.rs +++ b/src/river_protocols/status/dispatch.rs @@ -79,7 +79,13 @@ where match event { Event::FocusedTags { tags } => data.focused_tags_updated(conn, qh, &status, tags), Event::UrgentTags { tags } => data.urgent_tags_updated(conn, qh, &status, tags), - Event::ViewTags { tags: _ } => (), + Event::ViewTags { tags } => { + let tags = tags + .chunks_exact(4) + .map(|bytes| u32::from_ne_bytes(bytes.try_into().unwrap())) + .collect(); + data.views_tags_updated(conn, qh, &status, tags); + } } } } diff --git a/src/state.rs b/src/state.rs index 131e20f..4dd9414 100644 --- a/src/state.rs +++ b/src/state.rs @@ -380,6 +380,22 @@ impl RiverStatusHandler for State { bar.tags_info.urgent = urgent; bar.draw(self.shared_state.as_mut().unwrap()); } + + fn views_tags_updated( + &mut self, + _conn: &Connection, + _qh: &QueueHandle, + output_status: &RiverOutputStatus, + tags: Vec, + ) { + let bar = self + .bars + .iter_mut() + .find(|b| b.river_output_status.as_ref() == Some(output_status)) + .unwrap(); + bar.tags_info.active = tags.into_iter().fold(0, |a, b| a | b); + bar.draw(self.shared_state.as_mut().unwrap()); + } } impl RiverControlHandler for State { diff --git a/src/tags.rs b/src/tags.rs index 0c63570..5a4a1b2 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -5,15 +5,17 @@ use pangocairo::{cairo, pango}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TagState { + Urgent, Focused, + Active, Inactive, - Urgent, } #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct TagsInfo { pub focused: u32, pub urgent: u32, + pub active: u32, } impl TagsInfo { @@ -22,6 +24,8 @@ impl TagsInfo { TagState::Urgent } else if self.focused >> tag & 1 == 1 { TagState::Focused + } else if self.active >> tag & 1 == 1 { + TagState::Active } else { TagState::Inactive } -- cgit v1.3.1 From 39cc147656e2f61f7467f4b67b4c76fd9207c2f6 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Thu, 25 Aug 2022 16:40:44 +0300 Subject: do not clone fonts --- src/bar.rs | 11 ++++------- src/tags.rs | 2 +- src/text.rs | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 18 deletions(-) (limited to 'src/tags.rs') diff --git a/src/bar.rs b/src/bar.rs index 18ad54c..188152f 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -129,11 +129,8 @@ impl Bar { if self.tags_computed.is_empty() { // TODO make configurable for text in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] { - self.tags_computed.push(compute_tag_label( - text, - ss.config.font.clone(), - &cairo_ctx, - )); + self.tags_computed + .push(compute_tag_label(text, &ss.config.font, &cairo_ctx)); } } self.tags_btns.clear(); @@ -225,7 +222,7 @@ fn render_blocks( text::ComputedText::new( &block.full_text, text::Attributes { - font: config.font.clone(), + font: &config.font, padding_left: 0.0, padding_right: 0.0, min_width, @@ -241,7 +238,7 @@ fn render_blocks( text::ComputedText::new( short_text, text::Attributes { - font: config.font.clone(), + font: &config.font, padding_left: 0.0, padding_right: 0.0, min_width, diff --git a/src/tags.rs b/src/tags.rs index 5a4a1b2..d22853d 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -32,7 +32,7 @@ impl TagsInfo { } } -pub fn compute_tag_label(label: &str, font: FontDescription, context: &Context) -> ComputedText { +pub fn compute_tag_label(label: &str, font: &FontDescription, context: &Context) -> ComputedText { ComputedText::new( label, Attributes { diff --git a/src/text.rs b/src/text.rs index 978a9cc..7a3b7fc 100644 --- a/src/text.rs +++ b/src/text.rs @@ -16,8 +16,8 @@ pub struct RenderOptions { } #[derive(Clone, Debug, PartialEq)] -pub struct Attributes { - pub font: FontDescription, +pub struct Attributes<'a> { + pub font: &'a FontDescription, pub padding_left: f64, pub padding_right: f64, pub min_width: Option, @@ -41,16 +41,16 @@ impl Default for Align { #[derive(Clone, Debug, PartialEq)] pub struct ComputedText { - pub layout: pango::Layout, - pub attr: Attributes, pub width: f64, - pub height: f64, + layout: pango::Layout, + height: f64, + padding_left: f64, } 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)); + layout.set_font_description(Some(attr.font)); if attr.markup { layout.set_markup(text); } else { @@ -77,10 +77,10 @@ impl ComputedText { } Self { - layout, - attr, width, + layout, height, + padding_left: attr.padding_left, } } @@ -105,7 +105,7 @@ impl ComputedText { options.fg_color.apply(context); context.translate( - self.attr.padding_left + options.overlap, + self.padding_left + options.overlap, (options.bar_height - self.height) * 0.5, ); pangocairo::show_layout(context, &self.layout); @@ -138,7 +138,7 @@ pub fn width_of(text: &str, context: &cairo::Context, markup: bool, font: &FontD ComputedText::new( text, Attributes { - font: font.clone(), + font, padding_left: 0.0, padding_right: 0.0, min_width: None, -- cgit v1.3.1