1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Implementation of `app::ui` for iOS
24 // Objective-C object receiving callbacks from UI events
25 @interface NitCallbackReference: NSObject
27 // Nit object target of the callbacks from UI events
28 @property (nonatomic) View nit_view;
30 // Actual callback method
31 -(void) nitOnEvent: (UIView*) sender;
34 @implementation NitCallbackReference
36 -(void) nitOnEvent: (UIView*) sender {
37 View_on_ios_event(self.nit_view);
41 // Proxy for both delegates of UITableView relaying all callbacks to `nit_list_layout
`
42 @interface UITableViewAndDataSource: NSObject <UITableViewDelegate, UITableViewDataSource>
44 // Nit object receiving the callbacks
45 @property TableView nit_list_layout;
47 // List of native views added to this list view from the Nit side
48 @property NSMutableArray *views;
51 @implementation UITableViewAndDataSource
56 self.views = [[NSMutableArray alloc] initWithCapacity:8];
60 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
61 return TableView_number_of_sections_in_table_view(self.nit_list_layout, tableView);
64 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
65 return TableView_number_of_rows_in_section(self.nit_list_layout, tableView, section);
68 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
69 return TableView_title_for_header_in_section(self.nit_list_layout, tableView, section);
72 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
73 return TableView_cell_for_row_at_index_path(self.nit_list_layout, tableView, indexPath);
79 redef fun did_finish_launching_with_options
81 app_delegate
.window
= new UIWindow
82 app_delegate
.window
.background_color
= new UIColor.white_color
84 app_delegate
.window
.make_key_and_visible
88 private fun set_view_controller
(window
: UIWindow, native
: UIViewController)
90 // Set the required root view controller
91 UINavigationController *navController = (UINavigationController*)window.rootViewController;
93 if (navController == NULL) {
94 navController = [[UINavigationController alloc]initWithRootViewController:native];
95 navController.edgesForExtendedLayout = UIRectEdgeNone;
97 // Must be non-translucent for the controls to be placed under
98 // (as in Y axis) of the navigation bar.
99 navController.navigationBar.translucent = NO;
101 window.rootViewController = navController;
104 [navController pushViewController:native animated:YES];
107 native.edgesForExtendedLayout = UIRectEdgeNone;
110 redef fun window
=(window
)
112 set_view_controller
(app_delegate
.window
, window
.native
)
117 redef class AppDelegate
119 # The main application window, must be set by `App::on_create`
120 fun window
: UIWindow in "ObjC" `{ return [self window]; `}
122 # The main application window, must be set by `App::on_create
`
123 fun window=(window: UIWindow) in "ObjC" `{ self.window = window; `}
128 # Native implementation of this control
129 fun native
: NATIVE is abstract
131 # Type of the `native` implementation of this control
132 type NATIVE: NSObject
136 redef type NATIVE: UIView
138 redef var enabled
= null is lazy
140 private fun on_ios_event
do end
143 redef class CompositeControl
145 redef fun remove
(view
)
149 if view
isa View then
150 view
.native
.remove_from_superview
157 redef type NATIVE: UIViewController
158 redef var native
= new UIViewController
160 # Title of this window
161 fun title
: String do return native
.title
.to_s
163 # Set the title of this window
164 fun title
=(title
: String) do native
.title
= title
.to_nsstring
170 var native_view
= view
.native
171 assert native_view
isa UIView
173 native
.view
= native_view
179 redef type NATIVE: UIStackView
180 redef var native
= new UIStackView
184 native
.alignment
= new UIStackViewAlignment.fill
186 # TODO make customizable
194 var native_view
= view
.native
195 assert native_view
isa UIView
196 self.native
.add_arranged_subview native_view
200 redef class HorizontalLayout
203 native
.axis
= new UILayoutConstraintAxis.horizontal
204 native
.distribution
= new UIStackViewDistribution.fill_equally
208 redef class VerticalLayout
211 native
.axis
= new UILayoutConstraintAxis.vertical
212 native
.distribution
= new UIStackViewDistribution.equal_spacing
218 redef type NATIVE: UILabel
219 redef var native
= new UILabel
221 redef fun text
=(text
) do native
.text
= (text
or else "").to_nsstring
222 redef fun text
do return native
.text
.to_s
224 redef fun size
=(size
)
226 size
= size
or else 1.0
227 var points
= 8.0 + size
* 8.0
228 set_size_native
(native
, points
)
231 private fun set_size_native
(native
: UILabel, points
: Float)
233 native.font = [UIFont systemFontOfSize: points];
236 redef fun align
=(align
) do set_align_native
(native
, align
or else 0.0)
238 private fun set_align_native
(native
: UILabel, align
: Float)
242 native.textAlignment = NSTextAlignmentCenter;
243 else if (align < 0.5)
244 native.textAlignment = NSTextAlignmentLeft;
245 else//if (align > 0.5)
246 native.textAlignment = NSTextAlignmentRight;
250 # On iOS, check boxes are a layout composed of a label and an `UISwitch`
253 redef type NATIVE: UIStackView
254 redef fun native
do return layout
.native
256 # Root layout implementing this check box
257 var layout
= new HorizontalLayout(parent
=self.parent
)
259 # Label with the text
260 var lbl
= new Label(parent
=layout
)
262 # `UISwitch` acting as the real check box
263 var ui_switch
: UISwitch is noautoinit
265 redef fun on_ios_event
do notify_observers
new ToggleEvent(self)
269 # Tweak the layout so it is centered
270 layout
.native
.distribution
= new UIStackViewDistribution.fill_proportionally
271 layout
.native
.alignment
= new UIStackViewAlignment.center
272 layout
.native
.layout_margins_relative_arrangement
= true
275 native
.add_arranged_subview s
278 ui_switch
.set_callback
self
281 redef fun text
=(text
) do lbl
.text
= text
282 redef fun text
do return lbl
.text
284 redef fun is_checked
do return ui_switch
.on
285 redef fun is_checked
=(value
) do ui_switch
.set_on_animated
(value
, true)
289 # Register callbacks on this switch to be relayed to `sender`
290 private fun set_callback
(sender
: View)
291 import View.on_ios_event
in "ObjC" `{
293 NitCallbackReference *ncr = [[NitCallbackReference alloc] init];
294 ncr.nit_view = sender;
296 // Pin the objects in both Objective-C and Nit GC
297 View_incr_ref(sender);
298 ncr = (__bridge NitCallbackReference*)CFBridgingRetain(ncr);
300 [self addTarget:ncr action:@selector(nitOnEvent:)
301 forControlEvents:UIControlEventValueChanged];
305 redef class TextInput
307 redef type NATIVE: UITextField
308 redef var native
= new UITextField
310 redef fun text
=(text
) do native
.text
= (text
or else "").to_nsstring
311 redef fun text
do return native
.text
.to_s
313 redef fun is_password
=(value
)
315 native
.secure_text_entry
= value
or else false
322 redef type NATIVE: UIButton
323 redef var native
= new UIButton(new UIButtonType.system
)
325 init do native
.set_callback
self
327 redef fun on_ios_event
do notify_observers
new ButtonPressEvent(self)
329 redef fun text
=(text
) do if text
!= null then native
.title
= text
.to_nsstring
330 redef fun text
do return native
.current_title
.to_s
332 redef fun enabled
=(enabled
) do native
.enabled
= enabled
or else true
333 redef fun enabled
do return native
.enabled
337 # Register callbacks on this button to be relayed to `sender`
338 private fun set_callback
(sender
: View)
339 import View.on_ios_event
in "ObjC" `{
341 NitCallbackReference *ncr = [[NitCallbackReference alloc] init];
342 ncr.nit_view = sender;
344 // Pin the objects in both Objective-C and Nit GC
345 View_incr_ref(sender);
346 ncr = (__bridge NitCallbackReference*)CFBridgingRetain(ncr);
348 [self addTarget:ncr action:@selector(nitOnEvent:)
349 forControlEvents:UIControlEventTouchUpInside];
353 # On iOS, implemented by a `UIStackView` inside a ` UIScrollView`
354 redef class ListLayout
356 redef type NATIVE: UIScrollView
357 redef var native
= new UIScrollView
359 # Real container of the subviews, contained within `native`
360 var native_stack_view
= new UIStackView
364 native_stack_view
.translates_autoresizing_mask_into_constraits
= false
365 native_stack_view
.axis
= new UILayoutConstraintAxis.vertical
366 native_stack_view
.alignment
= new UIStackViewAlignment.fill
367 native_stack_view
.distribution
= new UIStackViewDistribution.fill_equally
368 native_stack_view
.spacing
= 4.0
370 native
.add_subview native_stack_view
371 native_add_constraints
(native
, native_stack_view
)
374 private fun native_add_constraints
(scroll_view
: UIScrollView, stack_view
: UIStackView) in "ObjC" `{
375 [scroll_view addConstraints:[NSLayoutConstraint
376 constraintsWithVisualFormat: @"V:|-8-[view]-8-|"
377 options: NSLayoutFormatAlignAllCenterX metrics: nil views: @{@"view": stack_view}]];
378 [scroll_view addConstraints:[NSLayoutConstraint
379 constraintsWithVisualFormat: @"H:|-8-[view]"
380 options: NSLayoutFormatAlignAllCenterX metrics: nil views: @{@"view": stack_view}]];
387 if view
isa View then
388 native_stack_view
.add_arranged_subview view
.native
393 # iOS specific layout using a `UITableView`, works only with simple children views
395 super CompositeControl
397 redef type NATIVE: UITableView
398 redef var native
= new UITableView(new UITableViewStyle.plain
)
402 native
.autoresizing_mask
403 native
.assign_delegate_and_data_source
self
408 # Adding a view to a UITableView is a bit tricky.
410 # Items are added to the Objective-C view only by callbacks.
411 # We must store the sub views in local lists while waiting
414 # As usual, we keep the Nity object in `items`.
415 # But we also keep their native counterparts in a list of
416 # the `UITableViewAndDataSource` set as `native.delegate`.
417 # Otherwise the native views could be freed by the Objective-C GC.
419 # TODO use an adapter for the app.nit ListLayout closer to what exists
420 # on both iOS and Android, to support large data sets.
422 if item
isa View then
423 add_view_to_native_list
(native
, item
.native
)
428 # Force redraw and trigger callbacks
432 private fun add_view_to_native_list
(native
: UITableView, item
: UIView) in "ObjC" `{
433 [((UITableViewAndDataSource*)native.delegate).views addObject:item];
436 private fun get_view_from_native_list
(native
: UITableView, index
: Int): UIView in "ObjC" `{
437 return [((UITableViewAndDataSource*)native.delegate).views objectAtIndex:index];
440 # Number of sections in this view
442 # By default, we assume that all `items` are in a single section,
443 # so there is only one section.
445 # iOS callback: `numberOfSectionsInTableView`
446 protected fun number_of_sections_in_table_view
(view
: UITableView): Int
449 # Number of entries in `section`
451 # By default, we assume that all `items` are in a single section,
452 # so no matter the section, this returns `items.length`.
454 # iOS callback: `numberOfRowsInSection`
455 protected fun number_of_rows_in_section
(view
: UITableView, section
: Int): Int
456 do return items
.length
458 # Title for `section`, return `new NSString.nil` for no title
460 # By default, this returns no title.
462 # iOS callback: `titleForHeaderInSection`
463 protected fun title_for_header_in_section
(view
: UITableView, section
: Int): NSString
464 do return new NSString.nil
466 # Return a `UITableViewCell` for the item at `index_path`
468 # By default, we assume that all `items` are in a single section.
469 # So no matter the depth of the `index_path`, this returns a cell with
470 # the view at index part of `index_path`.
472 # iOS callback: `cellForRowAtIndexPath`
473 protected fun cell_for_row_at_index_path
(table_view
: UITableView, index_path
: NSIndexPath): UITableViewCell
475 var reuse_id
= "NitCell".to_nsstring
476 var cell
= new UITableViewCell(reuse_id
)
478 # TODO if there is performance issues, reuse cells with
479 # the following code, but clear the cell before use.
481 #var cell = table_view.dequeue_reusable_cell_with_identifier(reuse_id)
482 #if cell.address_is_null then cell = new UITableViewCell(reuse_id)
484 var index
= index_path
.index_at_position
(1)
485 var view_native
= get_view_from_native_list
(table_view
, index
)
486 var cv
= cell
.content_view
487 cv
.add_subview view_native
493 redef class UITableView
495 # Assign `list_view` as `delegate` and `dataSource`, and pin all references in both GCs
496 private fun assign_delegate_and_data_source
(list_view
: TableView)
497 import TableView.number_of_sections_in_table_view
,
498 TableView.number_of_rows_in_section
,
499 TableView.title_for_header_in_section
,
500 TableView.cell_for_row_at_index_path
in "ObjC" `{
502 UITableViewAndDataSource *objc_delegate = [[UITableViewAndDataSource alloc] init];
503 objc_delegate = (__bridge UITableViewAndDataSource*)CFBridgingRetain(objc_delegate);
505 objc_delegate.nit_list_layout = list_view;
506 TableView_incr_ref(list_view);
509 self.delegate = objc_delegate;
510 self.dataSource = objc_delegate;