-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (113 loc) · 3.12 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
)
type draggableBox struct {
widget.Box
}
var _ fyne.Draggable = (*draggableBox)(nil)
var dragging = false
var draggingCard fyne.CanvasObject
var draggingCardPos = 0
func (db *draggableBox) DragEnd() {
dragging = false
draggingCard = nil
}
func (db *draggableBox) Dragged(e *fyne.DragEvent) {
if dragging == false {
dragging = true
for i, child := range db.Children {
if e.Position.X > child.Position().X && e.Position.X < child.Position().X+child.Size().Width {
if e.Position.Y > child.Position().Y && e.Position.Y < child.Position().Y+child.Size().Height {
draggingCard = child
draggingCardPos = i
break
}
}
}
if draggingCard == nil {
return
}
}
var currentChild fyne.CanvasObject
var currentChildPos = 0
for i, child := range db.Children {
if e.Position.X > child.Position().X && e.Position.X < child.Position().X+child.Size().Width {
if e.Position.Y > child.Position().Y && e.Position.Y < child.Position().Y+child.Size().Height {
currentChild = child
currentChildPos = i
break
}
}
}
if currentChild == nil || currentChild == draggingCard {
return
}
if db.Horizontal == true {
if currentChild.Position().X > draggingCard.Position().X {
// Moving Right
if e.Position.X > currentChild.Position().X+currentChild.Size().Width/2 {
// Swap
db.Children[draggingCardPos], db.Children[currentChildPos] =
db.Children[currentChildPos], db.Children[draggingCardPos]
draggingCardPos = currentChildPos
db.Refresh()
}
} else {
// Moving Let
if e.Position.X < currentChild.Position().X+currentChild.Size().Width/2 {
// Swap
db.Children[draggingCardPos], db.Children[currentChildPos] =
db.Children[currentChildPos], db.Children[draggingCardPos]
draggingCardPos = currentChildPos
db.Refresh()
}
}
} else {
if currentChild.Position().Y > draggingCard.Position().Y {
// Moving Down
if e.Position.Y > currentChild.Position().Y+currentChild.Size().Height/2 {
// Swap
db.Children[draggingCardPos], db.Children[currentChildPos] =
db.Children[currentChildPos], db.Children[draggingCardPos]
draggingCardPos = currentChildPos
db.Refresh()
}
} else {
// Moving Up
if e.Position.Y < currentChild.Position().Y+currentChild.Size().Height/2 {
// Swap
db.Children[draggingCardPos], db.Children[currentChildPos] =
db.Children[currentChildPos], db.Children[draggingCardPos]
draggingCardPos = currentChildPos
db.Refresh()
}
}
}
}
func newDraggableBox() *draggableBox {
db := &draggableBox{}
db.ExtendBaseWidget(db)
return db
}
func main() {
app := app.New()
w := app.NewWindow("Fyne Demo")
db := newDraggableBox()
for i := 0; i < 10; i++ {
text := fmt.Sprintf("Card Number: %d", i)
label := widget.NewLabel(text)
label.Show()
db.Append(label)
}
db.Show()
mainHbox := widget.NewHBox(db)
mainContainer := fyne.NewContainerWithLayout(layout.NewBorderLayout(nil, nil, nil, nil), mainHbox)
w.SetContent(mainContainer)
w.Resize(fyne.NewSize(1024, 576))
w.ShowAndRun()
}