-
Notifications
You must be signed in to change notification settings - Fork 6
/
m_editcropsettings.go
94 lines (88 loc) · 2.61 KB
/
m_editcropsettings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"slices"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func (e *Editor) editCropSettingsDialog() {
e.clearCrop()
var d *dialog.CustomDialog
cc := container.NewVBox()
for i := 0; i < len(a.state.FavoriteCrops); i++ {
crop := a.state.FavoriteCrops[i]
row := e.cropSettingsDeleteRow(crop, cc, i)
cc.Add(row)
}
c := container.NewVBox()
c.Add(cc)
// insert new crop aspect
newCropRow := e.cropSettingsAddRow(c, cc)
c.Add(newCropRow)
d = dialog.NewCustom("Set favorite crops", "Ok", c, a.topWindow)
d.SetOnClosed(func() {
e.fixedCrops.RemoveAll()
for i := 0; i < len(a.state.FavoriteCrops); i++ {
crop := a.state.FavoriteCrops[i]
e.fixedCrops.Objects = append(e.fixedCrops.Objects, widget.NewButton(fmt.Sprintf("%.f:%.f", crop.X, crop.Y), func() { e.fixCrop(crop.X / crop.Y) }))
}
e.fixedCrops.Refresh()
})
d.Show()
}
func (e *Editor) cropSettingsDeleteRow(crop fyne.Position, cc *fyne.Container, i int) *fyne.Container {
label := widget.NewLabel(fmt.Sprintf("%.f:%.f", crop.X, crop.Y))
label.Alignment = fyne.TextAlignCenter
button := widget.NewButtonWithIcon("",
theme.ContentRemoveIcon(),
func() {
a.state.FavoriteCrops = slices.Delete(a.state.FavoriteCrops, i, i+1)
cc.RemoveAll()
for i := 0; i < len(a.state.FavoriteCrops); i++ {
crop := a.state.FavoriteCrops[i]
row := e.cropSettingsDeleteRow(crop, cc, i)
cc.Add(row)
}
cc.Refresh()
},
)
return container.NewGridWithColumns(2, label, button)
}
func (e *Editor) cropSettingsAddRow(c, cc *fyne.Container) *fyne.Container {
newX := widget.NewEntry()
newX.SetPlaceHolder("10")
newX.Validator = validation.NewRegexp(`\d{1,2}`, "invalid width")
newY := widget.NewEntry()
newY.SetPlaceHolder("10")
newY.Validator = validation.NewRegexp(`\d{1,2}`, "invalid height")
return container.NewGridWithColumns(2,
container.NewHBox(newX, widget.NewLabel(":"), newY),
widget.NewButtonWithIcon("",
theme.ContentAddIcon(),
func() {
if err := newX.Validate(); err != nil {
return
}
if err := newY.Validate(); err != nil {
return
}
x, _ := strconv.ParseFloat(newX.Text, 32)
y, _ := strconv.ParseFloat(newY.Text, 32)
a.state.FavoriteCrops = append(a.state.FavoriteCrops, fyne.NewPos(float32(x), float32(y)))
cc.RemoveAll()
for i := 0; i < len(a.state.FavoriteCrops); i++ {
crop := a.state.FavoriteCrops[i]
row := e.cropSettingsDeleteRow(crop, cc, i)
cc.Add(row)
}
newX.Text = ""
newY.Text = ""
c.Refresh()
},
))
}