joshka/tui-popup


A Popup widget for Ratatui. Repo moved to tui-widgets

https://github.com/joshka/tui-widgets

License: MIT

Language: Rust

Keywords: popup, ratatui, tui, tui-rs, widget


Important

This repo has been consolidated into https://github.com/tui-widgets. All future work will happen there. The crate will remain available as tui-big-text, but this repo is now archived.

tui-popup

Crates.io badge License badge Docs.rs badge Deps.rs badge Discord badge

A popup widget for Ratatui

The popup widget is a simple widget that renders a popup in the center of the screen.

Example

use ratatui::prelude::*;
use tui_popup::Popup;

fn render_popup(frame: &mut Frame) {
    let popup = Popup::new("tui-popup demo", "Press any key to exit")
       .style(Style::new().white().on_blue());
    frame.render_widget(&popup, frame.size());
}

demo

State

The widget supports storing the position of the popup in PopupState. This is experimental and the exact api for this will likely change.

use ratatui::prelude::*;
use tui_popup::Popup;

fn render_stateful_popup(frame: &mut Frame, popup_state: &mut PopupState) {
    let popup = Popup::new("tui-popup demo", "Press any key to exit")
       .style(Style::new().white().on_blue());
    frame.render_stateful_widget_ref(popup, frame.size(), popup_state);
}

fn move_up(popup_state: &mut PopupState) {
    popup_state.move_by(0, -1);
}

The popup can automatically handle being moved around by the mouse, by passing in the column and row of Mouse Up / Down / Drag events. The current implemntation of this checks whether the click is in the first row of the popup, otherwise ignores the event.

match event.read()? {
    Event::Mouse(event) => {
        match event.kind {
            event::MouseEventKind::Down(MouseButton::Left) => {
                popup_state.mouse_down(event.column, event.row)
            }
            event::MouseEventKind::Up(MouseButton::Left) => {
                popup_state.mouse_up(event.column, event.row);
            }
            event::MouseEventKind::Drag(MouseButton::Left) => {
                popup_state.mouse_drag(event.column, event.row);
            }
            _ => {}
        };
    }
    // -- snip --
}

state demo

The popup also supports rendering arbitrary widgets by implementing SizedWidgetRef (or wrapping them with the provided SizedWrapper). This makes it possible to support wrapping and scrolling in using a Paragraph widget, or scrolling any amount of widgets using tui-scrollview.

let lines: Text = (0..10).map(|i| Span::raw(format!("Line {}", i))).collect();
let paragraph = Paragraph::new(lines).scroll((scroll, 0));
let sized_paragraph = SizedWrapper {
    inner: paragraph,
    width: 21,
    height: 5,
};
let popup = Popup::new("scroll: ↑/↓ quit: Esc", sized_paragraph)
    .style(Style::new().white().on_blue());
frame.render_widget_ref(popup, area);

paragraph example

Features

  • automatically centers
  • automatically sizes to content
  • style popup
  • move the popup (using state)
  • handle mouse events for dragging
  • move to position
  • resize
  • set border set / style
  • add close button
  • add nicer styling of header etc.
  • configure text wrapping in body to conform to a specific size

Project Statistics

Sourcerank 2
Repository Size 142 KB
Stars 24
Forks 2
Watchers 3
Open issues 0
Dependencies 0
Contributors 5
Tags 15
Created
Last updated
Last pushed

Top Contributors See all

Josh McKinney dependabot[bot] Levi Zim Patryk Strużek

Recent Tags See all

v0.4.2 July 23, 2024
v0.4.1 July 15, 2024
v0.4.0 July 08, 2024
v0.3.4 July 07, 2024
v0.3.3 June 25, 2024
v0.3.2 May 21, 2024
v0.3.1 May 01, 2024
v0.3.0 April 24, 2024
v0.2.4 April 01, 2024
v0.2.3 March 12, 2024
v0.2.2 January 02, 2024
v0.2.1 December 31, 2023
v0.2.0 December 31, 2023
v0.1.1 December 31, 2023
v0.1.0 December 31, 2023

Something wrong with this page? Make a suggestion

Last synced: 2024-07-25 02:30:07 UTC

Login to resync this repository