From dba30fb00fa5bb031ca6eafc356406b03a650827 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 2 May 2019 09:08:33 +0200 Subject: ui: add multiple tunnel deletion Signed-off-by: Anthony Dong Signed-off-by: Jason A. Donenfeld --- ui/tunnelspage.go | 71 +++++++++++++++++++++++++++++++++++-------------------- ui/tunnelsview.go | 1 - 2 files changed, 46 insertions(+), 26 deletions(-) (limited to 'ui') diff --git a/ui/tunnelspage.go b/ui/tunnelspage.go index bc749814..bf80107d 100644 --- a/ui/tunnelspage.go +++ b/ui/tunnelspage.go @@ -23,9 +23,10 @@ import ( type TunnelsPage struct { *walk.TabPage - tunnelsView *TunnelsView - confView *ConfView - fillerButton *walk.PushButton + tunnelsView *TunnelsView + confView *ConfView + fillerButton *walk.PushButton + fillerHandler func() fillerContainer *walk.Composite currentTunnelContainer *walk.Composite @@ -122,15 +123,19 @@ func NewTunnelsPage() (*TunnelsPage, error) { tp.Layout().(interface{ SetStretchFactor(walk.Widget, int) error }).SetStretchFactor(tp.fillerContainer, 10) walk.NewHSpacer(tp.fillerContainer) tp.fillerButton, _ = walk.NewPushButton(tp.fillerContainer) - tp.fillerButton.SetText(importAction.Text()) buttonWidth := tp.DPI() * 2 //TODO: Use dynamic DPI tp.fillerButton.SetMinMaxSize(walk.Size{buttonWidth, 0}, walk.Size{buttonWidth, 0}) - tp.fillerButton.Clicked().Attach(tp.onImport) + tp.fillerButton.Clicked().Attach(func() { + if tp.fillerHandler != nil { + tp.fillerHandler() + } + }) walk.NewHSpacer(tp.fillerContainer) //TODO: expose walk.TableView.itemCountChangedPublisher.Event() tp.tunnelsView.Property("ItemCount").Changed().Attach(tp.onTunnelsChanged) tp.onTunnelsChanged() + tp.tunnelsView.SelectedIndexesChanged().Attach(tp.onSelectedTunnelsChanged) tp.confView, _ = NewConfView(tp.currentTunnelContainer) @@ -371,21 +376,27 @@ func (tp *TunnelsPage) onAddTunnel() { } func (tp *TunnelsPage) onDelete() { - currentTunnel := tp.tunnelsView.CurrentTunnel() - if currentTunnel == nil { - // Misfired event? + indices := tp.tunnelsView.SelectedIndexes() + if len(indices) == 0 { return } + var topic string + if len(indices) > 1 { + topic = fmt.Sprintf("%d tunnels", len(indices)) + } else { + topic = fmt.Sprintf("ā€˜%sā€™", tp.tunnelsView.model.tunnels[0].Name) + } if walk.DlgCmdNo == walk.MsgBox( tp.Form(), - fmt.Sprintf(`Delete "%s"`, currentTunnel.Name), - fmt.Sprintf(`Are you sure you want to delete "%s"?`, currentTunnel.Name), + fmt.Sprintf("Delete %s", topic), + fmt.Sprintf("Are you sure you would like to delete %s? You cannot undo this action.", topic), walk.MsgBoxYesNo|walk.MsgBoxIconWarning) { return } - - tp.deleteTunnel(currentTunnel) + for _, i := range indices { + tp.deleteTunnel(&tp.tunnelsView.model.tunnels[i]) + } } func (tp *TunnelsPage) onImport() { @@ -420,19 +431,29 @@ func (tp *TunnelsPage) onExportTunnels() { tp.exportTunnels(dlg.FilePath) } +func (tp *TunnelsPage) swapFiller(enabled bool) bool { + //BUG: flicker switching with the currentTunnelContainer + if tp.fillerContainer.Visible() == enabled { + return enabled + } + tp.SetSuspended(true) + tp.fillerContainer.SetVisible(enabled) + tp.currentTunnelContainer.SetVisible(!enabled) + tp.SetSuspended(false) + return enabled +} + func (tp *TunnelsPage) onTunnelsChanged() { - //BUG: flickerr switching with the currentTunnelContainer - if tp.tunnelsView.model.RowCount() != 0 { - if tp.fillerContainer.Visible() { - tp.SetSuspended(true) - tp.fillerContainer.SetVisible(false) - tp.currentTunnelContainer.SetVisible(true) - tp.SetSuspended(false) - } - } else if !tp.fillerContainer.Visible() { - tp.SetSuspended(true) - tp.fillerContainer.SetVisible(true) - tp.currentTunnelContainer.SetVisible(false) - tp.SetSuspended(false) + if tp.swapFiller(tp.tunnelsView.model.RowCount() == 0) { + tp.fillerButton.SetText("Import tunnel(s) from file") + tp.fillerHandler = tp.onImport + } +} + +func (tp *TunnelsPage) onSelectedTunnelsChanged() { + indices := tp.tunnelsView.SelectedIndexes() + if tp.swapFiller(len(indices) > 1) { + tp.fillerButton.SetText(fmt.Sprintf("Delete %d tunnels", len(indices))) + tp.fillerHandler = tp.onDelete } } diff --git a/ui/tunnelsview.go b/ui/tunnelsview.go index 26099c2c..c2ce7946 100644 --- a/ui/tunnelsview.go +++ b/ui/tunnelsview.go @@ -73,7 +73,6 @@ func NewTunnelsView(parent walk.Container) (*TunnelsView, error) { tv.SetModel(model) tv.SetLastColumnStretched(true) tv.SetHeaderHidden(true) - tv.SetMultiSelection(false) tv.Columns().Add(walk.NewTableViewColumn()) tunnelsView := &TunnelsView{ -- cgit v1.2.3-59-g8ed1b