theodox / mGui

Python module for cleaner maya GUI layout syntax
MIT License
123 stars 23 forks source link

_set_stylesheet fails if passed a dictionary #93

Open bob-white opened 6 years ago

bob-white commented 6 years ago
    def _set_stylesheet(self, css):
        if not isinstance(css, CSS):
            css = CSS(**css) # <<-- Lacks a target argument
        self._style = css
        css.apply_recursive(self)

Not sure what an appropriate substitute key would be here, though maybe self.key is workable? Or we could raise TypeError if not passed a CSS instance.

theodox commented 6 years ago

Does this fail for a case like this?

dummy = {'backgroundColor': 'red', 'width': 200}
Button(style=dummy)
bob-white commented 6 years ago

That one fails, but this works:

dummy = {'backgroundColor': (1,0,0), 'width': 200}
Button(css=dummy)

its really just the Styled.stylesheet property that causes the issue, at it tries to create a CSS instance from the dict. Which fails because CSS expects a target param in addition to the usual style kwargs.

But after that everything just treats the _style attribute as a mapping.

The only circumstance I could see that would fail would be:

with VerticalForm(css={'backgroundColor': (1,0,0), 'width': 200}) as f:
    for i in xrange(5):
        Button()
f.stylesheet.apply_recursive(f)

Which will fail with:

# AttributeError: 'dict' object has no attribute 'apply_recursive' # 
severn2 commented 6 years ago

What is the use-case for specifying a dictionary outside of css={} ? I'm noticing that the apply_recursive method in _set_stylesheet does not apply the style to the controls defined. For example:

style = styles.CSS('_', backgroundColor = (1,0,0), width = 200)
with VerticalForm() as f:
    for i in xrange(5):
        Button()
f.stylesheet = style

Let me know if this is intended behavior.

theodox commented 6 years ago

Maybe we just give it an empty, like this:

   def _set_stylesheet(self, css):
        css = css or {} 
        if not isinstance(css, CSS):
            css = CSS(**css) # <<-- Lacks a target argument
        self._style = css
theodox commented 6 years ago

It's just syntax sugar for users, since there are a lot of use cases where you just want to say 'use these options' and the more elaborate stuff is not on your radar

theodox commented 6 years ago

Doh, we could just target Control when we make the fake CSS....

def _set_stylesheet(self, css): if not isinstance(css, CSS): css = CSS(gui.Control, **css) self._style = css css.apply_recursive(self)

self.key would apply it only to this instance, control would do it to anything under this instance. I'm not sure which would be a commoner case. Maybe self.key is better because it prevents outflows from this non-standard syntax. Thoughts?