Skip to content

Commit

Permalink
improve: focusable original primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
skanehira committed Feb 21, 2021
1 parent 5cd5aa4 commit b4bd1eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
13 changes: 5 additions & 8 deletions ui/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ var IssueFilterUI *FilterUI
type (
SetFilterOpt func(ui *FilterUI)
FilterUI struct {
*tview.Form
*tview.InputField
}
)

func NewFilterUI() {
ui := &FilterUI{
Form: tview.NewForm().AddInputField("Filters", "", 100, nil, nil),
InputField: tview.NewInputField().SetLabel("Filters").SetLabelWidth(8),
}
ui.SetBorderPadding(0, 0, 1, 0)

ui.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
Expand All @@ -29,16 +30,12 @@ func NewFilterUI() {
IssueFilterUI = ui
}

func (ui *FilterUI) GetInputField() *tview.InputField {
return ui.GetFormItem(0).(*tview.InputField)
}

func (ui *FilterUI) SetQuery(query string) {
ui.GetInputField().SetText(query)
ui.SetText(query)
}

func (ui *FilterUI) GetQuery() string {
return ui.GetInputField().GetText()
return ui.GetText()
}

func (ui *FilterUI) focus() {
Expand Down
2 changes: 1 addition & 1 deletion ui/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func NewIssueUI() {
func createIssueForm() {
// repo
var repo string
input := IssueFilterUI.GetFormItem(0).(*tview.InputField).GetText()
input := IssueFilterUI.GetQuery()
for _, word := range strings.Split(input, " ") {
if strings.Contains(word, "repo:") {
repo = strings.TrimPrefix(word, "repo:")
Expand Down
20 changes: 19 additions & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,22 @@ func New() *ui {
return ui
}

func (ui *ui) canFocus() bool {
fs := ui.app.GetFocus()
if fs == nil {
return false
}
switch fs.(type) {
case *FilterUI, *SelectUI, *viewUI:
return true
}
return false
}

func (ui *ui) toNextUI() {
if !ui.canFocus() {
return
}
ui.primitives[ui.current].blur()
if ui.primitiveLen-1 > ui.current {
ui.current++
Expand All @@ -49,6 +64,9 @@ func (ui *ui) toNextUI() {
}

func (ui *ui) toPrevUI() {
if !ui.canFocus() {
return
}
ui.primitives[ui.current].blur()
if ui.current == 0 {
ui.current = ui.primitiveLen - 1
Expand Down Expand Up @@ -86,7 +104,7 @@ func (ui *ui) Start() error {
// for readability
row, col, rowSpan, colSpan := 0, 0, 0, 0

grid := tview.NewGrid().SetRows(3, 0, 0, 0, 0, 0, 0, 0, 0, 1).
grid := tview.NewGrid().SetRows(1, 0, 0, 0, 0, 0, 0, 0, 0, 1).
AddItem(IssueFilterUI, row, col, rowSpan+1, colSpan+3, 0, 0, true).
AddItem(IssueUI, row+1, col+1, rowSpan+4, colSpan+3, 0, 0, true).
AddItem(AssigneesUI, row+1, col, rowSpan+1, colSpan+1, 0, 0, true).
Expand Down

0 comments on commit b4bd1eb

Please sign in to comment.