Skip to content

feat(ios): apple intelligence writing tools #10643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/automated/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"executor": "@nativescript/nx:debug",
"options": {
"noHmr": true,
"debug": false,
"uglify": false,
"release": false,
"forDevice": false,
Expand Down
8 changes: 7 additions & 1 deletion apps/toolbox/src/pages/forms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page, Observable, EventData, TextField, PropertyChangeData } from '@nativescript/core';
import { Page, Observable, EventData, TextField, PropertyChangeData, TextView } from '@nativescript/core';

let page: Page;

Expand Down Expand Up @@ -28,6 +28,12 @@ export class SampleData extends Observable {
console.log(args.value);
this.notifyPropertyChange('formattedPhoneInput', args.value);
}

textChangeArea(args: PropertyChangeData) {
const textArea = args.object as TextView;
console.log('---- AI active:', textArea.isWritingToolsActive);
console.log('textChangeArea:', args.value);
}
}

function formatPhoneNumber(value: string, useParens?: boolean) {
Expand Down
4 changes: 4 additions & 0 deletions apps/toolbox/src/pages/forms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
</TextField>
<Label text="{{ formattedPhoneInput }}" marginTop="6" />

<Label text="TextView" fontWeight="bold" marginTop="12" />
<TextView hint="Type text..." style.placeholderColor="silver" textChange="{{textChangeArea}}" color="black" width="80%" borderColor="silver" borderWidth="1" height="200" borderRadius="4" fontSize="14">
</TextView>

</StackLayout>
</ScrollView>
</Page>
2 changes: 1 addition & 1 deletion packages/core/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export { TextBase, getTransformedText, letterSpacingProperty, textAlignmentPrope
export { FormattedString } from './text-base/formatted-string';
export { Span } from './text-base/span';
export { TextField } from './text-field';
export { TextView } from './text-view';
export { TextView, WritingToolsAllowedInput, WritingToolsBehavior } from './text-view';
export { TimePicker } from './time-picker';
export { Transition } from './transition';
export { ModalTransition } from './transition/modal-transition';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/ui/text-view/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TextViewBase as TextViewBaseCommon } from './text-view-common';
import { CSSType } from '../core/view';

export { WritingToolsAllowedInput, WritingToolsBehavior } from './text-view-common';
export * from '../text-base';

@CSSType('TextView')
Expand Down
11 changes: 11 additions & 0 deletions packages/core/ui/text-view/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EditableTextBase } from '../editable-text-base';
import { Property } from '../core/properties';
export { WritingToolsAllowedInput, WritingToolsBehavior } from './text-view-common';

/**
* Represents an editable multiline text view.
Expand Down Expand Up @@ -30,6 +31,16 @@ export class TextView extends EditableTextBase {
* @nsProperty
*/
maxLines: number;

/**
* (iOS Only) Are Apple Intelligence writing tools active
*/
isWritingToolsActive: boolean;

/**
* (iOS Only) Allow Apple Intelligence writing tools to emit text changes on each alteration instead of after the final change (default).
*/
enableWritingToolsEvents: boolean;
}

export const maxLinesProperty: Property<EditableTextBase, number>;
81 changes: 72 additions & 9 deletions packages/core/ui/text-view/index.ios.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ScrollEventData } from '../scroll-view';
import { textProperty } from '../text-base';
import { TextViewBase as TextViewBaseCommon } from './text-view-common';
import { iosWritingToolsAllowedInputProperty, iosWritingToolsBehaviorProperty, TextViewBase as TextViewBaseCommon, WritingToolsAllowedInput, WritingToolsBehavior } from './text-view-common';
import { editableProperty, hintProperty, placeholderColorProperty, _updateCharactersInRangeReplacementString } from '../editable-text-base';
import { CoreTypes } from '../../core-types';
import { CSSType } from '../core/view';
import { Color } from '../../color';
import { colorProperty, borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, Length } from '../styling/style-properties';
import { iOSNativeHelper, layout } from '../../utils';
import { layout, isRealDevice } from '../../utils';
import { SDK_VERSION } from '../../utils/constants';

import { profile } from '../../profiling';

const majorVersion = iOSNativeHelper.MajorVersion;
export { WritingToolsAllowedInput, WritingToolsBehavior } from './text-view-common';

@NativeClass
class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
Expand Down Expand Up @@ -70,6 +70,21 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
return owner.scrollViewDidScroll(sv);
}
}

public textViewWritingToolsWillBegin(textView: UITextView): void {
const owner = this._owner?.deref();
if (owner) {
owner.isWritingToolsActive = true;
}
}

public textViewWritingToolsDidEnd(textView: UITextView): void {
const owner = this._owner?.deref();
if (owner) {
owner.isWritingToolsActive = false;
owner.textViewDidChange(textView);
}
}
}

@NativeClass
Expand All @@ -92,8 +107,8 @@ export class TextView extends TextViewBaseCommon {

public _isEditing: boolean;

private _hintColor = majorVersion <= 12 || !UIColor.placeholderTextColor ? UIColor.blackColor.colorWithAlphaComponent(0.22) : UIColor.placeholderTextColor;
private _textColor = majorVersion <= 12 || !UIColor.labelColor ? null : UIColor.labelColor;
private _hintColor = SDK_VERSION <= 12 || !UIColor.placeholderTextColor ? UIColor.blackColor.colorWithAlphaComponent(0.22) : UIColor.placeholderTextColor;
private _textColor = SDK_VERSION <= 12 || !UIColor.labelColor ? null : UIColor.labelColor;

createNativeView() {
const textView = NoScrollAnimationUITextView.new();
Expand Down Expand Up @@ -144,10 +159,12 @@ export class TextView extends TextViewBaseCommon {
}

public textViewDidChange(textView: UITextView): void {
if (this.updateTextTrigger === 'textChanged') {
textProperty.nativeValueChange(this, textView.text);
if (!this.isWritingToolsActive || this.enableWritingToolsEvents) {
if (this.updateTextTrigger === 'textChanged') {
textProperty.nativeValueChange(this, textView.text);
}
this.requestLayout();
}
this.requestLayout();
}

public textViewShouldChangeTextInRangeReplacementText(textView: UITextView, range: NSRange, replacementString: string): boolean {
Expand Down Expand Up @@ -399,6 +416,52 @@ export class TextView extends TextViewBaseCommon {
right: inset.right,
};
}

[iosWritingToolsBehaviorProperty.setNative](value: WritingToolsBehavior) {
if (SDK_VERSION >= 18 && isRealDevice()) {
this.nativeTextViewProtected.writingToolsBehavior = this._writingToolsBehaviorType(value);
}
}

[iosWritingToolsAllowedInputProperty.setNative](value: Array<WritingToolsAllowedInput>) {
if (SDK_VERSION >= 18 && isRealDevice()) {
let writingToolsInput = null;
for (const inputType of value) {
writingToolsInput = (writingToolsInput != null ? writingToolsInput : 0) + this._writingToolsAllowedType(inputType);
}
if (writingToolsInput === null) {
writingToolsInput = UIWritingToolsResultOptions.Default;
}
this.nativeTextViewProtected.allowsEditingTextAttributes = true;
this.nativeTextViewProtected.allowedWritingToolsResultOptions = writingToolsInput;
}
}

private _writingToolsBehaviorType(value: WritingToolsBehavior) {
switch (value) {
case WritingToolsBehavior.Complete:
return UIWritingToolsBehavior.Complete;
case WritingToolsBehavior.Default:
return UIWritingToolsBehavior.Default;
case WritingToolsBehavior.Limited:
return UIWritingToolsBehavior.Limited;
case WritingToolsBehavior.None:
return UIWritingToolsBehavior.None;
}
}

private _writingToolsAllowedType(value: WritingToolsAllowedInput) {
switch (value) {
case WritingToolsAllowedInput.Default:
return UIWritingToolsResultOptions.Default;
case WritingToolsAllowedInput.List:
return UIWritingToolsResultOptions.List;
case WritingToolsAllowedInput.PlainText:
return UIWritingToolsResultOptions.PlainText;
case WritingToolsAllowedInput.RichText:
return UIWritingToolsResultOptions.RichText;
}
}
}

TextView.prototype.recycleNativeView = 'auto';
37 changes: 37 additions & 0 deletions packages/core/ui/text-view/text-view-common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import { Property } from '../core/properties';
import { booleanConverter } from '../core/view-base';
import { TextView as TextViewDefinition } from '.';
import { EditableTextBase } from '../editable-text-base';

export class TextViewBase extends EditableTextBase implements TextViewDefinition {
public static returnPressEvent = 'returnPress';

public maxLines: number;
public isWritingToolsActive: boolean;
public enableWritingToolsEvents: boolean;
}

/**
* (iOS Only) Behavior for Apple Intelligence Writing Tools
* @since 8.9
*/
export enum WritingToolsBehavior {
Complete,
Default,
Limited,
None,
}
export const iosWritingToolsBehaviorProperty = new Property<TextViewBase, WritingToolsBehavior>({
name: 'iosWritingToolsBehavior',
defaultValue: WritingToolsBehavior.Default,
});
iosWritingToolsBehaviorProperty.register(TextViewBase);

/**
* (iOS Only) Allowed input for Apple Intelligence Writing Tools
* @since 8.9
*/
export enum WritingToolsAllowedInput {
Default,
List,
PlainText,
RichText,
Table,
}
export const iosWritingToolsAllowedInputProperty = new Property<TextViewBase, Array<WritingToolsAllowedInput>>({
name: 'iosWritingToolsAllowedInput',
defaultValue: [WritingToolsAllowedInput.Default],
});
iosWritingToolsAllowedInputProperty.register(TextViewBase);
9 changes: 4 additions & 5 deletions packages/core/utils/ios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,10 @@ export function createUIDocumentInteractionControllerDelegate(): NSObject {

export function isRealDevice() {
try {
// https://stackoverflow.com/a/5093092/4936697
const sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
const mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);

return !!mediaTypes;
if (NSProcessInfo.processInfo.environment.valueForKey('SIMULATOR_DEVICE_NAME')) {
return false;
}
return true;
} catch (e) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/types-ios/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/types-ios",
"version": "8.8.0",
"version": "8.9.0-alpha.0",
"description": "NativeScript Types for iOS.",
"homepage": "https://nativescript.org",
"repository": {
Expand Down
Loading