Skip to content

Commit 30f399a

Browse files
Init hook (#127)
* Add init hook * Apply init hook throughout the whole repo
1 parent 6d363a1 commit 30f399a

35 files changed

Lines changed: 73 additions & 81 deletions

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ import collagraph as cg
3838
3939
4040
class Counter(cg.Component):
41-
def __init__(self, *args, **kwargs):
42-
super().__init__(*args, **kwargs)
41+
def init(self):
4342
self.state["count"] = 0
4443
4544
def bump(self):

collagraph/component.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def __init__(self, props=None, parent=None):
2727
self._parent = ref(parent) if parent else None
2828
self._provided = {}
2929

30+
# Call init hook at end of __init__
31+
self.init()
32+
33+
def init(self):
34+
pass
35+
3036
@property
3137
def props(self):
3238
"""The incoming props of this component."""

examples/pygfx/app_point_cloud.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ from examples.pygfx.point_cloud import PointCloud
1818

1919

2020
class Example(cg.Component):
21-
def __init__(self, *args, **kwargs):
22-
super().__init__(*args, **kwargs)
21+
def init(self):
2322
self.state["count"] = self.props["count"]
2423

2524
def add(self):

examples/pygfx/button.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ class Button(cg.Component):
3232
),
3333
}
3434

35-
def __init__(self, *args, **kwargs):
36-
super().__init__(*args, **kwargs)
35+
def init(self):
3736
self.state["pressed"] = False
3837
self.state["hovered"] = False
3938
self.state["scale"] = self.props.get("scale", [0.85] * 3)

examples/pygfx/numberpad.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ from examples.pygfx.button import Button
99

1010

1111
class NumberPad(cg.Component):
12-
def __init__(self, *args, **kwargs):
13-
super().__init__(*args, **kwargs)
12+
def init(self):
1413
self.state["columns"] = 4
1514
self.state["rows"] = 5
1615

examples/pygfx/point_cloud.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def rand_point():
3434

3535

3636
class PointCloud(collagraph.Component):
37-
def __init__(self, *args, **kwargs):
38-
super().__init__(*args, **kwargs)
37+
def init(self):
3938
self.state["positions"] = []
4039
self.state["hovered"] = -1
4140
self.state["selected"] = -1

examples/pyside/big_list.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ from examples.pyside.item import Item
6363

6464

6565
class TreeWidget(cg.Component):
66-
def __init__(self, *args, **kwargs):
67-
super().__init__(*args, **kwargs)
66+
def init(self):
6867
self.state["columns"] = ["text", "other"]
6968
self.state["items"] = [
7069
*[{"text": f"text-{idx}", "other": f"other-{idx}"} for idx in range(500)]

examples/pyside/cgx_component_example.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ from collagraph import Component
2222

2323

2424
class Example(Component):
25-
def __init__(self, *args, **kwargs):
26-
super().__init__(*args, **kwargs)
25+
def init(self):
2726
self.state["top_to_bottom"] = {
2827
"type": "box",
2928
"direction": "TopToBottom",

examples/pyside/counter.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import collagraph as cg
1717

1818

1919
class Counter(cg.Component):
20-
def __init__(self, *args, **kwargs):
21-
super().__init__(*args, **kwargs)
20+
def init(self):
2221
self.state["count"] = 0
2322

2423
def bump(self):

examples/pyside/dialog_example.cgx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ import collagraph as cg
4040

4141

4242
class ListDialog(cg.Component):
43-
def __init__(self, *args, **kwargs):
44-
super().__init__(*args, **kwargs)
43+
def init(self):
4544
self.state["selected"] = -1
4645

4746
def accepted(self):

0 commit comments

Comments
 (0)