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,
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<PathBuf>,
attrs: Attrs<'static>,
buffer: Mutex<TextBuffer<'static>>,
cache: Mutex<SwashCache<'static>>,
}
#[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)

View file

@ -48,20 +48,18 @@ impl StyleSheet for Theme {
pub struct TextBox<'a> {
buffer: &'a Mutex<TextBuffer<'static>>,
cache: &'a Mutex<SwashCache<'static>>,
}
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 {
buffer,
cache,
}
}
}
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>, cache: &'a Mutex<SwashCache<'static>>) -> TextBox<'a> {
TextBox::new(buffer, cache)
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
TextBox::new(buffer)
}
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];
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<SwashCache<'static>>,
pixels_opt: Mutex<Option<(u32, u32, Vec<u8>)>>,
}
@ -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),
}
}