From c2c6e6da2d827065536d4a171a0309165197b6dd Mon Sep 17 00:00:00 2001 From: morrison Date: Wed, 21 Jul 2021 13:16:02 +0100 Subject: [PATCH] add disabled state --- ui/decredmaterial/progressbar.go | 25 ------------------------- ui/decredmaterial/switch.go | 13 +++++++++++++ ui/decredmaterial/theme.go | 25 +++++++++++++++++++++++++ ui/page/settings_page.go | 1 - 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/ui/decredmaterial/progressbar.go b/ui/decredmaterial/progressbar.go index 792e15b4a..d51968311 100644 --- a/ui/decredmaterial/progressbar.go +++ b/ui/decredmaterial/progressbar.go @@ -72,28 +72,3 @@ func clamp1(v float32) float32 { return v } } - -// Disabled blends color towards the luminance and multiplies alpha. -// Blending towards luminance will desaturate the color. -// Multiplying alpha blends the color together more with the background. -func Disabled(c color.NRGBA) (d color.NRGBA) { - const r = 80 // blend ratio - lum := approxLuminance(c) - return color.NRGBA{ - R: byte((int(c.R)*r + int(lum)*(256-r)) / 256), - G: byte((int(c.G)*r + int(lum)*(256-r)) / 256), - B: byte((int(c.B)*r + int(lum)*(256-r)) / 256), - A: byte(int(c.A) * (128 + 32) / 256), - } -} - -// approxLuminance is a fast approximate version of RGBA.Luminance. -func approxLuminance(c color.NRGBA) byte { - const ( - r = 13933 // 0.2126 * 256 * 256 - g = 46871 // 0.7152 * 256 * 256 - b = 4732 // 0.0722 * 256 * 256 - t = r + g + b - ) - return byte((r*int(c.R) + g*int(c.G) + b*int(c.B)) / t) -} diff --git a/ui/decredmaterial/switch.go b/ui/decredmaterial/switch.go index a6ec0aed6..b700d624a 100644 --- a/ui/decredmaterial/switch.go +++ b/ui/decredmaterial/switch.go @@ -20,6 +20,7 @@ type Switch struct { active color.NRGBA inactive color.NRGBA thumbColor color.NRGBA + disabled bool switchWidget *widget.Bool } @@ -79,7 +80,12 @@ func (s *Switch) Layout(gtx layout.Context) layout.Dimensions { X: float32(trackWidth), Y: float32(trackHeight), }} + col := s.inactive + if s.disabled { + col = Disabled(s.inactive) + s.thumbColor = Disabled(s.thumbColor) + } if s.IsChecked() { col = s.active } @@ -139,6 +145,9 @@ func (s *Switch) Changed() bool { } func (s *Switch) IsChecked() bool { + if s.disabled { + s.SetChecked(false) + } return s.switchWidget.Value } @@ -154,6 +163,10 @@ func (s *Switch) SetTrackColor(activeColor, inactiveColor color.NRGBA) { s.inactive, s.active = inactiveColor, activeColor } +func (s *Switch) Disabled() { + s.disabled = true +} + func (s *SwitchButtonText) Layout(gtx layout.Context) layout.Dimensions { s.handleClickEvent() m8 := unit.Dp(8) diff --git a/ui/decredmaterial/theme.go b/ui/decredmaterial/theme.go index f78df7b92..5c36065cf 100644 --- a/ui/decredmaterial/theme.go +++ b/ui/decredmaterial/theme.go @@ -281,3 +281,28 @@ func (t *Theme) closeAllDropdownMenus(group uint) { } } } + +// Disabled blends color towards the luminance and multiplies alpha. +// Blending towards luminance will desaturate the color. +// Multiplying alpha blends the color together more with the background. +func Disabled(c color.NRGBA) (d color.NRGBA) { + const r = 80 // blend ratio + lum := approxLuminance(c) + return color.NRGBA{ + R: byte((int(c.R)*r + int(lum)*(256-r)) / 256), + G: byte((int(c.G)*r + int(lum)*(256-r)) / 256), + B: byte((int(c.B)*r + int(lum)*(256-r)) / 256), + A: byte(int(c.A) * (128 + 32) / 256), + } +} + +// approxLuminance is a fast approximate version of RGBA.Luminance. +func approxLuminance(c color.NRGBA) byte { + const ( + r = 13933 // 0.2126 * 256 * 256 + g = 46871 // 0.7152 * 256 * 256 + b = 4732 // 0.0722 * 256 * 256 + t = r + g + b + ) + return byte((r*int(c.R) + g*int(c.G) + b*int(c.B)) / t) +} diff --git a/ui/page/settings_page.go b/ui/page/settings_page.go index b0344712b..ce90d05e3 100644 --- a/ui/page/settings_page.go +++ b/ui/page/settings_page.go @@ -122,7 +122,6 @@ func NewSettingsPage(l *load.Load) *SettingsPage { pg.agentLabel.Color = pg.Theme.Color.Gray pg.chevronRightIcon.Color = color - return pg }