mirror of
https://github.com/vsariola/sointu.git
synced 2026-04-12 17:14:43 -04:00
Compare commits
2 Commits
v0.6.0
...
feat/enums
| Author | SHA1 | Date | |
|---|---|---|---|
| 02a9d9747f | |||
| f88986dc64 |
@ -4,6 +4,7 @@ import (
|
|||||||
"iter"
|
"iter"
|
||||||
"math"
|
"math"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Enabler is an interface that defines a single Enabled() method, which is used
|
// Enabler is an interface that defines a single Enabled() method, which is used
|
||||||
@ -111,7 +112,9 @@ type (
|
|||||||
// length, etc. It is a wrapper around an IntValue interface that provides
|
// length, etc. It is a wrapper around an IntValue interface that provides
|
||||||
// methods to manipulate the value, but Int guard that all changes are
|
// methods to manipulate the value, but Int guard that all changes are
|
||||||
// within the range of the underlying IntValue implementation and that
|
// within the range of the underlying IntValue implementation and that
|
||||||
// SetValue is not called when the value is unchanged.
|
// SetValue is not called when the value is unchanged. The IntValue can
|
||||||
|
// optionally implement the StringOfer interface to provide custom string
|
||||||
|
// representations of the integer values.
|
||||||
Int struct {
|
Int struct {
|
||||||
value IntValue
|
value IntValue
|
||||||
}
|
}
|
||||||
@ -121,6 +124,10 @@ type (
|
|||||||
SetValue(int) (changed bool)
|
SetValue(int) (changed bool)
|
||||||
Range() RangeInclusive
|
Range() RangeInclusive
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringOfer interface {
|
||||||
|
StringOf(value int) string
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeInt(value IntValue) Int { return Int{value} }
|
func MakeInt(value IntValue) Int { return Int{value} }
|
||||||
@ -152,6 +159,17 @@ func (v Int) Value() int {
|
|||||||
return v.value.Value()
|
return v.value.Value()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v Int) String() string {
|
||||||
|
return v.StringOf(v.Value())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v Int) StringOf(value int) string {
|
||||||
|
if s, ok := v.value.(StringOfer); ok {
|
||||||
|
return s.StringOf(value)
|
||||||
|
}
|
||||||
|
return strconv.Itoa(value)
|
||||||
|
}
|
||||||
|
|
||||||
// String
|
// String
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|||||||
@ -65,6 +65,20 @@ func (v *detectorWeighting) SetValue(value int) bool {
|
|||||||
func (v *detectorWeighting) Range() RangeInclusive {
|
func (v *detectorWeighting) Range() RangeInclusive {
|
||||||
return RangeInclusive{0, int(NumWeightingTypes) - 1}
|
return RangeInclusive{0, int(NumWeightingTypes) - 1}
|
||||||
}
|
}
|
||||||
|
func (v *detectorWeighting) StringOf(value int) string {
|
||||||
|
switch WeightingType(value) {
|
||||||
|
case KWeighting:
|
||||||
|
return "K-weighting"
|
||||||
|
case AWeighting:
|
||||||
|
return "A-weighting"
|
||||||
|
case CWeighting:
|
||||||
|
return "C-weighting (LUFS)"
|
||||||
|
case NoWeighting:
|
||||||
|
return "No weighting (RMS)"
|
||||||
|
default:
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type WeightingType int
|
type WeightingType int
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"gioui.org/io/event"
|
"gioui.org/io/event"
|
||||||
|
"gioui.org/io/key"
|
||||||
"gioui.org/io/pointer"
|
"gioui.org/io/pointer"
|
||||||
"gioui.org/layout"
|
"gioui.org/layout"
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
@ -17,11 +18,16 @@ import (
|
|||||||
type (
|
type (
|
||||||
// MenuState is the part of the menu that needs to be retained between frames.
|
// MenuState is the part of the menu that needs to be retained between frames.
|
||||||
MenuState struct {
|
MenuState struct {
|
||||||
visible bool
|
|
||||||
tags []bool
|
tags []bool
|
||||||
hover int
|
hover int
|
||||||
|
hoverOk bool
|
||||||
list layout.List
|
list layout.List
|
||||||
scrollBar ScrollBar
|
scrollBar ScrollBar
|
||||||
|
|
||||||
|
tag bool
|
||||||
|
visible bool
|
||||||
|
|
||||||
|
itemTmp []menuItem
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuStyle is the style for a menu that is stored in the theme.yml.
|
// MenuStyle is the style for a menu that is stored in the theme.yml.
|
||||||
@ -34,139 +40,249 @@ type (
|
|||||||
Height unit.Dp
|
Height unit.Dp
|
||||||
}
|
}
|
||||||
|
|
||||||
// ActionMenuItem is a menu item that has an icon, text, shortcut and an action.
|
|
||||||
ActionMenuItem struct {
|
|
||||||
Icon []byte
|
|
||||||
Text string
|
|
||||||
Shortcut string
|
|
||||||
Action tracker.Action
|
|
||||||
}
|
|
||||||
|
|
||||||
// MenuWidget has a Layout method to display a menu
|
// MenuWidget has a Layout method to display a menu
|
||||||
MenuWidget struct {
|
MenuWidget struct {
|
||||||
Theme *Theme
|
|
||||||
State *MenuState
|
State *MenuState
|
||||||
Style *MenuStyle
|
Style *MenuStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuButton displayes a button with text that opens a menu when clicked.
|
|
||||||
MenuButton struct {
|
|
||||||
Theme *Theme
|
|
||||||
Title string
|
|
||||||
Style *ButtonStyle
|
|
||||||
Clickable *Clickable
|
|
||||||
MenuState *MenuState
|
|
||||||
Width unit.Dp
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Menu(th *Theme, state *MenuState) MenuWidget {
|
func Menu(state *MenuState) MenuWidget { return MenuWidget{State: state} }
|
||||||
return MenuWidget{
|
func (w MenuWidget) WithStyle(style *MenuStyle) MenuWidget { w.Style = style; return w }
|
||||||
Theme: th,
|
|
||||||
State: state,
|
func (ms *MenuState) Tags(level int, yield TagYieldFunc) bool {
|
||||||
Style: &th.Menu.Main,
|
if ms.visible {
|
||||||
|
return yield(level, &ms.tag)
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func MenuItem(act tracker.Action, text, shortcut string, icon []byte) ActionMenuItem {
|
// MenuChild describes one or more menu items; if MenuChild is an Action or
|
||||||
return ActionMenuItem{
|
// Bool, it's one item per child, but Ints are treated as enumerations and
|
||||||
|
// create one item per different possible values of the int.
|
||||||
|
type MenuChild struct {
|
||||||
|
Icon []byte
|
||||||
|
Text string
|
||||||
|
Shortcut string
|
||||||
|
|
||||||
|
kind menuChildKind
|
||||||
|
action tracker.Action
|
||||||
|
bool tracker.Bool
|
||||||
|
int tracker.Int
|
||||||
|
widget layout.Widget // these should be passive separators and such
|
||||||
|
}
|
||||||
|
|
||||||
|
type menuChildKind int
|
||||||
|
|
||||||
|
const (
|
||||||
|
menuChildAction menuChildKind = iota
|
||||||
|
menuChildBool
|
||||||
|
menuChildInt
|
||||||
|
menuChildList
|
||||||
|
menuChildWidget
|
||||||
|
)
|
||||||
|
|
||||||
|
func ActionMenuChild(act tracker.Action, text, shortcut string, icon []byte) MenuChild {
|
||||||
|
return MenuChild{
|
||||||
Icon: icon,
|
Icon: icon,
|
||||||
Text: text,
|
Text: text,
|
||||||
Shortcut: shortcut,
|
Shortcut: shortcut,
|
||||||
Action: act,
|
|
||||||
|
kind: menuChildAction,
|
||||||
|
action: act,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MenuBtn(th *Theme, ms *MenuState, cl *Clickable, title string) MenuButton {
|
func BoolMenuChild(b tracker.Bool, text, shortcut string, icon []byte) MenuChild {
|
||||||
return MenuButton{
|
return MenuChild{
|
||||||
Theme: th,
|
Icon: icon,
|
||||||
Title: title,
|
Text: text,
|
||||||
Clickable: cl,
|
Shortcut: shortcut,
|
||||||
MenuState: ms,
|
|
||||||
Style: &th.Button.Menu,
|
kind: menuChildBool,
|
||||||
|
bool: b,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MenuWidget) Layout(gtx C, items ...ActionMenuItem) D {
|
func IntMenuChild(i tracker.Int, text, shortcut string, icon []byte) MenuChild {
|
||||||
|
return MenuChild{
|
||||||
|
Icon: icon,
|
||||||
|
Text: text,
|
||||||
|
Shortcut: shortcut,
|
||||||
|
kind: menuChildInt,
|
||||||
|
int: i,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Layout the menu with the given items
|
||||||
|
func (m MenuWidget) Layout(gtx C, children ...MenuChild) D {
|
||||||
|
t := TrackerFromContext(gtx)
|
||||||
|
if m.Style == nil {
|
||||||
|
m.Style = &t.Theme.Menu.Main
|
||||||
|
}
|
||||||
// unfortunately, there was no way to include items into the MenuWidget
|
// unfortunately, there was no way to include items into the MenuWidget
|
||||||
// without causing heap escapes, so they are passed as a parameter to the Layout
|
// without causing heap escapes, so they are passed as a parameter to the Layout
|
||||||
m.update(gtx, items...)
|
m.State.itemTmp = m.State.itemTmp[:0]
|
||||||
popup := Popup(m.Theme, &m.State.visible)
|
for i, c := range children {
|
||||||
popup.Style = &m.Theme.Popup.Menu
|
switch c.kind {
|
||||||
return popup.Layout(gtx, func(gtx C) D {
|
case menuChildAction:
|
||||||
|
m.State.itemTmp = append(m.State.itemTmp, menuItem{childIndex: i, icon: c.Icon, text: c.Text, shortcut: c.Shortcut, enabled: c.enabled()})
|
||||||
|
case menuChildBool:
|
||||||
|
mi := menuItem{childIndex: i, text: c.Text, shortcut: c.Shortcut, enabled: c.enabled()}
|
||||||
|
if c.bool.Value() {
|
||||||
|
mi.icon = c.Icon
|
||||||
|
}
|
||||||
|
m.State.itemTmp = append(m.State.itemTmp, mi)
|
||||||
|
case menuChildInt:
|
||||||
|
for i := c.int.Range().Min; i <= c.int.Range().Max; i++ {
|
||||||
|
mi := menuItem{childIndex: i, text: c.int.StringOf(i), value: i, enabled: c.enabled()}
|
||||||
|
if c.int.Value() == i {
|
||||||
|
mi.icon = c.Icon
|
||||||
|
}
|
||||||
|
if i == c.int.Range().Min {
|
||||||
|
mi.shortcut = c.Shortcut
|
||||||
|
}
|
||||||
|
m.State.itemTmp = append(m.State.itemTmp, mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.update(gtx, children, m.State.itemTmp)
|
||||||
|
listItem := func(gtx C, i int) D {
|
||||||
|
item := m.State.itemTmp[i]
|
||||||
|
icon := t.Theme.Icon(item.icon)
|
||||||
|
iconColor := m.Style.Text.Color
|
||||||
|
iconInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(6)}
|
||||||
|
textLabel := Label(t.Theme, &m.Style.Text, item.text)
|
||||||
|
shortcutLabel := Label(t.Theme, &m.Style.Shortcut, item.shortcut)
|
||||||
|
if !item.enabled {
|
||||||
|
iconColor = m.Style.Disabled
|
||||||
|
textLabel.Color = m.Style.Disabled
|
||||||
|
shortcutLabel.Color = m.Style.Disabled
|
||||||
|
}
|
||||||
|
shortcutInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(12), Bottom: unit.Dp(2), Top: unit.Dp(2)}
|
||||||
|
fg := func(gtx C) D {
|
||||||
|
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
|
||||||
|
layout.Rigid(func(gtx C) D {
|
||||||
|
return iconInset.Layout(gtx, func(gtx C) D {
|
||||||
|
p := gtx.Dp(unit.Dp(m.Style.Text.TextSize))
|
||||||
|
gtx.Constraints.Min = image.Pt(p, p)
|
||||||
|
return icon.Layout(gtx, iconColor)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
layout.Rigid(textLabel.Layout),
|
||||||
|
layout.Flexed(1, func(gtx C) D { return D{Size: image.Pt(gtx.Constraints.Max.X, 1)} }),
|
||||||
|
layout.Rigid(func(gtx C) D {
|
||||||
|
return shortcutInset.Layout(gtx, shortcutLabel.Layout)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
bg := func(gtx C) D {
|
||||||
|
rect := clip.Rect{Max: image.Pt(gtx.Constraints.Min.X, gtx.Constraints.Min.Y)}
|
||||||
|
if item.enabled && m.State.hoverOk && m.State.hover == i {
|
||||||
|
paint.FillShape(gtx.Ops, m.Style.Hover, rect.Op())
|
||||||
|
}
|
||||||
|
if item.enabled {
|
||||||
|
area := rect.Push(gtx.Ops)
|
||||||
|
event.Op(gtx.Ops, &m.State.tags[i])
|
||||||
|
area.Pop()
|
||||||
|
}
|
||||||
|
return D{Size: rect.Max}
|
||||||
|
}
|
||||||
|
return layout.Background{}.Layout(gtx, bg, fg)
|
||||||
|
}
|
||||||
|
menuList := func(gtx C) D {
|
||||||
gtx.Constraints.Max.X = gtx.Dp(m.Style.Width)
|
gtx.Constraints.Max.X = gtx.Dp(m.Style.Width)
|
||||||
gtx.Constraints.Max.Y = gtx.Dp(m.Style.Height)
|
gtx.Constraints.Max.Y = gtx.Dp(m.Style.Height)
|
||||||
|
r := clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops)
|
||||||
|
event.Op(gtx.Ops, &m.State.tag)
|
||||||
|
r.Pop()
|
||||||
m.State.list.Axis = layout.Vertical
|
m.State.list.Axis = layout.Vertical
|
||||||
m.State.scrollBar.Axis = layout.Vertical
|
m.State.scrollBar.Axis = layout.Vertical
|
||||||
return layout.Stack{Alignment: layout.SE}.Layout(gtx,
|
return layout.Stack{Alignment: layout.SE}.Layout(gtx,
|
||||||
|
layout.Expanded(func(gtx C) D { return m.State.list.Layout(gtx, len(m.State.itemTmp), listItem) }),
|
||||||
layout.Expanded(func(gtx C) D {
|
layout.Expanded(func(gtx C) D {
|
||||||
return m.State.list.Layout(gtx, len(items), func(gtx C, i int) D {
|
return m.State.scrollBar.Layout(gtx, &t.Theme.ScrollBar, len(m.State.itemTmp), &m.State.list.Position)
|
||||||
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
|
|
||||||
var macro op.MacroOp
|
|
||||||
item := &items[i]
|
|
||||||
if i == m.State.hover-1 && item.Action.Enabled() {
|
|
||||||
macro = op.Record(gtx.Ops)
|
|
||||||
}
|
|
||||||
icon := m.Theme.Icon(item.Icon)
|
|
||||||
iconColor := m.Style.Text.Color
|
|
||||||
iconInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(6)}
|
|
||||||
textLabel := Label(m.Theme, &m.Style.Text, item.Text)
|
|
||||||
shortcutLabel := Label(m.Theme, &m.Style.Shortcut, item.Shortcut)
|
|
||||||
if !item.Action.Enabled() {
|
|
||||||
iconColor = m.Style.Disabled
|
|
||||||
textLabel.Color = m.Style.Disabled
|
|
||||||
shortcutLabel.Color = m.Style.Disabled
|
|
||||||
}
|
|
||||||
shortcutInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(12), Bottom: unit.Dp(2), Top: unit.Dp(2)}
|
|
||||||
dims := layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
|
|
||||||
layout.Rigid(func(gtx C) D {
|
|
||||||
return iconInset.Layout(gtx, func(gtx C) D {
|
|
||||||
p := gtx.Dp(unit.Dp(m.Style.Text.TextSize))
|
|
||||||
gtx.Constraints.Min = image.Pt(p, p)
|
|
||||||
return icon.Layout(gtx, iconColor)
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
layout.Rigid(textLabel.Layout),
|
|
||||||
layout.Flexed(1, func(gtx C) D { return D{Size: image.Pt(gtx.Constraints.Max.X, 1)} }),
|
|
||||||
layout.Rigid(func(gtx C) D {
|
|
||||||
return shortcutInset.Layout(gtx, shortcutLabel.Layout)
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
if i == m.State.hover-1 && item.Action.Enabled() {
|
|
||||||
recording := macro.Stop()
|
|
||||||
paint.FillShape(gtx.Ops, m.Style.Hover, clip.Rect{
|
|
||||||
Max: image.Pt(dims.Size.X, dims.Size.Y),
|
|
||||||
}.Op())
|
|
||||||
recording.Add(gtx.Ops)
|
|
||||||
}
|
|
||||||
if item.Action.Enabled() {
|
|
||||||
rect := image.Rect(0, 0, dims.Size.X, dims.Size.Y)
|
|
||||||
area := clip.Rect(rect).Push(gtx.Ops)
|
|
||||||
event.Op(gtx.Ops, &m.State.tags[i])
|
|
||||||
area.Pop()
|
|
||||||
}
|
|
||||||
return dims
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
layout.Expanded(func(gtx C) D {
|
|
||||||
return m.State.scrollBar.Layout(gtx, &m.Theme.ScrollBar, len(items), &m.State.list.Position)
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
popup := Popup(t.Theme, &m.State.visible)
|
||||||
|
popup.Style = &t.Theme.Popup.Menu
|
||||||
|
return popup.Layout(gtx, menuList)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MenuWidget) update(gtx C, items ...ActionMenuItem) {
|
type menuItem struct {
|
||||||
for i, item := range items {
|
childIndex int
|
||||||
|
value int
|
||||||
|
icon []byte
|
||||||
|
text, shortcut string
|
||||||
|
enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuWidget) update(gtx C, children []MenuChild, items []menuItem) {
|
||||||
|
// handle keyboard events for the menu
|
||||||
|
for {
|
||||||
|
ev, ok := gtx.Event(
|
||||||
|
key.FocusFilter{Target: &m.State.tag},
|
||||||
|
key.Filter{Focus: &m.State.tag, Name: key.NameUpArrow},
|
||||||
|
key.Filter{Focus: &m.State.tag, Name: key.NameDownArrow},
|
||||||
|
key.Filter{Focus: &m.State.tag, Name: key.NameEnter},
|
||||||
|
key.Filter{Focus: &m.State.tag, Name: key.NameReturn},
|
||||||
|
)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
switch e := ev.(type) {
|
||||||
|
case key.Event:
|
||||||
|
if e.State != key.Press {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch e.Name {
|
||||||
|
case key.NameUpArrow:
|
||||||
|
if !m.State.hoverOk {
|
||||||
|
m.State.hover = 0 // if nothing is selected, select the first item before starting to move backwards
|
||||||
|
}
|
||||||
|
for i := 1; i < len(items); i++ {
|
||||||
|
idx := (m.State.hover - i + len(items)) % len(items)
|
||||||
|
child := &children[items[idx].childIndex]
|
||||||
|
if child.enabled() {
|
||||||
|
m.State.hover = idx
|
||||||
|
m.State.hoverOk = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case key.NameDownArrow:
|
||||||
|
if !m.State.hoverOk {
|
||||||
|
m.State.hover = len(items) - 1 // if nothing is selected, select the last item before starting to move backwards
|
||||||
|
}
|
||||||
|
for i := 1; i < len(items); i++ {
|
||||||
|
idx := (m.State.hover + i) % len(items)
|
||||||
|
child := &children[items[idx].childIndex]
|
||||||
|
if child.enabled() {
|
||||||
|
m.State.hover = idx
|
||||||
|
m.State.hoverOk = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case key.NameEnter, key.NameReturn:
|
||||||
|
if m.State.hoverOk && m.State.hover >= 0 && m.State.hover < len(items) {
|
||||||
|
m.activateItem(items[m.State.hover], children)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case key.FocusEvent:
|
||||||
|
if !m.State.hoverOk {
|
||||||
|
m.State.hover = 0
|
||||||
|
}
|
||||||
|
m.State.hoverOk = e.Focus
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := range items {
|
||||||
// make sure we have a tag for every item
|
// make sure we have a tag for every item
|
||||||
for len(m.State.tags) <= i {
|
for len(m.State.tags) <= i {
|
||||||
m.State.tags = append(m.State.tags, false)
|
m.State.tags = append(m.State.tags, false)
|
||||||
}
|
}
|
||||||
// handle pointer events for this item
|
// handle pointer events for this item
|
||||||
for {
|
for {
|
||||||
ev, ok := gtx.Event(pointer.Filter{
|
ev, ok := gtx.Event(pointer.Filter{Target: &m.State.tags[i], Kinds: pointer.Press | pointer.Enter | pointer.Leave})
|
||||||
Target: &m.State.tags[i],
|
|
||||||
Kinds: pointer.Press | pointer.Enter | pointer.Leave,
|
|
||||||
})
|
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -176,27 +292,84 @@ func (m *MenuWidget) update(gtx C, items ...ActionMenuItem) {
|
|||||||
}
|
}
|
||||||
switch e.Kind {
|
switch e.Kind {
|
||||||
case pointer.Press:
|
case pointer.Press:
|
||||||
item.Action.Do()
|
m.activateItem(items[i], children)
|
||||||
m.State.visible = false
|
|
||||||
case pointer.Enter:
|
case pointer.Enter:
|
||||||
m.State.hover = i + 1
|
m.State.hover = i
|
||||||
|
m.State.hoverOk = true
|
||||||
|
if !gtx.Focused(&m.State.tag) {
|
||||||
|
gtx.Execute(key.FocusCmd{Tag: &m.State.tag})
|
||||||
|
}
|
||||||
case pointer.Leave:
|
case pointer.Leave:
|
||||||
if m.State.hover == i+1 {
|
if m.State.hover == i {
|
||||||
m.State.hover = 0
|
m.State.hoverOk = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mb MenuButton) Layout(gtx C, items ...ActionMenuItem) D {
|
func (m *MenuWidget) activateItem(item menuItem, children []MenuChild) {
|
||||||
|
if item.childIndex < 0 || item.childIndex >= len(children) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
child := &children[item.childIndex]
|
||||||
|
if !child.enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch child.kind {
|
||||||
|
case menuChildAction:
|
||||||
|
child.action.Do()
|
||||||
|
case menuChildBool:
|
||||||
|
child.bool.Toggle()
|
||||||
|
case menuChildInt:
|
||||||
|
child.int.SetValue(item.value)
|
||||||
|
}
|
||||||
|
m.State.visible = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *MenuChild) enabled() bool {
|
||||||
|
switch c.kind {
|
||||||
|
case menuChildAction:
|
||||||
|
return c.action.Enabled()
|
||||||
|
case menuChildBool:
|
||||||
|
return c.bool.Enabled()
|
||||||
|
case menuChildWidget:
|
||||||
|
return false // the widget are passive separators and such
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MenuButton displays a button with text that opens a menu when clicked.
|
||||||
|
type MenuButton struct {
|
||||||
|
Title string
|
||||||
|
Style *ButtonStyle
|
||||||
|
Clickable *Clickable
|
||||||
|
MenuState *MenuState
|
||||||
|
Width unit.Dp
|
||||||
|
}
|
||||||
|
|
||||||
|
func MenuBtn(ms *MenuState, cl *Clickable, title string) MenuButton {
|
||||||
|
return MenuButton{MenuState: ms, Clickable: cl, Title: title}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mb MenuButton) WithStyle(style *ButtonStyle) MenuButton { mb.Style = style; return mb }
|
||||||
|
|
||||||
|
func (mb MenuButton) Layout(gtx C, children ...MenuChild) D {
|
||||||
for mb.Clickable.Clicked(gtx) {
|
for mb.Clickable.Clicked(gtx) {
|
||||||
mb.MenuState.visible = true
|
mb.MenuState.visible = true
|
||||||
|
gtx.Execute(key.FocusCmd{Tag: &mb.MenuState.tag})
|
||||||
}
|
}
|
||||||
btn := Btn(mb.Theme, mb.Style, mb.Clickable, mb.Title, "")
|
t := TrackerFromContext(gtx)
|
||||||
|
if mb.Style == nil {
|
||||||
|
mb.Style = &t.Theme.Button.Menu
|
||||||
|
}
|
||||||
|
btn := Btn(t.Theme, mb.Style, mb.Clickable, mb.Title, "")
|
||||||
dims := btn.Layout(gtx)
|
dims := btn.Layout(gtx)
|
||||||
defer op.Offset(image.Pt(0, dims.Size.Y)).Push(gtx.Ops).Pop()
|
if mb.MenuState.visible {
|
||||||
m := Menu(mb.Theme, mb.MenuState)
|
defer op.Offset(image.Pt(0, dims.Size.Y)).Push(gtx.Ops).Pop()
|
||||||
m.Layout(gtx, items...)
|
m := Menu(mb.MenuState)
|
||||||
|
m.Layout(gtx, children...)
|
||||||
|
}
|
||||||
return dims
|
return dims
|
||||||
}
|
}
|
||||||
|
|||||||
@ -465,7 +465,7 @@ type MenuBar struct {
|
|||||||
Clickables []Clickable
|
Clickables []Clickable
|
||||||
MenuStates []MenuState
|
MenuStates []MenuState
|
||||||
|
|
||||||
midiMenuItems []ActionMenuItem
|
midiMenuItems []MenuChild
|
||||||
|
|
||||||
panicHint string
|
panicHint string
|
||||||
PanicBtn *Clickable
|
PanicBtn *Clickable
|
||||||
@ -480,7 +480,7 @@ func NewMenuBar(tr *Tracker) *MenuBar {
|
|||||||
}
|
}
|
||||||
for input := range tr.MIDI().InputDevices {
|
for input := range tr.MIDI().InputDevices {
|
||||||
ret.midiMenuItems = append(ret.midiMenuItems,
|
ret.midiMenuItems = append(ret.midiMenuItems,
|
||||||
MenuItem(tr.MIDI().Open(input), input, "", icons.ImageControlPoint),
|
ActionMenuChild(tr.MIDI().Open(input), input, "", icons.ImageControlPoint),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
@ -492,40 +492,40 @@ func (t *MenuBar) Layout(gtx C) D {
|
|||||||
gtx.Constraints.Min.Y = gtx.Dp(unit.Dp(36))
|
gtx.Constraints.Min.Y = gtx.Dp(unit.Dp(36))
|
||||||
|
|
||||||
flex := layout.Flex{Axis: layout.Horizontal, Alignment: layout.End}
|
flex := layout.Flex{Axis: layout.Horizontal, Alignment: layout.End}
|
||||||
fileBtn := MenuBtn(tr.Theme, &t.MenuStates[0], &t.Clickables[0], "File")
|
fileBtn := MenuBtn(&t.MenuStates[0], &t.Clickables[0], "File")
|
||||||
fileFC := layout.Rigid(func(gtx C) D {
|
fileFC := layout.Rigid(func(gtx C) D {
|
||||||
items := [...]ActionMenuItem{
|
items := [...]MenuChild{
|
||||||
MenuItem(tr.Song().New(), "New Song", keyActionMap["NewSong"], icons.ContentClear),
|
ActionMenuChild(tr.Song().New(), "New Song", keyActionMap["NewSong"], icons.ContentClear),
|
||||||
MenuItem(tr.Song().Open(), "Open Song", keyActionMap["OpenSong"], icons.FileFolder),
|
ActionMenuChild(tr.Song().Open(), "Open Song", keyActionMap["OpenSong"], icons.FileFolder),
|
||||||
MenuItem(tr.Song().Save(), "Save Song", keyActionMap["SaveSong"], icons.ContentSave),
|
ActionMenuChild(tr.Song().Save(), "Save Song", keyActionMap["SaveSong"], icons.ContentSave),
|
||||||
MenuItem(tr.Song().SaveAs(), "Save Song As...", keyActionMap["SaveSongAs"], icons.ContentSave),
|
ActionMenuChild(tr.Song().SaveAs(), "Save Song As...", keyActionMap["SaveSongAs"], icons.ContentSave),
|
||||||
MenuItem(tr.Song().Export(), "Export Wav...", keyActionMap["ExportWav"], icons.ImageAudiotrack),
|
ActionMenuChild(tr.Song().Export(), "Export Wav...", keyActionMap["ExportWav"], icons.ImageAudiotrack),
|
||||||
MenuItem(tr.RequestQuit(), "Quit", keyActionMap["Quit"], icons.ActionExitToApp),
|
ActionMenuChild(tr.RequestQuit(), "Quit", keyActionMap["Quit"], icons.ActionExitToApp),
|
||||||
}
|
}
|
||||||
if !canQuit {
|
if !canQuit {
|
||||||
return fileBtn.Layout(gtx, items[:len(items)-1]...)
|
return fileBtn.Layout(gtx, items[:len(items)-1]...)
|
||||||
}
|
}
|
||||||
return fileBtn.Layout(gtx, items[:]...)
|
return fileBtn.Layout(gtx, items[:]...)
|
||||||
})
|
})
|
||||||
editBtn := MenuBtn(tr.Theme, &t.MenuStates[1], &t.Clickables[1], "Edit")
|
editBtn := MenuBtn(&t.MenuStates[1], &t.Clickables[1], "Edit")
|
||||||
editFC := layout.Rigid(func(gtx C) D {
|
editFC := layout.Rigid(func(gtx C) D {
|
||||||
return editBtn.Layout(gtx,
|
return editBtn.Layout(gtx,
|
||||||
MenuItem(tr.History().Undo(), "Undo", keyActionMap["Undo"], icons.ContentUndo),
|
ActionMenuChild(tr.History().Undo(), "Undo", keyActionMap["Undo"], icons.ContentUndo),
|
||||||
MenuItem(tr.History().Redo(), "Redo", keyActionMap["Redo"], icons.ContentRedo),
|
ActionMenuChild(tr.History().Redo(), "Redo", keyActionMap["Redo"], icons.ContentRedo),
|
||||||
MenuItem(tr.Order().RemoveUnusedPatterns(), "Remove unused data", keyActionMap["RemoveUnused"], icons.ImageCrop),
|
ActionMenuChild(tr.Order().RemoveUnusedPatterns(), "Remove unused data", keyActionMap["RemoveUnused"], icons.ImageCrop),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
midiBtn := MenuBtn(tr.Theme, &t.MenuStates[2], &t.Clickables[2], "MIDI")
|
midiBtn := MenuBtn(&t.MenuStates[2], &t.Clickables[2], "MIDI")
|
||||||
midiFC := layout.Rigid(func(gtx C) D {
|
midiFC := layout.Rigid(func(gtx C) D {
|
||||||
return midiBtn.Layout(gtx, t.midiMenuItems...)
|
return midiBtn.Layout(gtx, t.midiMenuItems...)
|
||||||
})
|
})
|
||||||
helpBtn := MenuBtn(tr.Theme, &t.MenuStates[3], &t.Clickables[3], "?")
|
helpBtn := MenuBtn(&t.MenuStates[3], &t.Clickables[3], "?")
|
||||||
helpFC := layout.Rigid(func(gtx C) D {
|
helpFC := layout.Rigid(func(gtx C) D {
|
||||||
return helpBtn.Layout(gtx,
|
return helpBtn.Layout(gtx,
|
||||||
MenuItem(tr.ShowManual(), "Manual", keyActionMap["ShowManual"], icons.AVLibraryBooks),
|
ActionMenuChild(tr.ShowManual(), "Manual", keyActionMap["ShowManual"], icons.AVLibraryBooks),
|
||||||
MenuItem(tr.AskHelp(), "Ask help", keyActionMap["AskHelp"], icons.ActionHelp),
|
ActionMenuChild(tr.AskHelp(), "Ask help", keyActionMap["AskHelp"], icons.ActionHelp),
|
||||||
MenuItem(tr.ReportBug(), "Report bug", keyActionMap["ReportBug"], icons.ActionBugReport),
|
ActionMenuChild(tr.ReportBug(), "Report bug", keyActionMap["ReportBug"], icons.ActionBugReport),
|
||||||
MenuItem(tr.ShowLicense(), "License", keyActionMap["ShowLicense"], icons.ActionCopyright))
|
ActionMenuChild(tr.ShowLicense(), "License", keyActionMap["ShowLicense"], icons.ActionCopyright))
|
||||||
})
|
})
|
||||||
panicBtn := ToggleIconBtn(tr.Play().Panicked(), tr.Theme, t.PanicBtn, icons.AlertErrorOutline, icons.AlertError, t.panicHint, t.panicHint)
|
panicBtn := ToggleIconBtn(tr.Play().Panicked(), tr.Theme, t.PanicBtn, icons.AlertErrorOutline, icons.AlertError, t.panicHint, t.panicHint)
|
||||||
if tr.Play().Panicked().Value() {
|
if tr.Play().Panicked().Value() {
|
||||||
@ -538,6 +538,15 @@ func (t *MenuBar) Layout(gtx C) D {
|
|||||||
return flex.Layout(gtx, fileFC, editFC, helpFC, panicFC)
|
return flex.Layout(gtx, fileFC, editFC, helpFC, panicFC)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sp *SongPanel) Tags(level int, yield TagYieldFunc) bool {
|
||||||
|
for i := range sp.MenuBar.MenuStates {
|
||||||
|
if !sp.MenuBar.MenuStates[i].Tags(level, yield) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
type PlayBar struct {
|
type PlayBar struct {
|
||||||
RewindBtn *Clickable
|
RewindBtn *Clickable
|
||||||
PlayingBtn *Clickable
|
PlayingBtn *Clickable
|
||||||
|
|||||||
@ -415,10 +415,11 @@ func (t *Tracker) openUrl(url string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tracker) Tags(curLevel int, yield TagYieldFunc) bool {
|
func (t *Tracker) Tags(curLevel int, yield TagYieldFunc) bool {
|
||||||
ret := t.PatchPanel.Tags(curLevel+1, yield)
|
curLevel++
|
||||||
|
ret := t.SongPanel.Tags(curLevel, yield) && t.PatchPanel.Tags(curLevel, yield)
|
||||||
if !t.Play().TrackerHidden().Value() {
|
if !t.Play().TrackerHidden().Value() {
|
||||||
ret = ret && t.OrderEditor.Tags(curLevel+1, yield) &&
|
ret = ret && t.OrderEditor.Tags(curLevel, yield) &&
|
||||||
t.TrackEditor.Tags(curLevel+1, yield)
|
t.TrackEditor.Tags(curLevel, yield)
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|||||||
@ -182,6 +182,12 @@ func (v *playSyntherIndex) SetValue(value int) bool {
|
|||||||
TrySend(v.broker.ToPlayer, any(v.synthers[value]))
|
TrySend(v.broker.ToPlayer, any(v.synthers[value]))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
func (v *playSyntherIndex) StringOf(value int) string {
|
||||||
|
if value < 0 || value >= len(v.synthers) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return v.synthers[value].Name()
|
||||||
|
}
|
||||||
|
|
||||||
// SyntherName returns the name of the currently selected synther.
|
// SyntherName returns the name of the currently selected synther.
|
||||||
func (v *Play) SyntherName() string { return v.synthers[v.syntherIndex].Name() }
|
func (v *Play) SyntherName() string { return v.synthers[v.syntherIndex].Name() }
|
||||||
|
|||||||
@ -71,6 +71,16 @@ func (v *spectrumChannels) SetValue(value int) bool {
|
|||||||
func (v *spectrumChannels) Range() RangeInclusive {
|
func (v *spectrumChannels) Range() RangeInclusive {
|
||||||
return RangeInclusive{0, int(NumSpecChnModes) - 1}
|
return RangeInclusive{0, int(NumSpecChnModes) - 1}
|
||||||
}
|
}
|
||||||
|
func (v *spectrumChannels) StringOf(value int) string {
|
||||||
|
switch SpecChnMode(value) {
|
||||||
|
case SpecChnModeSum:
|
||||||
|
return "Sum"
|
||||||
|
case SpecChnModeSeparate:
|
||||||
|
return "Separate"
|
||||||
|
default:
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type SpecChnMode int
|
type SpecChnMode int
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user