Skip to content

Commit

Permalink
feat: edit resource tags
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Sep 28, 2024
1 parent 024a1ce commit cc92de1
Show file tree
Hide file tree
Showing 16 changed files with 516 additions and 133 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
fyne.io/fyne/v2 v2.5.1
github.com/alecthomas/kong v1.2.1
github.com/chai2010/webp v1.1.1
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.1
github.com/schollz/progressbar/v3 v3.16.0
github.com/sirupsen/logrus v1.9.3
Expand All @@ -19,7 +20,6 @@ require (
require (
fyne.io/systray v1.11.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fredbi/uri v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
Expand Down
129 changes: 129 additions & 0 deletions internal/gui/bindings/bindings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bindings

import (
"errors"

"fyne.io/fyne/v2/data/binding"
)

Expand Down Expand Up @@ -142,3 +144,130 @@ func (bm *boundMapping[F, T]) Set(t T) error {
}
return nil
}

type ExternalBound[T any] interface {
Bound[T]
Reload() error
}

var errWrongType = errors.New("wrong type provided")

type mappedBinding[T any] struct {
Bound[T]
v T

counter int
self binding.ExternalInt

set func(T) error
get func() (T, error)
}

func MappedBind[T any](get func() (T, error), set func(T) error) ExternalBound[T] {
b := &mappedBinding[T]{
get: get,
set: set,
}
val, err := b.get()
if err == nil {
b.v = val
}
b.self = binding.BindInt(&b.counter)
return b
}

func (m *mappedBinding[T]) AddListener(listener binding.DataListener) {
m.self.AddListener(listener)
}

func (m *mappedBinding[T]) RemoveListener(listener binding.DataListener) {
m.self.RemoveListener(listener)
}

func (m *mappedBinding[T]) Reload() error {
val, err := m.get()
if err != nil {
return err
}
m.v = val
m.self.Set(m.counter + 1)
return nil
}

func (m *mappedBinding[T]) Get() (T, error) {
val, err := m.get()
if err != nil {
var t T
return t, err
}
m.v = val
return val, nil
}

func (m *mappedBinding[T]) Set(val T) error {
err := m.set(val)
if err != nil {
return err
}
m.v = val
m.self.Set(m.counter + 1)
return nil
}

type ExternalBoundList[T any] interface {
ExternalBound[[]T]
GetItem(index int) (binding.DataItem, error)
Length() int
}

var errOutOfBounds = errors.New("index out of bounds")

type mappedListBinding[T any] struct {
mappedBinding[[]T]
}

func MappedListBind[T any](get func() ([]T, error), set func([]T) error) ExternalBoundList[T] {
b := &mappedListBinding[T]{
mappedBinding: mappedBinding[[]T]{
get: get,
set: set,
},
}
val, err := b.get()
if err == nil {
b.v = val
}
b.self = binding.BindInt(&b.counter)
return b
}

func (ml *mappedListBinding[T]) GetItem(index int) (binding.DataItem, error) {
if index < 0 || index >= len(ml.v) {
return nil, errOutOfBounds
}
return NewReversableMapping(
ml,
func(l []T) (T, error) {
if index < 0 || index >= len(l) {
var t T
return t, errOutOfBounds
}
return l[index], nil
},
func(val T) ([]T, error) {
l, err := ml.Get()
if err != nil {
return nil, err
}
if index < 0 || index >= len(l) {
return nil, errOutOfBounds
}
l[index] = val
return l, nil
},
), nil
}

func (ml *mappedListBinding[T]) Length() int {
return len(ml.v)
}
3 changes: 3 additions & 0 deletions internal/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type App struct {
disableButtons binding.Bool
mainDisableListener binding.DataListener
currentDisableListeners []binding.DataListener

tagSaveTimer *time.Timer
}

//go:embed translation
Expand Down Expand Up @@ -94,6 +96,7 @@ func (a *App) buildMainUI() {
inputTimer.Stop()
}
inputTimer = time.AfterFunc(500*time.Millisecond, func() {
inputTimer = nil
_, err := os.Stat(path)
if err == nil {
a.operatingPath.Set(path)
Expand Down
95 changes: 95 additions & 0 deletions internal/gui/layouts/layouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,98 @@ func (v bottomExpandVBoxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
}
return minSize
}


// ** top expand VBox **

type topExpandVBoxLayout struct {
paddingFunc func() float32
}

func NewTopExpandVBoxLayout() fyne.Layout {
return topExpandVBoxLayout{
paddingFunc: theme.Padding,
}
}

func NewTopExpandVBox(objs ...fyne.CanvasObject) *fyne.Container {
return container.New(NewTopExpandVBoxLayout(), objs...)
}

func (v topExpandVBoxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0
visibleObjects := 0
// Size taken up by visible objects
bottommost := float32(0)

for i, child := range objects {
if !child.Visible() {
continue
}

if isVerticalSpacer(child) {
spacers++
continue
}

visibleObjects++
if i != 0 {
bottommost += child.MinSize().Height
}
}

padding := v.paddingFunc()

// Amount of space not taken up by visible objects and inter-object padding
topExtra := size.Height - bottommost - (float32(spacers) * padding) - (padding * float32(visibleObjects-1))
extra := size.Height - bottommost - topExtra - (padding * float32(visibleObjects-1))

// Spacers split extra space equally
spacerSize := float32(0)
if spacers > 0 {
spacerSize = extra / float32(spacers)
}

x, y := float32(0), float32(0)
for i, child := range objects {
if !child.Visible() {
continue
}

if isVerticalSpacer(child) {
y += spacerSize
continue
}
child.Move(fyne.NewPos(x, y))

height := child.MinSize().Height
if i == 0 {
height = topExtra
}
y += padding + height
child.Resize(fyne.NewSize(size.Width, height))
}
}

// MinSize finds the smallest size that satisfies all the child objects.
// For a BoxLayout this is the width of the widest item and the height is
// the sum of all children combined with padding between each.
func (v topExpandVBoxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0)
addPadding := false
padding := v.paddingFunc()
for _, child := range objects {
if !child.Visible() || isVerticalSpacer(child) {
continue
}

childMin := child.MinSize()
minSize.Width = fyne.Max(childMin.Width, minSize.Width)
minSize.Height += childMin.Height
if addPadding {
minSize.Height += padding
}
addPadding = true
}
return minSize
}
14 changes: 13 additions & 1 deletion internal/gui/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gui

import (
"errors"
"fmt"
"path/filepath"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -85,6 +86,17 @@ func (a *App) loadUnpackedPath(path string) {
)
return
}

err = pkg.LoadTags()
if err != nil {
a.showErrorDialog(errors.Join(err, fmt.Errorf(lang.X("package.tags.error", "Failed to read tags"))))
err = nil
}
err = pkg.LoadResourceMetadata()
if err != nil {
a.showErrorDialog(errors.Join(err, fmt.Errorf(lang.X("package.metadata.error", "Failed to read metadata"))))
err = nil
}
a.setUnpackedContent(pkg)
}()
}
Expand Down Expand Up @@ -151,7 +163,7 @@ func (a *App) setNotAPackageContent(path string) {
func (a *App) setUnpackedContent(pkg *ddpackage.Package) {
a.pkg = pkg

split := a.buildPackageTreeAndPreview()
split := a.buildPackageTreeAndInfoPane(true)

outputPath := binding.BindPreferenceString("pack.outPath", a.app.Preferences())

Expand Down
Loading

0 comments on commit cc92de1

Please sign in to comment.