Skip to content

Commit

Permalink
move keyer macros into a separate dialog to make room for future chan…
Browse files Browse the repository at this point in the history
…ges in the main window
  • Loading branch information
ftl committed May 20, 2024
1 parent 6c7f6e6 commit 468ba2c
Show file tree
Hide file tree
Showing 13 changed files with 1,177 additions and 284 deletions.
2 changes: 2 additions & 0 deletions core/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ var Default = Data{
"tu gl",
"nr {{.MyNumber}} {{.MyXchange}} {{.MyNumber}} {{.MyXchange}}",
},
SPLabels: []string{"MyCall", "R Exch", "TU GL", "Exch AGN"},
RunMacros: []string{
"cq {{.MyCall}} test",
"{{.TheirCall}} {{.MyReport}} {{.MyNumber}} {{.MyXchange}}",
"tu {{.MyCall}} test",
"nr {{.MyNumber}} {{.MyXchange}} {{.MyNumber}} {{.MyXchange}}",
},
RunLabels: []string{"CQ", "Exch", "TU QRZ?", "Exch AGN"},
},
},
SpotLifetime: "10m",
Expand Down
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,16 @@ type KeyerSettings struct {
Preset string `json:"preset"`
SPMacros []string `json:"sp_macros"`
RunMacros []string `json:"run_macros"`
SPLabels []string `json:"sp_labels"`
RunLabels []string `json:"run_labels"`
}

type KeyerPreset struct {
Name string `json:"name"`
SPMacros []string `json:"sp_macros"`
RunMacros []string `json:"run_macros"`
SPLabels []string `json:"sp_labels"`
RunLabels []string `json:"run_labels"`
}

type Score struct {
Expand Down
181 changes: 164 additions & 17 deletions core/keyer/keyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ import (

const PatternCount = 4

// View represents the visual parts of the keyer.
type View interface {
// ButtonView represents the visual parts of trigger the transmission of the keyer macros.
type ButtonView interface {
ShowMessage(...interface{})
SetLabel(int, string)
SetPattern(int, string)
SetSpeed(int)
}

// SettingsView represents the visual parts to enter keyer macros.
type SettingsView interface {
Show()
ShowMessage(...any)
ClearMessage()
SetLabel(core.Workmode, int, string)
SetMacro(core.Workmode, int, string)
SetPresetNames([]string)
SetPreset(string)
}
Expand All @@ -44,8 +54,10 @@ func New(settings core.Settings, client CWClient, keyerSettings core.KeyerSettin
writer: new(nullWriter),
stationCallsign: settings.Station().Callsign,
workmode: workmode,
spLabels: make(map[int]string),
spPatterns: make(map[int]string),
spTemplates: make(map[int]*template.Template),
runLabels: make(map[int]string),
runPatterns: make(map[int]string),
runTemplates: make(map[int]*template.Template),
presets: presets,
Expand All @@ -71,7 +83,8 @@ func presetNames(presets []core.KeyerPreset) []string {

type Keyer struct {
writer Writer
view View
buttonView ButtonView
settingsView SettingsView
client CWClient
presets []core.KeyerPreset
presetNames []string
Expand All @@ -84,10 +97,13 @@ type Keyer struct {
stationCallsign callsign.Callsign
workmode core.Workmode
wpm int
spLabels map[int]string
spPatterns map[int]string
spTemplates map[int]*template.Template
runLabels map[int]string
runPatterns map[int]string
runTemplates map[int]*template.Template
labels *map[int]string
patterns *map[int]string
templates *map[int]*template.Template
}
Expand All @@ -96,9 +112,11 @@ func (k *Keyer) setWorkmode(workmode core.Workmode) {
k.workmode = workmode
switch workmode {
case core.SearchPounce:
k.labels = &k.spLabels
k.patterns = &k.spPatterns
k.templates = &k.spTemplates
case core.Run:
k.labels = &k.runLabels
k.patterns = &k.runPatterns
k.templates = &k.runTemplates
}
Expand All @@ -115,29 +133,40 @@ func (k *Keyer) SetWriter(writer Writer) {
func (k *Keyer) SetSettings(settings core.KeyerSettings) {
k.savedSettings = settings

spLabels := settings.SPLabels
spMacros := settings.SPMacros
runLabels := settings.RunLabels
runMacros := settings.RunMacros

preset, ok := k.presetByName(settings.Preset)
if ok {
spLabels = applyPreset(settings.SPLabels, preset.SPLabels)
spMacros = applyPreset(settings.SPMacros, preset.SPMacros)
runLabels = applyPreset(settings.RunLabels, preset.RunLabels)
runMacros = applyPreset(settings.RunMacros, preset.RunMacros)
}

k.wpm = settings.WPM
for i, label := range spLabels {
k.spLabels[i] = label
}
for i, pattern := range spMacros {
k.spPatterns[i] = pattern
k.spTemplates[i], _ = template.New("").Parse(pattern)
}
for i, label := range runLabels {
k.runLabels[i] = label
}
for i, pattern := range runMacros {
k.runPatterns[i] = pattern
k.runTemplates[i], _ = template.New("").Parse(pattern)
}

k.showPatterns()
if k.view != nil {
k.view.SetSpeed(k.wpm)
if k.buttonView != nil {
k.buttonView.SetSpeed(k.wpm)
}
k.showKeyerSettings()
}

func (k *Keyer) presetByName(name string) (core.KeyerPreset, bool) {
Expand Down Expand Up @@ -172,19 +201,65 @@ func applyPreset(settingsPatterns []string, presetPatterns []string) []string {
return result
}

func (k *Keyer) SetView(view View) {
k.view = view
func (k *Keyer) SetView(view ButtonView) {
k.buttonView = view
k.showPatterns()
k.view.SetPresetNames(k.presetNames)
k.view.SetSpeed(k.wpm)
k.buttonView.SetSpeed(k.wpm)
}

func (k *Keyer) showPatterns() {
if k.view == nil {
if k.buttonView == nil {
return
}
for i, label := range *k.labels {
k.buttonView.SetLabel(i, label)
}
for i, pattern := range *k.patterns {
k.view.SetPattern(i, pattern)
k.buttonView.SetPattern(i, pattern)
}
}

func (k *Keyer) SetSettingsView(view SettingsView) {
k.settingsView = view
k.showKeyerSettings()
k.settingsView.SetPresetNames(k.presetNames)
if k.selectedPreset != nil {
k.settingsView.SetPreset(k.selectedPreset.Name)
} else {
k.settingsView.SetPreset("")
}
}

func (k *Keyer) OpenKeyerSettings() {
if k.settingsView == nil {
return
}

k.settingsView.Show()
k.settingsView.SetPresetNames(k.presetNames)
if k.selectedPreset != nil {
k.settingsView.SetPreset(k.selectedPreset.Name)
} else {
k.settingsView.SetPreset("")
}
k.showKeyerSettings()
}

func (k *Keyer) showKeyerSettings() {
if k.settingsView == nil {
return
}
for i, label := range k.spLabels {
k.settingsView.SetLabel(core.SearchPounce, i, label)
}
for i, pattern := range k.spPatterns {
k.settingsView.SetMacro(core.SearchPounce, i, pattern)
}
for i, label := range k.runLabels {
k.settingsView.SetLabel(core.Run, i, label)
}
for i, pattern := range k.runPatterns {
k.settingsView.SetMacro(core.Run, i, pattern)
}
}

Expand Down Expand Up @@ -218,6 +293,14 @@ func (k *Keyer) KeyerSettings() core.KeyerSettings {
func (k *Keyer) getKeyerSettings() (core.KeyerSettings, bool) {
var keyer core.KeyerSettings
keyer.WPM = k.wpm
keyer.SPLabels = make([]string, len(k.spLabels))
for i := range keyer.SPLabels {
label, ok := k.spLabels[i]
if !ok {
continue
}
keyer.SPLabels[i] = label
}
keyer.SPMacros = make([]string, len(k.spPatterns))
for i := range keyer.SPMacros {
pattern, ok := k.spPatterns[i]
Expand All @@ -226,6 +309,14 @@ func (k *Keyer) getKeyerSettings() (core.KeyerSettings, bool) {
}
keyer.SPMacros[i] = pattern
}
keyer.RunLabels = make([]string, len(k.runLabels))
for i := range keyer.RunLabels {
label, ok := k.runLabels[i]
if !ok {
continue
}
keyer.RunLabels[i] = label
}
keyer.RunMacros = make([]string, len(k.runPatterns))
for i := range keyer.RunMacros {
pattern, ok := k.runPatterns[i]
Expand All @@ -248,19 +339,23 @@ func (k *Keyer) SelectPreset(name string) {
}
}
if k.selectedPreset == nil {
k.view.SetPreset("")
k.settingsView.SetPreset("")
return
}
preset := *k.selectedPreset
k.view.SetPreset(preset.Name)
k.settingsView.SetPreset(preset.Name)

settings := core.KeyerSettings{
WPM: k.savedSettings.WPM,
Preset: name,
SPLabels: make([]string, len(preset.SPLabels)),
SPMacros: make([]string, len(preset.SPMacros)),
RunLabels: make([]string, len(preset.RunLabels)),
RunMacros: make([]string, len(preset.RunMacros)),
}
copy(settings.SPLabels, preset.SPLabels)
copy(settings.SPMacros, preset.SPMacros)
copy(settings.RunLabels, preset.RunLabels)
copy(settings.RunMacros, preset.RunMacros)
k.SetSettings(settings)
k.Save()
Expand All @@ -272,14 +367,66 @@ func (k *Keyer) EnterSpeed(speed int) {
k.client.Speed(k.wpm)
}

func (k *Keyer) EnterLabel(workmode core.Workmode, index int, text string) {
switch workmode {
case core.SearchPounce:
k.spLabels[index] = text
case core.Run:
k.runLabels[index] = text
}

if workmode == k.workmode {
k.buttonView.SetLabel(index, text)
}
}

func (k *Keyer) EnterMacro(workmode core.Workmode, index int, pattern string) {
t, err := template.New("").Parse(pattern)
if err != nil {
k.settingsView.ShowMessage(err)
} else {
k.settingsView.ClearMessage()
}

switch workmode {
case core.SearchPounce:
k.spPatterns[index] = pattern
k.spTemplates[index] = t
case core.Run:
k.runPatterns[index] = pattern
k.runTemplates[index] = t
}

if workmode == k.workmode {
k.buttonView.SetPattern(index, pattern)
}

if k.selectedPreset == nil {
return
}

presetPattern := ""
switch workmode {
case core.SearchPounce:
presetPattern = k.selectedPreset.SPMacros[index]
case core.Run:
presetPattern = k.selectedPreset.RunMacros[index]
}

if presetPattern != pattern {
k.selectedPreset = nil
k.settingsView.SetPreset("")
}
}

func (k *Keyer) EnterPattern(index int, pattern string) {
(*k.patterns)[index] = pattern
var err error
(*k.templates)[index], err = template.New("").Parse(pattern)
if err != nil {
k.view.ShowMessage(err)
k.buttonView.ShowMessage(err)
} else {
k.view.ShowMessage()
k.buttonView.ShowMessage()
}

if k.selectedPreset == nil {
Expand All @@ -296,7 +443,7 @@ func (k *Keyer) EnterPattern(index int, pattern string) {

if presetPattern != pattern {
k.selectedPreset = nil
k.view.SetPreset("")
k.settingsView.SetPreset("")
}
}

Expand Down Expand Up @@ -342,7 +489,7 @@ func (k *Keyer) fillins() map[string]string {
func (k *Keyer) Send(index int) {
message, err := k.GetText(index)
if err != nil {
k.view.ShowMessage(err)
k.buttonView.ShowMessage(err)
return
}
k.send(message)
Expand Down
1 change: 1 addition & 0 deletions core/keyer/keyer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestSend(t *testing.T) {
view.On("SetKeyerController", mock.Anything)
view.On("ShowMessage", mock.Anything)
view.On("SetSpeed", mock.Anything)
view.On("SetLabel", mock.Anything, mock.Anything)
view.On("SetPattern", mock.Anything, mock.Anything)
view.On("SetPresetNames", mock.Anything)
cwClient := new(mocked.CWClient)
Expand Down
4 changes: 4 additions & 0 deletions core/mocked/mocked.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ func (m *KeyerView) Pattern(index int) string {
return args.String(0)
}

func (m *KeyerView) SetLabel(index int, pattern string) {
m.Called(index, pattern)
}

func (m *KeyerView) SetPattern(index int, pattern string) {
m.Called(index, pattern)
}
Expand Down
4 changes: 4 additions & 0 deletions core/pb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,19 @@ func ContestToPB(contest core.Contest) Contest {
func ToKeyerSettings(pbSettings Keyer) (core.KeyerSettings, error) {
var result core.KeyerSettings
result.WPM = int(pbSettings.Wpm)
result.SPLabels = pbSettings.SpLabels
result.SPMacros = pbSettings.SpMacros
result.RunLabels = pbSettings.RunLabels
result.RunMacros = pbSettings.RunMacros
return result, nil
}

func KeyerSettingsToPB(settings core.KeyerSettings) Keyer {
return Keyer{
Wpm: int32(settings.WPM),
SpLabels: settings.SPLabels,
SpMacros: settings.SPMacros,
RunLabels: settings.RunLabels,
RunMacros: settings.RunMacros,
}
}
Loading

0 comments on commit 468ba2c

Please sign in to comment.