Fix text view size with multiline content

This commit is contained in:
Alexandre Bury 2015-05-18 11:18:04 -07:00
parent 74f0fee9b6
commit 0302e74063

View File

@ -1,3 +1,5 @@
use std::cmp;
use vec2::Vec2; use vec2::Vec2;
use view::{View,DimensionRequest,SizeRequest}; use view::{View,DimensionRequest,SizeRequest};
use div::*; use div::*;
@ -44,6 +46,18 @@ impl TextView {
.find(|w| self.get_num_lines(*w) <= max_height) .find(|w| self.get_num_lines(*w) <= max_height)
.unwrap() .unwrap()
} }
fn get_ideal_size(&self) -> Vec2 {
let mut maxWidth = 0;
let mut height = 0;
for line in self.content.split("\n") {
height += 1;
maxWidth = cmp::max(maxWidth, line.len() as u32);
}
Vec2::new(maxWidth, height)
}
} }
struct LinesIterator<'a> { struct LinesIterator<'a> {
@ -119,8 +133,10 @@ impl View for TextView {
Vec2::new(w, h) Vec2::new(w, h)
}, },
(DimensionRequest::AtMost(w),_) => { (DimensionRequest::AtMost(w),_) => {
if w >= self.content.len() as u32 { let ideal = self.get_ideal_size();
Vec2::new(self.content.len() as u32, 1)
if w >= ideal.x {
ideal
} else { } else {
let h = self.get_num_lines(w as usize) as u32; let h = self.get_num_lines(w as usize) as u32;
Vec2::new(w, h) Vec2::new(w, h)