Skip to content

Commit bc59f7a

Browse files
authoredJan 9, 2023
Remove hasattr test in _alertBindings. (#179)
* Remove hasattr test in _alertBindings. This is for #178. * Move destruction of _bindings to _breakCycles.
1 parent 8967670 commit bc59f7a

File tree

2 files changed

+128
-17
lines changed

2 files changed

+128
-17
lines changed
 
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import vanilla
2+
3+
instructions = """
4+
1. Launch the test window.
5+
- (Psst, press the button up there.)
6+
7+
2. Move the test window.
8+
- "move" - not observed
9+
10+
3. Resize the test window.
11+
- "resize" - not observed
12+
13+
4. Select this window...
14+
15+
- "resigned main" - not observed
16+
- "resigned key" - not observed
17+
18+
... then select the test window.
19+
20+
- "became main" - not observed
21+
- "became key" - not observed
22+
23+
5. Close the test window.
24+
- "should close" - not observed
25+
- "close" - not observed
26+
"""
27+
28+
class TestWindowBindingsLauncher:
29+
30+
def __init__(self):
31+
self.w = vanilla.Window((250, 500), "Test Launcher")
32+
self.w.launchButton = vanilla.Button(
33+
(10, 10, -10, 20),
34+
"Launch Test Window",
35+
self.launchButtonCallback
36+
)
37+
self.w.reportField = vanilla.TextEditor(
38+
(10, 40, -10, -10),
39+
instructions
40+
)
41+
self.w.open()
42+
43+
def launchButtonCallback(self, sender):
44+
TestWindowBindings(
45+
getter=self.w.reportField.get,
46+
setter=self.w.reportField.set
47+
)
48+
text = self.w.reportField.get()
49+
text = text.replace("- (Psst, press the button up there.)", "- Done!")
50+
self.w.reportField.set(text)
51+
52+
class TestWindowBindings:
53+
54+
def __init__(self, setter, getter):
55+
self.textSetter = setter
56+
self.textGetter = getter
57+
self.w = vanilla.Window(
58+
(300, 300),
59+
"Test Window",
60+
minSize=(200, 200)
61+
)
62+
self.w.text = vanilla.EditText(
63+
(10, 10, -10, -10),
64+
"Follow the instructions in the launcher window."
65+
)
66+
self.w.open()
67+
self.w.bind("should close", self.shouldCloseCallback)
68+
self.w.bind("close", self.closeCallback)
69+
self.w.bind("move", self.moveCallback)
70+
self.w.bind("resize", self.resizeCallback)
71+
self.w.bind("became main", self.becameMainCallback)
72+
self.w.bind("resigned main", self.resignedMainCallback)
73+
self.w.bind("became key", self.becameKeyCallback)
74+
self.w.bind("resigned key", self.resignedKeyCallback)
75+
76+
def shouldCloseCallback(self, sender):
77+
text = self.textGetter()
78+
text = text.replace("- \"should close\" - not observed", "- \"should close\" - Done!")
79+
self.textSetter(text)
80+
return True
81+
82+
def closeCallback(self, sender):
83+
text = self.textGetter()
84+
text = text.replace("- \"close\" - not observed", "- \"close\" - Done!")
85+
self.textSetter(text)
86+
87+
def moveCallback(self, sender):
88+
text = self.textGetter()
89+
text = text.replace("- \"move\" - not observed", "- \"move\" - Done!")
90+
self.textSetter(text)
91+
92+
def resizeCallback(self, sender):
93+
text = self.textGetter()
94+
text = text.replace("- \"resize\" - not observed", "- \"resize\" - Done!")
95+
self.textSetter(text)
96+
97+
def becameMainCallback(self, sender):
98+
text = self.textGetter()
99+
text = text.replace("- \"became main\" - not observed", "- \"became main\" - Done!")
100+
self.textSetter(text)
101+
102+
def resignedMainCallback(self, sender):
103+
text = self.textGetter()
104+
text = text.replace("- \"resigned main\" - not observed", "- \"resigned main\" - Done!")
105+
self.textSetter(text)
106+
107+
def becameKeyCallback(self, sender):
108+
text = self.textGetter()
109+
text = text.replace("- \"became key\" - not observed", "- \"became key\" - Done!")
110+
self.textSetter(text)
111+
112+
def resignedKeyCallback(self, sender):
113+
text = self.textGetter()
114+
text = text.replace("- \"resigned key\" - not observed", "- \"resigned key\" - Done!")
115+
self.textSetter(text)
116+
117+
118+
if __name__ == "__main__":
119+
from vanilla.test.testTools import executeVanillaTest
120+
executeVanillaTest(TestWindowBindingsLauncher)

‎Lib/vanilla/vanillaWindows.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ def __init__(self, posSize, title="", minSize=None, maxSize=None, textured=False
161161
self._window.setTitle_(title)
162162
self._window.setLevel_(self.nsWindowLevel)
163163
self._window.setReleasedWhenClosed_(False)
164+
self._bindings = {}
164165
self._window.setDelegate_(self)
165166
self._autoLayoutViews = {}
166-
self._bindings = {}
167167
self._initiallyVisible = initiallyVisible
168168
# full screen mode
169169
if osVersionCurrent >= osVersion10_7:
@@ -209,6 +209,7 @@ def _cascade(self):
209209
self._window.setFrameTopLeftPoint_(leftTop)
210210

211211
def _breakCycles(self):
212+
self._bindings = {}
212213
_breakCycles(self._window.contentView())
213214
drawers = self._window.drawers()
214215
if drawers is not None:
@@ -593,28 +594,18 @@ def unbind(self, event, callback):
593594

594595
@python_method
595596
def _alertBindings(self, key):
596-
# test to see if the attr exists.
597-
# this is necessary because NSWindow
598-
# can move the window (and therefore
599-
# call the delegate method which calls
600-
# this method) before the super
601-
# call in __init__ is complete.
602597
returnValues = []
603-
if hasattr(self, "_bindings"):
604-
if key in self._bindings:
605-
for callback in self._bindings[key]:
606-
value = callback(self)
607-
if value is not None:
608-
# elimitate None return value
609-
returnValues.append(value)
598+
if key in self._bindings:
599+
for callback in self._bindings[key]:
600+
value = callback(self)
601+
if value is not None:
602+
# elimitate None return value
603+
returnValues.append(value)
610604
return all(returnValues)
611605

612606
def windowWillClose_(self, notification):
613607
self.hide()
614608
self._alertBindings("close")
615-
# remove all bindings to prevent circular refs
616-
if hasattr(self, "_bindings"):
617-
del self._bindings
618609
self._breakCycles()
619610
# We must make sure that the window does _not_ get deallocated during
620611
# windowWillClose_, or weird things happen, such as that the window

0 commit comments

Comments
 (0)
Please sign in to comment.