From c247bbdad2f9842715fe5cf0a135275540624270 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 27 Oct 2022 20:31:05 -0600 Subject: [PATCH] Move cache to text box state --- examples/editor-libcosmic/src/main.rs | 8 +------- examples/editor-libcosmic/src/text_box.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 1c395c9..83b0845 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -14,7 +14,6 @@ use cosmic::{ horizontal_space, pick_list, row, - text, }, }, settings, @@ -27,7 +26,6 @@ use cosmic_text::{ Attrs, AttrsList, FontSystem, - SwashCache, TextBuffer, TextMetrics, }; @@ -70,7 +68,6 @@ pub struct Window { path_opt: Option, attrs: Attrs<'static>, buffer: Mutex>, - cache: Mutex>, } #[allow(dead_code)] @@ -119,14 +116,11 @@ impl Application for Window { FONT_SIZES[1 /* Body */], ); - let cache = SwashCache::new(&FONT_SYSTEM); - let mut window = Window { theme: Theme::Dark, path_opt: None, attrs, buffer: Mutex::new(buffer), - cache: Mutex::new(cache), }; if let Some(arg) = env::args().nth(1) { window.open(PathBuf::from(arg)); @@ -262,7 +256,7 @@ impl Application for Window { .align_items(Alignment::Center) .spacing(8) , - text_box(&self.buffer, &self.cache) + text_box(&self.buffer) ] .spacing(8) .padding(16) diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index 86062a8..944a515 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -48,20 +48,18 @@ impl StyleSheet for Theme { pub struct TextBox<'a> { buffer: &'a Mutex>, - cache: &'a Mutex>, } impl<'a> TextBox<'a> { - pub fn new(buffer: &'a Mutex>, cache: &'a Mutex>) -> Self { + pub fn new(buffer: &'a Mutex>) -> Self { Self { buffer, - cache, } } } -pub fn text_box<'a>(buffer: &'a Mutex>, cache: &'a Mutex>) -> TextBox<'a> { - TextBox::new(buffer, cache) +pub fn text_box<'a>(buffer: &'a Mutex>) -> TextBox<'a> { + TextBox::new(buffer) } impl<'a, Message, Renderer> Widget for TextBox<'a> @@ -164,7 +162,7 @@ where let mut pixels = vec![0; layout_w as usize * layout_h as usize * 4]; - buffer.draw(&mut self.cache.lock().unwrap(), text_color, |start_x, start_y, w, h, color| { + buffer.draw(&mut state.cache.lock().unwrap(), text_color, |start_x, start_y, w, h, color| { let alpha = (color.0 >> 24) & 0xFF; if alpha == 0 { // Do not draw if alpha is zero @@ -343,6 +341,7 @@ where pub struct State { is_dragging: bool, + cache: Mutex>, pixels_opt: Mutex)>>, } @@ -351,6 +350,7 @@ impl State { pub fn new() -> State { State { is_dragging: false, + cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)), pixels_opt: Mutex::new(None), } }