Skip to content

Commit

Permalink
First cut at converting Input widgets to ColorTheme support
Browse files Browse the repository at this point in the history
  • Loading branch information
japhb committed Feb 29, 2024
1 parent d6faf04 commit 6398c3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
38 changes: 10 additions & 28 deletions lib/Terminal/Widgets/Input.rakumod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ABSTRACT: Base role for input field widgets

use Terminal::Widgets::Utils::Color;
use Terminal::Widgets::ColorTheme;
use Terminal::Widgets::ColorThemes;
use Terminal::Widgets::Events;
use Terminal::Widgets::Widget;
use Terminal::Widgets::Form;
Expand All @@ -16,7 +18,8 @@ role Terminal::Widgets::Input
has $.error;
has %.color;

has Terminal::Widgets::Form $.form;
has Terminal::Widgets::ColorSet:D $.colorset = $DEFAULT-THEME.variants<attr4bit>;
has Terminal::Widgets::Form $.form;


# Input-specific gist flags
Expand Down Expand Up @@ -45,44 +48,23 @@ role Terminal::Widgets::Input

# Make sure unset colors are defaulted, and optionally add input to a form
submethod TWEAK() {
self.default-colors;
$!colorset .= clone(|%!color) if %!color;
.add-input(self) with $!form;
}

# Set color defaults
method default-colors() {
my constant %defaults =
error => 'red',
disabled => gray-color(.5e0),
active => 'bold inverse',
highlight => 'bold white on_blue',
blurred => 'on_' ~ gray-color(.25e0),
focused => 'on_' ~ rgb-color(.2e0, .2e0, 0e0), # Dim yellow
default => 'on_' ~ gray-color(.1e0),
;

for %defaults.kv -> $state, $color {
%!color{$state} //= $color;
}
}

#| Determine proper color based on state variables, taking care to handle
#| whatever color style mixtures have been requested
method current-color() {
# Determine current state flags
my $toplevel = self.toplevel;
my $focused = $toplevel.focused-widget === self;
my $blurred = $focused && !($toplevel.is-current-toplevel &&
$toplevel.terminal.terminal-focused);
my %states = :text, :$focused, :$blurred, :$!active,
error => ?$.error, disabled => !$.enabled;

# Merge all relevant colors into a single list of attribute requests
my @colors = %.color<default>,
(%.color<focused> if $focused),
(%.color<blurred> if $blurred),
(%.color<active> if $!active),
(%.color<disabled> unless $.enabled),
(%.color<error> if $.error);

color-merge(@colors)
# Map and merge colors according to current ColorSet
$.colorset.current-color(%states);
}

#| Set the hint to a plain Str
Expand Down
3 changes: 2 additions & 1 deletion lib/Terminal/Widgets/Input/Menu.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Terminal::Widgets::Input::Menu
my $w = $.w - $layout.width-correction;
my $h = $.h - $layout.height-correction;
my $base-color = self.current-color;
my $highlight = $.colorset.highlight;

self.draw-framing;

Expand All @@ -57,7 +58,7 @@ class Terminal::Widgets::Input::Menu
my $formatted = ' ' ~ ("$icon " if $icon) ~ $title ~ ' ';
my $extra = 0 max $w - $locale.width($formatted);
my $padding = ' ' x $extra;
my $color = $i == $!selected ?? %.color<highlight>
my $color = $i == $!selected ?? $highlight
!! $item<color> // $base-color;
$.grid.set-span($x, $y + $_, $formatted ~ $padding, $color);
}
Expand Down

0 comments on commit 6398c3c

Please sign in to comment.