Skip to content

Commit

Permalink
refactor: remove Arc for FontCollection in WindowManager
Browse files Browse the repository at this point in the history
  • Loading branch information
JarKz committed Jul 30, 2024
1 parent b20a227 commit 96592a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
39 changes: 16 additions & 23 deletions src/backend/render/banner.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -30,8 +30,6 @@ pub struct BannerRect {

stride: usize,
framebuffer: Vec<u8>,

font_collection: Option<Arc<FontCollection>>,
}

impl BannerRect {
Expand All @@ -42,15 +40,9 @@ impl BannerRect {

stride: 0,
framebuffer: vec![],

font_collection: None,
}
}

pub(crate) fn set_font_collection(&mut self, font_collection: Arc<FontCollection>) {
self.font_collection = Some(font_collection);
}

pub(crate) fn notification(&self) -> &Notification {
&self.data
}
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -200,6 +199,7 @@ impl BannerRect {
width: usize,
height: usize,
colors: &Colors,
font_collection: &FontCollection,
display: &DisplayConfig,
) -> TextRect {
let title_cfg = display.title();
Expand All @@ -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());
Expand Down Expand Up @@ -238,6 +238,7 @@ impl BannerRect {
width: usize,
height: usize,
colors: &Colors,
font_collection: &FontCollection,
display: &DisplayConfig,
) -> TextRect {
let body_cfg = display.body();
Expand All @@ -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());
Expand Down
20 changes: 6 additions & 14 deletions src/backend/render/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::VecDeque, sync::Arc};
use std::collections::VecDeque;

use derive_builder::Builder;
use itertools::Itertools;
Expand Down Expand Up @@ -36,11 +36,7 @@ pub(crate) struct TextRect {
}

impl TextRect {
pub(crate) fn from_str(
string: &str,
px_size: f32,
font_collection: Arc<FontCollection>,
) -> Self {
pub(crate) fn from_str(string: &str, px_size: f32, font_collection: &FontCollection) -> Self {
let glyph_collection: Vec<Glyph> = string
.chars()
.map(|ch| font_collection.load_glyph_by_style(&FontStyle::Regular, ch, px_size))
Expand All @@ -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<FontCollection>,
) -> 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());
Expand Down Expand Up @@ -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()
Expand All @@ -115,7 +107,7 @@ impl TextRect {
.collect()
}

fn get_spacebar_width(font_collection: &Arc<FontCollection>, 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
}

Expand Down
13 changes: 5 additions & 8 deletions src/backend/render/window.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -30,7 +30,7 @@ pub(crate) struct WindowManager {
qhandle: Option<QueueHandle<Window>>,
window: Option<Window>,

font_collection: Arc<FontCollection>,
font_collection: FontCollection,

banner_stack: Vec<BannerRect>,
events: Vec<RendererMessage>,
Expand All @@ -39,9 +39,7 @@ pub(crate) struct WindowManager {
impl WindowManager {
pub(crate) fn init() -> Result<Self> {
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,
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 96592a4

Please sign in to comment.