hanjinliu / magic-class

Generate multifunctional and macro recordable GUIs from Python classes
https://hanjinliu.github.io/magic-class/
BSD 3-Clause "New" or "Revised" License
40 stars 5 forks source link

Implement `Box` types #112

Closed hanjinliu closed 1 year ago

hanjinliu commented 1 year ago

Currently, to make widgets of scrollable, collapsible etc., one must rely on @magicclass decorator with the widget_type argument. This is, however, not efficient if the magic class has only one field.

from magicclass import magicclass, field
from magicclass.widgets import Figure

@magicclass
class A:
    @magicclass(widget_type="scrollable")
    class Plot:
        plt = field(Figure)

ui = A()
ui.Plot.plt.plot(...)  # <-- "Plot" is required here :(

This PR implements a type called Box, which adds some functions to a field or a widget class. The word "box" comes from the Box type in Rust, which behaves in a similar way but for different purposes.

from magicclass import magicclass, field
from magicclass.widgets import Figure
from magicclass.box import resizable

@magicclass
class A:
    plt = resizable(field(Figure))  # as if `plt = field(Figure)`

ui = A()
ui.plt.plot(...)  # <-- OK, and type safe!

TODOs