Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created November 3, 2014 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typemytype/b9c85801762c579448d7 to your computer and use it in GitHub Desktop.
Save typemytype/b9c85801762c579448d7 to your computer and use it in GitHub Desktop.
auto layout contraints proposal
import vanilla
from AppKit import *
"""
for the demo some vanilla objects are subclassed
- the main change would be in the VanillaBaseObject
- a window, sheet, group, popover (all elements that can set vanillaObjects) could have a addConstraints attribute
- map the attribute name and the view automatically whenever the posSize is "auto"
- the constraint cannot be a replacement of the posSize cause it the constraint Visual Format Language is a discription of a complete UI.
It needs to know the other elements aswell as the superview and some provided metrics
https://developer.apple.com/LIBRARY/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010853
https://developer.apple.com/LIBRARY/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3-SW1
"""
class ConstraintsWindow(vanilla.Window):
def init(self):
self = super(vanilla.Window, self).init()
self._autoLayoutViews = dict()
return self
def __setattr__(self, attr, value):
if hasattr(value, "getPosSize"):
if value.getPosSize() == "auto":
view = value._getContentView()
view.setTranslatesAutoresizingMaskIntoConstraints_(False)
self._autoLayoutViews[attr] = view
super(self.__class__, self).__setattr__(attr, value)
def __delattr__(self, attr):
if attr in self._autoLayoutViews:
del self._autoLayoutViews[attr]
super(self.__class__, self).__delattr__(attr)
def addConstraints(self, constraints, metrics=None):
"""
a list of auto layout constraints
"""
view = self._window.contentView()
if metrics is None:
metrics = dict()
for constrain in constraints:
constrain = NSLayoutConstraint.constraintsWithVisualFormat_options_metrics_views_(constrain, 0, metrics, self._autoLayoutViews)
view.addConstraints_(constrain)
class Group(vanilla.Group):
def __init__(self, posSize):
super(self.__class, self).__init__(posSize)
self._autoLayoutViews = dict()
def __setattr__(self, attr, value):
if hasattr(value, "getPosSize"):
if value.getPosSize() == "auto":
view = value._getContentView()
view.setTranslatesAutoresizingMaskIntoConstraints_(False)
self._autoLayoutViews[attr] = view
super(self.__class__, self).__setattr__(attr, value)
def __delattr__(self, attr):
if attr in self._autoLayoutViews:
del self._autoLayoutViews[attr]
super(self.__class__, self).__delattr__(attr)
def setPosSize(self, posSize):
# should move to the VanillaBaseObject
self._posSize = posSize
if posSize == "auto":
return
super(self.__class__, self).setPosSize(posSize)
def _setAutosizingFromPosSize(self, posSize):
# should move to the VanillaBaseObject
if posSize == "auto":
return
super(self.__class__, self)._setAutosizingFromPosSize(posSize)
def _setFrame(self, parentFrame):
# should move to the VanillaBaseObject
if self.getPosSize() == "auto":
return
super(self.__class__, self)._setFrame(parentFrame)
def addConstraints(self, constraints, metrics):
"""
a list of auto layout constraints
"""
for constrain in constraints:
constrain = NSLayoutConstraint.constraintsWithVisualFormat_options_metrics_views_(constrain, 0, metrics, self._autoLayoutViews)
view.addConstraints_(constrain)
class TextBox(vanilla.TextBox):
def setPosSize(self, posSize):
# should move to the VanillaBaseObject
self._posSize = posSize
if posSize == "auto":
return
super(self.__class__, self).setPosSize(posSize)
def _setAutosizingFromPosSize(self, posSize):
# should move to the VanillaBaseObject
if posSize == "auto":
return
super(self.__class__, self)._setAutosizingFromPosSize(posSize)
def _setFrame(self, parentFrame):
# should move to the VanillaBaseObject
if self.getPosSize() == "auto":
return
super(self.__class__, self)._setFrame(parentFrame)
class EditText(vanilla.EditText):
def setPosSize(self, posSize):
# should move to the VanillaBaseObject
self._posSize = posSize
if posSize == "auto":
return
super(self.__class__, self).setPosSize(posSize)
def _setAutosizingFromPosSize(self, posSize):
# should move to the VanillaBaseObject
if posSize == "auto":
return
super(self.__class__, self)._setAutosizingFromPosSize(posSize)
def _setFrame(self, parentFrame):
# should move to the VanillaBaseObject
if self.getPosSize() == "auto":
return
super(self.__class__, self)._setFrame(parentFrame)
## test window
class TestConstraints:
def __init__(self):
self.w = ConstraintsWindow((400, 400), "AutoLayout", minSize=(200, 200))
self.w.info = TextBox("auto", "auto layout:", alignment="right")
self.w.input = EditText("auto")
import random
randomText = random.randint(1, 5) * "."
self.w.info1 = TextBox("auto", "auto part layout%s:" % randomText, alignment="right")
self.w.input1 = EditText("auto")
self.w.t2 = TextBox((10, -30, -10, 22), "Old style vanilla with posSize")
constraints = [
"V:|-padding-[info(>=controlHeight)]-padding-[info1(>=controlHeight)]-80-|",
"V:|-padding-[input(>=controlHeight)]-padding-[input1(>=controlHeight)]-80-|",
"|-padding-[info(info1)]-padding-[input(>=200)]-padding-|",
"|-padding-[info1(info1)]-padding-[input1(>=200)]-padding-|",
]
metrics = dict(padding=10, controlHeight=22)
self.w.addConstraints(constraints, metrics)
self.w.open()
if __name__ == "__main__":
from vanilla.test.testTools import executeVanillaTest
executeVanillaTest(TestConstraints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment