Skip to content

Commit

Permalink
Move item removal out to a separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 29, 2023
1 parent c20f9b6 commit 6f25b44
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
45 changes: 24 additions & 21 deletions internal/transport/bridge/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,7 @@ func (d *RecvData) OnSelected(i int) {
var infoDialog *dialog.CustomDialog
removeLabel := &widget.Label{Text: "This item has completed the transfer and can be removed."}
removeButton := &widget.Button{Icon: theme.DeleteIcon(), Importance: widget.DangerImportance, Text: "Remove", OnTapped: func() {
// Make sure that no updates happen while we modify the slice.
d.deleting.Store(true)

if i < len(d.items)-1 {
copy(d.items[i:], d.items[i+1:])
}

d.items[len(d.items)-1] = nil // Allow the GC to reclaim memory.
d.items = d.items[:len(d.items)-1]

// Update the moved items to have the correct index.
for j := i; j < len(d.items); j++ {
d.items[j].index = j
}

// Refresh the whole list.
d.list.Refresh()

// Allow individual objects to be refreshed again.
d.deleting.Store(false)

d.remove(i)
infoDialog.Hide()
infoDialog = nil
}}
Expand Down Expand Up @@ -170,6 +150,29 @@ func (d *RecvData) refresh(index int) {
d.list.RefreshItem(index)
}

func (d *RecvData) remove(index int) {
// Make sure that no updates happen while we modify the slice.
d.deleting.Store(true)

if index < len(d.items)-1 {
copy(d.items[index:], d.items[index+1:])
}

d.items[len(d.items)-1] = nil // Allow the GC to reclaim memory.
d.items = d.items[:len(d.items)-1]

// Update the moved items to have the correct index.
for j := index; j < len(d.items); j++ {
d.items[j].index = j
}

// Refresh the whole list.
d.list.Refresh()

// Allow individual objects to be refreshed again.
d.deleting.Store(false)
}

// NewRecvList greates a list of progress bars.
func NewRecvList(data *RecvData) *widget.List {
list := &widget.List{
Expand Down
45 changes: 24 additions & 21 deletions internal/transport/bridge/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,7 @@ func (d *SendData) OnSelected(i int) {
var infoDialog *dialog.CustomDialog
removeLabel := &widget.Label{Text: "This item can be removed.\nThe transfer has completed."}
removeButton := &widget.Button{Icon: theme.DeleteIcon(), Importance: widget.DangerImportance, Text: "Remove", OnTapped: func() {
// Make sure that no updates happen while we modify the slice.
d.deleting.Store(true)

if i < len(d.items)-1 {
copy(d.items[i:], d.items[i+1:])
}

d.items[len(d.items)-1] = nil // Allow the GC to reclaim memory.
d.items = d.items[:len(d.items)-1]

// Update the moved items to have the correct index.
for j := i; j < len(d.items); j++ {
d.items[j].index = j
}

// Refresh the whole list.
d.list.Refresh()

// Allow individual objects to be refreshed again.
d.deleting.Store(false)

d.remove(i)
infoDialog.Hide()
infoDialog = nil
}}
Expand Down Expand Up @@ -357,6 +337,29 @@ func (d *SendData) refresh(index int) {
d.list.RefreshItem(index)
}

func (d *SendData) remove(index int) {
// Make sure that no updates happen while we modify the slice.
d.deleting.Store(true)

if index < len(d.items)-1 {
copy(d.items[index:], d.items[index+1:])
}

d.items[len(d.items)-1] = nil // Allow the GC to reclaim memory.
d.items = d.items[:len(d.items)-1]

// Update the moved items to have the correct index.
for j := index; j < len(d.items); j++ {
d.items[j].index = j
}

// Refresh the whole list.
d.list.Refresh()

// Allow individual objects to be refreshed again.
d.deleting.Store(false)
}

// NewSendList greates a list of progress bars.
func NewSendList(data *SendData) *widget.List {
list := &widget.List{
Expand Down

0 comments on commit 6f25b44

Please sign in to comment.