-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickerViewController.py
253 lines (206 loc) · 8.58 KB
/
pickerViewController.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
'''
note: Storyboard 実装なし
'''
import ctypes
from enum import IntEnum, auto
from pyrubicon.objc.api import ObjCClass, ObjCInstance
from pyrubicon.objc.api import objc_method
from pyrubicon.objc.runtime import send_super, objc_id
from rbedge.globalVariables import NSAttributedStringKey
from rbedge import pdbr
from pyLocalizedString import localizedString
UIViewController = ObjCClass('UIViewController')
NSLayoutConstraint = ObjCClass('NSLayoutConstraint')
UIColor = ObjCClass('UIColor')
NSDictionary = ObjCClass('NSDictionary')
UIPickerView = ObjCClass('UIPickerView')
UIView = ObjCClass('UIView')
NSMutableAttributedString = ObjCClass('NSMutableAttributedString')
class RGB:
max: float = 255.0
min: float = 0.0
offset: float = 5.0
class ColorComponent(IntEnum):
red = 0
green = auto()
blue = auto()
class PickerViewController(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('PickerViewTitle') if (
title := self.navigationItem.title) is None else title
self.view.backgroundColor = UIColor.systemBackgroundColor()
# --- pickerView
# todo: 変数名`pickerView` だと、関数名に干渉する
colorSwatchPickerView = UIPickerView.new()
colorSwatchPickerView.dataSource = self
colorSwatchPickerView.delegate = self
# --- colorSwatchView
colorSwatchView = UIView.new()
# --- Layout
safeAreaLayoutGuide = self.view.safeAreaLayoutGuide
layoutMarginsGuide = self.view.layoutMarginsGuide
self.view.addSubview_(colorSwatchPickerView)
colorSwatchPickerView.translatesAutoresizingMaskIntoConstraints = False
self.view.addSubview_(colorSwatchView)
colorSwatchView.translatesAutoresizingMaskIntoConstraints = False
NSLayoutConstraint.activateConstraints_([
colorSwatchPickerView.widthAnchor.constraintEqualToConstant_(375.0),
])
NSLayoutConstraint.activateConstraints_([
colorSwatchView.trailingAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.trailingAnchor, -20.0),
colorSwatchView.topAnchor.constraintEqualToAnchor_constant_(
colorSwatchPickerView.bottomAnchor, 8.0),
colorSwatchView.bottomAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.bottomAnchor, -20.0),
colorSwatchPickerView.centerXAnchor.constraintEqualToAnchor_(
safeAreaLayoutGuide.centerXAnchor),
colorSwatchPickerView.topAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.topAnchor, 13.0),
colorSwatchView.leadingAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.leadingAnchor, 20.0),
])
self.colorSwatchPickerView = colorSwatchPickerView
self.colorSwatchView = colorSwatchView
self.numberOfColorValuesPerComponent = int(RGB.max / RGB.offset) + 1
self.redColor = RGB.min
self.greenColor = RGB.min
self.blueColor = RGB.min
# todo: `pickerView_attributedTitleForRow_forComponent_` 内で定義すると落ちる
self.mutableAttributedString = NSMutableAttributedString.alloc()
self.configurePickerView()
@objc_method
def updateColorSwatchViewBackgroundColor(self):
self.colorSwatchView.backgroundColor = UIColor.colorWithRed_green_blue_alpha_(
self.redColor, self.greenColor, self.blueColor, 1.0)
@objc_method
def configurePickerView(self):
# Set the default selected rows (the desired rows to initially select will vary from app to app).
# デフォルトの選択行を設定します (最初に選択する行はアプリによって異なります)。
selectedRows = {
ColorComponent.red: 13,
ColorComponent.green: 41,
ColorComponent.blue: 24,
}
for colorComponent, selectedRow in selectedRows.items():
"""
Note that the delegate method on `UIPickerViewDelegate` is not triggered
when manually calling `selectRow(_:inComponent:animated:)`. To do
this, we fire off delegate method manually.
"""
"""
`selectRow(_:inComponent:animated:)` を手動で呼び出した場合、`UIPickerViewDelegate` のデリゲート メソッドはトリガーされないことに注意してください。これを行うには、デリゲート メソッドを手動で起動します。
"""
self.colorSwatchPickerView.selectRow_inComponent_animated_(
selectedRow, int(colorComponent), True)
self.pickerView(self.colorSwatchPickerView,
didSelectRow=selectedRow,
inComponent=int(colorComponent))
# MARK: - UIPickerViewDataSource
@objc_method
def numberOfComponentsInPickerView_(self, pickerView) -> int:
return len(ColorComponent)
@objc_method
def pickerView_numberOfRowsInComponent_(self, component) -> int:
return self.numberOfColorValuesPerComponent
# MARK: - UIPickerViewDelegate
@objc_method
def pickerView_attributedTitleForRow_forComponent_(self, pickerView,
row: int, component: int):
colorValue = row * RGB.offset
# Set the initial colors for each picker segment.
value = colorValue / RGB.max
redColorComponent = RGB.min
greenColorComponent = RGB.min
blueColorComponent = RGB.min
if component == ColorComponent.red:
redColorComponent = value
if component == ColorComponent.green:
greenColorComponent = value
if component == ColorComponent.blue:
blueColorComponent = value
if redColorComponent < 0.5:
redColorComponent = 0.5
if blueColorComponent < 0.5:
blueColorComponent = 0.5
if greenColorComponent < 0.5:
greenColorComponent = 0.5
foregroundColor = UIColor.colorWithRed_green_blue_alpha_(
redColorComponent, greenColorComponent, blueColorComponent, 1.0)
# Set the foreground color for the entire attributed string.
attributes = NSDictionary.dictionaryWithObject(
foregroundColor, forKey=NSAttributedStringKey.foregroundColor)
title = self.mutableAttributedString.initWithString_attributes_(
f'{int(colorValue)}', attributes)
return title
@objc_method
def pickerView_didSelectRow_inComponent_(self, pickerView, row: int,
component: int):
colorComponentValue = RGB.offset * row / RGB.max
if component == ColorComponent.red:
self.redColor = colorComponentValue
if component == ColorComponent.green:
self.greenColor = colorComponentValue
if component == ColorComponent.blue:
self.blueColor = colorComponentValue
self.updateColorSwatchViewBackgroundColor()
# MARK: - UIPickerViewAccessibilityDelegate
def pickerView_accessibilityLabelForComponent_(self, pickerView,
component: int) -> objc_id:
if component == ColorComponent.red:
return localizedString('Red color component value')
if component == ColorComponent.green:
return localizedString('Green color component value')
if component == ColorComponent.blue:
return localizedString('Blue color component value')
@objc_method
def viewWillAppear_(self, animated: bool):
send_super(__class__,
self,
'viewWillAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print('viewWillAppear')
@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')
if __name__ == '__main__':
from rbedge.functions import NSStringFromClass
from rbedge.enumerations import UIModalPresentationStyle
from rbedge import present_viewController
main_vc = PickerViewController.new()
_title = NSStringFromClass(PickerViewController)
main_vc.navigationItem.title = _title
presentation_style = UIModalPresentationStyle.fullScreen
present_viewController(main_vc, presentation_style)