Skip to content

Commit 33d65fb

Browse files
committed
reduce layout to one step. clean up code
1 parent 9379c80 commit 33d65fb

File tree

61 files changed

+442
-417
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+442
-417
lines changed

anathema-backend/src/lib.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use anathema_widgets::layout::{Constraints, LayoutCtx, LayoutFilter, PositionFil
99
use anathema_widgets::paint::PaintFilter;
1010
use anathema_widgets::{GlyphMap, LayoutForEach, PaintChildren, PositionChildren, WidgetTreeView};
1111

12-
pub mod tui;
1312
pub mod testing;
13+
pub mod tui;
1414

1515
pub trait Backend {
1616
fn size(&self) -> Size;
@@ -55,54 +55,45 @@ impl<'rt, 'bp, T: Backend> WidgetCycle<'rt, 'bp, T> {
5555
}
5656
}
5757

58-
fn floating(&mut self, ctx: &mut LayoutCtx<'_, 'bp>, needs_layout: bool) -> Result<()> {
58+
fn fixed(&mut self, ctx: &mut LayoutCtx<'_, 'bp>, needs_layout: bool) -> Result<()> {
5959
// -----------------------------------------------------------------------------
6060
// - Layout -
6161
// -----------------------------------------------------------------------------
6262
if needs_layout {
63-
let filter = LayoutFilter::floating();
63+
let filter = LayoutFilter::all();
6464
self.layout(ctx, filter)?;
6565
}
6666

6767
// -----------------------------------------------------------------------------
6868
// - Position -
6969
// -----------------------------------------------------------------------------
70-
self.position(ctx.attribute_storage, *ctx.viewport, PositionFilter::floating());
70+
self.position(ctx.attribute_storage, *ctx.viewport, PositionFilter::fixed());
7171

7272
// -----------------------------------------------------------------------------
7373
// - Paint -
7474
// -----------------------------------------------------------------------------
75-
self.paint(ctx, PaintFilter::floating());
75+
self.paint(ctx, PaintFilter::fixed());
7676

7777
Ok(())
7878
}
7979

80-
fn fixed(&mut self, ctx: &mut LayoutCtx<'_, 'bp>, needs_layout: bool) -> Result<()> {
81-
// -----------------------------------------------------------------------------
82-
// - Layout -
83-
// -----------------------------------------------------------------------------
84-
if needs_layout {
85-
let filter = LayoutFilter::fixed();
86-
self.layout(ctx, filter)?;
87-
}
88-
80+
fn floating(&mut self, ctx: &mut LayoutCtx<'_, 'bp>) -> Result<()> {
8981
// -----------------------------------------------------------------------------
9082
// - Position -
9183
// -----------------------------------------------------------------------------
92-
self.position(ctx.attribute_storage, *ctx.viewport, PositionFilter::fixed());
84+
self.position(ctx.attribute_storage, *ctx.viewport, PositionFilter::floating());
9385

9486
// -----------------------------------------------------------------------------
9587
// - Paint -
9688
// -----------------------------------------------------------------------------
97-
self.paint(ctx, PaintFilter::fixed());
89+
self.paint(ctx, PaintFilter::floating());
9890

9991
Ok(())
10092
}
10193

10294
pub fn run(&mut self, ctx: &mut LayoutCtx<'_, 'bp>, needs_layout: bool) -> Result<()> {
10395
self.fixed(ctx, needs_layout)?;
104-
self.floating(ctx, needs_layout)?;
105-
96+
self.floating(ctx)?;
10697
Ok(())
10798
}
10899

anathema-backend/src/testing/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::time::Duration;
44
use anathema_geometry::Size;
55
use anathema_value_resolver::AttributeStorage;
66
use anathema_widgets::components::events::ComponentEvent;
7-
use anathema_widgets::paint::{paint, Glyph};
7+
use anathema_widgets::paint::{Glyph, paint};
88
use anathema_widgets::{GlyphMap, PaintChildren};
99
use surface::TestSurface;
1010

@@ -21,7 +21,7 @@ impl<'a> GlyphSomething<'a> {
2121
let Some(glyph) = self.inner else { return false };
2222
match glyph {
2323
Glyph::Single(lhs, _) => *lhs == rhs,
24-
Glyph::Cluster(glyph_index, _) => todo!(),
24+
Glyph::Cluster(_glyph_index, _) => todo!(),
2525
}
2626
}
2727
}
@@ -51,7 +51,9 @@ impl TestBackend {
5151
}
5252

5353
pub fn at(&self, x: usize, y: usize) -> GlyphSomething<'_> {
54-
GlyphSomething { inner: self.surface.get(x, y) }
54+
GlyphSomething {
55+
inner: self.surface.get(x, y),
56+
}
5557
}
5658
}
5759

@@ -60,7 +62,7 @@ impl Backend for TestBackend {
6062
self.surface.size
6163
}
6264

63-
fn next_event(&mut self, timeout: Duration) -> Option<ComponentEvent> {
65+
fn next_event(&mut self, _timeout: Duration) -> Option<ComponentEvent> {
6466
self.event_queue.pop_front()?
6567
}
6668

@@ -77,10 +79,9 @@ impl Backend for TestBackend {
7779
paint(&mut self.surface, glyph_map, widgets, attribute_storage);
7880
}
7981

80-
fn render(&mut self, glyph_map: &mut GlyphMap) {
82+
fn render(&mut self, _glyph_map: &mut GlyphMap) {
8183
// this does nothing here as everything written to the test buffer
8284
}
8385

84-
fn clear(&mut self) {
85-
}
86+
fn clear(&mut self) {}
8687
}

anathema-backend/src/testing/surface.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ pub struct TestSurface {
1212
impl TestSurface {
1313
pub fn new(size: Size) -> Self {
1414
let len = size.width * size.height;
15-
let mut lines = vec![Glyph::space(); len as usize];
16-
15+
let lines = vec![Glyph::space(); len as usize];
1716
Self { size, lines }
1817
}
1918

20-
pub(crate) fn resize(&mut self, new_size: Size, glyph_map: &mut GlyphMap) {
19+
pub(crate) fn resize(&mut self, new_size: Size, _glyph_map: &mut GlyphMap) {
2120
self.size = new_size;
22-
panic!("truncate and pop lines outside of the new size")
21+
todo!("truncate and pop lines outside of the new size")
2322
}
2423

2524
pub(crate) fn get(&self, x: usize, y: usize) -> Option<&Glyph> {
@@ -40,7 +39,7 @@ impl WidgetRenderer for TestSurface {
4039
self.set_style(style, local_pos)
4140
}
4241

43-
fn set_style(&mut self, style: Style, local_pos: Pos) {}
42+
fn set_style(&mut self, _style: Style, _local_pos: Pos) {}
4443

4544
fn size(&self) -> Size {
4645
self.size

anathema-backend/src/tui/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Buffer {
211211
// If this is a unicode char that is wider than one cell,
212212
// add a continuation cell if it fits, this way if we overwrite it
213213
// we can set the continuation cell to `Empty`.
214-
if pos.x < self.size.width as u16 {
214+
if pos.x < self.size.width {
215215
if let 2.. = c.width() {
216216
self.put(Cell::continuation(cell.style), LocalPos::new(pos.x + 1, pos.y));
217217
}

anathema-backend/src/tui/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Events {
1919
pub fn poll(&self, timeout: Duration) -> Option<ComponentEvent> {
2020
match crossterm::event::poll(timeout).ok()? {
2121
true => {
22-
let event = read().map(Into::into).ok()?;
22+
let event = read().ok()?;
2323

2424
let event = match event {
2525
CTEvent::Paste(_) => ComponentEvent::Noop,

anathema-backend/src/tui/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
//! It uses two buffers and only draws the diffs from top left to bottom right, making it less
44
//! likely to flicker when moving the cursor etc.
55
#![deny(missing_docs)]
6-
use std::fs::File;
7-
use std::io::{BufWriter, Stdout, Write};
6+
use std::io::{Stdout, Write};
87
use std::ops::Add;
98
use std::time::Duration;
109

anathema-backend/src/tui/screen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ impl Screen {
6767
/// Erase a specific region.
6868
/// Will reset the styles for all the cells as well.
6969
pub(crate) fn erase_region(&mut self, pos: LocalPos, size: Size) {
70-
let to_x = (size.width as u16 + pos.x).min(self.size().width as u16);
71-
let to_y = (size.height as u16 + pos.y).min(self.size().height as u16);
70+
let to_x = (size.width + pos.x).min(self.size().width);
71+
let to_y = (size.height + pos.y).min(self.size().height);
7272

7373
for x in pos.x.min(to_x)..to_x {
7474
for y in pos.y.min(to_y)..to_y {
@@ -169,7 +169,7 @@ mod test {
169169
for y in 0..size.height {
170170
let c = y.to_string().chars().next().unwrap();
171171
for x in 0..size.width {
172-
screen.paint_glyph(Glyph::from_char(c, 1), LocalPos::new(x as u16, y as u16));
172+
screen.paint_glyph(Glyph::from_char(c, 1), LocalPos::new(x, y));
173173
}
174174
}
175175

anathema-default-widgets/src/border.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl TryFrom<&ValueKind<'_>> for Sides {
6464
fn try_from(value: &ValueKind<'_>) -> Result<Self, Self::Error> {
6565
let mut sides = Sides::EMPTY;
6666
match value {
67-
ValueKind::Str(cow) => Sides::from_str(&*cow),
67+
ValueKind::Str(cow) => Sides::from_str(cow),
6868
ValueKind::List(list) => {
6969
for x in list {
7070
sides |= Sides::try_from(x)?;
@@ -85,7 +85,7 @@ impl TryFrom<&ValueKind<'_>> for Sides {
8585
}
8686
Ok(sides)
8787
}
88-
_ => return Err(()),
88+
_ => Err(()),
8989
}
9090
}
9191
}
@@ -259,16 +259,16 @@ impl BorderPainter {
259259
end_cap: (border_size.top_right > 0).then(|| Brush::new(glyphs[2], border_size.top_right)),
260260
start: LocalPos::ZERO,
261261
axis: Axis::Horizontal,
262-
end: size.width as u16,
262+
end: size.width,
263263
};
264264

265265
let bottom = Line {
266266
start_cap: (border_size.bottom_left > 0).then(|| Brush::new(glyphs[6], border_size.bottom_left)),
267267
middle: (border_size.bottom > 0).then(|| Brush::new(glyphs[5], border_size.bottom)),
268268
end_cap: (border_size.bottom_right > 0).then(|| Brush::new(glyphs[4], border_size.bottom_right)),
269-
start: LocalPos::new(0, height as u16 - 1),
269+
start: LocalPos::new(0, height - 1),
270270
axis: Axis::Horizontal,
271-
end: size.width as u16,
271+
end: size.width,
272272
};
273273

274274
if bottom.will_draw() {
@@ -286,7 +286,7 @@ impl BorderPainter {
286286
end_cap: None,
287287
start: LocalPos::new(0, offset),
288288
axis: Axis::Vertical,
289-
end: height as u16,
289+
end: height,
290290
};
291291

292292
let right = Line {
@@ -295,7 +295,7 @@ impl BorderPainter {
295295
end_cap: None,
296296
start: LocalPos::new(size.width - border_size.right as u16, offset),
297297
axis: Axis::Vertical,
298-
end: height as u16,
298+
end: height,
299299
};
300300

301301
Self {

anathema-default-widgets/src/layout/many.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ impl Many {
8080
mut children: LayoutForEach<'_, 'bp>,
8181
constraints: Constraints,
8282
ctx: &mut LayoutCtx<'_, 'bp>,
83-
offset: usize,
8483
) -> Result<Size> {
8584
let max_constraints = constraints;
8685

@@ -89,7 +88,7 @@ impl Many {
8988

9089
let mut size = Size::ZERO;
9190

92-
_ = children.skip(offset).each(ctx, |ctx, node, children| {
91+
_ = children.each(ctx, |ctx, node, children| {
9392
if ["spacer", "expand"].contains(&node.ident) {
9493
return Ok(ControlFlow::Continue(()));
9594
}

anathema-default-widgets/src/overflow.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ impl Widget for Overflow {
115115
) -> Result<Size> {
116116
let attributes = ctx.attribute_storage.get(id);
117117
let axis = attributes.get_as(AXIS).unwrap_or(Axis::Vertical);
118-
let offset = attributes.get_as::<usize>("offest").unwrap_or_default();
119118

120119
let output_size: Size = (constraints.max_width(), constraints.max_height()).into();
121120

@@ -144,7 +143,7 @@ impl Widget for Overflow {
144143
let mut many = Many::new(self.direction, axis, unconstrained);
145144

146145
// NOTE: we use the inner size here from many.layout
147-
_ = many.layout(children, constraints, ctx, offset)?;
146+
_ = many.layout(children, constraints, ctx)?;
148147

149148
self.inner_size = many.used_size.inner_size();
150149

0 commit comments

Comments
 (0)