Skip to content

Commit 3f2f311

Browse files
committed
Respect margins when with/height is "fill."
1 parent 6ed8213 commit 3f2f311

File tree

1 file changed

+67
-28
lines changed

1 file changed

+67
-28
lines changed

Lib/vanilla/vanillaStackView.py

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ def __init__(self, posSize, views, spacing=0, alignment="center", distribution="
115115
stackView.setSpacing_(spacing)
116116
stackView.setAlignment_(alignment)
117117
stackView.setDistribution_(distribution)
118+
self.setEdgeInsets(edgeInsets)
118119
for view in views:
119120
if not isinstance(view, dict):
120121
view = dict(view=view)
121122
self.appendView(**view)
122-
self.setEdgeInsets(edgeInsets)
123123

124124
def getNSStackView(self):
125125
return self._nsObject
@@ -217,20 +217,37 @@ def insertView(self, index, view, width=None, height=None, spacing=None, gravity
217217
AppKit.NSLayoutConstraintOrientationVertical
218218
)
219219
# constraints
220+
bottomInset, leftInset, topInset, rightInset = self._nsObject.edgeInsets()
220221
if width is not None:
221222
if width == "fill":
222-
width = stackView.widthAnchor()
223-
_setAnchorContraint(
224-
view.widthAnchor(),
225-
width
226-
)
223+
_setAnchorConstraint(
224+
anchor=view.leftAnchor(),
225+
otherAnchor=stackView.leftAnchor(),
226+
constant=leftInset
227+
)
228+
_setAnchorConstraint(
229+
anchor=view.rightAnchor(),
230+
otherAnchor=stackView.rightAnchor(),
231+
constant=-rightInset
232+
)
233+
else:
234+
_setAnchorConstraint(
235+
view.widthAnchor(),
236+
width
237+
)
227238
if height is not None:
228239
if height == "fill":
229-
height = stackView.heightAnchor()
230-
_setAnchorContraint(
231-
view.heightAnchor(),
232-
height
233-
)
240+
view.topAnchor().constraintEqualToAnchor_(
241+
stackView.topAnchor()
242+
)
243+
view.bottomAnchor().constraintEqualToAnchor_(
244+
stackView.bottomAnchor()
245+
)
246+
else:
247+
_setAnchorConstraint(
248+
view.heightAnchor(),
249+
height
250+
)
234251
# spacing
235252
if spacing is not None:
236253
stackView.setCustomSpacing_afterView_(spacing, view)
@@ -351,26 +368,48 @@ def __init__(self):
351368
width=AppKit.NSLayoutAttributeWidth
352369
)
353370

354-
def _setAnchorContraint(anchor, value):
355-
for existing in anchor.constraintsAffectingLayout():
356-
existing.setActive_(False)
371+
def _setAnchorConstraint(
372+
anchor,
373+
value=None,
374+
otherAnchor=None,
375+
constant=0
376+
):
357377
if isinstance(value, str) and "," in value:
358378
for v in value.split(","):
359379
v = v.strip()
360-
_setAnchorContraint(anchor, v)
380+
_setAnchorConstraint(
381+
anchor,
382+
value=v,
383+
otherAnchor=otherAnchor,
384+
constant=constant
385+
)
361386
return
362-
methods = {
363-
"==" : anchor.constraintEqualToConstant_,
364-
">=" : anchor.constraintGreaterThanOrEqualToConstant_,
365-
"<=" : anchor.constraintLessThanOrEqualToConstant_,
366-
}
367-
if isinstance(value, AppKit.NSLayoutAnchor):
368-
method = anchor.constraintEqualToAnchor_
369-
elif isinstance(value, (int, float)):
370-
method = methods["=="]
371-
else:
387+
for existing in anchor.constraintsAffectingLayout():
388+
existing.setActive_(False)
389+
if otherAnchor is not None:
390+
anchorMethods = {
391+
"==" : anchor.constraintEqualToAnchor_constant_,
392+
">=" : anchor.constraintGreaterThanOrEqualToAnchor_constant_,
393+
"<=" : anchor.constraintLessThanOrEqualToAnchor_constant_,
394+
}
395+
args = [otherAnchor, constant]
396+
if not isinstance(value, str):
397+
value = "=="
372398
relation = value[:2]
373-
value = float(value[2:])
374-
method = methods[relation]
375-
constraint = method(value)
399+
method = anchorMethods[relation]
400+
else:
401+
assert value is not None, "A value must be given if otherAnchor is None."
402+
valueMethods = {
403+
"==" : anchor.constraintEqualToConstant_,
404+
">=" : anchor.constraintGreaterThanOrEqualToConstant_,
405+
"<=" : anchor.constraintLessThanOrEqualToConstant_,
406+
}
407+
if isinstance(value, (int, float)):
408+
method = valueMethods["=="]
409+
else:
410+
relation = value[:2]
411+
value = float(value[2:])
412+
method = valueMethods[relation]
413+
args = [value]
414+
constraint = method(*args)
376415
constraint.setActive_(True)

0 commit comments

Comments
 (0)