Skip to content

Commit 83e45db

Browse files
authored
refactor: popup rendering (#51)
1 parent 58371fa commit 83e45db

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

tui-popup/src/popup.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,35 +129,9 @@ impl<W: KnownSize + WidgetRef> StatefulWidget for &Popup<'_, W> {
129129
type State = PopupState;
130130

131131
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
132-
let popup_area = if let Some(next) = state.area.take() {
133-
// ensure that the popup remains on screen
134-
let width = min(next.width, area.width);
135-
let height = min(next.height, area.height);
136-
let x = next.x.clamp(buf.area.x, area.right() - width);
137-
let y = next.y.clamp(buf.area.y, area.bottom() - height);
138-
139-
Rect::new(x, y, width, height)
140-
} else {
141-
let border_height = usize::from(self.borders.intersects(Borders::TOP))
142-
+ usize::from(self.borders.intersects(Borders::BOTTOM));
143-
let border_width = usize::from(self.borders.intersects(Borders::LEFT))
144-
+ usize::from(self.borders.intersects(Borders::RIGHT));
145-
146-
let height = self
147-
.body
148-
.height()
149-
.saturating_add(border_height)
150-
.try_into()
151-
.unwrap_or(area.height);
152-
let width = self
153-
.body
154-
.width()
155-
.saturating_add(border_width)
156-
.try_into()
157-
.unwrap_or(area.width);
158-
centered_rect(width, height, area)
159-
};
132+
let area = area.clamp(buf.area);
160133

134+
let popup_area = self.popup_area(state, area);
161135
state.area.replace(popup_area);
162136

163137
Clear.render(popup_area, buf);
@@ -167,8 +141,34 @@ impl<W: KnownSize + WidgetRef> StatefulWidget for &Popup<'_, W> {
167141
.border_style(self.border_style)
168142
.title(self.title.clone())
169143
.style(self.style);
170-
Widget::render(&block, popup_area, buf);
171-
self.body.render_ref(block.inner(popup_area), buf);
144+
let inner_area = block.inner(popup_area);
145+
block.render(popup_area, buf);
146+
147+
self.body.render_ref(inner_area, buf);
148+
}
149+
}
150+
151+
impl<'content, W: KnownSize> Popup<'content, W> {
152+
fn popup_area(&self, state: &mut PopupState, area: Rect) -> Rect {
153+
if let Some(current) = state.area.take() {
154+
return current.clamp(area);
155+
}
156+
157+
let has_top = self.borders.intersects(Borders::TOP);
158+
let has_bottom = self.borders.intersects(Borders::BOTTOM);
159+
let has_left = self.borders.intersects(Borders::LEFT);
160+
let has_right = self.borders.intersects(Borders::RIGHT);
161+
162+
let border_height = usize::from(has_top) + usize::from(has_bottom);
163+
let border_width = usize::from(has_left) + usize::from(has_right);
164+
165+
let height = self.body.height().saturating_add(border_height);
166+
let width = self.body.width().saturating_add(border_width);
167+
168+
let height = u16::try_from(height).unwrap_or(area.height);
169+
let width = u16::try_from(width).unwrap_or(area.width);
170+
171+
centered_rect(width, height, area)
172172
}
173173
}
174174

0 commit comments

Comments
 (0)