Skip to content

Commit

Permalink
🚧 Add toggle pane
Browse files Browse the repository at this point in the history
A toggle pane shows a value that can be set to either on or off. A
title is visible above this setting.
  • Loading branch information
mikelorant committed Mar 1, 2023
1 parent 74a1c75 commit 14e8d20
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/ui/colour/colour.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type optionSetting struct {
SettingTitleSelected lipgloss.TerminalColor
SettingDotEmpty lipgloss.TerminalColor
SettingDotFilled lipgloss.TerminalColor
SettingSquareEmpty lipgloss.TerminalColor
SettingSquareFilled lipgloss.TerminalColor
}

type shortcut struct {
Expand Down Expand Up @@ -284,6 +286,8 @@ func (c *Colour) OptionSetting() optionSetting {
SettingTitleSelected: ToAdaptive(clr.BrightWhite()),
SettingDotEmpty: clr.Fg(),
SettingDotFilled: ToAdaptive(clr.Cyan()),
SettingSquareEmpty: clr.Fg(),
SettingSquareFilled: ToAdaptive(clr.Cyan()),
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/ui/colour/colour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ type optionSetting struct {
SettingTitleSelected Colour
SettingDotEmpty Colour
SettingDotFilled Colour
SettingSquareEmpty Colour
SettingSquareFilled Colour
}

type shortcut struct {
Expand Down Expand Up @@ -506,6 +508,8 @@ func TestOptionSetting(t *testing.T) {
SettingTitleSelected: Colour{Dark: "#ffffff", Light: "#ffffff"},
SettingDotEmpty: Colour{Dark: "#bbbbbb"},
SettingDotFilled: Colour{Dark: "#00bbbb", Light: "#bb0000"},
SettingSquareEmpty: Colour{Dark: "#bbbbbb"},
SettingSquareFilled: Colour{Dark: "#00bbbb", Light: "#bb0000"},
},
},
}
Expand All @@ -524,6 +528,8 @@ func TestOptionSetting(t *testing.T) {
assert.Equal(t, tt.optionSetting.SettingTitleSelected, toColour(clr.SettingTitleSelected), "SettingTitleSelected")
assert.Equal(t, tt.optionSetting.SettingDotEmpty, toColour(clr.SettingDotEmpty), "SettingDotEmpty")
assert.Equal(t, tt.optionSetting.SettingDotFilled, toColour(clr.SettingDotFilled), "SettingDotFilled")
assert.Equal(t, tt.optionSetting.SettingSquareEmpty, toColour(clr.SettingSquareEmpty), "SettingSquareEmpty")
assert.Equal(t, tt.optionSetting.SettingSquareFilled, toColour(clr.SettingSquareFilled), "SettingSquareFilled")
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/ui/option/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
TypeUnset = iota
TypeNoop
TypeRadio
TypeToggle
)

const (
Expand Down Expand Up @@ -92,6 +93,10 @@ func (m *Model) SelectPane(title string) {
if pane.Title != title {
continue
}
case *Toggle:
if pane.Title != title {
continue
}
}

m.Selected.Blur()
Expand Down
23 changes: 23 additions & 0 deletions internal/ui/option/setting/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,29 @@ func TestModel(t *testing.T) {
},
},
},
{
name: "toggle",
args: args{
panes: []setting.Paner{
&setting.Toggle{Title: "Title"},
},
model: func(m setting.Model) setting.Model {
m.SelectPane("Title")

ap := setting.ToToggle(m.ActivePane())
ap.Enable = true

return m
},
},
want: want{
model: func(m setting.Model) {
ap := setting.ToToggle(m.ActivePane())

assert.Equal(t, true, ap.Value())
},
},
},
}

for _, tt := range tests {
Expand Down
10 changes: 10 additions & 0 deletions internal/ui/option/setting/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Styles struct {
settingTitleSelected lipgloss.Style
settingDotEmpty lipgloss.Style
settingDotFilled lipgloss.Style
settingSquareEmpty lipgloss.Style
settingSquareFilled lipgloss.Style
}

func defaultStyles(th theme.Theme) Styles {
Expand Down Expand Up @@ -41,5 +43,13 @@ func defaultStyles(th theme.Theme) Styles {
Foreground(clr.SettingDotFilled).
SetString("●")

s.settingSquareEmpty = lipgloss.NewStyle().
Foreground(clr.SettingSquareEmpty).
SetString("▢")

s.settingSquareFilled = lipgloss.NewStyle().
Foreground(clr.SettingSquareFilled).
SetString("▣")

return s
}
2 changes: 2 additions & 0 deletions internal/ui/option/setting/testdata/toggle.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Title
▣ Enable
59 changes: 59 additions & 0 deletions internal/ui/option/setting/toggle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package setting

import (
"fmt"
"strings"
)

type Toggle struct {
Title string
Enable bool

focus bool
}

func (t *Toggle) Render(styles Styles) string {
var str []string

switch t.focus {
case true:
str = append(str, styles.settingTitleSelected.Render(t.Title))
default:
str = append(str, styles.settingTitle.Render(t.Title))
}

switch t.Enable {
case true:
v := styles.settingSelected.Render("Enable")
str = append(str, fmt.Sprintf("%v %v", styles.settingSquareFilled, v))
default:
v := styles.setting.Render("Enable")
str = append(str, fmt.Sprintf("%v %v", styles.settingSquareEmpty, v))
}

return strings.Join(str, "\n")
}

func (t *Toggle) Focus() {
t.focus = true
}

func (t *Toggle) Blur() {
t.focus = false
}

func (t *Toggle) Value() bool {
return t.Enable
}

func (t *Toggle) Toggle() {
t.Enable = !t.Enable
}

func (t *Toggle) Type() Type {
return TypeToggle
}

func ToToggle(p Paner) *Toggle {
return p.(*Toggle)
}

0 comments on commit 14e8d20

Please sign in to comment.