Implemented Expander

- Updated example to show behavior
- Created styles for Expander and ExpanderRow
- Simpler implementation of `ExpanderRow`
- Deleted `ExpanderData` and replaced it with `ExpanderRow`
- Every row can now have child rows.
- Ran cargo fmt.
- Deleted settings example
- Added expander to cosmic example
- Expander icons now render

ListBox partially implemented
This commit is contained in:
Eduardo Flores 2022-10-09 11:25:46 -07:00 committed by Michael Murphy
parent a50294676d
commit 7743d0d084
22 changed files with 1222 additions and 738 deletions

View file

@ -93,7 +93,12 @@ impl<'a> TextBuffer<'a> {
self.layout_lines.clear();
for line in self.shape_lines.iter() {
let layout_i = self.layout_lines.len();
line.layout(self.font_size, self.line_width, &mut self.layout_lines, layout_i);
line.layout(
self.font_size,
self.line_width,
&mut self.layout_lines,
layout_i,
);
}
self.redraw = true;
@ -173,7 +178,7 @@ impl<'a> TextBuffer<'a> {
self.cursor.glyph -= 1;
self.redraw = true;
}
},
}
TextAction::Right => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph > line.glyphs.len() {
@ -184,19 +189,19 @@ impl<'a> TextBuffer<'a> {
self.cursor.glyph += 1;
self.redraw = true;
}
},
}
TextAction::Up => {
if self.cursor.line > 0 {
self.cursor.line -= 1;
self.redraw = true;
}
},
}
TextAction::Down => {
if self.cursor.line + 1 < self.layout_lines.len() {
self.cursor.line += 1;
self.redraw = true;
}
},
}
TextAction::Backspace => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph > line.glyphs.len() {
@ -210,7 +215,7 @@ impl<'a> TextBuffer<'a> {
text_line.remove(glyph.start);
self.reshape_line(line.line_i);
}
},
}
TextAction::Delete => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph < line.glyphs.len() {
@ -219,7 +224,7 @@ impl<'a> TextBuffer<'a> {
text_line.remove(glyph.start);
self.reshape_line(line.line_i);
}
},
}
TextAction::Insert(character) => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph >= line.glyphs.len() {
@ -229,7 +234,7 @@ impl<'a> TextBuffer<'a> {
text_line.insert(glyph.end, character);
self.cursor.glyph += 1;
self.reshape_line(line.line_i);
},
}
None => {
let text_line = &mut self.text_lines[line.line_i.get()];
text_line.push(character);
@ -244,7 +249,7 @@ impl<'a> TextBuffer<'a> {
self.cursor.glyph += 1;
self.reshape_line(line.line_i);
}
},
}
}
}
}

View file

@ -29,9 +29,7 @@ impl<'a> FontLayoutLine<'a> {
let y = bb.min.y as i32;
outline.draw(|off_x, off_y, v| {
//TODO: ensure v * 255.0 does not overflow!
let color =
((v * 255.0) as u32) << 24 |
base & 0xFFFFFF;
let color = ((v * 255.0) as u32) << 24 | base & 0xFFFFFF;
f(x + off_x as i32, y + off_y as i32, color);
});
}
@ -42,9 +40,7 @@ impl<'a> FontLayoutLine<'a> {
let y = bb.min.y;
glyph.inner.draw(|off_x, off_y, v| {
//TODO: ensure v * 255.0 does not overflow!
let color =
((v * 255.0) as u32) << 24 |
base & 0xFFFFFF;
let color = ((v * 255.0) as u32) << 24 | base & 0xFFFFFF;
f(x + off_x as i32, y + off_y as i32, color);
});
}

View file

@ -1,4 +1,4 @@
use super::{Font, FontLineIndex, FontShapeGlyph, FontShapeWord, FontShapeLine, FontShapeSpan};
use super::{Font, FontLineIndex, FontShapeGlyph, FontShapeLine, FontShapeSpan, FontShapeWord};
pub struct FontMatches<'a> {
pub fonts: Vec<Font<'a>>,
@ -22,7 +22,7 @@ impl<'a> FontMatches<'a> {
let rtl = match buffer.direction() {
rustybuzz::Direction::RightToLeft => true,
//TODO: other directions?
_ => false,
_ => false,
};
assert_eq!(rtl, span_rtl);
@ -177,7 +177,14 @@ impl<'a> FontMatches<'a> {
FontShapeWord { blank, glyphs }
}
fn shape_span(&self, line: &str, start_span: usize, end_span: usize, line_rtl: bool, span_rtl: bool) -> FontShapeSpan {
fn shape_span(
&self,
line: &str,
start_span: usize,
end_span: usize,
line_rtl: bool,
span_rtl: bool,
) -> FontShapeSpan {
let span = &line[start_span..end_span];
log::debug!(" Span {}: '{}'", if span_rtl { "RTL" } else { "LTR" }, span);
@ -251,10 +258,6 @@ impl<'a> FontMatches<'a> {
line_rtl
};
FontShapeLine {
line_i,
rtl,
spans,
}
FontShapeLine { line_i, rtl, spans }
}
}

View file

@ -39,10 +39,7 @@ impl<'a> FontShapeGlyph<'a> {
#[cfg(feature = "rusttype")]
let inner = self.font.rusttype.glyph(self.inner)
.scaled(rusttype::Scale::uniform(font_size as f32))
.positioned(rusttype::point(
x + x_offset,
y - y_offset,
));
.positioned(rusttype::point(x + x_offset, y - y_offset));
#[cfg(feature = "swash")]
let inner = CacheKey::new(
@ -79,7 +76,13 @@ pub struct FontShapeLine<'a> {
}
impl<'a> FontShapeLine<'a> {
pub fn layout(&self, font_size: i32, line_width: i32, layout_lines: &mut Vec<FontLayoutLine<'a>>, mut layout_i: usize) {
pub fn layout(
&self,
font_size: i32,
line_width: i32,
layout_lines: &mut Vec<FontLayoutLine<'a>>,
mut layout_i: usize,
) {
let mut push_line = true;
let mut glyphs = Vec::new();
@ -128,7 +131,7 @@ impl<'a> FontShapeLine<'a> {
fit_x += word_size;
}
}
if ! word_ranges.is_empty() {
if !word_ranges.is_empty() {
while fitting_end > 0 {
if span.words[fitting_end - 1].blank {
fitting_end -= 1;
@ -174,6 +177,33 @@ impl<'a> FontShapeLine<'a> {
for (range, wrap) in word_ranges {
for word in span.words[range].iter() {
let mut word_size = 0.0;
for glyph in word.glyphs.iter() {
word_size += font_size as f32 * glyph.x_advance;
}
//TODO: make wrapping optional
let wrap = if self.rtl {
x - word_size < end_x
} else {
x + word_size > end_x
};
if wrap && !glyphs.is_empty() {
let mut glyphs_swap = Vec::new();
std::mem::swap(&mut glyphs, &mut glyphs_swap);
layout_lines.insert(
layout_i,
FontLayoutLine {
line_i: self.line_i,
glyphs: glyphs_swap,
},
);
layout_i += 1;
x = start_x;
y = 0.0;
}
for glyph in word.glyphs.iter() {
let x_advance = font_size as f32 * glyph.x_advance;
let y_advance = font_size as f32 * glyph.y_advance;
@ -185,7 +215,7 @@ impl<'a> FontShapeLine<'a> {
glyphs.push(glyph.layout(font_size, x, y));
push_line = true;
if ! self.rtl {
if !self.rtl {
x += x_advance;
}
y += y_advance;
@ -195,10 +225,13 @@ impl<'a> FontShapeLine<'a> {
if wrap {
let mut glyphs_swap = Vec::new();
std::mem::swap(&mut glyphs, &mut glyphs_swap);
layout_lines.insert(layout_i, FontLayoutLine {
line_i: self.line_i,
glyphs: glyphs_swap
});
layout_lines.insert(
layout_i,
FontLayoutLine {
line_i: self.line_i,
glyphs: glyphs_swap,
},
);
layout_i += 1;
x = start_x;
@ -208,10 +241,13 @@ impl<'a> FontShapeLine<'a> {
}
if push_line {
layout_lines.insert(layout_i, FontLayoutLine {
line_i: self.line_i,
glyphs
});
layout_lines.insert(
layout_i,
FontLayoutLine {
line_i: self.line_i,
glyphs,
},
);
}
}
}

View file

@ -18,7 +18,7 @@ fn main() {
Ok((w, h)) => {
eprintln!("Display size: {}, {}", w, h);
(h as i32 / 1600) + 1
},
}
Err(err) => {
eprintln!("Failed to get display size: {}", err);
1
@ -31,8 +31,9 @@ fn main() {
1024 * display_scale as u32,
768 * display_scale as u32,
"COSMIC TEXT",
&[WindowFlag::Resizable]
).unwrap();
&[WindowFlag::Resizable],
)
.unwrap();
let font_system = FontSystem::new();
@ -122,22 +123,27 @@ fn main() {
let mut new_cursor_opt = None;
let mut line_y = line_height;
for (line_i, line) in buffer.layout_lines().iter().skip(scroll as usize).enumerate() {
for (line_i, line) in buffer
.layout_lines()
.iter()
.skip(scroll as usize)
.enumerate()
{
if line_y >= window.height() as i32 {
break;
}
if mouse_left
&& mouse_y >= line_y - font_size
&& mouse_y < line_y - font_size + line_height
&& mouse_y >= line_y - font_size
&& mouse_y < line_y - font_size + line_height
{
let new_cursor_line = line_i + scroll as usize;
let mut new_cursor_glyph = line.glyphs.len();
for (glyph_i, glyph) in line.glyphs.iter().enumerate() {
if mouse_x >= line_x + glyph.x as i32
&& mouse_x <= line_x + (glyph.x + glyph.w) as i32
&& mouse_x <= line_x + (glyph.x + glyph.w) as i32
{
new_cursor_glyph = glyph_i;
new_cursor_glyph = glyph_i;
}
}
new_cursor_opt = Some(TextCursor::new(new_cursor_line, new_cursor_glyph));
@ -181,14 +187,14 @@ fn main() {
if buffer.cursor.glyph >= line.glyphs.len() {
let x = match line.glyphs.last() {
Some(glyph) => glyph.x + glyph.w,
None => 0.0
None => 0.0,
};
window.rect(
line_x + x as i32,
line_y - font_size,
(font_size / 2) as u32,
line_height as u32,
Color::rgba(0xFF, 0xFF, 0xFF, 0x20)
Color::rgba(0xFF, 0xFF, 0xFF, 0x20),
);
} else {
let glyph = &line.glyphs[buffer.cursor.glyph];
@ -197,7 +203,7 @@ fn main() {
line_y - font_size,
glyph.w as u32,
line_height as u32,
Color::rgba(0xFF, 0xFF, 0xFF, 0x20)
Color::rgba(0xFF, 0xFF, 0xFF, 0x20),
);
let text_line = &buffer.text_lines()[line.line_i.get()];
@ -269,24 +275,26 @@ fn main() {
orbclient::K_MINUS if event.pressed && ctrl_pressed => if font_size_i > 0 {
font_size_i -= 1;
buffer.set_font_size(font_sizes[font_size_i].0 * display_scale);
},
orbclient::K_EQUALS if event.pressed && ctrl_pressed => if font_size_i + 1 < font_sizes.len() {
font_size_i += 1;
buffer.set_font_size(font_sizes[font_size_i].0 * display_scale);
},
_ => (),
}
orbclient::K_EQUALS if event.pressed && ctrl_pressed => {
if font_size_i + 1 < font_sizes.len() {
font_size_i += 1;
buffer.set_font_size(font_sizes[font_size_i].0 * display_scale);
}
}
_ => (),
}
},
EventOption::TextInput(event) if ! ctrl_pressed => {
EventOption::TextInput(event) if !ctrl_pressed => {
buffer.action(TextAction::Insert(event.character));
},
}
EventOption::Mouse(event) => {
mouse_x = event.x;
mouse_y = event.y;
if mouse_left {
rehit = true;
}
},
}
EventOption::Button(event) => {
if event.left != mouse_left {
mouse_left = event.left;
@ -297,11 +305,11 @@ fn main() {
}
EventOption::Resize(event) => {
buffer.set_line_width(event.width as i32 - line_x * 2);
},
}
EventOption::Scroll(event) => {
scroll -= event.y * 3;
buffer.redraw = true;
},
}
EventOption::Quit(_) => return,
_ => (),
}