theodox / mGui

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

Event handler stash. #69

Closed bob-white closed 7 years ago

bob-white commented 7 years ago

A possible solution to #40

Instead of trying to infer some kind of stash object like my last solution, this one requires that you explicitly pass a stash object along with the handler.

Updated the example from the issue .

from maya import cmds
from mGui import gui, forms, lists
from mGui.observable import ObservableCollection
from mGui.bindings import bind

def event_test():
    with gui.BindingWindow(title = 'example window') as test_window:
        bound = ObservableCollection("A", "B")

        with forms.VerticalForm() as main:
            gui.Text(label = "The following items don't have vertex colors")
            list_view = lists.VerticalList()
            list_view.bind.collection < bind() < bound
            with forms.HorizontalStretchForm('buttons'):
                refresh = gui.Button('refresh', l='Refresh')
                close = gui.Button('close', l='Close')

    def close_window(*_, **__):
        cmds.deleteUI(test_window)

    def refresh_window(*_, **__):
        list_view.redraw()

    # We pass both the handler, and the object to stash it on
    refresh.command += refresh_window, test_window
    close.command += close_window, test_window
    return test_window

win = event_test()
win.show()

Updated the example to use the += syntax, as per the suggestion.

theodox commented 7 years ago

this looks neater. Maybe at that point we don't need the *= and we instead catch that the arg list is a tuple?

bob-white commented 7 years ago

Oh I like that, it didn't even occur to me.

bob-white commented 7 years ago

Updated it so that we only need to care about += and -=. Added some tests for the new behavior.

theodox commented 7 years ago

Do you want to add docs, or shall I?

theodox commented 7 years ago

Doh, sorry

bob-white commented 7 years ago

Updated the docs, and the basicList example.

Also realized that using this we can bind a lambda, though only 1 because the __name__ field is always <lambda> So it will overwrite if you try to bind a second one.

theodox commented 7 years ago

why not detect it and use id (lambda) as the key?

bob-white commented 7 years ago

Completely forgot about id I'll swap that around. Its probably better to just use id(handler) as that would cover all callables.

theodox commented 7 years ago

does (id) work on weak refs?

bob-white commented 7 years ago

It does, but you get the id of the weakref not the original object's id. Same thing with a proxy