Skip to content

Commit

Permalink
add a FactoryHelper constraint helper
Browse files Browse the repository at this point in the history
This constraints helper allows the user to define a function (or any
callable) which will be invoked as-needed to generate the constraints
for the layout.
  • Loading branch information
sccolbert committed Mar 10, 2014
1 parent aed5ddd commit 41480f2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion enaml/layout/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
FloatItem, FloatArea, RemoveItem, ExtendItem, RetractItem
)
from .layout_helpers import (
align, hbox, vbox, horizontal, vertical, grid, spacer,
align, hbox, vbox, horizontal, vertical, factory, grid, spacer,
)
from .geometry import Box, BoxF, Pos, PosF, Rect, RectF, Size, SizeF
63 changes: 63 additions & 0 deletions enaml/layout/factory_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#------------------------------------------------------------------------------
# Copyright (c) 2014, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Callable, Tuple, Dict

from .constraint_helper import ConstraintHelper


class FactoryHelper(ConstraintHelper):
""" A constraint helper which delegates to a factory callable.
"""
#: The callable object which will generate the list of constraints.
#: It will be passed the constraints widget owner as the first
#: argument, and should return the list of generated constraints.
factory = Callable()

#: Additional positional arguments to pass to the callable.
args = Tuple()

#: Additional keyword arguments to pass to the callable.
kwargs = Dict()

def __init__(self, factory, *args, **kwargs):
""" Initialize a FactoryHelper.
Parameters
----------
factory : callable
The callable object which generates the constraints.
*args
Additional positional arguments to pass to the factory.
**kwargs
Additional keyword arguments to pass to the factory.
"""
self.factory = factory
self.args = args
self.kwargs = kwargs

def constraints(self, component):
""" Generate the constraints using the underlying factory.
This method will automatically expand ConstraintHelper objects
which are generated by the factory.
"""
factory = self.factory
if factory is None:
return []
cns = [] # inline `expand_constraints`
for cn in factory(component, *self.args, **self.kwargs):
if isinstance(cn, ConstraintHelper):
cns.extend(cn.create_constraints(component))
elif cn is not None:
cns.append(cn)
return cns
20 changes: 20 additions & 0 deletions enaml/layout/layout_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
from .constraint_helper import ConstraintHelper
from .factory_helper import FactoryHelper
from .grid_helper import GridHelper
from .linear_box_helper import LinearBoxHelper
from .sequence_helper import SequenceHelper
Expand Down Expand Up @@ -94,6 +95,25 @@ def align(anchor, *items, **config):
return SequenceHelper(anchor, anchor, items, **config)


def factory(func, *args, **kwargs):
""" Create a FactoryHelper with the given factory function.
Parameters
----------
func : callable
The callable which will generate the list of constraints.
The owner widget will be passed as the first argument.
*args
Additional positional arguments to pass to the factory.
**kwargs
Additional keyword arguments to pass to the factory.
"""
return FactoryHelper(func, *args, **kwargs)


def grid(*rows, **config):
""" Create a GridHelper object with the given rows.
Expand Down

0 comments on commit 41480f2

Please sign in to comment.