gcla / gowid

Compositional widgets for terminal user interfaces, written in Go, inspired by urwid.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How implement event 'lost focus' on IWidget?

asv opened this issue · comments

For example, implement content validation for Edit widget.

Hi - sorry for the long delay in replying. One suggestion would be to do it like this, from termshark:

https://github.com/gcla/termshark/blob/master/widgets/filter/filter.go#L241

In short, a callback is set on the edit widget which is invoked when the text changes. I use that to set the background color of the filter, depending on whether what is entered is syntactically valid. Will that work for your use case?

Another approach might be to keep a boolean flag that is set when your widget is rendered (via Render()) with the focus flag set; then when Render() is next invoked with focus not set, and with your custom flag set, clear it, and run the validation. So I suppose the custom flag is just to make sure you don't run validation every single screen render, only each time focus is lost. I haven't tried this way myself though. Let me know!

Hi @asv - I know it's been a while since you opened this...

I thought I'd let you know I implemented a similar feature to this in termshark. Here's a widget that helps track losing or gaining focus: https://github.com/gcla/termshark/tree/streams/widgets/trackfocus. The widget itself keeps track of how it was last rendered, and when that changes (not focus -> focus and vice-versa) issues a callback. Here's an example use of it:

https://github.com/gcla/termshark/blob/streams/widgets/streamwidget/streamwidget.go#L639

There's a small catch. Depending on the way you layout widgets, sometimes a gowid app has to call a widget's Render() function in order to determine the size at which it will render within a container, even though the canvas returned isn't used at that point. For example, a list widget sometimes needs to know how much space each child takes to make scrolling decisions - but if the child is rendered as a "flow" widget (columns specified but as many rows as it needs), then gowid has to actually render it to find out. I haven't accounted for that case yet, but this works for my current use.