diff options
| author | MaxVerevkin <maxxverrr@gmail.com> | 2023-04-27 15:06:34 +0300 |
|---|---|---|
| committer | MaxVerevkin <maxxverrr@gmail.com> | 2023-04-27 15:06:34 +0300 |
| commit | 8f2338f465a62e80a72a6b06a4a329f19ab8d48f (patch) | |
| tree | f0c63bd48ad4462fe3032016dd8fe15c28a98ead | |
| parent | a605021fe95716264ac1d2a03248f36b64a6fa21 (diff) | |
| download | i3bar-mango-8f2338f465a62e80a72a6b06a4a329f19ab8d48f.tar.gz | |
some refactoring and removing of unnecessary clones
| -rw-r--r-- | src/blocks_cache.rs | 156 | ||||
| -rw-r--r-- | src/i3bar_protocol.rs | 2 | ||||
| -rw-r--r-- | src/state.rs | 2 |
3 files changed, 78 insertions, 82 deletions
diff --git a/src/blocks_cache.rs b/src/blocks_cache.rs index 890c13f..1e607a6 100644 --- a/src/blocks_cache.rs +++ b/src/blocks_cache.rs @@ -15,88 +15,17 @@ pub struct ComputedBlock { } impl BlocksCache { - pub fn process_new_blocks(&mut self, config: &Config, blocks: &[Block]) { - let comp_min_width = |block: &Block| { - let markup = block.markup.as_deref() == Some("pango"); - match &block.min_width { - Some(MinWidth::Pixels(p)) => Some(*p as f64), - Some(MinWidth::Text(t)) => Some(text::width_of(t, markup, &config.font.0)), - None => None, - } - }; - let comp_full = |block: &Block, min_width: Option<f64>| { - let markup = block.markup.as_deref() == Some("pango"); - text::ComputedText::new( - &block.full_text, - text::Attributes { - font: &config.font, - padding_left: 0.0, - padding_right: 0.0, - min_width, - align: block.align.unwrap_or_default(), - markup, - }, - ) - }; - 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::ComputedText::new( - short_text, - text::Attributes { - font: &config.font, - padding_left: 0.0, - padding_right: 0.0, - min_width, - align: block.align.unwrap_or_default(), - markup, - }, - ) - }) - }; - - // update cashe + pub fn process_new_blocks(&mut self, config: &Config, blocks: Vec<Block>) { if blocks.len() != self.computed.len() { self.computed.clear(); - for block in blocks { - let mw = comp_min_width(block); - self.computed.push(ComputedBlock { - block: block.clone(), - full: comp_full(block, mw), - short: comp_short(block, mw), - min_width: mw, - }); - } - } else { - for (block, computed) in blocks.iter().zip(self.computed.iter_mut()) { - if block.min_width != computed.block.min_width - || block.markup != computed.block.markup - { - let mw = comp_min_width(block); - *computed = ComputedBlock { - block: block.clone(), - full: comp_full(block, mw), - short: comp_short(block, mw), - min_width: mw, - }; - } else { - if block.full_text != computed.block.full_text { - computed.block.full_text = block.full_text.clone(); - computed.full = comp_full(block, computed.min_width); - } - if block.short_text != computed.block.short_text { - computed.block.full_text = block.full_text.clone(); - computed.short = comp_short(block, computed.min_width); - } - computed.block.color = block.color; - computed.block.background = block.background; - computed.block.align = block.align; - computed.block.name = block.name.clone(); - computed.block.instance = block.instance.clone(); - computed.block.separator = block.separator; - computed.block.separator_block_width = block.separator_block_width; - } - } + self.computed.reserve(blocks.len()); + self.computed + .extend(blocks.into_iter().map(|b| ComputedBlock::new(b, config))); + return; + } + + for (block, computed) in blocks.into_iter().zip(self.computed.iter_mut()) { + computed.update(block, config); } } @@ -104,3 +33,70 @@ impl BlocksCache { &self.computed } } + +impl ComputedBlock { + fn new(block: Block, config: &Config) -> Self { + let mw = comp_min_width(&block, config); + Self { + full: comp_full(&block, mw, config), + short: comp_short(&block, mw, config), + min_width: mw, + block, + } + } + + fn update(&mut self, block: Block, config: &Config) { + if block.min_width != self.block.min_width || block.markup != self.block.markup { + *self = ComputedBlock::new(block, config); + } else { + if block.full_text != self.block.full_text { + self.full = comp_full(&block, self.min_width, config); + } + if block.short_text != self.block.short_text { + self.short = comp_short(&block, self.min_width, config); + } + self.block = block; + } + } +} + +fn comp_min_width(block: &Block, config: &Config) -> Option<f64> { + let markup = block.markup.as_deref() == Some("pango"); + match &block.min_width { + Some(MinWidth::Pixels(p)) => Some(*p as f64), + Some(MinWidth::Text(t)) => Some(text::width_of(t, markup, &config.font.0)), + None => None, + } +} + +fn comp_full(block: &Block, min_width: Option<f64>, config: &Config) -> ComputedText { + let markup = block.markup.as_deref() == Some("pango"); + ComputedText::new( + &block.full_text, + text::Attributes { + font: &config.font, + padding_left: 0.0, + padding_right: 0.0, + min_width, + align: block.align, + markup, + }, + ) +} + +fn comp_short(block: &Block, min_width: Option<f64>, config: &Config) -> Option<ComputedText> { + let markup = block.markup.as_deref() == Some("pango"); + block.short_text.as_ref().map(|short_text| { + text::ComputedText::new( + short_text, + text::Attributes { + font: &config.font, + padding_left: 0.0, + padding_right: 0.0, + min_width, + align: block.align, + markup, + }, + ) + }) +} diff --git a/src/i3bar_protocol.rs b/src/i3bar_protocol.rs index 67e03f1..be2828b 100644 --- a/src/i3bar_protocol.rs +++ b/src/i3bar_protocol.rs @@ -18,7 +18,7 @@ pub struct Block { #[serde(default)] pub min_width: Option<MinWidth>, #[serde(default)] - pub align: Option<Align>, + pub align: Align, #[serde(default)] pub name: Option<String>, #[serde(default)] diff --git a/src/state.rs b/src/state.rs index 5476f6c..b9a5ac1 100644 --- a/src/state.rs +++ b/src/state.rs @@ -107,7 +107,7 @@ impl State { pub fn set_blocks(&mut self, conn: &mut Connection<Self>, blocks: Vec<Block>) { self.shared_state .blocks_cache - .process_new_blocks(&self.shared_state.config, &blocks); + .process_new_blocks(&self.shared_state.config, blocks); self.draw_all(conn); } |
