mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-18 21:14:31 -04:00
feat!: rewrote the GUI and model for better testability
The Model was getting unmaintanable mess. This is an attempt to refactor/rewrite the Model so that data of certain type is exposed in standardized way, offering certain standard manipulations for that data type, and on the GUI side, certain standard widgets to tied to that data. This rewrite closes #72, #106 and #120.
This commit is contained in:
parent
6d3c65e11d
commit
d92426a100
@ -1,56 +1,82 @@
|
||||
package gioui
|
||||
|
||||
import (
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"gioui.org/widget/material"
|
||||
"github.com/vsariola/sointu/tracker"
|
||||
)
|
||||
|
||||
type Dialog struct {
|
||||
Visible bool
|
||||
BtnAlt widget.Clickable
|
||||
BtnOk widget.Clickable
|
||||
BtnCancel widget.Clickable
|
||||
BtnAlt *ActionClickable
|
||||
BtnOk *ActionClickable
|
||||
BtnCancel *ActionClickable
|
||||
tag bool
|
||||
}
|
||||
|
||||
type DialogStyle struct {
|
||||
dialog *Dialog
|
||||
Title string
|
||||
Text string
|
||||
Inset layout.Inset
|
||||
ShowAlt bool
|
||||
TextInset layout.Inset
|
||||
AltStyle material.ButtonStyle
|
||||
OkStyle material.ButtonStyle
|
||||
CancelStyle material.ButtonStyle
|
||||
Shaper *text.Shaper
|
||||
}
|
||||
|
||||
func ConfirmDialog(th *material.Theme, dialog *Dialog, text string, shaper *text.Shaper) DialogStyle {
|
||||
func NewDialog(ok, alt, cancel tracker.Action) *Dialog {
|
||||
return &Dialog{
|
||||
BtnOk: NewActionClickable(ok),
|
||||
BtnAlt: NewActionClickable(alt),
|
||||
BtnCancel: NewActionClickable(cancel),
|
||||
}
|
||||
}
|
||||
|
||||
func ConfirmDialog(th *material.Theme, dialog *Dialog, title, text string) DialogStyle {
|
||||
ret := DialogStyle{
|
||||
dialog: dialog,
|
||||
Title: title,
|
||||
Text: text,
|
||||
Inset: layout.Inset{Top: unit.Dp(12), Bottom: unit.Dp(12), Left: unit.Dp(20), Right: unit.Dp(20)},
|
||||
AltStyle: HighEmphasisButton(th, &dialog.BtnAlt, "Alt"),
|
||||
OkStyle: HighEmphasisButton(th, &dialog.BtnOk, "Ok"),
|
||||
CancelStyle: HighEmphasisButton(th, &dialog.BtnCancel, "Cancel"),
|
||||
Shaper: shaper,
|
||||
TextInset: layout.Inset{Top: unit.Dp(12), Bottom: unit.Dp(12)},
|
||||
AltStyle: ActionButton(th, dialog.BtnAlt, "Alt"),
|
||||
OkStyle: ActionButton(th, dialog.BtnOk, "Ok"),
|
||||
CancelStyle: ActionButton(th, dialog.BtnCancel, "Cancel"),
|
||||
Shaper: th.Shaper,
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (d *DialogStyle) Layout(gtx C) D {
|
||||
if d.dialog.Visible {
|
||||
paint.Fill(gtx.Ops, dialogBgColor)
|
||||
return layout.Center.Layout(gtx, func(gtx C) D {
|
||||
return Popup(&d.dialog.Visible).Layout(gtx, func(gtx C) D {
|
||||
return d.Inset.Layout(gtx, func(gtx C) D {
|
||||
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle}.Layout(gtx,
|
||||
layout.Rigid(Label(d.Text, highEmphasisTextColor, d.Shaper)),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
if !d.dialog.BtnOk.Clickable.Focused() && !d.dialog.BtnCancel.Clickable.Focused() && !d.dialog.BtnAlt.Clickable.Focused() {
|
||||
d.dialog.BtnCancel.Clickable.Focus()
|
||||
}
|
||||
paint.Fill(gtx.Ops, dialogBgColor)
|
||||
text := func(gtx C) D {
|
||||
return d.TextInset.Layout(gtx, LabelStyle{Text: d.Text, Color: highEmphasisTextColor, Font: labelDefaultFont, FontSize: unit.Sp(14), Shaper: d.Shaper}.Layout)
|
||||
}
|
||||
for _, e := range gtx.Events(&d.dialog.tag) {
|
||||
if e, ok := e.(key.Event); ok && e.State == key.Press {
|
||||
d.command(e)
|
||||
}
|
||||
}
|
||||
visible := true
|
||||
return layout.Center.Layout(gtx, func(gtx C) D {
|
||||
return Popup(&visible).Layout(gtx, func(gtx C) D {
|
||||
key.InputOp{Tag: &d.dialog.tag, Keys: "⎋|←|→|Tab"}.Add(gtx.Ops)
|
||||
return d.Inset.Layout(gtx, func(gtx C) D {
|
||||
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle}.Layout(gtx,
|
||||
layout.Rigid(Label(d.Title, highEmphasisTextColor, d.Shaper)),
|
||||
layout.Rigid(text),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return layout.E.Layout(gtx, func(gtx C) D {
|
||||
gtx.Constraints.Min.X = gtx.Dp(unit.Dp(120))
|
||||
if d.ShowAlt {
|
||||
if d.dialog.BtnAlt.Action.Allowed() {
|
||||
return layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceBetween}.Layout(gtx,
|
||||
layout.Rigid(d.OkStyle.Layout),
|
||||
layout.Rigid(d.AltStyle.Layout),
|
||||
@ -61,11 +87,43 @@ func (d *DialogStyle) Layout(gtx C) D {
|
||||
layout.Rigid(d.OkStyle.Layout),
|
||||
layout.Rigid(d.CancelStyle.Layout),
|
||||
)
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
return D{}
|
||||
})
|
||||
}
|
||||
|
||||
func (d *DialogStyle) command(e key.Event) {
|
||||
switch e.Name {
|
||||
case key.NameEscape:
|
||||
d.dialog.BtnCancel.Action.Do()
|
||||
case key.NameLeftArrow:
|
||||
switch {
|
||||
case d.dialog.BtnOk.Clickable.Focused():
|
||||
d.dialog.BtnCancel.Clickable.Focus()
|
||||
case d.dialog.BtnCancel.Clickable.Focused():
|
||||
if d.dialog.BtnAlt.Action.Allowed() {
|
||||
d.dialog.BtnAlt.Clickable.Focus()
|
||||
} else {
|
||||
d.dialog.BtnOk.Clickable.Focus()
|
||||
}
|
||||
case d.dialog.BtnAlt.Clickable.Focused():
|
||||
d.dialog.BtnOk.Clickable.Focus()
|
||||
}
|
||||
case key.NameRightArrow, key.NameTab:
|
||||
switch {
|
||||
case d.dialog.BtnOk.Clickable.Focused():
|
||||
if d.dialog.BtnAlt.Action.Allowed() {
|
||||
d.dialog.BtnAlt.Clickable.Focus()
|
||||
} else {
|
||||
d.dialog.BtnCancel.Clickable.Focus()
|
||||
}
|
||||
case d.dialog.BtnCancel.Clickable.Focused():
|
||||
d.dialog.BtnOk.Clickable.Focus()
|
||||
case d.dialog.BtnAlt.Clickable.Focused():
|
||||
d.dialog.BtnCancel.Clickable.Focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user