Skip to content

Commit

Permalink
ui key selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SayHeyD committed Jan 28, 2024
1 parent 3186973 commit 924fd14
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 22 deletions.
114 changes: 93 additions & 21 deletions internal/ui/sys_tray_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,35 @@ import (
"log"
)

// TODO: restructure the whole file, this reads like spaghetti leftovers of a 3 year old

const (
ConfigEncryption = 0
ConfigDecryption = 1
ConfigEncryptionDecryption = 2
)

func CreateSysTrayMenu(a fyne.App, keys []*key.Key, config *config.Config) {
var menuItems []*fyne.MenuItem

desk, ok := a.(desktop.App)

for _, ageKey := range keys {

selectedKey := *ageKey

ageKeyEncryptionDecryptionMenuEntry := fyne.NewMenuItem("Encryption and decryption", func() {
selectedKey.SetActiveDecryption()
selectedKey.SetActiveEncryption()
UpdateSysTrayMenu(desk, menuItems, selectedKey, ConfigEncryptionDecryption)
})

ageKeyEncryptionMenuEntry := fyne.NewMenuItem("Encryption", func() {
selectedKey.SetActiveEncryption()
UpdateSysTrayMenu(desk, menuItems, selectedKey, ConfigEncryption)
})

ageKeyDecryptionMenuEntry := fyne.NewMenuItem("Decryption", func() {
selectedKey.SetActiveDecryption()
UpdateSysTrayMenu(desk, menuItems, selectedKey, ConfigDecryption)
})

if config.DecryptionKeyName == selectedKey.Name && config.EncryptionKeyName == selectedKey.Name {
ageKeyEncryptionDecryptionMenuEntry.Checked = true
}

if config.EncryptionKeyName == selectedKey.Name && config.DecryptionKeyName != selectedKey.Name {
ageKeyEncryptionDecryptionMenuEntry.Checked = true
}

if config.EncryptionKeyName != selectedKey.Name && config.DecryptionKeyName == selectedKey.Name {
ageKeyEncryptionDecryptionMenuEntry.Checked = true
}

ageKeyMenu := fyne.NewMenuItem(selectedKey.Name, func() {})
ageKeyMenu.ChildMenu = fyne.NewMenu("key options for "+selectedKey.Name,
ageKeyEncryptionDecryptionMenuEntry,
Expand Down Expand Up @@ -69,11 +66,86 @@ func CreateSysTrayMenu(a fyne.App, keys []*key.Key, config *config.Config) {
menuItems = append(menuItems, ageKeyMenu)
}

if desk, ok := a.(desktop.App); ok {
keyMenu := fyne.NewMenuItem("Keys", func() {})
keyMenu.ChildMenu = fyne.NewMenu("Key menu", menuItems...)
if ok {
setMenu(desk, menuItems)
}
}

m := fyne.NewMenu("SAM", keyMenu)
desk.SetSystemTrayMenu(m)
func setMenu(desk desktop.App, menuItems []*fyne.MenuItem) {
keyMenu := fyne.NewMenuItem("Keys", func() {})
keyMenu.ChildMenu = fyne.NewMenu("Key menu", menuItems...)

m := fyne.NewMenu("SAM", keyMenu)
desk.SetSystemTrayMenu(m)
}

func UpdateSysTrayMenu(desk desktop.App, menuItems []*fyne.MenuItem, key key.Key, setMode uint) {

samConfig, err := config.NewConfigFromFile()
if err != nil {
log.Fatal(err)
}

currentEncryptionKey := samConfig.EncryptionKeyName
currentDecryptionKey := samConfig.DecryptionKeyName

for _, menuItem := range menuItems {

childMenuItems := menuItem.ChildMenu.Items

for _, childMenuItem := range childMenuItems {
childMenuItem.Checked = false
}

if key.Name == menuItem.Label {

if setMode == ConfigEncryptionDecryption {
for _, childMenuItem := range childMenuItems {
if childMenuItem.Label == "Encryption and decryption" {
childMenuItem.Checked = true
key.SetActiveEncryption()
key.SetActiveDecryption()
} else {
childMenuItem.Checked = false
}
}
} else if setMode == ConfigEncryption {
for _, childMenuItem := range childMenuItems {
if childMenuItem.Label == "Encryption" {
childMenuItem.Checked = true
key.SetActiveEncryption()
} else {
childMenuItem.Checked = false
}
}
} else if setMode == ConfigDecryption {
for _, childMenuItem := range childMenuItems {
if childMenuItem.Label == "Decryption" {
childMenuItem.Checked = true
key.SetActiveDecryption()
} else {
childMenuItem.Checked = false
}
}
}
}

if setMode == ConfigEncryption && currentEncryptionKey == menuItem.Label {
for _, childMenuItem := range childMenuItems {
if childMenuItem.Label == "Decryption" {
childMenuItem.Checked = true
}
}
}

if setMode == ConfigDecryption && currentDecryptionKey == menuItem.Label {
for _, childMenuItem := range childMenuItems {
if childMenuItem.Label == "Encryption" {
childMenuItem.Checked = true
}
}
}
}

setMenu(desk, menuItems)
}
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func setActivationPolicy() {
func Init(config *config.Config) {
a := app.New()

keys := key.GetAvailableKeys("")
keys := key.GetAvailableKeys(config.KeyDir)

CreateSysTrayMenu(a, keys, config)

Expand Down

0 comments on commit 924fd14

Please sign in to comment.