Skip to content

Commit

Permalink
fix(ui): fix popup ESC key and add popup mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Rugalev committed Sep 14, 2020
1 parent 9077b06 commit 013c2bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/ui/resourceMenu/resource_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newResourcePicker(container commander.ResourceContainer, f GroupKindFunc) (
}, true))
resMap[res.Gk.String()] = res
}
lt := listTable.NewStaticListTable(columns, rows, listTable.NoHorizontalScroll|listTable.WithFilter|listTable.WithHeaders|listTable.StartFilter, container.ScreenHandler())
lt := listTable.NewStaticListTable(columns, rows, listTable.NoHorizontalScroll|listTable.WithFilter|listTable.WithHeaders|listTable.AlwaysFilter, container.ScreenHandler())
lt.BindOnKeyPress(func(row commander.Row, event *tcell.EventKey) bool {
if row == nil {
return false
Expand Down
2 changes: 1 addition & 1 deletion app/ui/resources/namespace/namespace_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func PickNamespace(workspace commander.Workspace, resource *commander.Resource,
}

func NewNamespacePicker(container commander.ResourceContainer, resource *commander.Resource, f NamespaceFunc) (*listTable.ResourceListTable, error) {
rlt := listTable.NewResourceListTable(container, resource, listTable.NameOnly|listTable.NoActions|listTable.NoWatch|listTable.WithFilter|listTable.StartFilter)
rlt := listTable.NewResourceListTable(container, resource, listTable.NameOnly|listTable.NoActions|listTable.NoWatch|listTable.WithFilter|listTable.AlwaysFilter)
rlt.BindOnKeyPress(func(row commander.Row, event *tcell.EventKey) bool {
if event.Key() == tcell.KeyEnter {
go func() {
Expand Down
10 changes: 5 additions & 5 deletions app/ui/widgets/listTable/listTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
NoActions
NoWatch
WithFilter
StartFilter
AlwaysFilter
)

func (tf TableFormat) Has(flag TableFormat) bool {
Expand Down Expand Up @@ -103,7 +103,7 @@ func NewListTable(prov commander.RowProvider, format TableFormat, screen command
preloader: NewPreloader(screen),
rowProvider: prov,
screen: screen,
filterMode: format.Has(StartFilter),
filterMode: format.Has(AlwaysFilter),
}
lt.Render()
return lt
Expand Down Expand Up @@ -294,7 +294,7 @@ func (lt *ListTable) BindOnChange(rowFunc RowFunc) {
}

func (lt *ListTable) resetFilter() {
lt.filterMode = lt.format.Has(StartFilter)
lt.filterMode = lt.format.Has(AlwaysFilter)
lt.filter = ""
lt.Render()
lt.reindexSelection()
Expand Down Expand Up @@ -589,7 +589,7 @@ func (lt *ListTable) HandleEvent(ev tcell.Event) bool {
return true
}
if lt.format.Has(WithFilter) {
if (lt.filterMode || lt.filter != "") && ev.Key() == tcell.KeyEsc && lt.IsFocused() {
if (lt.filterMode || lt.filter != "") && ev.Key() == tcell.KeyEsc && lt.IsFocused() && !lt.format.Has(AlwaysFilter) {
lt.resetFilter()
return true
}
Expand All @@ -603,7 +603,7 @@ func (lt *ListTable) HandleEvent(ev tcell.Event) bool {
}
return true
case tcell.KeyEnter:
if !lt.format.Has(StartFilter) {
if !lt.format.Has(AlwaysFilter) {
lt.filterMode = false
lt.Render()
lt.reindexSelection()
Expand Down
25 changes: 20 additions & 5 deletions app/ui/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
"k8s.io/apimachinery/pkg/runtime/schema"
"sync"
)

type workspace struct {
Expand All @@ -22,9 +23,10 @@ type workspace struct {
container commander.Container
focus commander.FocusManager

popup commander.Popup
menu *resourceMenu.ResourceMenu
widget commander.Widget
popupMu sync.Mutex
popup commander.Popup
menu *resourceMenu.ResourceMenu
widget commander.Widget

namespace string
namespaceResource *commander.Resource
Expand All @@ -34,10 +36,12 @@ type workspace struct {

func (w *workspace) Resize() {
w.BoxLayout.Resize()
w.popupMu.Lock()
if w.popup != nil {
w.popup.Reposition(w.view)
w.popup.Resize()
}
w.popupMu.Unlock()
}

func (w *workspace) SetView(view views.View) {
Expand Down Expand Up @@ -93,21 +97,27 @@ func (w *workspace) FocusManager() commander.FocusManager {
}

func (w *workspace) ShowPopup(title string, widget commander.MaxSizeWidget) {
w.popupMu.Lock()
w.popup = popup.NewPopup(w.view, w.Theme(), title, widget, func() {
w.popupMu.Lock()
w.popup.OnHide()
w.popup = nil
w.popupMu.Unlock()
w.UpdateScreen()
})
w.popup.OnShow()
w.focus.Focus(w.popup)
w.popupMu.Unlock()
w.UpdateScreen()
}

func (w *workspace) UpdateScreen() {
w.popupMu.Lock()
if w.popup != nil {
w.popup.Reposition(w.container.Screen().View())
w.popup.Resize()
}
w.popupMu.Unlock()
if screen := w.container.Screen(); screen != nil {
screen.UpdateScreen()
}
Expand All @@ -131,19 +141,24 @@ func (w *workspace) Status() commander.StatusReporter {

func (w *workspace) Draw() {
w.BoxLayout.Draw()
w.popupMu.Lock()
if w.popup != nil {
w.popup.Draw()
}
w.popupMu.Unlock()
}

func (w *workspace) HandleEvent(e tcell.Event) bool {
if w.Status().HandleEvent(e) {
return true
}
if w.focus.HandleEvent(e, w.popup == nil) {
w.popupMu.Lock()
hasPopup := w.popup != nil
w.popupMu.Unlock()
if w.focus.HandleEvent(e, !hasPopup) {
return true
}
if w.popup != nil {
if hasPopup {
return false
}
switch ev := e.(type) {
Expand Down

0 comments on commit 013c2bf

Please sign in to comment.