Struct Image

Source
pub struct Image<'a> { /* private fields */ }
Expand description

Fixed size image widget that uses Protocol.

The widget does not react to area resizes, and is not even guaranteed to not overdraw. Its advantage lies in that the Protocol needs only one initial resize.

struct App {
    image_static: Protocol,
}
fn ui(f: &mut Frame<'_>, app: &mut App) {
    let image = Image::new(&mut app.image_static);
    f.render_widget(image, f.size());
}

Implementations§

Source§

impl<'a> Image<'a>

Source

pub fn new(image: &'a mut Protocol) -> Self

Examples found in repository?
examples/screenshot.rs (line 93)
82fn ui(f: &mut Frame<'_>, app: &mut App) {
83    let area = Rect::new(0, 0, SCREEN_SIZE.0, SCREEN_SIZE.1);
84    let block = Block::default()
85        .borders(Borders::ALL)
86        .title("Screenshot test");
87
88    f.render_widget(
89        Paragraph::new("PartiallyHiddenScreenshotParagraphBackground\n".repeat(10)),
90        block.inner(area),
91    );
92
93    let image = Image::new(&mut app.image);
94    f.render_widget(image, block.inner(area));
95    f.render_widget(block, area);
96}
More examples
Hide additional examples
examples/demo/main.rs (line 249)
219fn ui(f: &mut Frame<'_>, app: &mut App) {
220    let outer_block = Block::default()
221        .borders(Borders::TOP)
222        .title(app.title.as_str());
223
224    let chunks = Layout::default()
225        .direction(Direction::Horizontal)
226        .constraints(
227            [
228                Constraint::Percentage(app.split_percent),
229                Constraint::Percentage(100 - app.split_percent),
230            ]
231            .as_ref(),
232        )
233        .split(outer_block.inner(f.area()));
234    f.render_widget(outer_block, f.area());
235
236    let left_chunks = vertical_layout().split(chunks[0]);
237    let right_chunks = vertical_layout().split(chunks[1]);
238
239    let block_left_top = block("Fixed");
240    let area = block_left_top.inner(left_chunks[0]);
241    f.render_widget(
242        paragraph(app.background.as_str()).style(Color::Yellow),
243        area,
244    );
245    f.render_widget(block_left_top, left_chunks[0]);
246    match app.show_images {
247        ShowImages::Resized => {}
248        _ => {
249            let image = Image::new(&mut app.image_static);
250            // Let it be surrounded by styled text.
251            let offset_area = Rect {
252                x: area.x + 1,
253                y: area.y + 1,
254                width: area.width.saturating_sub(2),
255                height: area.height.saturating_sub(2),
256            };
257            f.render_widget(image, offset_area);
258        }
259    }
260
261    let chunks_left_bottom = Layout::default()
262        .direction(Direction::Horizontal)
263        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
264        .split(left_chunks[1]);
265
266    app.render_resized_image(f, Resize::Crop(None), chunks_left_bottom[0]);
267    app.render_resized_image(f, Resize::Scale(None), chunks_left_bottom[1]);
268    app.render_resized_image(f, Resize::Fit(None), right_chunks[0]);
269
270    let block_right_bottom = block("Help");
271    let area = block_right_bottom.inner(right_chunks[1]);
272    f.render_widget(
273        paragraph(vec![
274            Line::from("Key bindings:"),
275            Line::from("H/L: resize"),
276            Line::from(format!(
277                "i: cycle image protocols (current: {:?})",
278                app.picker.protocol_type()
279            )),
280            Line::from("o: cycle image"),
281            Line::from(format!("t: toggle ({:?})", app.show_images)),
282            Line::from(format!("Font size: {:?}", app.picker.font_size())),
283        ]),
284        area,
285    );
286    f.render_widget(block_right_bottom, right_chunks[1]);
287}

Trait Implementations§

Source§

impl Widget for Image<'_>

Source§

fn render(self, area: Rect, buf: &mut Buffer)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom widget.

Auto Trait Implementations§

§

impl<'a> Freeze for Image<'a>

§

impl<'a> RefUnwindSafe for Image<'a>

§

impl<'a> Send for Image<'a>

§

impl<'a> Sync for Image<'a>

§

impl<'a> Unpin for Image<'a>

§

impl<'a> !UnwindSafe for Image<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V