rat_popup/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![doc = include_str!("../readme.md")]
//
#![allow(clippy::collapsible_else_if)]

use ratatui::layout::{Alignment, Rect};

mod popup;

pub use popup::*;

pub mod event {
    use rat_event::*;

    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub enum PopupOutcome {
        /// The given event has not been used at all.
        Continue,
        /// The event has been recognized, but nothing noticeable has changed.
        /// Further processing for this event may stop.
        /// Rendering the ui is not necessary.
        Unchanged,
        /// The event has been recognized and there is some change due to it.
        /// Further processing for this event may stop.
        /// Rendering the ui is advised.
        Changed,
        /// Popup should be hidden.
        Hide,
    }

    impl ConsumedEvent for PopupOutcome {
        fn is_consumed(&self) -> bool {
            *self != PopupOutcome::Continue
        }
    }

    impl From<PopupOutcome> for Outcome {
        fn from(value: PopupOutcome) -> Self {
            match value {
                PopupOutcome::Continue => Outcome::Continue,
                PopupOutcome::Unchanged => Outcome::Unchanged,
                PopupOutcome::Changed => Outcome::Changed,
                PopupOutcome::Hide => Outcome::Changed,
            }
        }
    }
}

/// Placement of the popup.
///
/// This enum is for use in a widget that then uses PopupCore
/// internally. Expose Placement to the users of your widget
/// to let them define a popup placement. Convert the Placement
/// to a PopupConstraint internally when forwarding this
/// to PopupCore.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Placement {
    /// Use the render-area for the popup as is.
    #[default]
    None,
    /// Place above the given area.
    Above,
    /// Place below the given area:
    Below,
    /// Place left of the given area.
    Left,
    /// Place right of the given area.
    Right,
    /// Above or below dependent on available space. Aligned left.
    AboveOrBelow,
    /// Below or above dependent on available space. Aligned left.
    BelowOrAbove,
    /// Use the render-area for the popup, but place it at position (x,y).
    Position(u16, u16),
}

impl Placement {
    pub fn into_constraint(self, alignment: Alignment, rel_area: Rect) -> PopupConstraint {
        match self {
            Placement::None => PopupConstraint::None,
            Placement::Above => PopupConstraint::Above(alignment, rel_area),
            Placement::Below => PopupConstraint::Below(alignment, rel_area),
            Placement::Left => PopupConstraint::Left(alignment, rel_area),
            Placement::Right => PopupConstraint::Right(alignment, rel_area),
            Placement::AboveOrBelow => PopupConstraint::AboveOrBelow(alignment, rel_area),
            Placement::BelowOrAbove => PopupConstraint::BelowOrAbove(alignment, rel_area),
            Placement::Position(x, y) => PopupConstraint::Position(x, y),
        }
    }
}

/// Placement relative to the widget area + the widget area.
///
/// The render() call for PopupCore will only use the size of
/// the area given to the render call as the size of the popup.
/// It will calculate the position of the popup given one of
/// these constraints.
///
/// If you build a widget that uses a PopupCore internally you
/// will rather use Placement as a parameter
///
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PopupConstraint {
    /// Use the render-area for the popup as is.
    #[default]
    None,
    /// Synonym for AboveLeft
    Above(Alignment, Rect),
    /// Synonym for BelowLeft
    Below(Alignment, Rect),
    /// Synonym for LeftTop
    Left(Alignment, Rect),
    /// Synonym for RightTop
    Right(Alignment, Rect),
    /// Above or below dependent on available space. Aligned left.
    AboveOrBelow(Alignment, Rect),
    /// Below or above dependent on available space. Aligned left.
    BelowOrAbove(Alignment, Rect),
    /// Use the render-area for the popup, but place it at position (x,y).
    Position(u16, u16),
}

mod _private {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct NonExhaustive;
}