#widgets #ratatui #clip

rat-reloc

relocate widgets after rendering

1 unstable release

0.2.0 Nov 1, 2024

#1111 in Command-line interface

Download history 84/week @ 2024-10-26 85/week @ 2024-11-02 15/week @ 2024-11-09 75/week @ 2024-11-16 238/week @ 2024-11-23 135/week @ 2024-11-30 214/week @ 2024-12-07 176/week @ 2024-12-14

797 downloads per month
Used in 10 crates (6 directly)

MIT/Apache

9KB
56 lines

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. This trait defines a relocate function that corrects the areas afterwards.

  • 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

~7–15MB
~211K SLoC