Move cache to text box state

This commit is contained in:
Jeremy Soller 2022-10-27 20:31:05 -06:00
parent 4e2e41470b
commit c247bbdad2
2 changed files with 7 additions and 13 deletions

View file

@ -14,7 +14,6 @@ use cosmic::{
horizontal_space, horizontal_space,
pick_list, pick_list,
row, row,
text,
}, },
}, },
settings, settings,
@ -27,7 +26,6 @@ use cosmic_text::{
Attrs, Attrs,
AttrsList, AttrsList,
FontSystem, FontSystem,
SwashCache,
TextBuffer, TextBuffer,
TextMetrics, TextMetrics,
}; };
@ -70,7 +68,6 @@ pub struct Window {
path_opt: Option<PathBuf>, path_opt: Option<PathBuf>,
attrs: Attrs<'static>, attrs: Attrs<'static>,
buffer: Mutex<TextBuffer<'static>>, buffer: Mutex<TextBuffer<'static>>,
cache: Mutex<SwashCache<'static>>,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -119,14 +116,11 @@ impl Application for Window {
FONT_SIZES[1 /* Body */], FONT_SIZES[1 /* Body */],
); );
let cache = SwashCache::new(&FONT_SYSTEM);
let mut window = Window { let mut window = Window {
theme: Theme::Dark, theme: Theme::Dark,
path_opt: None, path_opt: None,
attrs, attrs,
buffer: Mutex::new(buffer), buffer: Mutex::new(buffer),
cache: Mutex::new(cache),
}; };
if let Some(arg) = env::args().nth(1) { if let Some(arg) = env::args().nth(1) {
window.open(PathBuf::from(arg)); window.open(PathBuf::from(arg));
@ -262,7 +256,7 @@ impl Application for Window {
.align_items(Alignment::Center) .align_items(Alignment::Center)
.spacing(8) .spacing(8)
, ,
text_box(&self.buffer, &self.cache) text_box(&self.buffer)
] ]
.spacing(8) .spacing(8)
.padding(16) .padding(16)

View file

@ -48,20 +48,18 @@ impl StyleSheet for Theme {
pub struct TextBox<'a> { pub struct TextBox<'a> {
buffer: &'a Mutex<TextBuffer<'static>>, buffer: &'a Mutex<TextBuffer<'static>>,
cache: &'a Mutex<SwashCache<'static>>,
} }
impl<'a> TextBox<'a> { impl<'a> TextBox<'a> {
pub fn new(buffer: &'a Mutex<TextBuffer<'static>>, cache: &'a Mutex<SwashCache<'static>>) -> Self { pub fn new(buffer: &'a Mutex<TextBuffer<'static>>) -> Self {
Self { Self {
buffer, buffer,
cache,
} }
} }
} }
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>, cache: &'a Mutex<SwashCache<'static>>) -> TextBox<'a> { pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
TextBox::new(buffer, cache) TextBox::new(buffer)
} }
impl<'a, Message, Renderer> Widget<Message, Renderer> for TextBox<'a> impl<'a, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
@ -164,7 +162,7 @@ where
let mut pixels = vec![0; layout_w as usize * layout_h as usize * 4]; 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; let alpha = (color.0 >> 24) & 0xFF;
if alpha == 0 { if alpha == 0 {
// Do not draw if alpha is zero // Do not draw if alpha is zero
@ -343,6 +341,7 @@ where
pub struct State { pub struct State {
is_dragging: bool, is_dragging: bool,
cache: Mutex<SwashCache<'static>>,
pixels_opt: Mutex<Option<(u32, u32, Vec<u8>)>>, pixels_opt: Mutex<Option<(u32, u32, Vec<u8>)>>,
} }
@ -351,6 +350,7 @@ impl State {
pub fn new() -> State { pub fn new() -> State {
State { State {
is_dragging: false, is_dragging: false,
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)),
pixels_opt: Mutex::new(None), pixels_opt: Mutex::new(None),
} }
} }