Skip to content

Commit

Permalink
Factor group management out of RadioButton into GroupedBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
japhb committed Apr 6, 2024
1 parent 187c3be commit 4eb2ae5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
32 changes: 32 additions & 0 deletions lib/Terminal/Widgets/Input/Boolean.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use Terminal::Widgets::Events;
use Terminal::Widgets::Input;


#| Base functionality for any boolean-valued input widget
role Terminal::Widgets::Input::Boolean
does Terminal::Widgets::Input {
has Bool:D $.state = False;
Expand Down Expand Up @@ -66,3 +67,34 @@ does Terminal::Widgets::Input {
self.toggle-state if $.enabled;
}
}


#| Additional functionality for grouped boolean widgets, such as radio buttons
class Terminal::Widgets::Input::GroupedBoolean
does Terminal::Widgets::Input::Boolean {
has Str:D $.group is required;

#| Make sure grouped boolean widget is added to group within toplevel
submethod TWEAK() {
self.Terminal::Widgets::Input::TWEAK;
self.toplevel.add-to-group(self, $!group);
}

#| All grouped boolean widgets in this widget's group
method group-members() {
self.toplevel.group-members($!group)
}

#| Selected member of this widget's group
method selected-member() {
self.group-members.first(*.state)
}

#| If setting this widget, unset remainder in group
method set-state(Bool:D $state) {
self.Terminal::Widgets::Input::Boolean::set-state($state);
if $state {
.set-state(False) for self.group-members.grep(* !=== self);
}
}
}
28 changes: 1 addition & 27 deletions lib/Terminal/Widgets/Input/RadioButton.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,8 @@ use Terminal::Widgets::Input::Labeled;

#| A single optionally labeled radio button
class Terminal::Widgets::Input::RadioButton
does Terminal::Widgets::Input::Boolean
is Terminal::Widgets::Input::GroupedBoolean
does Terminal::Widgets::Input::Labeled {
has Str:D $.group is required;

#| Make sure radio button is added to group within toplevel
submethod TWEAK() {
self.Terminal::Widgets::Input::TWEAK;
self.toplevel.add-to-group(self, $!group);
}

#| All buttons in this button's group
method group-members() {
self.toplevel.group-members($!group)
}

#| Selected member of this button's group
method selected-member() {
self.group-members.first(*.state)
}

#| If setting this button, unset remainder in group
method set-state(Bool:D $state) {
self.Terminal::Widgets::Input::Boolean::set-state($state);
if $state {
.set-state(False) for self.group-members.grep(* !=== self);
}
}

#| Compute minimum content width for requested style and attributes
method min-width(:$locale!, :$context!, :$label = '') {
my @buttons = self.buttons($context.caps);
Expand Down

0 comments on commit 4eb2ae5

Please sign in to comment.