py_cui icon indicating copy to clipboard operation
py_cui copied to clipboard

Add custom widget without forking project and modifying __init__.py

Open brian-romanowski-nuance opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. I'm new to py_cui so apologies if I'm jumping the gun here. I'm interested in building a custom widget but, according to the documentation, it's necessary to edit the py_cui/init.py source code to add a add_my_widget() function. It'd be nice to allow extending the main py_cui release without needing to build a custom version of the package.

Describe the solution you'd like Is it possible to define a generic function for adding custom widgets? Like:

def add_custom_widget(self, widget_class, title, row, column, row_span=1, column_span=1, padx=1, pady=0, is_selectable=True, **kwargs):
    id = 'Widget{}'.format(len(self.get_widgets().keys()))
    new_widget = widget_class(id, 
                                            title, 
                                            self._grid, 
                                            row, 
                                            column, 
                                            row_span, 
                                            column_span, 
                                            padx, 
                                            pady, 
                                            self._logger,
                                            **kwargs)
    self.get_widgets()[id] = new_widget
    if is_selectable and self._selected_widget is None:
        self.set_selected_widget(id)
    self._logger.debug('Adding widget {} w/ ID {} of type {}'.format(title, id, str(type(new_widget))))
    return new_widget

A side benefit is that the existing add_* functions could just call this function, which would remove some of the repeated code. The main benefit is that projects could add their own custom widgets without modifying the core py_cui code.

brian-romanowski-nuance avatar Jul 29 '21 14:07 brian-romanowski-nuance

This is a fantastic idea! I was thinking about how to allow something like this for a while, but didn't come up with just adding an add_generic_widget function. The whole add_* functions were a relic of the original version of py_cui prior to even being made public on github. I had considered changing the entire structure of the interface to something where instead of adding the widget to the parent, you would instantiate widgets directly and pass the parent interface as an argument, but at this point it was too big of a breaking change. I like this solution though, since it wouldn't break any compatibility.

At the moment I want to finish the already selected for development stuff for the next release that has already taken longer than I wanted, but this will be first on the list once I get to the next version. Of course if you would like to try yourself a PR would be welcome. Current development is on the v0.1.4 branch, so you'd probably want to start from there.

jwlodek avatar Jul 29 '21 15:07 jwlodek

Great! I'd like to keep playing around with the framework and to understand it a bit better before making a contribution, at least for a few more days. I'm up for making a PR eventually but please don't wait for me if you want to make the change sooner.

brian-romanowski-nuance avatar Jul 29 '21 15:07 brian-romanowski-nuance