Cleanup text_view

This commit is contained in:
Alexandre Bury 2019-08-16 10:15:00 -07:00
parent dc10fd8c44
commit 63302bf01b
2 changed files with 15 additions and 39 deletions

View File

@ -155,13 +155,8 @@ impl Backend {
// Find if we have this color in stock // Find if we have this color in stock
let result = find_closest_pair(pair); let result = find_closest_pair(pair);
let lookup = pairs.get(&result); let lookup = pairs.get(&result).copied();
if lookup.is_some() { lookup.unwrap_or_else(|| self.insert_color(&mut *pairs, result))
// We got it!
*lookup.unwrap()
} else {
self.insert_color(&mut *pairs, result)
}
} }
fn set_colors(&self, pair: ColorPair) { fn set_colors(&self, pair: ColorPair) {

View File

@ -1,6 +1,4 @@
use std::cell::RefCell;
use std::ops::Deref; use std::ops::Deref;
use std::ptr;
use std::sync::Arc; use std::sync::Arc;
use std::sync::{Mutex, MutexGuard}; use std::sync::{Mutex, MutexGuard};
@ -15,7 +13,7 @@ use crate::view::{SizeCache, View};
use crate::{Printer, Vec2, With, XY}; use crate::{Printer, Vec2, With, XY};
// Content type used internally for caching and storage // Content type used internally for caching and storage
type InnerContentType = RefCell<Arc<StyledString>>; type InnerContentType = Arc<StyledString>;
/// Provides access to the content of a [`TextView`]. /// Provides access to the content of a [`TextView`].
/// ///
@ -51,8 +49,8 @@ impl TextContent {
TextContent { TextContent {
content: Arc::new(Mutex::new(TextContentInner { content: Arc::new(Mutex::new(TextContentInner {
content_value: RefCell::new(content), content_value: content,
content_cache: RefCell::new(Arc::new(StyledString::default())), content_cache: Arc::new(StyledString::default()),
size_cache: None, size_cache: None,
})), })),
} }
@ -66,7 +64,6 @@ impl TextContent {
/// [`StyledString`]: ../utils/markup/type.StyledString.html /// [`StyledString`]: ../utils/markup/type.StyledString.html
/// ///
/// This keeps the content locked. Do not store this! /// This keeps the content locked. Do not store this!
pub struct TextContentRef { pub struct TextContentRef {
_handle: OwningHandle< _handle: OwningHandle<
ArcRef<Mutex<TextContentInner>>, ArcRef<Mutex<TextContentInner>>,
@ -92,7 +89,7 @@ impl TextContent {
S: Into<StyledString>, S: Into<StyledString>,
{ {
self.with_content(|c| { self.with_content(|c| {
c.content_value.replace(Arc::new(content.into())) *Arc::make_mut(&mut c.content_value) = content.into();
}); });
} }
@ -104,13 +101,7 @@ impl TextContent {
self.with_content(|c| { self.with_content(|c| {
// This will only clone content if content_cached and content_value // This will only clone content if content_cached and content_value
// are sharing the same underlying Rc. // are sharing the same underlying Rc.
if c.is_content_shared() { Arc::make_mut(&mut c.content_value).append(content);
c.content_value
.replace(Arc::new(c.get_value().as_ref().clone()));
};
Arc::get_mut(&mut c.content_value.borrow_mut())
.expect("should not have a shared content here")
.append(content)
}) })
} }
@ -126,11 +117,11 @@ impl TextContent {
where where
F: FnOnce(&mut TextContentInner) -> O, F: FnOnce(&mut TextContentInner) -> O,
{ {
let mut lock = self.content.lock().unwrap(); let mut content = self.content.lock().unwrap();
let out = f(&mut lock); let out = f(&mut content);
lock.size_cache = None; content.size_cache = None;
out out
} }
@ -160,7 +151,7 @@ impl TextContentInner {
(*mutex).lock().unwrap() (*mutex).lock().unwrap()
}); });
let data = _handle.get_value(); let data = Arc::clone(&_handle.content_value);
TextContentRef { _handle, data } TextContentRef { _handle, data }
} }
@ -172,16 +163,8 @@ impl TextContentInner {
} }
} }
fn get_value(&self) -> Arc<StyledString> { fn get_cache(&self) -> &InnerContentType {
Arc::clone(&*self.content_value.borrow()) &self.content_cache
}
fn get_cache(&self) -> Arc<StyledString> {
Arc::clone(&*self.content_cache.borrow())
}
fn is_content_shared(&self) -> bool {
ptr::eq(self.get_value().as_ref(), self.get_cache().as_ref())
} }
} }
@ -239,7 +222,7 @@ impl TextView {
/// ``` /// ```
pub fn new_with_content(content: TextContent) -> Self { pub fn new_with_content(content: TextContent) -> Self {
TextView { TextView {
content: content, content,
effect: Effect::Simple, effect: Effect::Simple,
rows: Vec::new(), rows: Vec::new(),
wrap: true, wrap: true,
@ -360,9 +343,7 @@ impl TextView {
// Completely bust the cache // Completely bust the cache
// Just in case we fail, we don't want to leave a bad cache. // Just in case we fail, we don't want to leave a bad cache.
content.size_cache = None; content.size_cache = None;
content content.content_cache = Arc::clone(&content.content_value);
.content_cache
.replace(content.content_value.borrow().clone());
if size.x == 0 { if size.x == 0 {
// Nothing we can do at this point. // Nothing we can do at this point.