From 96592a41a6971f23a0b7149a782b2296f650d7d9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Jul 2024 10:07:50 +0300 Subject: [PATCH] refactor: remove `Arc` for `FontCollection` in `WindowManager` --- src/backend/render/banner.rs | 39 +++++++++++++++--------------------- src/backend/render/text.rs | 20 ++++++------------ src/backend/render/window.rs | 13 +++++------- 3 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/backend/render/banner.rs b/src/backend/render/banner.rs index bcbf2c5..53cc534 100644 --- a/src/backend/render/banner.rs +++ b/src/backend/render/banner.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::Write, sync::Arc, time}; +use std::{fs::File, io::Write, time}; use crate::{ config::{spacing::Spacing, Colors, DisplayConfig, CONFIG}, @@ -30,8 +30,6 @@ pub struct BannerRect { stride: usize, framebuffer: Vec, - - font_collection: Option>, } impl BannerRect { @@ -42,15 +40,9 @@ impl BannerRect { stride: 0, framebuffer: vec![], - - font_collection: None, } } - pub(crate) fn set_font_collection(&mut self, font_collection: Arc) { - self.font_collection = Some(font_collection); - } - pub(crate) fn notification(&self) -> &Notification { &self.data } @@ -72,7 +64,7 @@ impl BannerRect { file.write_all(&self.framebuffer).unwrap(); } - pub(crate) fn draw(&mut self) { + pub(crate) fn draw(&mut self, font_collection: &FontCollection) { let (mut width, mut height) = ( CONFIG.general().width() as usize, CONFIG.general().height() as usize, @@ -100,12 +92,19 @@ impl BannerRect { offset.x += img_width; - let summary = self.draw_summary(offset.clone(), width, height, colors, display); + let summary = self.draw_summary( + offset.clone(), + width, + height, + colors, + font_collection, + display, + ); height -= summary.height(); offset.y += summary.height(); - let _ = self.draw_text(offset, width, height, colors, display); + let _ = self.draw_text(offset, width, height, colors, font_collection, display); self.draw_border( CONFIG.general().width().into(), @@ -200,6 +199,7 @@ impl BannerRect { width: usize, height: usize, colors: &Colors, + font_collection: &FontCollection, display: &DisplayConfig, ) -> TextRect { let title_cfg = display.title(); @@ -210,7 +210,7 @@ impl BannerRect { let mut summary = TextRect::from_str( &self.data.summary, CONFIG.general().font().size() as f32, - self.font_collection.as_ref().cloned().unwrap(), + font_collection, ); summary.set_margin(title_cfg.margin()); @@ -238,6 +238,7 @@ impl BannerRect { width: usize, height: usize, colors: &Colors, + font_collection: &FontCollection, display: &DisplayConfig, ) -> TextRect { let body_cfg = display.body(); @@ -246,17 +247,9 @@ impl BannerRect { let background: Bgra = colors.background().into(); let mut text = if display.markup() { - TextRect::from_text( - &self.data.body, - font_size, - self.font_collection.as_ref().cloned().unwrap(), - ) + TextRect::from_text(&self.data.body, font_size, font_collection) } else { - TextRect::from_str( - &self.data.body.body, - font_size, - self.font_collection.as_ref().cloned().unwrap(), - ) + TextRect::from_str(&self.data.body.body, font_size, font_collection) }; text.set_margin(body_cfg.margin()); diff --git a/src/backend/render/text.rs b/src/backend/render/text.rs index f47dc0d..9e68da5 100644 --- a/src/backend/render/text.rs +++ b/src/backend/render/text.rs @@ -1,4 +1,4 @@ -use std::{collections::VecDeque, sync::Arc}; +use std::collections::VecDeque; use derive_builder::Builder; use itertools::Itertools; @@ -36,11 +36,7 @@ pub(crate) struct TextRect { } impl TextRect { - pub(crate) fn from_str( - string: &str, - px_size: f32, - font_collection: Arc, - ) -> Self { + pub(crate) fn from_str(string: &str, px_size: f32, font_collection: &FontCollection) -> Self { let glyph_collection: Vec = string .chars() .map(|ch| font_collection.load_glyph_by_style(&FontStyle::Regular, ch, px_size)) @@ -49,18 +45,14 @@ impl TextRect { let words = Self::convert_to_words(glyph_collection); Self { words, - spacebar_width: Self::get_spacebar_width(&font_collection, px_size), + spacebar_width: Self::get_spacebar_width(font_collection, px_size), ellipsis: font_collection.get_ellipsis(px_size), line_height: font_collection.max_height(px_size), ..Default::default() } } - pub(crate) fn from_text( - text: &Text, - px_size: f32, - font_collection: Arc, - ) -> Self { + pub(crate) fn from_text(text: &Text, px_size: f32, font_collection: &FontCollection) -> Self { let Text { body, entities } = text; let mut entities = VecDeque::from_iter(entities.iter()); @@ -99,7 +91,7 @@ impl TextRect { let words = Self::convert_to_words(glyph_collection); Self { words, - spacebar_width: Self::get_spacebar_width(&font_collection, px_size), + spacebar_width: Self::get_spacebar_width(font_collection, px_size), ellipsis: font_collection.get_ellipsis(px_size), line_height: font_collection.max_height(px_size), ..Default::default() @@ -115,7 +107,7 @@ impl TextRect { .collect() } - fn get_spacebar_width(font_collection: &Arc, px_size: f32) -> usize { + fn get_spacebar_width(font_collection: &FontCollection, px_size: f32) -> usize { font_collection.get_spacebar_width(px_size).round() as usize } diff --git a/src/backend/render/window.rs b/src/backend/render/window.rs index 9915410..4e4e9c5 100644 --- a/src/backend/render/window.rs +++ b/src/backend/render/window.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::Write, os::fd::AsFd, sync::Arc}; +use std::{fs::File, io::Write, os::fd::AsFd}; use crate::{ config::{self, CONFIG}, @@ -30,7 +30,7 @@ pub(crate) struct WindowManager { qhandle: Option>, window: Option, - font_collection: Arc, + font_collection: FontCollection, banner_stack: Vec, events: Vec, @@ -39,9 +39,7 @@ pub(crate) struct WindowManager { impl WindowManager { pub(crate) fn init() -> Result { let connection = Connection::connect_to_env()?; - let font_collection = Arc::new(FontCollection::load_by_font_name( - CONFIG.general().font().name(), - )?); + let font_collection = FontCollection::load_by_font_name(CONFIG.general().font().name())?; Ok(Self { connection, @@ -68,8 +66,7 @@ impl WindowManager { .into_iter() .map(|notification| { let mut banner_rect = BannerRect::init(notification); - banner_rect.set_font_collection(self.font_collection.clone()); - banner_rect.draw(); + banner_rect.draw(&self.font_collection); banner_rect }) .collect(); @@ -263,7 +260,7 @@ impl WindowManager { let notification = notifications.remove(notification_index); let rect = &mut self.banner_stack[stack_index]; rect.update_data(notification); - rect.draw(); + rect.draw(&self.font_collection); } }