forked from richardwilkes/unison
/
table_drop.go
288 lines (270 loc) · 8.75 KB
/
table_drop.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"slices"
"github.com/ddkwork/unison/enums/paintstyle"
"github.com/google/uuid"
)
// TableDrop provides default support for dropping data into a table. This should only be instantiated by a call to
// Table.InstallDropSupport().
type TableDrop[T any, U any] struct {
Table *Table[T]
DragKey string
TargetParent *Table[T]
TargetIndex int
AllDragData map[string]any
TableDragData *TableDragData[T]
originalDrawOver func(*Canvas, Rect)
shouldMoveDataCallback func(from, to *Table[T]) bool
willDropCallback func(from, to *Table[T], move bool) *UndoEdit[U]
didDropCallback func(undo *UndoEdit[U], from, to *Table[T], move bool)
top float32
left float32
inDragOver bool
}
// DrawOverCallback handles drawing the drop zone feedback.
func (d *TableDrop[T, U]) DrawOverCallback(gc *Canvas, rect Rect) {
if d.originalDrawOver != nil {
d.originalDrawOver(gc, rect)
}
if d.inDragOver {
r := d.Table.ContentRect(false).Inset(NewUniformInsets(1))
paint := DropAreaColor.Paint(gc, r, paintstyle.Stroke)
paint.SetStrokeWidth(2)
paint.SetColorFilter(Alpha30Filter())
gc.DrawRect(r, paint)
paint.SetColorFilter(nil)
paint.SetPathEffect(DashEffect())
gc.DrawLine(d.left, d.top, r.Right(), d.top, paint)
}
}
// DataDragOverCallback handles determining if a given drag is one that we are interested in.
func (d *TableDrop[T, U]) DataDragOverCallback(where Point, data map[string]any) bool {
if d.Table.filteredRows != nil {
return false
}
var zero *Table[T]
d.inDragOver = false
if dd, ok := data[d.DragKey]; ok {
if d.TableDragData, ok = dd.(*TableDragData[T]); ok {
d.inDragOver = true
last := d.Table.LastRowIndex()
contentRect := d.Table.ContentRect(false)
hierarchyColumnIndex := d.Table.ColumnIndexForID(d.Table.HierarchyColumnID)
if where.Y >= contentRect.Bottom()-2 {
// Over bottom edge, adding to end of top-level rows
d.TargetParent = zero
d.TargetIndex = d.Table.RootRowCount()
rect := d.Table.RowFrame(last)
d.top = min(rect.Bottom()+1+d.Table.Padding.Bottom, contentRect.Bottom()-1)
d.left, _ = d.Table.ColumnEdges(max(hierarchyColumnIndex, 0))
d.Table.MarkForRedraw()
return true
}
if rowIndex := d.Table.OverRow(where.Y); rowIndex != -1 {
// Over row
d.TargetIndex = -1
row := d.Table.RowFromIndex(rowIndex)
rect := d.Table.CellFrame(rowIndex, max(hierarchyColumnIndex, 0))
if where.Y >= d.Table.RowFrame(rowIndex).CenterY() {
d.top = min(rect.Bottom()+1+d.Table.Padding.Bottom, contentRect.Bottom()-1)
d.left = rect.X
// Over lower half of row
if row.CanHaveChildren() {
// Row is a container; add to container at index 0
d.TargetParent = row
d.TargetIndex = 0
if hierarchyColumnIndex != -1 {
d.left += d.Table.HierarchyIndent
}
} else {
// Row is not a container; add as sibling below this row
d.TargetParent = row.Parent()
if row = d.Table.RowFromIndex(rowIndex + 1); row == zero {
if d.TargetParent == zero {
d.TargetIndex = len(d.Table.RootRows())
} else {
d.TargetIndex = len(d.TargetParent.Children())
}
}
}
} else {
// Over upper half of row; add to parent of this row at this row's index
d.TargetParent = row.Parent()
d.top = max(rect.Y-d.Table.Padding.Bottom, 1)
d.left = rect.X
}
if d.TargetIndex == -1 && row != zero {
var children []*Table[T]
if d.TargetParent == zero {
children = d.Table.RootRows()
} else {
children = d.TargetParent.Children()
}
for i, child := range children {
if child.UUID() == row.UUID() {
d.TargetIndex = i
break
}
}
if d.TargetIndex == -1 {
d.TargetIndex = len(children)
}
}
// Check to make sure we aren't trying to drop into the items being moved
if d.TargetParent != zero && d.TableDragData.Table == d.Table {
for _, r := range d.TableDragData.Rows {
if RowContainsRow(r, d.TargetParent) {
// Can't drop into itself, so abort
d.inDragOver = false
d.TargetParent = zero
break
}
}
}
d.Table.MarkForRedraw()
return true
}
// Not over any row, adding to end of top-level rows
d.TargetParent = zero
d.TargetIndex = d.Table.RootRowCount()
rect := d.Table.RowFrame(last)
d.top = min(rect.Bottom()+1+d.Table.Padding.Bottom, contentRect.Bottom()-1)
d.left, _ = d.Table.ColumnEdges(max(hierarchyColumnIndex, 0))
d.Table.MarkForRedraw()
return true
}
}
return false
}
// DataDragExitCallback handles resetting the state when a drag is no longer of interest.
func (d *TableDrop[T, U]) DataDragExitCallback() {
d.inDragOver = false
var zero *Table[T]
d.TargetParent = zero
d.Table.MarkForRedraw()
}
// DataDragDropCallback handles processing a drop.
func (d *TableDrop[T, U]) DataDragDropCallback(_ Point, data map[string]any) {
var savedScrollX, savedScrollY float32
if scroller := d.Table.ScrollRoot(); scroller != nil {
savedScrollX, savedScrollY = scroller.Position()
defer func() {
scroller.SetPosition(savedScrollX, savedScrollY)
}()
}
var zero *Table[T]
d.inDragOver = false
var ok bool
if d.TableDragData, ok = data[d.DragKey].(*TableDragData[T]); ok {
d.AllDragData = data
move := d.shouldMoveDataCallback(d.TableDragData.Table, d.Table)
var undo *UndoEdit[U]
if d.willDropCallback != nil {
undo = d.willDropCallback(d.TableDragData.Table, d.Table, move)
}
rows := slices.Clone(d.TableDragData.Rows)
if move {
// Remove the drag rows from their original places
commonParents := collectCommonParents(rows)
for parent, list := range commonParents {
var children []*Table[T]
if parent == zero {
children = d.TableDragData.Table.RootRows()
} else {
children = parent.Children()
}
list = d.pruneRows(parent, children, makeRowSet(list))
if parent == zero {
d.TableDragData.Table.Model.SetRootRows(d.Table, list)
} else {
parent.SetChildren(list)
}
}
d.TableDragData.Table.ClearSelection()
d.TableDragData.Table.SyncToModel()
// Set the new parent
for _, row := range rows {
row.SetParent(d.TargetParent)
}
// Notify the source table if it is different from the destination
if d.Table != d.TableDragData.Table {
if d.Table != d.TableDragData.Table && d.TableDragData.Table.DragRemovedRowsCallback != nil {
d.TableDragData.Table.DragRemovedRowsCallback()
}
}
} else {
// Make a copy of the data
for i, row := range rows {
rows[i] = row.CloneForTarget(d.Table, d.TargetParent)
}
}
// Insert the rows into their new location
var targetRows []*Table[T]
if d.TargetParent == zero {
targetRows = d.Table.RootRows()
} else {
targetRows = d.TargetParent.Children()
}
targetRows = slices.Insert(slices.Clone(targetRows), max(min(d.TargetIndex, len(targetRows)), 0), rows...)
if d.TargetParent == zero {
d.Table.SetRootRows(d.Table, targetRows)
} else {
d.TargetParent.SetChildren(targetRows)
d.Table.SyncToModel()
}
// Restore selection
selMap := make(map[uuid.UUID]bool, len(rows))
for _, row := range rows {
selMap[row.UUID()] = true
}
d.Table.SetSelectionMap(selMap)
// Notify the destination table
if d.Table.DropOccurredCallback != nil {
d.Table.DropOccurredCallback()
}
if d.didDropCallback != nil {
d.didDropCallback(undo, d.TableDragData.Table, d.Table, move)
}
}
d.Table.MarkForRedraw()
d.TargetParent = zero
d.AllDragData = nil
d.TableDragData = nil
}
func (d *TableDrop[T, U]) pruneRows(parent *Table[T], rows []*Table[T], movingSet map[uuid.UUID]bool) []*Table[T] {
movingToThisParent := d.TargetParent == parent
list := make([]*Table[T], 0, len(rows))
for i, row := range rows {
if movingSet[row.UUID()] {
if movingToThisParent && d.TargetIndex >= i {
d.TargetIndex--
}
} else {
list = append(list, row)
}
}
return list
}
func makeRowSet[T any](rows []*Table[T]) map[uuid.UUID]bool {
set := make(map[uuid.UUID]bool, len(rows))
for _, row := range rows {
set[row.UUID()] = true
}
return set
}
func collectCommonParents[T any](rows []*Table[T]) map[*Table[T]][]*Table[T] {
m := make(map[*Table[T]][]*Table[T])
for _, row := range rows {
parent := row.Parent()
m[parent] = append(m[parent], row)
}
return m
}