6 releases (stable)

1.1.2 Jan 26, 2025
1.1.1 Jan 19, 2025
1.0.0 Dec 29, 2024
0.2.0 Nov 1, 2024

#1155 in Command-line interface

Download history 138/week @ 2024-10-27 32/week @ 2024-11-03 14/week @ 2024-11-10 94/week @ 2024-11-17 250/week @ 2024-11-24 115/week @ 2024-12-01 223/week @ 2024-12-08 214/week @ 2024-12-15 101/week @ 2024-12-22 237/week @ 2024-12-29 193/week @ 2025-01-05 168/week @ 2025-01-12 267/week @ 2025-01-19 426/week @ 2025-01-26 311/week @ 2025-02-02 294/week @ 2025-02-09

1,326 downloads per month
Used in 14 crates (6 directly)

MIT/Apache

8KB
78 lines

semver stable crates.io Documentation License License

This crate is a part of rat-salsa.

Rat-Reloc(ate)

RelocatableState enables rendering StatefulWidget's to a temporary buffer.

After rendering a stateful widget all areas derived from the render area will be wrong if the temporary buffer is rendered to screen at a different location and with some clipping.

This trait defines a relocate function that corrects the areas at some point after rendering the widget.

  • Doesn't impact normal rendering of the widget. It can just use the area and be done with it.

  • Straightforward

      use rat_reloc::{RelocatableState, relocate_area};
      use ratatui::layout::Rect;
    
      # struct ButtonState{ area:Rect, inner:Rect}
    
      impl RelocatableState for ButtonState {
          fn relocate(&mut self, shift: (i16, i16), clip: Rect) {
              self.area = relocate_area(self.area, shift, clip);
              self.inner = relocate_area(self.inner, shift, clip);
          }
      }
    
  • Decent to implement for a view widget

      use ratatui::layout::Rect;
      use ratatui::buffer::Buffer;
      use ratatui::widgets::StatefulWidget;
      use rat_reloc::RelocatableState;
    
      pub struct RelocatedRender;
    
      impl RelocatedRender {
          fn render<W, S>(widget: W, area: Rect, state: &mut S)
          where
              W: StatefulWidget<State = S>,
              S: RelocatableState,
          {
              // remap area
              let area = Rect::default();
              // use inner buffer
              let mut buf = Buffer::default();
    
              // render to buffer
              widget.render(area, &mut buf, state);
    
              // calculate shift and clip
              let shift = (-1, -1);
              let clip = Rect::default();
    
              // correct state
              state.relocate(shift, clip);
          }
      }
    

Dependencies

~6–16MB
~214K SLoC