Skip to content

Commit

Permalink
re-design edit bucket/key ui
Browse files Browse the repository at this point in the history
  • Loading branch information
songlim327 committed Aug 15, 2023
1 parent afd364b commit 40f5539
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
18 changes: 16 additions & 2 deletions internal/gui/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package gui

import (
"image/color"
"recoil/internal/cons"
"recoil/resources/images"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)

Expand All @@ -22,15 +24,27 @@ func subWindow(title string) fyne.Window {
}

// itemList returns a generic list widget
func itemList(data binding.DataList, icon *fyne.StaticResource) *widget.List {
func itemList(data binding.DataList, icon *fyne.StaticResource, entity string) *widget.List {
return widget.NewListWithData(data,
// Create item
func() fyne.CanvasObject {
return container.NewHBox(widget.NewIcon(icon), widget.NewLabel(""))
return container.NewHBox(widget.NewIcon(icon), widget.NewLabel(""), layout.NewSpacer(), container.NewGridWithColumns(1, widget.NewButtonWithIcon("", images.Edit, func() {})))
},
// Update item
func(di binding.DataItem, item fyne.CanvasObject) {
item.(*fyne.Container).Objects[1].(*widget.Label).Bind(di.(binding.String))
v, _ := di.(binding.String).Get()

// edit button
editBtn := item.(*fyne.Container).Objects[3].(*fyne.Container).Objects[0].(*widget.Button)

editBtn.OnTapped = func() {
if entity == cons.BucketEntity {
editBucketHandler(v)
} else {
editKeyHandler(v)
}
}
})
}

Expand Down
27 changes: 8 additions & 19 deletions internal/gui/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,13 @@ func deleteKeyHandler(item string) {
}

// editBucketHandler open a new window for editing bucket name
func editBucketHandler() {
func editBucketHandler(b string) {
f, _ := filename.Get()
if f == "" {
dialog.NewInformation(cons.BucketEdit, cons.ErrNoDb, mw).Show()
} else if selBucket == "" {
dialog.NewInformation(cons.BucketEdit, cons.ErrNoBucket, mw).Show()
} else {
bw := subWindow(selBucket)
lBucket := widget.NewLabel(selBucket)
bw := subWindow(b)
lBucket := widget.NewLabel(b)
eBucket := widget.NewEntry()
eBucket.SetPlaceHolder("Enter a new bucket name")
eBucket.Validator = func(text string) error {
Expand All @@ -266,7 +264,7 @@ func editBucketHandler() {
errorHandler(err, bw)
}

err = updateBucket(selBucket, eBucket.Text)
err = updateBucket(b, eBucket.Text)
if err != nil {
errorHandler(err, bw)
} else {
Expand All @@ -285,33 +283,31 @@ func editBucketHandler() {
}

// editKeyHandler open a new window and display key value
func editKeyHandler() {
func editKeyHandler(k string) {
f, _ := filename.Get()
if f == "" {
dialog.NewInformation(cons.KeyEdit, cons.ErrNoDb, mw).Show()
} else if selKey == "" {
dialog.NewInformation(cons.KeyEdit, cons.ErrNoKey, mw).Show()
} else {
// Define textarea widget
textArea := widget.NewMultiLineEntry()
textArea.Wrapping = fyne.TextWrapWord
textArea.SetMinRowsVisible(20)

// Get key value and set to text area
v, err := db.GetKey(selBucket, selKey)
v, err := db.GetKey(selBucket, k)
if err != nil {
errorHandler(err, mw)
}
textArea.SetText(string(v))

kw := subWindow(selKey)
kw := subWindow(k)
f := widget.NewForm(widget.NewFormItem("Value", textArea))
f.SubmitText = "Save"
f.OnCancel = func() {
kw.Close()
}
f.OnSubmit = func() {
err := updateKey(selBucket, selKey, textArea.Text)
err := updateKey(selBucket, k, textArea.Text)
if err != nil {
errorHandler(err, kw)
} else {
Expand All @@ -325,12 +321,6 @@ func editKeyHandler() {
}
}

// keyHandler retrieve key and set to global variable
func keyHandler(id widget.ListItemID) {
v, _ := keys.GetValue(id)
selKey = string(v)
}

// bucketHandler opens bolt database bucket
func bucketHandler(id widget.ListItemID) {
// Get selected bucket
Expand All @@ -345,7 +335,6 @@ func bucketHandler(id widget.ListItemID) {
}

// Clear key item list selected value
selKey = ""
keyItemList.UnselectAll()
}

Expand Down
2 changes: 0 additions & 2 deletions internal/gui/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func bindAllBuckets() error {
buckets.Set(bytesArrToStringArr(bs))

// Clear key item list value
selKey = ""
keys.Set([]string{})
return nil
}
Expand All @@ -76,7 +75,6 @@ func bindAllKeys(bucket string) error {
keys.Set(bytesArrToStringArr(ks))

// clear selected key value
selKey = ""
keyItemList.UnselectAll()
return nil
}
Expand Down
7 changes: 2 additions & 5 deletions internal/gui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

// keyBox renders the key list container
func keyBox() *fyne.Container {
keyItemList = itemList(keys, images.Key)
keyItemList.OnSelected = keyHandler
keyItemList = itemList(keys, images.Key, cons.KeyEntity)
eKey := widget.NewEntry()
eKey.PlaceHolder = "Search for keys..."
eKey.OnChanged = searchKeyHandler
Expand All @@ -23,7 +22,7 @@ func keyBox() *fyne.Container {

// bucBox renders the bucket list container
func bucBox() *fyne.Container {
bucketItemList := itemList(buckets, images.Bucket)
bucketItemList := itemList(buckets, images.Bucket, cons.BucketEntity)
bucketItemList.OnSelected = bucketHandler
eBucket := widget.NewEntry()
eBucket.PlaceHolder = "Search for buckets..."
Expand Down Expand Up @@ -54,8 +53,6 @@ func opsBoxTopView() []fyne.CanvasObject {
openDbHandler()
}),
opsButton(cons.Add, images.Add, func() { addHandler() }),
opsButton(cons.BucketEdit, images.Edit, func() { editBucketHandler() }),
opsButton(cons.KeyEdit, images.Edit, func() { editKeyHandler() }),
opsButton(cons.BucketDelete, images.Delete, func() { deleteBucketHandler(selBucket) }),
opsButton(cons.KeyDelete, images.Delete, func() { deleteKeyHandler(selKey) }),
}
Expand Down

0 comments on commit 40f5539

Please sign in to comment.