aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/darwin/autolayout.m
blob: 9964155fd36e1e3293461b19cae996fcb5b19b0f (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 15 august 2015
#import "uipriv_darwin.h"

NSLayoutConstraint *mkConstraint(id view1, NSLayoutAttribute attr1, NSLayoutRelation relation, id view2, NSLayoutAttribute attr2, CGFloat multiplier, CGFloat c, NSString *desc)
{
	NSLayoutConstraint *constraint;

	constraint = [NSLayoutConstraint constraintWithItem:view1
		attribute:attr1
		relatedBy:relation
		toItem:view2
		attribute:attr2
		multiplier:multiplier
		constant:c];
	// apparently only added in 10.9
	if ([constraint respondsToSelector:@selector(setIdentifier:)])
		[((id) constraint) setIdentifier:desc];
	return constraint;
}

CGFloat uiDarwinMarginAmount(void *reserved)
{
	return 20.0;
}

CGFloat uiDarwinPaddingAmount(void *reserved)
{
	return 8.0;
}

// this is needed for NSSplitView to work properly; see http://stackoverflow.com/questions/34574478/how-can-i-set-the-position-of-a-nssplitview-nowadays-setpositionofdivideratind (stal in irc.freenode.net/#macdev came up with the exact combination)
// turns out it also works on NSTabView and NSBox too, possibly others!
// and for bonus points, it even seems to fix unsatisfiable-constraint-autoresizing-mask issues with NSTabView and NSBox too!!! this is nuts
void jiggleViewLayout(NSView *view)
{
	[view setNeedsLayout:YES];
	[view layoutSubtreeIfNeeded];
}

static CGFloat margins(int margined)
{
	if (!margined)
		return 0.0;
	return uiDarwinMarginAmount(NULL);
}

void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *contentView, NSView *childView, BOOL hugsTrailing, BOOL hugsBottom, int margined, NSString *desc)
{
	CGFloat margin;

	margin = margins(margined);

	c->leadingConstraint = mkConstraint(contentView, NSLayoutAttributeLeading,
		NSLayoutRelationEqual,
		childView, NSLayoutAttributeLeading,
		1, -margin,
		[desc stringByAppendingString:@" leading constraint"]);
	[contentView addConstraint:c->leadingConstraint];
	[c->leadingConstraint retain];

	c->topConstraint = mkConstraint(contentView, NSLayoutAttributeTop,
		NSLayoutRelationEqual,
		childView, NSLayoutAttributeTop,
		1, -margin,
		[desc stringByAppendingString:@" top constraint"]);
	[contentView addConstraint:c->topConstraint];
	[c->topConstraint retain];

	c->trailingConstraintGreater = mkConstraint(contentView, NSLayoutAttributeTrailing,
		NSLayoutRelationGreaterThanOrEqual,
		childView, NSLayoutAttributeTrailing,
		1, margin,
		[desc stringByAppendingString:@" trailing >= constraint"]);
	if (hugsTrailing)
		[c->trailingConstraintGreater setPriority:NSLayoutPriorityDefaultLow];
	[contentView addConstraint:c->trailingConstraintGreater];
	[c->trailingConstraintGreater retain];

	c->trailingConstraintEqual = mkConstraint(contentView, NSLayoutAttributeTrailing,
		NSLayoutRelationEqual,
		childView, NSLayoutAttributeTrailing,
		1, margin,
		[desc stringByAppendingString:@" trailing == constraint"]);
	if (!hugsTrailing)
		[c->trailingConstraintEqual setPriority:NSLayoutPriorityDefaultLow];
	[contentView addConstraint:c->trailingConstraintEqual];
	[c->trailingConstraintEqual retain];

	c->bottomConstraintGreater = mkConstraint(contentView, NSLayoutAttributeBottom,
		NSLayoutRelationGreaterThanOrEqual,
		childView, NSLayoutAttributeBottom,
		1, margin,
		[desc stringByAppendingString:@" bottom >= constraint"]);
	if (hugsBottom)
		[c->bottomConstraintGreater setPriority:NSLayoutPriorityDefaultLow];
	[contentView addConstraint:c->bottomConstraintGreater];
	[c->bottomConstraintGreater retain];

	c->bottomConstraintEqual = mkConstraint(contentView, NSLayoutAttributeBottom,
		NSLayoutRelationEqual,
		childView, NSLayoutAttributeBottom,
		1, margin,
		[desc stringByAppendingString:@" bottom == constraint"]);
	if (!hugsBottom)
		[c->bottomConstraintEqual setPriority:NSLayoutPriorityDefaultLow];
	[contentView addConstraint:c->bottomConstraintEqual];
	[c->bottomConstraintEqual retain];
}

void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv)
{
	if (c->leadingConstraint != nil) {
		[cv removeConstraint:c->leadingConstraint];
		[c->leadingConstraint release];
		c->leadingConstraint = nil;
	}
	if (c->topConstraint != nil) {
		[cv removeConstraint:c->topConstraint];
		[c->topConstraint release];
		c->topConstraint = nil;
	}
	if (c->trailingConstraintGreater != nil) {
		[cv removeConstraint:c->trailingConstraintGreater];
		[c->trailingConstraintGreater release];
		c->trailingConstraintGreater = nil;
	}
	if (c->trailingConstraintEqual != nil) {
		[cv removeConstraint:c->trailingConstraintEqual];
		[c->trailingConstraintEqual release];
		c->trailingConstraintEqual = nil;
	}
	if (c->bottomConstraintGreater != nil) {
		[cv removeConstraint:c->bottomConstraintGreater];
		[c->bottomConstraintGreater release];
		c->bottomConstraintGreater = nil;
	}
	if (c->bottomConstraintEqual != nil) {
		[cv removeConstraint:c->bottomConstraintEqual];
		[c->bottomConstraintEqual release];
		c->bottomConstraintEqual = nil;
	}
}

void singleChildConstraintsSetMargined(struct singleChildConstraints *c, int margined)
{
	CGFloat margin;

	margin = margins(margined);
	if (c->leadingConstraint != nil)
		[c->leadingConstraint setConstant:-margin];
	if (c->topConstraint != nil)
		[c->topConstraint setConstant:-margin];
	if (c->trailingConstraintGreater != nil)
		[c->trailingConstraintGreater setConstant:margin];
	if (c->trailingConstraintEqual != nil)
		[c->trailingConstraintEqual setConstant:margin];
	if (c->bottomConstraintGreater != nil)
		[c->bottomConstraintGreater setConstant:margin];
	if (c->bottomConstraintEqual != nil)
		[c->bottomConstraintEqual setConstant:margin];
}