Skip to content

Commit

Permalink
Support disabling angle brackets in SelectView popup button
Browse files Browse the repository at this point in the history
Allow a SelectView to be configured as "raw" (using the same terminology
as Button) to omit the angle brackets around the current item label.
  • Loading branch information
bgilbert committed Jun 12, 2024
1 parent 176102a commit 7d55b82
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cursive-core/src/views/select_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub struct SelectView<T = String> {

// `true` if we show a one-line view, with popup on selection.
popup: bool,
// `true` if we skip the angle brackets around the popup button
raw: bool,

// We need the last offset to place the popup window
// We "cache" it during the draw, so we need interior mutability.
Expand Down Expand Up @@ -102,6 +104,7 @@ impl<T: 'static + Send + Sync> SelectView<T> {
on_submit: None,
align: Align::top_left(),
popup: false,
raw: false,
autojump: false,
last_offset: Mutex::new(Vec2::zero()),
last_size: Vec2::zero(),
Expand Down Expand Up @@ -167,6 +170,19 @@ impl<T: 'static + Send + Sync> SelectView<T> {
self.last_required_size = None;
}

/// Disable drawing angle brackets around the popup button.
///
/// Chainable variant.
#[must_use]
pub fn raw(self) -> Self {
self.with(|s| s.set_raw(true))
}

/// Disable drawing angle brackets around the popup button.
pub fn set_raw(&mut self, raw: bool) {
self.raw = raw;
}

/// Sets a callback to be used when an item is selected.
#[crate::callback_helpers]
pub fn set_on_select<F>(&mut self, cb: F)
Expand Down Expand Up @@ -944,9 +960,11 @@ impl<T: 'static + Send + Sync> View for SelectView<T> {
printer.with_style(style, |printer| {
// Prepare the entire background
printer.print_hline((1, 0), x, " ");
// Draw the borders
printer.print((0, 0), "<");
printer.print((x, 0), ">");
if !self.raw {
// Draw the borders
printer.print((0, 0), "<");
printer.print((x, 0), ">");
}

if let Some(label) = self.items.get(focus).map(|item| &item.label) {
// And center the text?
Expand Down Expand Up @@ -1006,7 +1024,8 @@ impl<T: 'static + Send + Sync> View for SelectView<T> {
.max()
.unwrap_or(1);
let size = if self.popup {
Vec2::new(w + 2, 1)
let extra = if self.raw { 0 } else { 2 };
Vec2::new(w + extra, 1)
} else {
let h = self.items.len();

Expand Down

0 comments on commit 7d55b82

Please sign in to comment.