jroimartin / gocui

Minimalist Go package aimed at creating Console User Interfaces.
BSD 3-Clause "New" or "Revised" License
9.91k stars 610 forks source link

Unable to distinguish between resize events and others #109

Open tscott0 opened 7 years ago

tscott0 commented 7 years ago

Firstly, I'm a huge fan of gocui!

I'm very likely missing something but I can't find a way to detect a resize event.

I can put resize code in my ManagerFunc (Layout) but that will be executed for every iteration of the main loop and for every event, including keypresses.

Ideally I would be able to bind a function to resize events in a similar way to SetKeybinding. This could function in a similar way, allowing the calling code to specify a view by name, passing the View to the handler function.

Many thanks, Tom

jroimartin commented 7 years ago

Hi! First of all, sorry for my late response.

My next goal is to rewrite the edition mode (I should've finished it long time ago :P). But, after that, I'll revisit the feature requests.

One question, what's your specific use case? I'd like to keep the API as simple as possible and it's important to know it in order to decide if it should be included.

tscott0 commented 7 years ago

Hi. Thanks for the response!

It's been a while so I'm struggling to remember :) but my specific use case is having some code executed on a resize event. Ideally this code would only be executed then and not for every event. One use for this could be to completely hide a view if the terminal is too small.

Keypress events are nicely separated already, allowing SetKeybinding to pass a func that will be called for a specific key. I'd love an equivalent for resizes, like SetResizeFunc or OnResize or something.

An alternative would be to handle all events and have my code handle only resize events but I couldn't see a way to do this as I don't think the type of event is exposed. I could be wrong here.

Let me know what you think. Happy to elaborate on the use cases if necessary.

Cheers

jroimartin commented 7 years ago

One use for this could be to completely hide a view if the terminal is too small.

That make sense. It should be easy, so I'll add this functionality before the next release.

Thanks for the feedback! :)

jroimartin commented 7 years ago

I've just implemented the functionality in 80994c4668bd480f68ea48186ccf1125b355b76a (issue-109 branch). I also modified _examples/size.go to use it.

However, after using it, my impression is that the same thing can be done within a Manager/ManagerFunc calling maxX, maxY := g.Size() and updating the views accordingly. For instance, deleting a view if maxX < x || maxY < y, etc. You could even create a Manager to modify the layout depending on different sizes.

More info about Managers in the docs and in the following examples: flow_layout.go, widgets.go.

In short,

Does it make sense? What do you think?