2021-03-06 11:30:44 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-06 14:21:30 +01:00
|
|
|
"embed"
|
2021-03-06 11:30:44 +01:00
|
|
|
"encoding/json"
|
2021-03-06 14:24:21 +01:00
|
|
|
"flag"
|
2021-03-06 11:30:44 +01:00
|
|
|
"fmt"
|
2021-03-06 14:33:29 +01:00
|
|
|
"log"
|
2021-03-06 11:30:44 +01:00
|
|
|
"sync"
|
2021-03-06 14:33:29 +01:00
|
|
|
"time"
|
2021-03-06 11:30:44 +01:00
|
|
|
|
2021-03-06 11:37:06 +01:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/app"
|
|
|
|
"fyne.io/fyne/v2/canvas"
|
|
|
|
"fyne.io/fyne/v2/container"
|
|
|
|
"fyne.io/fyne/v2/theme"
|
|
|
|
"fyne.io/fyne/v2/widget"
|
2021-03-06 11:30:44 +01:00
|
|
|
"github.com/sahilm/fuzzy"
|
|
|
|
)
|
|
|
|
|
2021-03-06 14:21:30 +01:00
|
|
|
//go:embed data/gitmojis.json data/images
|
|
|
|
var data embed.FS
|
|
|
|
|
2021-03-10 17:38:42 +01:00
|
|
|
var preferredTheme = flag.String("theme", "", "Specify the theme to be used. dark/light")
|
2021-03-06 14:33:29 +01:00
|
|
|
var linger = flag.String("linger", "", "If specified, linger for the given duration (e.g. 10s) before actually closing.")
|
2021-03-06 14:24:21 +01:00
|
|
|
|
2021-03-06 11:30:44 +01:00
|
|
|
type Gitmoji struct {
|
|
|
|
Emoji string
|
|
|
|
Code string
|
|
|
|
Description string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitmojiData struct {
|
|
|
|
Gitmojis []Gitmoji
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitmojiEntry struct {
|
|
|
|
Gitmoji
|
|
|
|
Icon fyne.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
type keyedEntry struct {
|
|
|
|
widget.Entry
|
|
|
|
onKeyTyped func(keyEvent *fyne.KeyEvent) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *keyedEntry) TypedKey(keyEvent *fyne.KeyEvent) {
|
|
|
|
if e.onKeyTyped == nil || !e.onKeyTyped(keyEvent) {
|
|
|
|
e.Entry.TypedKey(keyEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newKeyedEntry() *keyedEntry {
|
|
|
|
e := &keyedEntry{}
|
|
|
|
e.ExtendBaseWidget(e)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-03-06 14:24:21 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-03-06 11:30:44 +01:00
|
|
|
gitmojiData, err := loadData()
|
|
|
|
if err != nil {
|
|
|
|
// TODO show dialog
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := app.New()
|
2021-03-06 14:33:29 +01:00
|
|
|
|
2021-03-06 14:24:21 +01:00
|
|
|
switch *preferredTheme {
|
|
|
|
case "dark":
|
|
|
|
a.Settings().SetTheme(theme.DarkTheme())
|
2021-03-10 17:38:42 +01:00
|
|
|
case "light":
|
2021-03-06 14:24:21 +01:00
|
|
|
a.Settings().SetTheme(theme.LightTheme())
|
|
|
|
}
|
2021-03-06 14:33:29 +01:00
|
|
|
|
|
|
|
var lingerDuration *time.Duration = nil
|
|
|
|
if *linger != "" {
|
|
|
|
if d, err := time.ParseDuration(*linger); err != nil {
|
|
|
|
log.Fatalf("invalid linger time: %v\n", err)
|
|
|
|
} else {
|
|
|
|
lingerDuration = &d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:30:44 +01:00
|
|
|
w := a.NewWindow("Gitmoji Picker")
|
|
|
|
w.CenterOnScreen()
|
|
|
|
|
|
|
|
allEntries := make([]*GitmojiEntry, len(gitmojiData.Gitmojis))
|
|
|
|
allTerms := make([]string, len(gitmojiData.Gitmojis))
|
|
|
|
currentId := 0
|
|
|
|
|
|
|
|
for i, gitmoji := range gitmojiData.Gitmojis {
|
2021-03-06 14:21:30 +01:00
|
|
|
b, err := data.ReadFile("data/images/" + gitmoji.Name + ".png")
|
2021-03-06 11:30:44 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
allEntries[i] = &GitmojiEntry{
|
|
|
|
Gitmoji: gitmoji,
|
2021-03-06 14:21:30 +01:00
|
|
|
Icon: fyne.NewStaticResource(gitmoji.Name, b),
|
2021-03-06 11:30:44 +01:00
|
|
|
}
|
|
|
|
allTerms[i] = gitmoji.Name + " " + gitmoji.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
buildFilteredList := func(filter string) []*GitmojiEntry {
|
|
|
|
r := fuzzy.Find(filter, allTerms)
|
|
|
|
result := make([]*GitmojiEntry, len(r))
|
|
|
|
for i := range r {
|
|
|
|
result[i] = allEntries[r[i].Index]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
var m sync.Mutex
|
|
|
|
currentEntries := allEntries
|
|
|
|
|
|
|
|
e := newKeyedEntry()
|
|
|
|
|
|
|
|
l := widget.NewList(
|
|
|
|
func() int {
|
|
|
|
return len(currentEntries)
|
|
|
|
},
|
|
|
|
func() fyne.CanvasObject {
|
2021-03-06 14:16:59 +01:00
|
|
|
tn := canvas.NewText("Dummy", theme.ForegroundColor())
|
2021-03-06 11:30:44 +01:00
|
|
|
tn.TextStyle.Bold = true
|
|
|
|
|
2021-03-06 14:16:59 +01:00
|
|
|
td := canvas.NewText("Dummy", theme.ForegroundColor())
|
2021-03-06 11:30:44 +01:00
|
|
|
td.TextSize = theme.TextSize() * 8 / 10
|
|
|
|
|
|
|
|
return container.NewHBox(
|
|
|
|
widget.NewIcon(allEntries[0].Icon),
|
|
|
|
container.NewVBox(tn, td),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
func(id widget.ListItemID, object fyne.CanvasObject) {
|
|
|
|
m.Lock()
|
|
|
|
entry := currentEntries[id]
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
hb := object.(*fyne.Container)
|
|
|
|
hb.Objects[0].(*widget.Icon).SetResource(entry.Icon)
|
|
|
|
vb := hb.Objects[1].(*fyne.Container)
|
|
|
|
vb.Objects[0].(*canvas.Text).Text = entry.Name
|
|
|
|
vb.Objects[1].(*canvas.Text).Text = entry.Description
|
|
|
|
|
|
|
|
vb.Objects[0].(*canvas.Text).Refresh()
|
|
|
|
vb.Objects[1].(*canvas.Text).Refresh()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
e.OnChanged = func(s string) {
|
|
|
|
m.Lock()
|
|
|
|
if s == "" {
|
|
|
|
currentEntries = allEntries
|
|
|
|
} else {
|
|
|
|
currentEntries = buildFilteredList(s)
|
|
|
|
}
|
|
|
|
if len(currentEntries) > 0 {
|
|
|
|
currentId = 0
|
|
|
|
} else {
|
|
|
|
currentId = -1
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
l.Select(currentId)
|
|
|
|
l.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Select(currentId)
|
2021-09-23 14:51:34 +02:00
|
|
|
l.OnSelected = func(id widget.ListItemID) {
|
|
|
|
currentId = id
|
|
|
|
w.Canvas().Focus(e)
|
|
|
|
}
|
2021-03-06 11:30:44 +01:00
|
|
|
|
|
|
|
w.SetContent(container.NewBorder(e, nil, nil, nil, l))
|
|
|
|
w.Resize(fyne.NewSize(300, 300))
|
|
|
|
|
|
|
|
e.Show()
|
|
|
|
|
|
|
|
e.onKeyTyped = func(event *fyne.KeyEvent) bool {
|
|
|
|
switch event.Name {
|
|
|
|
case fyne.KeyDown:
|
|
|
|
m.Lock()
|
|
|
|
if currentId < len(currentEntries)-1 {
|
|
|
|
currentId++
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
l.Select(currentId)
|
|
|
|
return true
|
|
|
|
case fyne.KeyUp:
|
|
|
|
m.Lock()
|
|
|
|
if currentId > 0 {
|
|
|
|
currentId--
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
l.Select(currentId)
|
|
|
|
return true
|
|
|
|
case fyne.KeyEscape:
|
|
|
|
w.Close()
|
|
|
|
return true
|
|
|
|
case fyne.KeyReturn, fyne.KeyEnter:
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if currentId > -1 {
|
|
|
|
w.Clipboard().SetContent(currentEntries[currentId].Emoji)
|
2021-03-06 14:33:29 +01:00
|
|
|
if lingerDuration != nil {
|
|
|
|
w.Hide()
|
|
|
|
time.Sleep(*lingerDuration)
|
|
|
|
}
|
2021-03-06 11:30:44 +01:00
|
|
|
w.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-03-06 14:16:59 +01:00
|
|
|
w.Canvas().SetOnTypedKey(func(event *fyne.KeyEvent) {
|
|
|
|
e.onKeyTyped(event)
|
|
|
|
})
|
2021-03-06 11:30:44 +01:00
|
|
|
|
|
|
|
w.Canvas().Focus(e)
|
|
|
|
|
|
|
|
w.ShowAndRun()
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadData() (*GitmojiData, error) {
|
2021-03-06 14:21:30 +01:00
|
|
|
b, err := data.ReadFile("data/gitmojis.json")
|
2021-03-06 11:30:44 +01:00
|
|
|
if err != nil {
|
2021-03-06 14:21:30 +01:00
|
|
|
return nil, fmt.Errorf("cannot read gitmoji data: %w", err)
|
2021-03-06 11:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var result GitmojiData
|
2021-03-06 14:21:30 +01:00
|
|
|
if err := json.Unmarshal(b, &result); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot decode gitmoji data: %w", err)
|
2021-03-06 11:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|