Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How implement event 'lost focus' on IWidget? #22

Closed
asv opened this issue Jul 31, 2019 · 2 comments
Closed

How implement event 'lost focus' on IWidget? #22

asv opened this issue Jul 31, 2019 · 2 comments

Comments

@asv
Copy link
Contributor

asv commented Jul 31, 2019

For example, implement content validation for Edit widget.

@gcla
Copy link
Owner

gcla commented Aug 6, 2019

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!

@gcla gcla closed this as completed Sep 16, 2019
@gcla
Copy link
Owner

gcla commented Nov 9, 2019

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.

@gcla gcla reopened this Nov 9, 2019
@gcla gcla closed this as completed May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants