forked from Tricertops/KeepLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeepTypes.h
112 lines (80 loc) · 3.93 KB
/
KeepTypes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// KeepTypes.h
// Keep Layout
//
// Created by Martin Kiss on 28.1.13.
// Copyright (c) 2013 Triceratops. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#define KeepAssert(CONDITION, DESCRIPTION...) NSCAssert((CONDITION), @"Keep Layout: " DESCRIPTION)
#define KeepParameterAssert(CONDITION) NSCAssert((CONDITION), @"Keep Layout: " @"Invalid parameter not satisfying: %s", #CONDITION)
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#define KPView UIView
#define KPEdgeInsets UIEdgeInsets
#define KPEdgeInsetsZero UIEdgeInsetsZero
#define KPOffset UIOffset
#define KPOffsetZero UIOffsetZero
/// Use custom names.
typedef float KeepPriority;
static const KeepPriority KeepPriorityRequired = UILayoutPriorityRequired;
static const KeepPriority KeepPriorityHigh = UILayoutPriorityDefaultHigh;
static const KeepPriority KeepPriorityLow = UILayoutPriorityDefaultLow;
static const KeepPriority KeepPriorityFitting = UILayoutPriorityFittingSizeLevel;
#else
#import <AppKit/AppKit.h>
#define KPView NSView
#define KPEdgeInsets NSEdgeInsets
extern const KPEdgeInsets KPEdgeInsetsZero;
typedef struct KPOffset {
CGFloat horizontal, vertical;
} KPOffset;
static inline KPOffset KPOffsetMake(CGFloat horizontal, CGFloat vertical) {
KPOffset offset = {horizontal, vertical};
return offset;
}
extern const KPOffset KPOffsetZero;
/// Use custom names.
typedef float KeepPriority;
static const KeepPriority KeepPriorityRequired = NSLayoutPriorityRequired;
static const KeepPriority KeepPriorityHigh = NSLayoutPriorityDefaultHigh;
static const KeepPriority KeepPriorityLow = NSLayoutPriorityDefaultLow;
static const KeepPriority KeepPriorityFitting = NSLayoutPriorityFittingSizeCompression;
/// OS X doesn’t have margins.
#define NSLayoutAttributeTopMargin NSLayoutAttributeTop
#define NSLayoutAttributeLeftMargin NSLayoutAttributeLeft
#define NSLayoutAttributeRightMargin NSLayoutAttributeRight
#define NSLayoutAttributeBottomMargin NSLayoutAttributeBottom
#define NSLayoutAttributeLeadingMargin NSLayoutAttributeLeading
#define NSLayoutAttributeTrailingMargin NSLayoutAttributeTrailing
#define NSLayoutAttributeFirstBaseline NSLayoutAttributeBaseline
#define NSLayoutAttributeLastBaseline NSLayoutAttributeBaseline
#endif
extern NSString *KeepPriorityDescription(KeepPriority);
#pragma mark Value
/// Represents a value with associated priority (imaginary part). Used as values for attributes and underlaying constraints. Complex type is used to provide compatibility with scalars.
typedef _Complex double KeepValue;
/// Extracts priority (imaginary part). The value itself can be obtained by casting to double.
extern double KeepValueGetPriority(KeepValue);
/// If the priority is 0, sets the priority provided.
extern KeepValue KeepValueSetDefaultPriority(KeepValue, KeepPriority);
/// Use these macros to build KeepValues easily: x = 10 keepHigh;
#define keepAt(Priority) +((Priority) * 1i)
#define keepRequired keepAt(KeepPriorityRequired)
#define keepHigh keepAt(KeepPriorityHigh)
#define keepLow keepAt(KeepPriorityLow)
#define keepFitting keepAt(KeepPriorityFitting)
/// Value, that represents no value. KeepValueIsNone will return YES.
extern const KeepValue KeepNone;
/// Returns YES for any value that has real value of NAN.
extern BOOL KeepValueIsNone(KeepValue);
/// Constructor with arbitrary priority
extern KeepValue KeepValueMake(CGFloat, KeepPriority);
/// Constructors for 4 basic priorities
extern KeepValue KeepRequired(CGFloat) __deprecated_msg("You don’t need this. Just assign the number directly. Magic!");
extern KeepValue KeepHigh(CGFloat);
extern KeepValue KeepLow(CGFloat);
extern KeepValue KeepFitting(CGFloat);
/// Debug description (example “42@750”, or just “42” if priority is Required)
extern NSString *KeepValueDescription(KeepValue);