-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a new button type to switch pages
- Loading branch information
Showing
7 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package hamdeck | ||
|
||
import "image" | ||
|
||
/* | ||
PageButton | ||
*/ | ||
|
||
type PageButton struct { | ||
BaseButton | ||
image image.Image | ||
|
||
pageSwitcher PageSwitcher | ||
id string | ||
label string | ||
} | ||
|
||
func NewPageButton(pageSwitcher PageSwitcher, id string, label string) *PageButton { | ||
return &PageButton{ | ||
pageSwitcher: pageSwitcher, | ||
id: id, | ||
label: label, | ||
} | ||
} | ||
|
||
func (b *PageButton) Image(gc GraphicContext, redrawImages bool) image.Image { | ||
if b.image == nil || redrawImages { | ||
gc.SetForeground(White) | ||
gc.SetBackground(Black) | ||
b.image = gc.DrawSingleLineTextButton(b.label) | ||
} | ||
return b.image | ||
} | ||
|
||
func (b *PageButton) Pressed() { | ||
b.pageSwitcher.AttachPage(b.id) | ||
} | ||
|
||
func (b *PageButton) Released() { | ||
// nop | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package hamdeck | ||
|
||
import "log" | ||
|
||
const ( | ||
ConfigPage = "page" | ||
ConfigLabel = "label" | ||
) | ||
|
||
const ( | ||
PageButtonType = "hamdeck.Page" | ||
) | ||
|
||
type Factory struct { | ||
pageSwitcher PageSwitcher | ||
} | ||
|
||
type PageSwitcher interface { | ||
AttachPage(string) error | ||
} | ||
|
||
func NewButtonFactory(pageSwitcher PageSwitcher) *Factory { | ||
return &Factory{ | ||
pageSwitcher: pageSwitcher, | ||
} | ||
} | ||
|
||
func (f *Factory) Close() { | ||
// nop | ||
} | ||
|
||
func (f *Factory) CreateButton(config map[string]any) Button { | ||
switch config[ConfigType] { | ||
case PageButtonType: | ||
return f.createPageButton(config) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (f *Factory) createPageButton(config map[string]any) Button { | ||
id, haveID := ToString(config[ConfigPage]) | ||
label, haveLabel := ToString(config[ConfigLabel]) | ||
if !haveID { | ||
log.Print("A hamdeck.Page button must have a page field.") | ||
} | ||
if !haveLabel { | ||
log.Print("A hamdeck.Page button must have a label field.") | ||
} | ||
return NewPageButton(f.pageSwitcher, id, label) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 0, "page": "1", "label": "1"}, | ||
{"type": "hamdeck.Page", "index": 1, "page": "2", "label": "2"}, | ||
{"type": "hamdeck.Page", "index": 2, "page": "3", "label": "3"}, | ||
{"type": "hamdeck.Page", "index": 3, "page": "4", "label": "4"}, | ||
{"type": "hamdeck.Page", "index": 4, "page": "5", "label": "5"}, | ||
{"type": "hamdeck.Page", "index": 5, "page": "6", "label": "6"}, | ||
{"type": "hamdeck.Page", "index": 6, "page": "7", "label": "7"}, | ||
{"type": "hamdeck.Page", "index": 7, "page": "8", "label": "8"} | ||
], | ||
"pages": { | ||
"1": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 0, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"2": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 1, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"3": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 2, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"4": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 3, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"5": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 4, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"6": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 5, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"7": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 6, "page": "", "label": "Back"} | ||
] | ||
}, | ||
"8": { | ||
"buttons": [ | ||
{"type": "hamdeck.Page", "index": 7, "page": "", "label": "Back"} | ||
] | ||
} | ||
} | ||
} |