-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonViewController.py
854 lines (694 loc) · 32.6 KB
/
buttonViewController.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
import ctypes
from enum import Enum
from pyrubicon.objc.api import ObjCClass, ObjCInstance, Block
from pyrubicon.objc.api import objc_method, objc_property
from pyrubicon.objc.runtime import send_super, objc_id, SEL
from pyrubicon.objc.types import NSInteger, CGPoint
from rbedge.enumerations import (
UIUserInterfaceIdiom,
UIControlState,
UIControlEvents,
UIButtonConfigurationCornerStyle,
UIImageRenderingMode,
NSUnderlineStyle,
UIImageSymbolScale,
NSDirectionalRectEdge,
UIButtonConfigurationSize,
)
from rbedge.globalVariables import (
NSAttributedStringKey,
UIFontTextStyle,
)
from rbedge.pythonProcessUtils import (
mainScreen_scale,
dataWithContentsOfURL,
)
from rbedge.functions import NSStringFromClass
from rbedge import pdbr
from caseElement import CaseElement
from pyLocalizedString import localizedString
from baseTableViewController import BaseTableViewController
from storyboard.buttonViewController import prototypes
UIButtonConfiguration = ObjCClass('UIButtonConfiguration')
UIColor = ObjCClass('UIColor')
NSAttributedString = ObjCClass('NSAttributedString')
UIImageSymbolConfiguration = ObjCClass('UIImageSymbolConfiguration')
UIFont = ObjCClass('UIFont')
UIImage = ObjCClass('UIImage')
NSDictionary = ObjCClass('NSDictionary')
UIToolTipConfiguration = ObjCClass('UIToolTipConfiguration')
UIAction = ObjCClass('UIAction')
UIButton = ObjCClass('UIButton') # todo: 型確認用
class ButtonKind(Enum):
buttonSystem = 'buttonSystem'
buttonDetailDisclosure = 'buttonDetailDisclosure'
buttonSystemAddContact = 'buttonSystemAddContact'
buttonClose = 'buttonClose'
buttonStyleGray = 'buttonStyleGray'
buttonStyleTinted = 'buttonStyleTinted'
buttonStyleFilled = 'buttonStyleFilled'
buttonCornerStyle = 'buttonCornerStyle'
buttonToggle = 'buttonToggle'
buttonTitleColor = 'buttonTitleColor'
buttonImage = 'buttonImage'
buttonAttrText = 'buttonAttrText'
buttonSymbol = 'buttonSymbol'
buttonLargeSymbol = 'buttonLargeSymbol'
buttonTextSymbol = 'buttonTextSymbol'
buttonSymbolText = 'buttonSymbolText'
buttonMultiTitle = 'buttonMultiTitle'
buttonBackground = 'buttonBackground'
addToCartButton = 'addToCartButton'
buttonUpdateActivityHandler = 'buttonUpdateActivityHandler'
buttonUpdateHandler = 'buttonUpdateHandler'
buttonImageUpdateHandler = 'buttonImageUpdateHandler'
class ButtonViewController(BaseTableViewController):
cartItemCount: NSInteger = objc_property()
@objc_method
def dealloc(self):
# xxx: 呼ばない-> `send_super(__class__, self, 'dealloc')`
print(f'\t- {NSStringFromClass(__class__)}: dealloc')
@objc_method
def initWithStyle_(self, style: NSInteger) -> ObjCInstance:
send_super(__class__,
self,
'initWithStyle:',
style,
restype=objc_id,
argtypes=[
NSInteger,
])
#print(f'\t{NSStringFromClass(__class__)}: initWithStyle_')
return self
@objc_method
def loadView(self):
send_super(__class__, self, 'loadView')
#print(f'\t{NSStringFromClass(__class__)}: loadView')
[
self.tableView.registerClass_forCellReuseIdentifier_(
prototype['cellClass'], prototype['identifier'])
for prototype in prototypes
]
self.cartItemCount = 0
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad')
# --- Navigation
self.navigationItem.title = localizedString('ButtonsTitle') if (
title := self.navigationItem.title) is None else title
self.testCellsAppendContentsOf_([
# 00
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('DefaultTitle'), ButtonKind.buttonSystem.value,
'configureSystemTextButton:'),
# 01
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('DetailDisclosureTitle'),
ButtonKind.buttonDetailDisclosure.value,
'configureSystemDetailDisclosureButton:'),
# 02
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('AddContactTitle'),
ButtonKind.buttonSystemAddContact.value,
'configureSystemContactAddButton:'),
# 03
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('CloseTitle'), ButtonKind.buttonClose.value,
'configureCloseButton:'),
])
if True: # xxx: `#available(iOS 15, *)`
# These button styles are available on iOS 15 or later.
self.testCellsAppendContentsOf_([
# 04
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('GrayTitle'), ButtonKind.buttonStyleGray.value,
'configureStyleGrayButton:'),
# 05
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('TintedTitle'), ButtonKind.buttonStyleTinted.value,
'configureStyleTintedButton:'),
# 06
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('FilledTitle'), ButtonKind.buttonStyleFilled.value,
'configureStyleFilledButton:'),
# 07
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('CornerStyleTitle'),
ButtonKind.buttonCornerStyle.value, 'configureCornerStyleButton:'),
# 15
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('ToggleTitle'), ButtonKind.buttonToggle.value,
'configureToggleButton:'),
])
if True: # xxx: `traitCollection.userInterfaceIdiom != .mac`
self.testCellsAppendContentsOf_([
# 16
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('ButtonColorTitle'),
ButtonKind.buttonTitleColor.value, 'configureTitleTextButton:'),
])
self.testCellsAppendContentsOf_([
# 08
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('ImageTitle'), ButtonKind.buttonImage.value,
'configureImageButton:'),
# 09
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('AttributedStringTitle'),
ButtonKind.buttonAttrText.value,
'configureAttributedTextSystemButton:'),
# 10
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('SymbolTitle'), ButtonKind.buttonSymbol.value,
'configureSymbolButton:'),
])
if True: # xxx: `#available(iOS 15, *)`
if self.traitCollection.userInterfaceIdiom != UIUserInterfaceIdiom.mac:
self.testCellsAppendContentsOf_([
# 11
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('LargeSymbolTitle'),
ButtonKind.buttonLargeSymbol.value, 'configureLargeSymbolButton:'),
])
if True: # xxx: `#available(iOS 15, *)`
self.testCellsAppendContentsOf_([
# 12
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('SymbolStringTitle'),
ButtonKind.buttonSymbolText.value, 'configureSymbolTextButton:'),
# 13
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('StringSymbolTitle'),
ButtonKind.buttonTextSymbol.value, 'configureTextSymbolButton:'),
# 17
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('BackgroundTitle'),
ButtonKind.buttonBackground.value, 'configureBackgroundButton:'),
# 14
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('MultiTitleTitle'),
ButtonKind.buttonMultiTitle.value, 'configureMultiTitleButton:'),
# 21
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('AddToCartTitle'), ButtonKind.addToCartButton.value,
'configureAddToCartButton:'),
# 18
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('UpdateActivityHandlerTitle'),
ButtonKind.buttonUpdateActivityHandler.value,
'configureUpdateActivityHandlerButton:'),
# 19
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('UpdateHandlerTitle'),
ButtonKind.buttonUpdateHandler.value,
'configureUpdateHandlerButton:'),
# 20
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('UpdateImageHandlerTitle'),
ButtonKind.buttonImageUpdateHandler.value,
'configureUpdateImageHandlerButton:'),
])
@objc_method
def viewWillAppear_(self, animated: bool):
send_super(__class__,
self,
'viewWillAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewWillAppear_')
@objc_method
def viewDidAppear_(self, animated: bool):
send_super(__class__,
self,
'viewDidAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewDidAppear_')
@objc_method
def viewWillDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewWillDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
# print(f'\t{NSStringFromClass(__class__)}: viewWillDisappear_')
@objc_method
def viewDidDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewDidDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
print(f'\t{NSStringFromClass(__class__)}: viewDidDisappear_')
@objc_method
def didReceiveMemoryWarning(self):
send_super(__class__, self, 'didReceiveMemoryWarning')
print(f'{__class__}: didReceiveMemoryWarning')
# --- extension
# xxx: extension 別にしたい
# 00
@objc_method
def configureSystemTextButton_(self, button):
# Nothing particular to set here, it's all been done in the storyboard.
# > ここでは特に設定するものはなく、すべてストーリーボードで行われます。
# todo: 冗長、差し替えを容易にしたい為
title = localizedString('Button')
state = UIControlState.normal
button.setTitle_forState_(title, state)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 01
@objc_method
def configureSystemDetailDisclosureButton_(self, button):
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 02
@objc_method
def configureSystemContactAddButton_(self, button):
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 03
@objc_method
def configureCloseButton_(self, button):
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 04
# todo: `@available(iOS 15.0, *)`
# xxx: あとでやる
@objc_method
def configureStyleGrayButton_(self, button):
config = UIButtonConfiguration.grayButtonConfiguration()
button.configuration = config
title = localizedString('Button')
state = UIControlState.normal
button.setTitle_forState_(title, state)
# xxx: `toolTip` 挙動未確認
button.toolTip = localizedString('GrayStyleButtonToolTipTitle')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 05
# todo: `@available(iOS 15.0, *)`
@objc_method
def configureStyleTintedButton_(self, button):
config = UIButtonConfiguration.tintedButtonConfiguration()
# todo: `if traitCollection.userInterfaceIdiom == .mac`
# xxx: あとでやる
systemRed = UIColor.systemRedColor()
config.baseBackgroundColor = systemRed
config.baseForegroundColor = systemRed
button.configuration = config
title = localizedString('Button')
state = UIControlState.normal
button.setTitle_forState_(title, state)
button.toolTip = localizedString('TintedStyleButtonToolTipTitle')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 06
# todo: `@available(iOS 15.0, *)`
@objc_method
def configureStyleFilledButton_(self, button):
config = UIButtonConfiguration.filledButtonConfiguration()
systemRed = UIColor.systemRedColor()
config.background.backgroundColor = systemRed
button.configuration = config
title = localizedString('Button')
state = UIControlState.normal
button.setTitle_forState_(title, state)
button.toolTip = localizedString('FilledStyleButtonToolTipTitle')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 07
# todo: `@available(iOS 15.0, *)`
@objc_method
def configureCornerStyleButton_(self, button):
# To keep the look the same betwen iOS and macOS:
# For cornerStyle to work in Mac Catalyst, use UIBehavioralStyle as ".pad", Available in macOS 12 or later (Mac Catalyst 15.0 or later). Use this for controls that need to look the same between iOS and macOS.
# > iOS と macOS の間で外観を同じにするには: CornerStyle を Mac Catalyst で機能させるには、UIBehavioralStyle を「.pad」として使用します。macOS 12 以降 (Mac Catalyst 15.0 以降) で使用できます。 iOS と macOS の間で同じように見える必要があるコントロールにこれを使用します。
config = UIButtonConfiguration.grayButtonConfiguration()
# todo: `if traitCollection.userInterfaceIdiom == .mac`
# xxx: あとでやる
cornerStyle = UIButtonConfigurationCornerStyle.capsule
config.cornerStyle = cornerStyle
button.configuration = config
title = localizedString('Button')
state = UIControlState.normal
button.setTitle_forState_(title, state)
button.toolTip = localizedString('CapsuleStyleButtonToolTipTitle')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 08
@objc_method
def configureImageButton_(self, button):
_systemImageNamed = UIImage.systemImageNamed('xmark')
systemPurple = UIColor.systemPurpleColor()
renderingMode = UIImageRenderingMode.alwaysOriginal
# ref: [swift - iOS 13 `withTintColor` not obeying the color I assign - Stack Overflow](https://stackoverflow.com/questions/58867627/ios-13-withtintcolor-not-obeying-the-color-i-assign)
image = _systemImageNamed.imageWithTintColor_(
systemPurple).imageWithRenderingMode_(renderingMode)
button.accessibilityLabel = localizedString('X')
state = UIControlState.normal
button.setImage_forState_(image, state)
# todo: `@available(iOS 15.0, *)`
button.toolTip = localizedString('XButtonToolTipTitle')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 09
# todo: `@available(iOS 15.0, *)`
@objc_method
def configureAttributedTextSystemButton_(self, button):
buttonTitle = localizedString('Button')
# Set the button's title for normal state.
# > 通常状態のボタンのタイトルを設定します。
normalTitleAttributes = NSDictionary.dictionaryWithObjects_forKeys_([
NSUnderlineStyle.single,
], [
NSAttributedStringKey.strikethroughStyle,
])
normalAttributedTitle = NSAttributedString.alloc(
).initWithString_attributes_(buttonTitle, normalTitleAttributes)
normal = UIControlState.normal
button.setAttributedTitle_forState_(normalAttributedTitle, normal)
# Set the button's title for highlighted state (note this is not supported in Mac Catalyst).
# > ボタンのタイトルを強調表示状態に設定します (これは Mac Catalyst ではサポートされていないことに注意してください)。
highlightedTitleAttributes = NSDictionary.dictionaryWithObjects_forKeys_([
UIColor.systemGreenColor(),
NSUnderlineStyle.thick,
], [
NSAttributedStringKey.foregroundColor,
NSAttributedStringKey.strikethroughStyle
])
highlightedAttributedTitle = NSAttributedString.alloc(
).initWithString_attributes_(buttonTitle, highlightedTitleAttributes)
highlighted = UIControlState.highlighted
button.setAttributedTitle_forState_(highlightedAttributedTitle,
highlighted)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 10
@objc_method
def configureSymbolButton_(self, button):
buttonImage = UIImage.systemImageNamed('person')
if True: # xxx: `available(iOS 15, *)`
# For iOS 15 use the UIButtonConfiguration to set the image.
# iOS 15 の場合は、UIButtonConfiguration を使用して画像を設定します。
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.image = buttonImage
button.configuration = buttonConfig
button.toolTip = localizedString('PersonButtonToolTipTitle')
else:
button.setImage_forState_(buttonImage, UIControlState.normal)
config = UIImageSymbolConfiguration.configurationWithTextStyle_scale_(
UIFontTextStyle.body, UIImageSymbolScale.large)
button.setPreferredSymbolConfiguration_forImageInState_(
config, UIControlState.normal)
button.accessibilityLabel = localizedString('Person')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 11
@objc_method
def configureLargeSymbolButton_(self, button):
buttonImage = UIImage.systemImageNamed('person')
if True: # xxx: `available(iOS 15, *)`
# For iOS 15 use the UIButtonConfiguration to set the image.
# iOS 15 の場合は、UIButtonConfiguration を使用して画像を設定します。
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.preferredSymbolConfigurationForImage = UIImageSymbolConfiguration.configurationWithTextStyle_(
UIFontTextStyle.largeTitle)
buttonConfig.image = buttonImage
button.configuration = buttonConfig
else:
button.setImage_forState_(buttonImage, UIControlState.normal)
button.accessibilityLabel = localizedString('Person')
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 12
@objc_method
def configureSymbolTextButton_(self, button):
# Button with image to the left of the title.
# > タイトルの左側にある画像付きのボタン。
buttonImage = UIImage.systemImageNamed('person')
if True: # xxx: `available(iOS 15, *)`
# For iOS 15 use the UIButtonConfiguration to set the image.
# iOS 15 の場合は、UIButtonConfiguration を使用して画像を設定します。
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.preferredSymbolConfigurationForImage = UIImageSymbolConfiguration.configurationWithTextStyle_(
UIFontTextStyle.body)
buttonConfig.image = buttonImage
button.configuration = buttonConfig
else:
button.setImage_forState_(buttonImage, UIControlState.normal)
config = UIImageSymbolConfiguration.configurationWithTextStyle_scale_(
UIFontTextStyle.body, UIImageSymbolScale.small)
button.setPreferredSymbolConfiguration_forImageInState_(
config, UIControlState.normal)
button.setTitle_forState_(localizedString('Person'), UIControlState.normal)
button.titleLabel.font = UIFont.preferredFontForTextStyle_(
UIFontTextStyle.body)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 13
@objc_method
def configureTextSymbolButton_(self, button):
# Button with image to the right of the title.
# > タイトルの右側にある画像付きのボタン。
buttonImage = UIImage.systemImageNamed('person')
if True: # xxx: `available(iOS 15, *)`
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.preferredSymbolConfigurationForImage = UIImageSymbolConfiguration.configurationWithTextStyle_(
UIFontTextStyle.body)
buttonConfig.image = buttonImage
# if traitCollection.userInterfaceIdiom == .mac
# button.preferredBehavioralStyle = .pad
buttonConfig.imagePlacement = NSDirectionalRectEdge.trailing
button.configuration = buttonConfig
button.setTitle_forState_(localizedString('Person'), UIControlState.normal)
button.titleLabel.font = UIFont.preferredFontForTextStyle_(
UIFontTextStyle.body)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 14
@objc_method
def configureMultiTitleButton_(self, button):
# if traitCollection.userInterfaceIdiom == .mac
# button.preferredBehavioralStyle = .pad
button.setTitle_forState_(localizedString('Button'), UIControlState.normal)
button.setTitle_forState_(localizedString('Person'),
UIControlState.highlighted)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 15
@objc_method
def configureToggleButton_(self, button):
button.changesSelectionAsPrimaryAction = True
# 16
@objc_method
def configureTitleTextButton_(self, button):
# Note: Only for iOS the title's color can be changed.
# 注: タイトルの色を変更できるのは iOS の場合のみです。
button.setTitleColor_forState_(UIColor.systemGreenColor(),
UIControlState.normal)
button.setTitleColor_forState_(UIColor.systemRedColor(),
UIControlState.highlighted)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 17
@objc_method
def configureBackgroundButton_(self, button):
if True: # xxx: `available(iOS 15, *)`
# if traitCollection.userInterfaceIdiom == .mac
# button.preferredBehavioralStyle = .pad
pass
scale = int(mainScreen_scale)
normal_str = f'./UIKitCatalogCreatingAndCustomizingViewsAndControls/UIKitCatalog/Assets.xcassets/background.imageset/stepper_and_segment_background_{scale}x.png'
highlighted_str = f'./UIKitCatalogCreatingAndCustomizingViewsAndControls/UIKitCatalog/Assets.xcassets/background_highlighted.imageset/stepper_and_segment_background_highlighted_{scale}x.png'
disabled_str = f'./UIKitCatalogCreatingAndCustomizingViewsAndControls/UIKitCatalog/Assets.xcassets/background_disabled.imageset/stepper_and_segment_background_disabled_{scale}x.png'
background = UIImage.alloc().initWithData_scale_(
dataWithContentsOfURL(normal_str), scale)
background_highlighted = UIImage.alloc().initWithData_scale_(
dataWithContentsOfURL(highlighted_str), scale)
background_disabled = UIImage.alloc().initWithData_scale_(
dataWithContentsOfURL(disabled_str), scale)
button.setBackgroundImage_forState_(background, UIControlState.normal)
button.setBackgroundImage_forState_(background_highlighted,
UIControlState.highlighted)
button.setBackgroundImage_forState_(background_disabled,
UIControlState.disabled)
button.addTarget_action_forControlEvents_(self, SEL('buttonClicked:'),
UIControlEvents.touchUpInside)
# 18
# This handler is called when this button needs updating.
# このハンドラーは、このボタンを更新する必要がある場合に呼び出されます。
@objc_method
def configureUpdateActivityHandlerButton_(self, button):
@Block
def activityUpdateHandler(button_id: objc_id) -> None:
_button = ObjCInstance(button_id)
config = _button.configuration
config.showsActivityIndicator = False if _button.isSelected() else True
_button.configuration = config
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.image = UIImage.systemImageNamed('tray')
buttonConfig.preferredSymbolConfigurationForImage = UIImageSymbolConfiguration.configurationWithTextStyle_(
UIFontTextStyle.body)
button.configuration = buttonConfig
button.setTitle_forState_(localizedString('Button'), UIControlState.normal)
button.titleLabel.font = UIFont.preferredFontForTextStyle_(
UIFontTextStyle.body)
button.changesSelectionAsPrimaryAction = True
button.configurationUpdateHandler = activityUpdateHandler
# if traitCollection.userInterfaceIdiom == .mac
# button.preferredBehavioralStyle = .pad
button.addTarget_action_forControlEvents_(self,
SEL('toggleButtonClicked:'),
UIControlEvents.touchUpInside)
# 19
@objc_method
def configureUpdateHandlerButton_(self, button):
# This is called when a button needs an update.
# > これは、ボタンを更新する必要がある場合に呼び出されます。
@Block
def colorUpdateHandler(button_id: objc_id) -> None:
_button = ObjCInstance(button_id)
_title = _button.configuration.title
_systemPinkColor = UIColor.systemPinkColor()
baseBackgroundColor = _systemPinkColor.colorWithAlphaComponent_(
0.4) if _button.isSelected() else _systemPinkColor
# xxx: `button.configuration?.baseBackgroundColor` を直接呼んでも変化しないので再定義している
buttonConfig = UIButtonConfiguration.filledButtonConfiguration()
buttonConfig.title = _title
buttonConfig.baseBackgroundColor = baseBackgroundColor
_button.configuration = buttonConfig
buttonConfig = UIButtonConfiguration.filledButtonConfiguration()
button.configuration = buttonConfig
button.changesSelectionAsPrimaryAction = True
button.configurationUpdateHandler = colorUpdateHandler
# if traitCollection.userInterfaceIdiom == .mac
# button.preferredBehavioralStyle = .pad
button.addTarget_action_forControlEvents_(self,
SEL('toggleButtonClicked:'),
UIControlEvents.touchUpInside)
# 20
@objc_method
def configureUpdateImageHandlerButton_(self, button):
# This is called when a button needs an update.
# > これは、ボタンを更新する必要がある場合に呼び出されます。
@Block
def colorUpdateHandler(button_id: objc_id) -> None:
_button = ObjCInstance(button_id)
image = UIImage.systemImageNamed('cart.fill') if _button.isSelected(
) else UIImage.systemImageNamed('cart')
# xxx: `button.configuration?.image` を直接呼んでも変化しないので再定義している
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.image = image
buttonConfig.preferredSymbolConfigurationForImage = UIImageSymbolConfiguration.configurationWithTextStyle_(
UIFontTextStyle.largeTitle)
_button.configuration = buttonConfig
# xxx: `toolTip` 挙動未確認
_button.toolTip = localizedString(
'CartFilledButtonToolTipTitle') if _button.isSelected(
) else localizedString('CartEmptyButtonToolTipTitle')
buttonConfig = UIButtonConfiguration.plainButtonConfiguration()
buttonConfig.image = UIImage.systemImageNamed('cart')
buttonConfig.preferredSymbolConfigurationForImage = UIImageSymbolConfiguration.configurationWithTextStyle_(
UIFontTextStyle.largeTitle)
button.configuration = buttonConfig
button.changesSelectionAsPrimaryAction = True
button.configurationUpdateHandler = colorUpdateHandler
# if traitCollection.userInterfaceIdiom == .mac
# button.preferredBehavioralStyle = .pad
button.setTitle_forState_(
'', UIControlState.normal) # No title, just an image.
# button.isSelected = True
button.setSelected_(False)
button.addTarget_action_forControlEvents_(self,
SEL('toggleButtonClicked:'),
UIControlEvents.touchUpInside)
# MARK: - Add To Cart Button
# xxx: wip
@objc_method
def toolTipInteraction_configurationAtPoint_(
self, interaction, point: CGPoint) -> ctypes.c_void_p:
return UIToolTipConfiguration.configurationWithToolTip_('hoge').ptr
@objc_method
def addToCart_(self, _action: ctypes.c_void_p) -> None:
action = ObjCInstance(_action)
self.cartItemCount = 0 if self.cartItemCount.intValue > 0 else 12
if action.sender.isKindOfClass_(UIButton):
button = action.sender
button.setNeedsUpdateConfiguration()
# 21
@objc_method
def configureAddToCartButton_(self, button):
config = UIButtonConfiguration.filledButtonConfiguration()
config.buttonSize = UIButtonConfigurationSize.large
config.image = UIImage.systemImageNamed('cart.fill')
config.title = 'Add to Cart'
config.cornerStyle = UIButtonConfigurationCornerStyle.capsule
config.baseBackgroundColor = UIColor.systemTealColor()
button.configuration = config
button.toolTip = '' # The value will be determined in its delegate. > 値はデリゲート内で決定されます。
# xxx: wip
# button.toolTipInteraction.delegate = self
button.addAction_forControlEvents_(
UIAction.actionWithHandler_(Block(self.addToCart_, None,
ctypes.c_void_p)),
UIControlEvents.touchUpInside)
button.changesSelectionAsPrimaryAction = True
@Block
def _handler(button_id: objc_id) -> None:
_button = ObjCInstance(button_id)
# Start with the current button's configuration.
# > 現在のボタンの設定から始めます。
# newConfig = _button.configuration
newConfig = UIButtonConfiguration.filledButtonConfiguration()
newConfig.buttonSize = UIButtonConfigurationSize.large
newConfig.title = 'Add to Cart'
newConfig.cornerStyle = UIButtonConfigurationCornerStyle.capsule
newConfig.baseBackgroundColor = UIColor.systemTealColor()
if _button.isSelected():
# xxx: これだと`0` の時、取れない?
newConfig.image = UIImage.systemImageNamed(
'cart.fill.badge.plus'
) if self.cartItemCount.intValue > 0 else UIImage.systemImageNamed(
'cart.badge.plus')
# xxx: 力技
newConfig.subtitle = f'{self.cartItemCount.intValue}items'
else:
# As the button is highlighted (pressed), apply a temporary image and subtitle.
# > ボタンがハイライト表示される(押される)と、一時的な画像と字幕が適用されます。
newConfig.image = UIImage.systemImageNamed('cart.fill')
newConfig.subtitle = ' ' # xxx: 文字パディング
newConfig.imagePadding = 8
_button.configuration = newConfig
# This handler is called when this button needs updating.
# > このハンドラーは、このボタンを更新する必要がある場合に呼び出されます。
button.configurationUpdateHandler = _handler
# MARK: - Button Actions
@objc_method
def buttonClicked_(self, sender):
print(f'Button was clicked.{sender}')
@objc_method
def toggleButtonClicked_(self, sender):
print(f'Toggle action: {sender}')
if __name__ == '__main__':
from rbedge.app import App
from rbedge.enumerations import (
UITableViewStyle,
UIModalPresentationStyle,
)
table_style = UITableViewStyle.grouped
main_vc = ButtonViewController.alloc().initWithStyle_(table_style)
_title = NSStringFromClass(ButtonViewController)
main_vc.navigationItem.title = _title
# presentation_style = UIModalPresentationStyle.fullScreen
presentation_style = UIModalPresentationStyle.pageSheet
app = App(main_vc, presentation_style)
app.present()