/* * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * */ #import "ASEnvironment.h" #pragma once BOOL ASEnvironmentStateUpwardPropagationEnabled(); BOOL ASEnvironmentStateDownwardPropagationEnabled(); #pragma mark - Set and get extensible values for layout options void _ASEnvironmentLayoutOptionsExtensionSetBoolAtIndex(id object, int idx, BOOL value); BOOL _ASEnvironmentLayoutOptionsExtensionGetBoolAtIndex(id object, int idx); void _ASEnvironmentLayoutOptionsExtensionSetIntegerAtIndex(id object, int idx, NSInteger value); NSInteger _ASEnvironmentLayoutOptionsExtensionGetIntegerAtIndex(id object, int idx); void _ASEnvironmentLayoutOptionsExtensionSetEdgeInsetsAtIndex(id object, int idx, UIEdgeInsets value); UIEdgeInsets _ASEnvironmentLayoutOptionsExtensionGetEdgeInsetsAtIndex(id object, int idx); #pragma mark - Traversing an ASEnvironment Tree void ASEnvironmentPerformBlockOnObjectAndChildren(id object, void(^block)(id object)); void ASEnvironmentPerformBlockOnObjectAndParents(id object, void(^block)(id object)); #pragma mark - enum class ASEnvironmentStatePropagation { DOWN, UP }; #pragma mark - Merging static const struct ASEnvironmentStateExtensions ASEnvironmentDefaultStateExtensions = {}; static const struct ASEnvironmentLayoutOptionsState ASEnvironmentDefaultLayoutOptionsState = {}; ASEnvironmentState ASEnvironmentMergeObjectAndState(ASEnvironmentState environmentState, ASEnvironmentLayoutOptionsState state, ASEnvironmentStatePropagation propagation); static const struct ASEnvironmentHierarchyState ASEnvironmentDefaultHierarchyState = {}; ASEnvironmentState ASEnvironmentMergeObjectAndState(ASEnvironmentState environmentState, ASEnvironmentHierarchyState state, ASEnvironmentStatePropagation propagation); static const struct ASDisplayTraits ASEnvironmentDefaultDisplayTraits = {}; ASEnvironmentState ASEnvironmentMergeObjectAndState(ASEnvironmentState environmentState, ASDisplayTraits state, ASEnvironmentStatePropagation propagation); #pragma mark - Propagation template void ASEnvironmentStatePropagateDown(id object, ASEnvironmentStateType state) { ASEnvironmentPerformBlockOnObjectAndChildren(object, ^(id node) { object.environmentState = ASEnvironmentMergeObjectAndState(object.environmentState, state, ASEnvironmentStatePropagation::DOWN); }); } template void ASEnvironmentStatePropagateUp(id object, ASEnvironmentStateType state) { ASEnvironmentPerformBlockOnObjectAndParents(object, ^(id node) { object.environmentState = ASEnvironmentMergeObjectAndState(object.environmentState, state, ASEnvironmentStatePropagation::UP); }); } template void ASEnvironmentStateApply(id object, ASEnvironmentStateType& state, ASEnvironmentStatePropagation propagate) { if (propagate == ASEnvironmentStatePropagation::DOWN) { ASEnvironmentStatePropagateUp(object, state); } else if (propagate == ASEnvironmentStatePropagation::UP) { ASEnvironmentStatePropagateDown(object, state); } }