@@ -35,8 +35,10 @@ const offscreen = -1000
3535const modalWidth = 80
3636const modalHeight = 7
3737
38+ // A Widget represents a Todo widget
3839type Widget struct {
3940 wtf.HelpfulWidget
41+ wtf.KeyboardWidget
4042 wtf.TextWidget
4143
4244 app * tview.Application
@@ -46,10 +48,12 @@ type Widget struct {
4648 pages * tview.Pages
4749}
4850
51+ // NewWidget creates a new instance of a widget
4952func NewWidget (app * tview.Application , pages * tview.Pages , settings * Settings ) * Widget {
5053 widget := Widget {
51- HelpfulWidget : wtf .NewHelpfulWidget (app , pages , HelpText ),
52- TextWidget : wtf .NewTextWidget (app , settings .common , true ),
54+ HelpfulWidget : wtf .NewHelpfulWidget (app , pages , HelpText ),
55+ KeyboardWidget : wtf .NewKeyboardWidget (),
56+ TextWidget : wtf .NewTextWidget (app , settings .common , true ),
5357
5458 app : app ,
5559 settings : settings ,
@@ -59,11 +63,13 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
5963 }
6064
6165 widget .init ()
62- widget .HelpfulWidget . SetView ( widget . View )
66+ widget .initializeKeyboardControls ( )
6367
64- widget .View .SetScrollable ( true )
68+ widget .View .SetInputCapture ( widget . InputCapture )
6569 widget .View .SetRegions (true )
66- widget .View .SetInputCapture (widget .keyboardIntercept )
70+ widget .View .SetScrollable (true )
71+
72+ widget .HelpfulWidget .SetView (widget .View )
6773
6874 return & widget
6975}
@@ -74,20 +80,21 @@ func (widget *Widget) Refresh() {
7480 widget .load ()
7581
7682 widget .app .QueueUpdateDraw (func () {
77- widget .View .SetTitle (widget .ContextualTitle (widget .CommonSettings .Title ))
83+ title := widget .ContextualTitle (widget .CommonSettings .Title )
84+ widget .View .SetTitle (title )
7885 widget .display ()
7986 })
8087
8188}
8289
83- func (widget * Widget ) SetList (newList checklist.Checklist ) {
84- widget .list = newList
90+ func (widget * Widget ) SetList (list checklist.Checklist ) {
91+ widget .list = list
8592}
8693
8794/* -------------------- Unexported Functions -------------------- */
8895
8996// edit opens a modal dialog that permits editing the text of the currently-selected item
90- func (widget * Widget ) editItem () {
97+ func (widget * Widget ) editSelected () {
9198 if widget .list .SelectedItem () == nil {
9299 return
93100 }
@@ -115,81 +122,6 @@ func (widget *Widget) init() {
115122 }
116123}
117124
118- func (widget * Widget ) keyboardIntercept (event * tcell.EventKey ) * tcell.EventKey {
119- switch string (event .Rune ()) {
120- case " " :
121- // Check/uncheck selected item
122- widget .list .Toggle ()
123- widget .persist ()
124- widget .display ()
125- return nil
126- case "/" :
127- widget .ShowHelp ()
128- return nil
129- case "j" :
130- // Select the next item down
131- widget .list .Next ()
132- widget .display ()
133- return nil
134- case "k" :
135- // Select the next item up
136- widget .list .Prev ()
137- widget .display ()
138- return nil
139- case "n" :
140- // Add a new item
141- widget .newItem ()
142- return nil
143- case "o" :
144- // Open the file
145- confDir , _ := cfg .ConfigDir ()
146- wtf .OpenFile (fmt .Sprintf ("%s/%s" , confDir , widget .filePath ))
147- return nil
148- }
149-
150- switch event .Key () {
151- case tcell .KeyCtrlD :
152- // Delete the selected item
153- widget .list .Delete ()
154- widget .persist ()
155- widget .display ()
156- return nil
157- case tcell .KeyCtrlJ :
158- // Move selected item down in the list
159- widget .list .Demote ()
160- widget .persist ()
161- widget .display ()
162- return nil
163- case tcell .KeyCtrlK :
164- // Move selected item up in the list
165- widget .list .Promote ()
166- widget .persist ()
167- widget .display ()
168- return nil
169- case tcell .KeyDown :
170- // Select the next item down
171- widget .list .Next ()
172- widget .display ()
173- return nil
174- case tcell .KeyEnter :
175- widget .editItem ()
176- return nil
177- case tcell .KeyEsc :
178- // Unselect the current row
179- widget .list .Unselect ()
180- widget .display ()
181- return event
182- case tcell .KeyUp :
183- // Select the next item up
184- widget .list .Prev ()
185- widget .display ()
186- return nil
187- default :
188- // Pass it along
189- return event
190- }
191- }
192-
193125// Loads the todo list from Yaml file
194126func (widget * Widget ) load () {
195127 confDir , _ := cfg .ConfigDir ()
0 commit comments