nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.53k stars 130 forks source link

Mechanism to provide custom size hints #473

Closed MatthieuDartiailh closed 2 years ago

MatthieuDartiailh commented 2 years ago

In some situation the toolkit provided size hint may be less than ideal. Matplotlib widgets are a perfect example of this as discussed on the mailing list: https://groups.google.com/d/msgid/enaml/CAPc_zRWYW0jUMP-m2YrYNmCihryV4Lgr_VUvJAQjQWM1gPU%2BGA%40mail.gmail.com?utm_medium=email&utm_source=footer

Providing a declarative way to override the toolkit size hint would be nice since the procedure to ignore the existing size hint based constraints and generating new ones is not trivial.

@sccolbert any take on the API we may want for this (attribute vs declarative method) ?

@bburan once we settle on an API I will try to implement the solution but since I have other enaml work that has been delayed a bit it may take some time. Feel free to make a PR yourself if you are so inclined.

bburan commented 2 years ago

@MatthieuDartiailh Happy to take a stab at implementing once we come up with the API. The MainWindow class does have an initial_size attribute which seems along the lines of what one might expect (e.g., add a size_hint option). What's the basis for deciding between an attribute vs. method in this case?

sccolbert commented 2 years ago

As far as attribute vs. method, making it a method would denote something needs to be computed. Qt makes it a method, but you have access to information like text measurement from which to compute a preferred size. You won't have that at the Enaml level. Everything you compute would need to be derived from information provided entirely from the Enaml side (exposing the toolkit-provided size hint is probably impossible to make work for a variety of reasons).

My first instinct would be to make it an attribute, but I could probably drum up a use case for making it a method.

On Fri, Jan 21, 2022 at 8:33 AM Brad Buran @.***> wrote:

@MatthieuDartiailh https://github.com/MatthieuDartiailh Happy to take a stab at implementing once we come up with the API. The MainWindow class does have an initial_size attribute which seems along the lines of what one might expect (e.g., add a size_hint option). What's the basis for deciding between an attribute vs. method in this case?

— Reply to this email directly, view it on GitHub https://github.com/nucleic/enaml/issues/473#issuecomment-1018555483, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABBQSP732PHQBQPVVSPCZ3UXFVCFANCNFSM5MPKPIFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

MatthieuDartiailh commented 2 years ago

Going for a method would allow to retrieve the toolkit hint and then adapt it. It would also allow to adjust the guess based on the parent content. In the case discussed in the mailing list, one could retrieve the number of plots in the grid to provide a better hint.

Also with an attribute I could see people expecting that updating the attribute would update the layout.

So personally I favor a method whose default implementation retrieves the toolkit hint.

bburan commented 2 years ago

A method does add flexibility and if all one wants to do is provide a size with no calculations, it's still fairly readable code, e.g.:

MPLContainer:
    size_hint => ():
        return (800, 400)

as opposed to:

MPLContainer:
    size_hint = (800, 400)

However, now that I better understand how to set up resit/hug and provide constraints, does adding a size_hint method add any additional value if one knows how to accomplish the same by setting constraints properly? If not, then I would be happy to submit a code example as an alternative (e.g., I could clean up and document the code example in this thread to add to the Enaml docs).

MatthieuDartiailh commented 2 years ago

Adding more examples around advanced layout techniques is likely worthwhile no matter what. I don't remember of the top of my head if we have examples dealing in details with hug and resist. I think I documented it at one point but I don't remember how deep I went.

I opened the issue since @sccolbert suggested that since it was a recurrent request it was worth adding a nicer way to do things.

MatthieuDartiailh commented 2 years ago

@bburan do you still plan a new example ?

bburan commented 2 years ago

@MatthieuDartiailh Thanks for checking in. Just submitted an example.

MatthieuDartiailh commented 2 years ago

Thanks

MatthieuDartiailh commented 2 years ago

Closing now that we have a new example that covers this question.