diff --git a/todo/list.go b/todo/list.go index 9783cb4f..824462f7 100644 --- a/todo/list.go +++ b/todo/list.go @@ -1,8 +1,6 @@ package todo -import ( - "fmt" -) +import () type List struct { Items []*Item @@ -19,7 +17,6 @@ func (list *List) Add(text string) { } list.Items = append([]*Item{&item}, list.Items...) - fmt.Println("added") } func (list *List) Delete() { @@ -77,6 +74,16 @@ func (list *List) Unselect() { list.selected = -1 } +func (list *List) Update(text string) { + item := list.Selected() + + if item == nil { + return + } + + item.Text = text +} + /* -------------------- Sort Interface -------------------- */ func (list *List) Len() int { diff --git a/todo/widget.go b/todo/widget.go index f95e0c79..75932240 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -66,15 +66,22 @@ func (widget *Widget) editItem() { SetButtonsAlign(tview.AlignCenter). SetButtonTextColor(tview.Styles.PrimaryTextColor) - form.AddInputField("New text:", widget.selectedText(), 60, nil, nil) + form.AddInputField("Edit item:", widget.list.Selected().Text, 60, nil, nil) form.AddButton("Save", func() { fld := form.GetFormItem(0) + text := fld.(*tview.InputField).GetText() - widget.updateItem(fld.(*tview.InputField).GetText()) + widget.list.Update(text) + widget.persist() + widget.pages.RemovePage("modal") + widget.app.SetFocus(widget.View) + widget.display() + }) + + form.AddButton("Cancel", func() { widget.pages.RemovePage("modal") widget.app.SetFocus(widget.View) - widget.persist() widget.display() }) @@ -87,7 +94,37 @@ func (widget *Widget) editItem() { } func (widget *Widget) newItem() { + _, _, w, h := widget.View.GetInnerRect() + form := tview.NewForm(). + SetButtonsAlign(tview.AlignCenter). + SetButtonTextColor(tview.Styles.PrimaryTextColor) + + form.AddInputField("New item:", "", 60, nil, nil) + + form.AddButton("Save", func() { + fld := form.GetFormItem(0) + text := fld.(*tview.InputField).GetText() + + widget.list.Add(text) + widget.persist() + widget.pages.RemovePage("modal") + widget.app.SetFocus(widget.View) + widget.display() + }) + + form.AddButton("Cancel", func() { + widget.pages.RemovePage("modal") + widget.app.SetFocus(widget.View) + widget.display() + }) + + frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0) + frame.SetBorder(true) + frame.SetRect(w+20, h+2, 80, 7) + + widget.pages.AddPage("modal", frame, false, true) + widget.app.SetFocus(frame) } func (widget *Widget) init() { @@ -194,25 +231,3 @@ func (widget *Widget) persist() { panic(err) } } - -func (widget *Widget) selectedText() string { - selectedItem := widget.list.Selected() - - if selectedItem == nil { - return "" - } else { - return selectedItem.Text - } -} - -func (widget *Widget) updateItem(text string) { - selectedItem := widget.list.Selected() - - if selectedItem == nil { - // Create a new item - widget.list.Add(text) - } else { - // Update the text of the existing item - selectedItem.Text = text - } -} diff --git a/wtf/focus_tracker.go b/wtf/focus_tracker.go index 120f3c7d..23d75ffd 100644 --- a/wtf/focus_tracker.go +++ b/wtf/focus_tracker.go @@ -17,6 +17,10 @@ type FocusTracker struct { // Next sets the focus on the next widget in the widget list. If the current widget is // the last widget, sets focus on the first widget. func (tracker *FocusTracker) Next() { + //if tracker.widgetHasFocus() == false { + //return + //} + tracker.blur(tracker.Idx) tracker.increment() tracker.focus(tracker.Idx) @@ -30,12 +34,16 @@ func (tracker *FocusTracker) None() { // Prev sets the focus on the previous widget in the widget list. If the current widget is // the last widget, sets focus on the last widget. func (tracker *FocusTracker) Prev() { + //if tracker.widgetHasFocus() == false { + //return + //} + tracker.blur(tracker.Idx) tracker.decrement() tracker.focus(tracker.Idx) } -/* -------------------- Exported Functions -------------------- */ +/* -------------------- Unexported Functions -------------------- */ func (tracker *FocusTracker) blur(idx int) { view := tracker.Widgets[idx].TextView() @@ -64,3 +72,15 @@ func (tracker *FocusTracker) increment() { tracker.Idx = 0 } } + +// widgetHasFocus returns true if one of the widgets currently has the app's focus, +// false if none of them do (ie: perhaps a modal dialog currently has it instead) +//func (tracker *FocusTracker) widgetHasFocus() bool { +//for _, widget := range tracker.Widgets { +//if widget.TextView() == tracker.App.GetFocus() { +//return true +//} +//} + +//return false +//} diff --git a/wtf/utils.go b/wtf/utils.go index 6dcb35e0..f1868927 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -66,6 +66,7 @@ func NamesFromEmails(emails []string) []string { return names } +// OpenFile opens the file defined in `path` via the operating system func OpenFile(path string) { confDir, _ := ConfigDir() filePath := fmt.Sprintf("%s/%s", confDir, path)