-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackViewController.py
409 lines (344 loc) · 14.9 KB
/
stackViewController.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''
note: Storyboard 実装なし
todo:
- 時々落ちる、稀に無限クラッシュ
- 不要な呼び出しを整理
- `viewDidLoad` 肥大化問題
'''
import ctypes
from pyrubicon.objc.api import ObjCClass, ObjCInstance, Block
from pyrubicon.objc.api import objc_method
from pyrubicon.objc.runtime import send_super, objc_id, SEL
from pyrubicon.objc.types import CGRect, CGRectMake, CGSizeMake, NSZeroPoint, UIEdgeInsetsMake, NSTimeInterval
from rbedge.enumerations import (
UILayoutConstraintAxis,
NSTextAlignment,
UITextBorderStyle,
UIButtonType,
UIControlState,
UIControlEvents,
NSLineBreakMode,
UIStackViewAlignment,
UIViewAnimationCurve,
)
from rbedge.globalVariables import (
UIFontTextStyle, )
from rbedge.functions import arc4random_uniform
from rbedge import pdbr
from pyLocalizedString import localizedString
UIViewController = ObjCClass('UIViewController')
NSLayoutConstraint = ObjCClass('NSLayoutConstraint')
UIStackView = ObjCClass('UIStackView')
UILabel = ObjCClass('UILabel')
UIFont = ObjCClass('UIFont')
UITextField = ObjCClass('UITextField')
UIButton = ObjCClass('UIButton')
UIButtonConfiguration = ObjCClass('UIButtonConfiguration')
UIImage = ObjCClass('UIImage')
UIColor = ObjCClass('UIColor')
UIViewPropertyAnimator = ObjCClass('UIViewPropertyAnimator')
UIView = ObjCClass('UIView')
class StackViewController(UIViewController):
@objc_method
def dealloc(self):
# xxx: 呼ばない-> `send_super(__class__, self, 'dealloc')`
#print('\tdealloc')
pass
# MARK: - View Life Cycle
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad')
self.navigationItem.title = localizedString('StackViewsTitle') if (
title := self.navigationItem.title) is None else title
self.view.backgroundColor = UIColor.systemBackgroundColor()
# xxx: あとで、`setup` 的なのを作る?
# --- Symbols
plusSymbol = UIImage.systemImageNamed('plus')
minusSymbol = UIImage.systemImageNamed('minus')
touchUpInside = UIControlEvents.touchUpInside
# --- showingHidingStackView
showingHidingStackView = UIStackView.alloc()
# --- --- showingHidingLabel
showingHidingLabel = UILabel.new()
showingHidingLabel.text = 'Showing/hiding views'
showingHidingLabel.textAlignment = NSTextAlignment.center
showingHidingLabel.setFont_(
UIFont.preferredFontForTextStyle_(UIFontTextStyle.headline))
# --- --- / detailStackView
detailStackView = UIStackView.alloc()
# --- --- ---- detailLabel
detailLabel = UILabel.new()
detailLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.horizontal)
detailLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.vertical)
detailLabel.text = 'Detail'
detailLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail
detailLabel.setFont_(
UIFont.preferredFontForTextStyle_(UIFontTextStyle.body))
# --- --- ---- detailTextField
detailTextField = UITextField.new()
detailTextField.setContentHuggingPriority_forAxis_(
249.0, UILayoutConstraintAxis.horizontal)
detailTextField.borderStyle = UITextBorderStyle.roundedRect
detailTextField.setFont_(UIFont.systemFontOfSize_(14.0))
detailTextField.font.systemMinimumFontSize = 17.0
# --- --- ---- detailPlusButton
detailPlusButton = UIButton.buttonWithType_(UIButtonType.system)
detailPlusButton.setImage_forState_(plusSymbol, UIControlState.normal)
detailPlusButton.contentEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0)
detailPlusButton.addTarget_action_forControlEvents_(
self, SEL('showFurtherDetail:'), touchUpInside)
# --- --- arrangedSubviews
detailStackView.initWithArrangedSubviews_([
detailLabel,
detailTextField,
detailPlusButton,
])
detailStackView.spacing = 10.0
# --- --- detailStackView /
# --- --- / furtherStackView
furtherStackView = UIStackView.alloc()
# --- --- --- furtherlLabel
furtherlLabel = UILabel.new()
furtherlLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.horizontal)
furtherlLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.vertical)
furtherlLabel.text = 'Further Detail'
furtherlLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail
furtherlLabel.setFont_(
UIFont.preferredFontForTextStyle_(UIFontTextStyle.body))
# --- --- --- furtherTextField
furtherTextField = UITextField.new()
furtherTextField.setContentHuggingPriority_forAxis_(
249.0, UILayoutConstraintAxis.horizontal)
furtherTextField.borderStyle = UITextBorderStyle.roundedRect
furtherTextField.setFont_(UIFont.systemFontOfSize_(14.0))
furtherTextField.font.systemMinimumFontSize = 17.0
# --- --- --- furtherMinusButton
furtherMinusButton = UIButton.buttonWithType_(UIButtonType.system)
furtherMinusButton.setImage_forState_(minusSymbol, UIControlState.normal)
furtherMinusButton.contentEdgeInsets = UIEdgeInsetsMake(
0.0, 10.0, 0.0, 10.0)
furtherMinusButton.addTarget_action_forControlEvents_(
self, SEL('hideFurtherDetail:'), touchUpInside)
# --- --- arrangedSubviews
furtherStackView.initWithArrangedSubviews_([
furtherlLabel,
furtherTextField,
furtherMinusButton,
])
furtherStackView.spacing = 10.0
# --- --- furtherStackView /
footerLabel = UILabel.new()
footerLabel.text = 'Footer Label'
footerLabel.textAlignment = NSTextAlignment.center
footerLabel.setFont_(
UIFont.preferredFontForTextStyle_(UIFontTextStyle.footnote))
# --- --- arrangedSubviews
showingHidingStackView.initWithArrangedSubviews_([
showingHidingLabel,
detailStackView,
furtherStackView,
footerLabel,
])
showingHidingStackView.axis = UILayoutConstraintAxis.vertical
showingHidingStackView.spacing = 10.0
# --- addRemoveStackView
addRemoveStackView = UIStackView.alloc()
# --- --- ---- addRemoveLabel
addRemoveLabel = UILabel.new()
addRemoveLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.horizontal)
addRemoveLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.vertical)
addRemoveLabel.text = 'Add/remove views'
addRemoveLabel.textAlignment = NSTextAlignment.center
addRemoveLabel.setFont_(
UIFont.preferredFontForTextStyle_(UIFontTextStyle.headline))
# --- --- ---- addbutton
addbutton = UIButton.buttonWithType_(UIButtonType.system)
addbutton.setImage_forState_(plusSymbol, UIControlState.normal)
addbutton.setContentHuggingPriority_forAxis_(
252.0, UILayoutConstraintAxis.horizontal)
addbutton.contentEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)
addbutton.addTarget_action_forControlEvents_(
self, SEL('addArrangedSubviewToStack:'), touchUpInside)
# --- --- ---- removebutton
removebutton = UIButton.buttonWithType_(UIButtonType.system)
removebutton.setImage_forState_(minusSymbol, UIControlState.normal)
removebutton.setContentHuggingPriority_forAxis_(
253.0, UILayoutConstraintAxis.horizontal)
removebutton.contentEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)
removebutton.addTarget_action_forControlEvents_(
self, SEL('removeArrangedSubviewFromStack:'), touchUpInside)
# --- --- arrangedSubviews
addRemoveStackView.initWithArrangedSubviews_([
addRemoveLabel,
addbutton,
removebutton,
])
# --- addRemoveExampleStackView
addRemoveExampleStackView = UIStackView.new()
addRemoveExampleStackView.axis = UILayoutConstraintAxis.vertical
addRemoveExampleStackView.alignment = UIStackViewAlignment.center
addRemoveExampleStackView.spacing = 10.0
# --- Layout
self.view.addSubview_(showingHidingStackView)
self.view.addSubview_(addRemoveStackView)
self.view.addSubview_(addRemoveExampleStackView)
showingHidingStackView.translatesAutoresizingMaskIntoConstraints = False
addRemoveStackView.translatesAutoresizingMaskIntoConstraints = False
addRemoveExampleStackView.translatesAutoresizingMaskIntoConstraints = False
layoutMarginsGuide = self.view.layoutMarginsGuide
safeAreaLayoutGuide = self.view.safeAreaLayoutGuide
# --- showingHidingStackView
NSLayoutConstraint.activateConstraints_([
showingHidingStackView.leadingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.leadingAnchor),
showingHidingStackView.trailingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.trailingAnchor),
showingHidingStackView.topAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.topAnchor, 8.0),
])
# todo: `detailLabel` と`furtherlLabel` の幅を連動
NSLayoutConstraint.activateConstraints_([
detailLabel.widthAnchor.constraintEqualToAnchor_(
furtherlLabel.widthAnchor),
])
# --- addRemoveStackView
NSLayoutConstraint.activateConstraints_([
addRemoveStackView.topAnchor.constraintEqualToAnchor_constant_(
showingHidingStackView.bottomAnchor, 20.0),
addRemoveStackView.leadingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.leadingAnchor),
addRemoveStackView.trailingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.trailingAnchor),
])
# --- addRemoveExampleStackView
NSLayoutConstraint.activateConstraints_([
addRemoveExampleStackView.topAnchor.constraintEqualToAnchor_constant_(
addRemoveStackView.bottomAnchor, 8.0),
addRemoveExampleStackView.leadingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.leadingAnchor),
addRemoveExampleStackView.trailingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.trailingAnchor),
#addRemoveExampleStackView.heightAnchor.constraintEqualToConstant_(42.0), # xxx: `placeholder="YES"` ?
])
self.plusButton = detailPlusButton
self.furtherDetailStackView = furtherStackView
self.addRemoveExampleStackView = addRemoveExampleStackView
self.addArrangedViewButton = addbutton
self.removeArrangedViewButton = removebutton
self.maximumArrangedSubviewCount = 3
# MARK: - View Life Cycle
@objc_method
def viewWillAppear_(self, animated: bool):
send_super(__class__,
self,
'viewWillAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
self.furtherDetailStackView.setHidden_(True)
self.plusButton.setHidden_(False)
self.updateAddRemoveButtons()
@objc_method
def viewDidAppear_(self, animated: bool):
send_super(__class__,
self,
'viewDidAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print('viewDidAppear')
@objc_method
def viewDidDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewDidDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print('viewDidDisappear')
@objc_method
def didReceiveMemoryWarning(self):
send_super(__class__, self, 'didReceiveMemoryWarning')
print(f'{__class__}: didReceiveMemoryWarning')
# MARK: - Actions
@objc_method
def showFurtherDetail_(self, _):
# Animate the changes by performing them in a `UIViewPropertyAnimator` animation block.
@Block
def animationsBlock() -> None:
# Reveal the further details stack view and hide the plus button.
self.furtherDetailStackView.setHidden_(False)
self.plusButton.setHidden_(True)
showDetailAnimator = UIViewPropertyAnimator.alloc(
).initWithDuration_curve_animations_(NSTimeInterval(0.25),
UIViewAnimationCurve.easeIn,
animationsBlock)
showDetailAnimator.startAnimation()
@objc_method
def hideFurtherDetail_(self, _):
# Animate the changes by performing them in a `UIViewPropertyAnimator` animation block.
@Block
def animationsBlock() -> None:
# Reveal the further details stack view and hide the plus button.
self.furtherDetailStackView.setHidden_(True)
self.plusButton.setHidden_(False)
hideDetailAnimator = UIViewPropertyAnimator.alloc(
).initWithDuration_curve_animations_(NSTimeInterval(0.25),
UIViewAnimationCurve.easeOut,
animationsBlock)
hideDetailAnimator.startAnimation()
@objc_method
def addArrangedSubviewToStack_(self, _):
# Create a simple, fixed-size, square view to add to the stack view.
newViewSize = CGSizeMake(38.0, 38.0)
newView = UIView.alloc().initWithFrame_(CGRect(NSZeroPoint, newViewSize))
newView.backgroundColor = self.randomColor()
NSLayoutConstraint.activateConstraints_([
newView.widthAnchor.constraintEqualToConstant_(newViewSize.width),
newView.heightAnchor.constraintEqualToConstant_(newViewSize.height),
])
# Adding an arranged subview automatically adds it as a child of the stack view.
self.addRemoveExampleStackView.addArrangedSubview_(newView)
self.updateAddRemoveButtons()
@objc_method
def removeArrangedSubviewFromStack_(self, _):
# Make sure there is an arranged view to remove.
if (viewToRemove :=
self.addRemoveExampleStackView.arrangedSubviews.lastObject()) is None:
return
self.addRemoveExampleStackView.removeArrangedSubview_(viewToRemove)
# Calling `removeArrangedSubview` does not remove the provided view from the stack view's `subviews` array. Since we no longer want the view we removed to appear, we have to explicitly remove it from its superview.
# `removeArrangedSubview` を呼び出しても、スタック ビューの `subviews` 配列から指定されたビューは削除されません。削除したビューを表示したくないので、スーパービューから明示的に削除する必要があります。
viewToRemove.removeFromSuperview()
self.updateAddRemoveButtons()
# MARK: - Convenience
@objc_method
def updateAddRemoveButtons(self):
arrangedSubviewCount = len(self.addRemoveExampleStackView.arrangedSubviews)
self.addArrangedViewButton.setEnabled_(
arrangedSubviewCount < self.maximumArrangedSubviewCount)
self.removeArrangedViewButton.setEnabled_(arrangedSubviewCount > 0)
@objc_method
def randomColor(self):
red = arc4random_uniform(255) / 255.0
green = arc4random_uniform(255) / 255.0
blue = arc4random_uniform(255) / 255.0
return UIColor.colorWithRed_green_blue_alpha_(red, green, blue, 1.0)
if __name__ == '__main__':
from rbedge.functions import NSStringFromClass
from rbedge.enumerations import UIModalPresentationStyle
from rbedge import present_viewController
main_vc = StackViewController.new()
_title = NSStringFromClass(StackViewController)
main_vc.navigationItem.title = _title
presentation_style = UIModalPresentationStyle.fullScreen
present_viewController(main_vc, presentation_style)