Skip to content

Commit

Permalink
add disabled state
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirmorrison committed Jul 21, 2021
1 parent 736c2dd commit c2c6e6d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
25 changes: 0 additions & 25 deletions ui/decredmaterial/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 13 additions & 0 deletions ui/decredmaterial/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Switch struct {
active color.NRGBA
inactive color.NRGBA
thumbColor color.NRGBA
disabled bool
switchWidget *widget.Bool
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -139,6 +145,9 @@ func (s *Switch) Changed() bool {
}

func (s *Switch) IsChecked() bool {
if s.disabled {
s.SetChecked(false)
}
return s.switchWidget.Value
}

Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions ui/decredmaterial/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 0 additions & 1 deletion ui/page/settings_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func NewSettingsPage(l *load.Load) *SettingsPage {
pg.agentLabel.Color = pg.Theme.Color.Gray

pg.chevronRightIcon.Color = color

return pg
}

Expand Down

0 comments on commit c2c6e6d

Please sign in to comment.