-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuButtonViewController.py
373 lines (324 loc) · 14.1 KB
/
menuButtonViewController.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
import ctypes
from enum import Enum
from pyrubicon.objc.api import ObjCClass, ObjCInstance, Block
from pyrubicon.objc.api import objc_method
from pyrubicon.objc.runtime import send_super, objc_id
from pyrubicon.objc.types import NSInteger
from rbedge.enumerations import (
UIMenuElementState,
UIMenuElementAttributes,
UIMenuOptions,
)
from rbedge.functions import NSStringFromClass
from rbedge import pdbr
from caseElement import CaseElement
from pyLocalizedString import localizedString
from baseTableViewController import BaseTableViewController
from storyboard.menuButtonViewController import prototypes
UIMenu = ObjCClass('UIMenu')
UIAction = ObjCClass('UIAction')
UIImage = ObjCClass('UIImage')
# Cell identifier for each menu button table view cell.
# > 各メニュー ボタン テーブル ビュー セルのセル識別子。
class MenuButtonKind(Enum):
buttonMenuProgrammatic = 'buttonMenuProgrammatic'
buttonMenuMultiAction = 'buttonMenuMultiAction'
buttonSubMenu = 'buttonSubMenu'
buttonMenuSelection = 'buttonMenuSelection'
class ButtonMenuActionIdentifiers(Enum):
item1 = 'item1'
item2 = 'item2'
item3 = 'item3'
class MenuButtonViewController(BaseTableViewController):
@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
]
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad') # xxx: 不要?
# --- Navigation
self.navigationItem.title = localizedString('MenuButtonsTitle') if (
title := self.navigationItem.title) is None else title
self.testCellsAppendContentsOf_([
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('DropDownProgTitle'),
MenuButtonKind.buttonMenuProgrammatic.value,
'configureDropDownProgrammaticButton:'),
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('DropDownMultiActionTitle'),
MenuButtonKind.buttonMenuMultiAction.value,
'configureDropdownMultiActionButton:'),
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('DropDownButtonSubMenuTitle'),
MenuButtonKind.buttonSubMenu.value, 'configureDropdownSubMenuButton:'),
CaseElement.alloc().initWithTitle_cellID_configHandlerName_(
localizedString('PopupSelection'),
MenuButtonKind.buttonMenuSelection.value,
'configureSelectionPopupButton:'),
])
@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')
# MARK: - Handlers
@objc_method
def menuHandler_(self, _action: ctypes.c_void_p) -> None:
action = ObjCInstance(_action)
switch_case = str(action.identifier)
# xxx: `match` がPythonista3 (PEP8) だと、フォーマットできないため`if`
if switch_case == ButtonMenuActionIdentifiers.item1.value:
print('Menu Action: item 1')
elif switch_case == ButtonMenuActionIdentifiers.item2.value:
print('Menu Action: item 2')
elif switch_case == ButtonMenuActionIdentifiers.item3.value:
print('Menu Action: item 3')
else:
print('Menu Action: None')
@objc_method
def item4Handler_(self, _action: ctypes.c_void_p) -> None:
action = ObjCInstance(_action)
print(f'Menu Action: {action.title}')
# MARK: - Drop Down Menu Buttons
@objc_method
def configureDropDownProgrammaticButton_(self, button):
# xxx: 正規表現でやる?
button.menu = UIMenu.menuWithChildren_([
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '1'), None,
ButtonMenuActionIdentifiers.item1.value,
Block(self.menuHandler_, None, ctypes.c_void_p)),
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '2'), None,
ButtonMenuActionIdentifiers.item2.value,
Block(self.menuHandler_, None, ctypes.c_void_p)),
])
button.showsMenuAsPrimaryAction = True
@objc_method
def configureDropdownMultiActionButton_(self, button):
# todo: closure をblock 処理
@Block
def menuAction5_closure(_action: objc_id) -> None:
print(f'Menu Action: {ObjCInstance(_action).title}')
@Block
def menuAction6_closure(_action: objc_id) -> None:
print(f'Menu Action: {ObjCInstance(_action).title}')
# xxx: 正規表現でやる?
buttonMenu = UIMenu.menuWithChildren_([
# Share a single handler for the first 3 actions.
# > 最初の 3 つのアクションに対して 1 つのハンドラーを共有します。
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '1'),
UIImage.systemImageNamed('1.circle'),
ButtonMenuActionIdentifiers.item1.value,
Block(self.menuHandler_, None, ctypes.c_void_p)),
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '2'),
UIImage.systemImageNamed('2.circle'),
ButtonMenuActionIdentifiers.item2.value,
Block(self.menuHandler_, None, ctypes.c_void_p)),
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '3'),
UIImage.systemImageNamed('3.circle'),
ButtonMenuActionIdentifiers.item3.value,
Block(self.menuHandler_, None, ctypes.c_void_p)),
# Use a separate handler for this 4th action.
# > この 4 番目のアクションには別のハンドラーを使用します。
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '4'),
UIImage.systemImageNamed('4.circle'), None,
Block(self.item4Handler_, None, ctypes.c_void_p)),
# Use a closure for the 5th action.
# > 5 番目のアクションにはクロージャを使用します。
# todo: closure をblock 処理
UIAction.actionWithTitle_image_identifier_handler_(
localizedString('ItemTitle').replace('%@', '5'),
UIImage.systemImageNamed('5.circle'), None, menuAction5_closure),
# Use attributes to make the 6th action disabled.
# > 属性を使用して 6 番目のアクションを無効にします。
# todo: closure をblock 処理
UIAction.alloc().
initWithTitle_image_identifier_discoverabilityTitle_attributes_state_handler_(
localizedString('ItemTitle').replace('%@', '6'),
UIImage.systemImageNamed('6.circle'), None, None,
UIMenuElementAttributes.disabled, UIMenuElementState.off,
menuAction6_closure),
])
button.menu = buttonMenu
button.showsMenuAsPrimaryAction = True
@objc_method
def configureDropdownSubMenuButton_(self, button):
@Block
def sortClosure(_action: objc_id) -> None:
print(f'Sort by: {ObjCInstance(_action).title}')
@Block
def refreshClosure(_action: objc_id) -> None:
print('Refresh handler')
@Block
def accountHandler(_action: objc_id) -> None:
print('Account handler')
sortMenu: UIMenu
# xxx: `#available(iOS 15, *)`
if True: # .singleSelection option only on iOS 15 or later
# The sort sub menu supports a selection.
# > 並べ替えサブメニューは選択をサポートします。
sortMenu = sortMenu = UIMenu.menuWithTitle_image_identifier_options_children_(
'Sort By', None, None, UIMenuOptions.singleSelection, [
UIAction.alloc().initWithTitle('Date',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.on,
handler=sortClosure),
UIAction.alloc().initWithTitle('Size',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.off,
handler=sortClosure),
])
else:
sortMenu = UIMenu.menuWithTitle_children_('Sort By', [
UIAction.alloc().initWithTitle('Date',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.on,
handler=sortClosure),
UIAction.alloc().initWithTitle('Size',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.off,
handler=sortClosure),
])
topMenu = UIMenu.menuWithChildren_([
UIAction.actionWithTitle_image_identifier_handler_(
'Refresh', None, None, refreshClosure),
UIAction.actionWithTitle_image_identifier_handler_(
'Account', None, None, accountHandler),
sortMenu,
])
# This makes the button behave like a drop down menu.
# > これにより、ボタンがドロップダウン メニューのように動作します。
button.showsMenuAsPrimaryAction = True
button.menu = topMenu
# MARK: - Selection Popup Menu Button
@objc_method
def updateColor_(self, _title: ctypes.c_void_p) -> None:
print(f'Color selected: {ObjCInstance(_title)}')
@objc_method
def configureSelectionPopupButton_(self, button):
@Block
def colorClosure(_action: objc_id) -> None:
self.updateColor_(ObjCInstance(_action).title)
button.menu = UIMenu.menuWithChildren_([
UIAction.alloc().initWithTitle('Red',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.off,
handler=colorClosure),
UIAction.alloc().initWithTitle('Green',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.on,
handler=colorClosure),
UIAction.alloc().initWithTitle('Blue',
image=None,
identifier=None,
discoverabilityTitle=None,
attributes=0,
state=UIMenuElementState.off,
handler=colorClosure),
])
# This makes the button behave like a drop down menu.
# > これにより、ボタンがドロップダウン メニューのように動作します。
button.showsMenuAsPrimaryAction = True
if True: # xxx: `#available(iOS 15, *)`
button.changesSelectionAsPrimaryAction = True
# Select the default menu item (green).
# > デフォルトのメニュー項目 (緑色) を選択します。
self.updateColor_(button.menu.selectedElements.firstObject().title)
if __name__ == '__main__':
from rbedge.app import App
from rbedge.enumerations import (
UITableViewStyle,
UIModalPresentationStyle,
)
table_style = UITableViewStyle.grouped
main_vc = MenuButtonViewController.alloc().initWithStyle_(table_style)
_title = NSStringFromClass(MenuButtonViewController)
main_vc.navigationItem.title = _title
# presentation_style = UIModalPresentationStyle.fullScreen
presentation_style = UIModalPresentationStyle.pageSheet
app = App(main_vc, presentation_style)
app.present()