Skip to content

Commit

Permalink
Refactor min width default for SingleLineInput layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
japhb committed Feb 27, 2024
1 parent b006a04 commit e7b150e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
7 changes: 7 additions & 0 deletions lib/Terminal/Widgets/Input/Button.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ use Terminal::Widgets::Input::Labeled;
class Terminal::Widgets::Input::Button
does Terminal::Widgets::Input
does Terminal::Widgets::Input::Labeled {
#| Compute minimum content width for requested style and attributes
method min-width(:$locale!, :%style!, :$label = 'Button') {
my $bw = %style<border-width>;
my $has-border = $bw ~~ Positional ?? $bw.grep(?*) !! ?$bw;
$locale.width($label) + 2 * !$has-border
}

#| Refresh the whole input
method full-refresh(Bool:D :$print = True) {
self.clear-frame;
Expand Down
19 changes: 16 additions & 3 deletions lib/Terminal/Widgets/Input/Checkbox.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@ use Terminal::Widgets::Input::Labeled;
class Terminal::Widgets::Input::Checkbox
does Terminal::Widgets::Input::Boolean
does Terminal::Widgets::Input::Labeled {
#| Checkbox glyphs
method checkbox-text() {
#| Compute minimum content width for requested style and attributes
method min-width(:$locale!, :$context!, :$label = '') {
my @boxes = self.checkboxes($context.caps);
my $maxbox = @boxes.map({ $locale.width($_) }).max;

$maxbox + ?$label + $locale.width($label)
}

#| Checkbox glyphs for given terminal capabilities
method checkboxes($caps = self.terminal.caps) {
my constant %boxes =
ASCII => « '[ ]' [x] »,
Uni1 => « ☐ ☒ »,
Uni7 => « 🞏 🞕 »;

self.terminal.caps.best-symbol-choice(%boxes)[+$.state]
$caps.best-symbol-choice(%boxes)
}

#| Checkbox glyphs for current state
method checkbox-text($caps = self.terminal.caps) {
self.checkboxes($caps)[+$.state]
}

#| Refresh just the value, without changing anything else
Expand Down
19 changes: 16 additions & 3 deletions lib/Terminal/Widgets/Input/RadioButton.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ class Terminal::Widgets::Input::RadioButton
}
}

#| Radio button glyphs
method button-text() {
#| Compute minimum content width for requested style and attributes
method min-width(:$locale!, :$context!, :$label = '') {
my @buttons = self.buttons($context.caps);
my $maxbutton = @buttons.map({ $locale.width($_) }).max;

$maxbutton + ?$label + $locale.width($label)
}

#| Radio button glyphs for given terminal capabilities
method buttons($caps = self.terminal.caps) {
my constant %buttons =
ASCII => « '( )' (*) »,
Uni1 => « ○ ⊙ »,
Uni7 => « 🞅 🞊 »;

self.terminal.caps.best-symbol-choice(%buttons)[+$.state]
$caps.best-symbol-choice(%buttons)
}

#| Radio button glyphs for current state
method button-text($caps = self.terminal.caps) {
self.buttons($caps)[+$.state]
}

#| Refresh just the value, without changing anything else
Expand Down
24 changes: 17 additions & 7 deletions lib/Terminal/Widgets/Layout.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -432,24 +432,34 @@ class Menu is Leaf {

#| Single line inputs
class SingleLineInput is Leaf {
method default-styles() { hash(set-h => 1) }
method input-class() { ... }

method default-styles(|c) {
my $min-w = self.input-class.min-width(|c);
%( :$min-w, set-h => 1 )
}
}

#| A single button
class Button is SingleLineInput {
method default-styles(:$locale!, :$label = '') {
%( |callsame, min-w => 2 + $locale.width($label) )
}
method input-class() { ::('Terminal::Widgets::Input::Button') }
}

#| A single checkbox
class Checkbox is SingleLineInput { }
class Checkbox is SingleLineInput {
method input-class() { ::('Terminal::Widgets::Input::Checkbox') }
}

#| A single radio button
class RadioButton is SingleLineInput { }
class RadioButton is SingleLineInput {
method input-class() { ::('Terminal::Widgets::Input::RadioButton') }
}

#| A single-line text input field
class TextInput is SingleLineInput { }
class TextInput is SingleLineInput {
method input-class() { ::('Terminal::Widgets::Input::Text') }
method default-styles() { hash(set-h => 1) }
}


#| A widget node; localizes xy coordinate frame for children
Expand Down

0 comments on commit e7b150e

Please sign in to comment.