From 6398c3cb0d7179d67835b678ca085326d8b922c5 Mon Sep 17 00:00:00 2001 From: Geoffrey Broadwell Date: Thu, 29 Feb 2024 00:18:36 -0800 Subject: [PATCH] First cut at converting Input widgets to ColorTheme support --- lib/Terminal/Widgets/Input.rakumod | 38 +++++++------------------ lib/Terminal/Widgets/Input/Menu.rakumod | 3 +- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/lib/Terminal/Widgets/Input.rakumod b/lib/Terminal/Widgets/Input.rakumod index 47491e5..ff6a0d8 100644 --- a/lib/Terminal/Widgets/Input.rakumod +++ b/lib/Terminal/Widgets/Input.rakumod @@ -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; @@ -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; + has Terminal::Widgets::Form $.form; # Input-specific gist flags @@ -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, - (%.color if $focused), - (%.color if $blurred), - (%.color if $!active), - (%.color unless $.enabled), - (%.color if $.error); - - color-merge(@colors) + # Map and merge colors according to current ColorSet + $.colorset.current-color(%states); } #| Set the hint to a plain Str diff --git a/lib/Terminal/Widgets/Input/Menu.rakumod b/lib/Terminal/Widgets/Input/Menu.rakumod index ef31552..89e2e2f 100644 --- a/lib/Terminal/Widgets/Input/Menu.rakumod +++ b/lib/Terminal/Widgets/Input/Menu.rakumod @@ -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; @@ -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 + my $color = $i == $!selected ?? $highlight !! $item // $base-color; $.grid.set-span($x, $y + $_, $formatted ~ $padding, $color); }