mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-19 21:44:38 -04:00
refactor(tracker/gioui): bind Alerts to Model during Layout
This commit is contained in:
parent
355ccefb6f
commit
18d198d764
@ -9,44 +9,63 @@ import (
|
|||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
"gioui.org/op/clip"
|
"gioui.org/op/clip"
|
||||||
"gioui.org/op/paint"
|
"gioui.org/op/paint"
|
||||||
"gioui.org/unit"
|
|
||||||
"github.com/vsariola/sointu/tracker"
|
"github.com/vsariola/sointu/tracker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PopupAlert struct {
|
type (
|
||||||
alerts *tracker.Alerts
|
AlertsState struct {
|
||||||
prevUpdate time.Time
|
prevUpdate time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type PopupAlertStyle struct {
|
AlertStyle struct {
|
||||||
Bg color.NRGBA
|
Bg color.NRGBA
|
||||||
Text LabelStyle
|
Text LabelStyle
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertStyles struct {
|
||||||
|
Info AlertStyle
|
||||||
|
Warning AlertStyle
|
||||||
|
Error AlertStyle
|
||||||
|
Margin layout.Inset
|
||||||
|
Inset layout.Inset
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertsWidget struct {
|
||||||
|
Theme *Theme
|
||||||
|
Model *tracker.Alerts
|
||||||
|
State *AlertsState
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewAlertsState() *AlertsState {
|
||||||
|
return &AlertsState{prevUpdate: time.Now()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var alertMargin = layout.UniformInset(unit.Dp(6))
|
func Alerts(m *tracker.Alerts, th *Theme, st *AlertsState) AlertsWidget {
|
||||||
var alertInset = layout.UniformInset(unit.Dp(6))
|
return AlertsWidget{
|
||||||
|
Theme: th,
|
||||||
func NewPopupAlert(alerts *tracker.Alerts) *PopupAlert {
|
Model: m,
|
||||||
return &PopupAlert{alerts: alerts, prevUpdate: time.Now()}
|
State: st,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *PopupAlert) Layout(gtx C, th *Theme) D {
|
func (a *AlertsWidget) Layout(gtx C) D {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if a.alerts.Update(now.Sub(a.prevUpdate)) {
|
if a.Model.Update(now.Sub(a.State.prevUpdate)) {
|
||||||
gtx.Execute(op.InvalidateCmd{At: now.Add(50 * time.Millisecond)})
|
gtx.Execute(op.InvalidateCmd{At: now.Add(50 * time.Millisecond)})
|
||||||
}
|
}
|
||||||
a.prevUpdate = now
|
a.State.prevUpdate = now
|
||||||
|
|
||||||
var totalY float64 = float64(gtx.Dp(38))
|
var totalY float64 = float64(gtx.Dp(38))
|
||||||
for _, alert := range a.alerts.Iterate {
|
for _, alert := range a.Model.Iterate {
|
||||||
var alertStyle *PopupAlertStyle
|
var alertStyle *AlertStyle
|
||||||
switch alert.Priority {
|
switch alert.Priority {
|
||||||
case tracker.Warning:
|
case tracker.Warning:
|
||||||
alertStyle = &th.Alert.Warning
|
alertStyle = &a.Theme.Alert.Warning
|
||||||
case tracker.Error:
|
case tracker.Error:
|
||||||
alertStyle = &th.Alert.Error
|
alertStyle = &a.Theme.Alert.Error
|
||||||
default:
|
default:
|
||||||
alertStyle = &th.Alert.Info
|
alertStyle = &a.Theme.Alert.Info
|
||||||
}
|
}
|
||||||
bgWidget := func(gtx C) D {
|
bgWidget := func(gtx C) D {
|
||||||
paint.FillShape(gtx.Ops, alertStyle.Bg, clip.Rect{
|
paint.FillShape(gtx.Ops, alertStyle.Bg, clip.Rect{
|
||||||
@ -54,8 +73,8 @@ func (a *PopupAlert) Layout(gtx C, th *Theme) D {
|
|||||||
}.Op())
|
}.Op())
|
||||||
return D{Size: gtx.Constraints.Min}
|
return D{Size: gtx.Constraints.Min}
|
||||||
}
|
}
|
||||||
labelStyle := Label(th, &alertStyle.Text, alert.Message)
|
labelStyle := Label(a.Theme, &alertStyle.Text, alert.Message)
|
||||||
alertMargin.Layout(gtx, func(gtx C) D {
|
a.Theme.Alert.Margin.Layout(gtx, func(gtx C) D {
|
||||||
return layout.S.Layout(gtx, func(gtx C) D {
|
return layout.S.Layout(gtx, func(gtx C) D {
|
||||||
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
|
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
|
||||||
gtx.Constraints.Min.X = gtx.Constraints.Max.X
|
gtx.Constraints.Min.X = gtx.Constraints.Max.X
|
||||||
@ -63,11 +82,11 @@ func (a *PopupAlert) Layout(gtx C, th *Theme) D {
|
|||||||
dims := layout.Stack{Alignment: layout.Center}.Layout(gtx,
|
dims := layout.Stack{Alignment: layout.Center}.Layout(gtx,
|
||||||
layout.Expanded(bgWidget),
|
layout.Expanded(bgWidget),
|
||||||
layout.Stacked(func(gtx C) D {
|
layout.Stacked(func(gtx C) D {
|
||||||
return alertInset.Layout(gtx, labelStyle.Layout)
|
return a.Theme.Alert.Inset.Layout(gtx, labelStyle.Layout)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
macro := recording.Stop()
|
macro := recording.Stop()
|
||||||
delta := float64(dims.Size.Y + gtx.Dp(alertMargin.Bottom))
|
delta := float64(dims.Size.Y + gtx.Dp(a.Theme.Alert.Margin.Bottom))
|
||||||
op.Offset(image.Point{0, int(-totalY*alert.FadeLevel + delta*(1-alert.FadeLevel))}).Add((gtx.Ops))
|
op.Offset(image.Point{0, int(-totalY*alert.FadeLevel + delta*(1-alert.FadeLevel))}).Add((gtx.Ops))
|
||||||
totalY += delta
|
totalY += delta
|
||||||
macro.Add(gtx.Ops)
|
macro.Add(gtx.Ops)
|
@ -35,11 +35,7 @@ type Theme struct {
|
|||||||
ErrorColor color.NRGBA
|
ErrorColor color.NRGBA
|
||||||
Bg color.NRGBA
|
Bg color.NRGBA
|
||||||
}
|
}
|
||||||
Alert struct {
|
Alert AlertStyles
|
||||||
Warning PopupAlertStyle
|
|
||||||
Error PopupAlertStyle
|
|
||||||
Info PopupAlertStyle
|
|
||||||
}
|
|
||||||
NoteEditor struct {
|
NoteEditor struct {
|
||||||
TrackTitle LabelStyle
|
TrackTitle LabelStyle
|
||||||
OrderRow LabelStyle
|
OrderRow LabelStyle
|
||||||
|
@ -110,6 +110,8 @@ alert:
|
|||||||
info:
|
info:
|
||||||
bg: { r: 50, g: 50, b: 51, a: 255 }
|
bg: { r: 50, g: 50, b: 51, a: 255 }
|
||||||
text: { textsize: 16, color: *highemphasis, shadowcolor: *black }
|
text: { textsize: 16, color: *highemphasis, shadowcolor: *black }
|
||||||
|
margin: { top: 6, bottom: 6, left: 6, right: 6 }
|
||||||
|
inset: { top: 6, bottom: 6, left: 6, right: 6 }
|
||||||
ordereditor:
|
ordereditor:
|
||||||
tracktitle: { textsize: 12, color: *mediumemphasis }
|
tracktitle: { textsize: 12, color: *mediumemphasis }
|
||||||
rowtitle:
|
rowtitle:
|
||||||
|
@ -37,7 +37,7 @@ type (
|
|||||||
BottomHorizontalSplit *Split
|
BottomHorizontalSplit *Split
|
||||||
VerticalSplit *Split
|
VerticalSplit *Split
|
||||||
KeyNoteMap Keyboard[key.Name]
|
KeyNoteMap Keyboard[key.Name]
|
||||||
PopupAlert *PopupAlert
|
PopupAlert *AlertsState
|
||||||
Zoom int
|
Zoom int
|
||||||
|
|
||||||
DialogState *DialogState
|
DialogState *DialogState
|
||||||
@ -96,7 +96,7 @@ func NewTracker(model *tracker.Model) *Tracker {
|
|||||||
}
|
}
|
||||||
t.SongPanel = NewSongPanel(t)
|
t.SongPanel = NewSongPanel(t)
|
||||||
t.KeyNoteMap = MakeKeyboard[key.Name](model.Broker())
|
t.KeyNoteMap = MakeKeyboard[key.Name](model.Broker())
|
||||||
t.PopupAlert = NewPopupAlert(model.Alerts())
|
t.PopupAlert = NewAlertsState()
|
||||||
var warn error
|
var warn error
|
||||||
if t.Theme, warn = NewTheme(); warn != nil {
|
if t.Theme, warn = NewTheme(); warn != nil {
|
||||||
model.Alerts().AddAlert(tracker.Alert{
|
model.Alerts().AddAlert(tracker.Alert{
|
||||||
@ -222,7 +222,8 @@ func (t *Tracker) Layout(gtx layout.Context, w *app.Window) {
|
|||||||
t.layoutTop,
|
t.layoutTop,
|
||||||
t.layoutBottom)
|
t.layoutBottom)
|
||||||
}
|
}
|
||||||
t.PopupAlert.Layout(gtx, t.Theme)
|
alerts := Alerts(t.Alerts(), t.Theme, t.PopupAlert)
|
||||||
|
alerts.Layout(gtx)
|
||||||
t.showDialog(gtx)
|
t.showDialog(gtx)
|
||||||
// this is the top level input handler for the whole app
|
// this is the top level input handler for the whole app
|
||||||
// it handles all the global key events and clipboard events
|
// it handles all the global key events and clipboard events
|
||||||
|
Reference in New Issue
Block a user