File: NSAutoresizingMaskLayoutConstraint.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (295 lines) | stat: -rw-r--r-- 11,514 bytes parent folder | download | duplicates (2)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* Implementation of class NSAutoresizingMaskLayoutConstraint
   Copyright (C) 2023 Free Software Foundation, Inc.

   By: Benjamin Johnson <benjaminkylejohnson@gmail.com>
   Date: 2023

   This file is part of the GNUstep Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110 USA.
*/
#import "NSAutoresizingMaskLayoutConstraint.h"

@implementation NSAutoresizingMaskLayoutConstraint

static NSUInteger minXAttribute = 32;
static NSUInteger minYAttribute = 33;
static NSUInteger maxXAttribute = 36;
static NSUInteger maxYAttribute = 37;

+ (NSAutoresizingMaskLayoutConstraint *)
    _xConstraintForAutoresizingMask: (NSAutoresizingMaskOptions)autoresizingMask
                            subitem: (NSView *)subItem
                              frame: (NSRect)frame
                          superitem: (NSView *)superItem
                             bounds: (NSRect)bounds
{
  if ((autoresizingMask & NSViewMinXMargin)
      && (autoresizingMask & NSViewMaxXMargin))
    {
      CGFloat width = bounds.size.width - frame.size.width;
      CGFloat x = frame.origin.x - bounds.origin.x;
      CGFloat xWidthRatio = x / width;
      CGFloat constant = xWidthRatio * frame.size.width * -1;

      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: minXAttribute
                   relatedBy: NSLayoutRelationEqual
                      toItem: superItem
                   attribute: NSLayoutAttributeWidth
                  multiplier: xWidthRatio
                    constant: constant];
    }
  else if (autoresizingMask & NSViewMinXMargin)
    {
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: superItem
                   attribute: NSLayoutAttributeWidth
                   relatedBy: NSLayoutRelationEqual
                      toItem: subItem
                   attribute: maxXAttribute
                  multiplier: 1.0
                    constant: (frame.size.width + frame.origin.x
                              - bounds.size.width - bounds.origin.x)
                             * -1];
    }
  else if (autoresizingMask & NSViewMaxXMargin)
    {
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: minXAttribute
                   relatedBy: NSLayoutRelationEqual
                      toItem: nil
                   attribute: NSLayoutAttributeNotAnAttribute
                  multiplier: 1.0
                    constant: frame.origin.x - bounds.origin.x];
    }
  else
    {
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: minXAttribute
                   relatedBy: NSLayoutRelationEqual
                      toItem: nil
                   attribute: NSLayoutAttributeNotAnAttribute
                  multiplier: 1.0
                    constant: frame.origin.x - bounds.origin.x];
    }
}

+ (NSAutoresizingMaskLayoutConstraint *)
    _yConstraintForAutoresizingMask: (NSAutoresizingMaskOptions)autoresizingMask
                            subitem: (NSView *)subItem
                              frame: (NSRect)frame
                          superitem: (NSView *)superItem
                             bounds: (NSRect)bounds
{
  if ((autoresizingMask & NSViewMinYMargin)
      && (autoresizingMask & NSViewMaxYMargin))
    {
      CGFloat height = bounds.size.height - frame.size.height;
      CGFloat y = frame.origin.y - bounds.origin.y;
      CGFloat yHeightRatio = y / height;
      CGFloat constant = yHeightRatio * frame.size.height * -1;

      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: minYAttribute
                   relatedBy: NSLayoutRelationEqual
                      toItem: superItem
                   attribute: NSLayoutAttributeHeight
                  multiplier: yHeightRatio
                    constant: constant];
    }
  else if (autoresizingMask & NSViewMinYMargin)
    {
      CGFloat constant = ((frame.size.height + frame.origin.y)
                          - (bounds.size.height + bounds.origin.y))
                         * -1;
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: superItem
                   attribute: NSLayoutAttributeHeight
                   relatedBy: NSLayoutRelationEqual
                      toItem: subItem
                   attribute: maxYAttribute
                  multiplier: 1.0
                    constant: constant];
    }
  else if (autoresizingMask & NSViewMaxYMargin)
    {
      CGFloat constant = (frame.origin.y - bounds.origin.y);
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: minYAttribute
                   relatedBy: NSLayoutRelationEqual
                      toItem: nil
                   attribute: NSLayoutAttributeNotAnAttribute
                  multiplier: 1.0
                    constant: constant];
    }
  else
    {
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: minYAttribute
                   relatedBy: NSLayoutRelationEqual
                      toItem: nil
                   attribute: NSLayoutAttributeNotAnAttribute
                  multiplier: 1.0
                    constant: frame.origin.y - bounds.origin.y];
    }
}

+ (NSAutoresizingMaskLayoutConstraint *)
    _widthConstraintForAutoresizingMask:
        (NSAutoresizingMaskOptions)autoresizingMask
                                subitem: (NSView *)subItem
                                  frame: (NSRect)frame
                              superitem: (NSView *)superItem
                                 bounds: (NSRect)bounds
{
  if (autoresizingMask & NSViewWidthSizable)
    {
      CGFloat widthConstant = ((frame.size.width + frame.origin.x)
                               - (bounds.origin.x + bounds.size.width))
                              * -1;
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: superItem
                   attribute: NSLayoutAttributeWidth
                   relatedBy: NSLayoutRelationEqual
                      toItem: subItem
                   attribute: maxXAttribute
                  multiplier: 1.0
                    constant: widthConstant];
    }
  else
    {
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: NSLayoutAttributeWidth
                   relatedBy: NSLayoutRelationEqual
                      toItem: nil
                   attribute: NSLayoutAttributeNotAnAttribute
                  multiplier: 1.0
                    constant: frame.size.width];
    }
}

+ (NSAutoresizingMaskLayoutConstraint *)
    _heightConstraintForAutoresizingMask:
        (NSAutoresizingMaskOptions)autoresizingMask
                                 subitem: (NSView *)subItem
                                   frame: (NSRect)frame
                               superitem: (NSView *)superItem
                                  bounds: (NSRect)bounds
{
  if (autoresizingMask & NSViewHeightSizable)
    {
      CGFloat heightConstant = (frame.size.height + frame.origin.y
                                - (bounds.origin.y + bounds.size.height))
                               * -1;
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: superItem
                   attribute: NSLayoutAttributeHeight
                   relatedBy: NSLayoutRelationEqual
                      toItem: subItem
                   attribute: maxYAttribute
                  multiplier: 1.0
                    constant: heightConstant];
    }
  else
    {
      return [NSAutoresizingMaskLayoutConstraint
          constraintWithItem: subItem
                   attribute: NSLayoutAttributeHeight
                   relatedBy: NSLayoutRelationEqual
                      toItem: nil
                   attribute: NSLayoutAttributeNotAnAttribute
                  multiplier: 1.0
                    constant: frame.size.height];
    }
}

+ (NSArray *)constraintsWithAutoresizingMask:
                 (NSAutoresizingMaskOptions) autoresizingMask
                                     subitem: (NSView *) subItem
                                       frame: (NSRect) frame
                                   superitem: (NSView *) superItem
                                      bounds: (NSRect) bounds
{
  if (autoresizingMask == NSViewNotSizable)
    {
      return [NSArray array];
    }

  NSAutoresizingMaskLayoutConstraint *xConstraint =
      [self _xConstraintForAutoresizingMask: autoresizingMask
                                    subitem: subItem
                                      frame: frame
                                  superitem: superItem
                                     bounds: bounds];
  NSAutoresizingMaskLayoutConstraint *yConstraint =
      [self _yConstraintForAutoresizingMask: autoresizingMask
                                    subitem: subItem
                                      frame: frame
                                  superitem: superItem
                                     bounds: bounds];
  NSAutoresizingMaskLayoutConstraint *widthConstraint =
      [self _widthConstraintForAutoresizingMask: autoresizingMask
                                        subitem: subItem
                                          frame: frame
                                      superitem: superItem
                                         bounds: bounds];
  NSAutoresizingMaskLayoutConstraint *heightConstraint =
      [self _heightConstraintForAutoresizingMask: autoresizingMask
                                         subitem: subItem
                                           frame: frame
                                       superitem: superItem
                                          bounds: bounds];

  NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:4];

  // The order of constraints was determined from MacOS behaviour.
  // For the majority of users it will not matter, but we should assume that users have coupled to the ordering.
  if ((autoresizingMask & NSViewMinXMargin)
      && (autoresizingMask & NSViewMaxXMargin))
    {
      [constraints addObject: widthConstraint];
      [constraints addObject: xConstraint];
    }
  else
    {
      [constraints addObject: xConstraint];
      [constraints addObject: widthConstraint];
    }

  if ((autoresizingMask & NSViewMinYMargin)
      && (autoresizingMask & NSViewMaxYMargin))
    {
      [constraints addObject: heightConstraint];
      [constraints addObject: yConstraint];
    }
  else
    {
      [constraints addObject: yConstraint];
      [constraints addObject: heightConstraint];
    }

  return constraints;
}

@end