Skip to content
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

Timob 6006: Implement setSelection for TextArea and TextField #2675

Merged
merged 5 commits into from
Aug 8, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.widget.TiUIText;
import android.app.Activity;
import android.os.Message;

@Kroll.proxy(creatableInModule=UIModule.class, propertyAccessors = {
TiC.PROPERTY_AUTOCAPITALIZATION,
Expand All @@ -37,6 +40,9 @@
})
public class TextAreaProxy extends TiViewProxy
{
private static final int MSG_FIRST_ID = TiViewProxy.MSG_LAST_ID + 1;
private static final int MSG_SET_SELECTION = MSG_FIRST_ID + 201;

public TextAreaProxy()
{
super();
Expand Down Expand Up @@ -71,4 +77,41 @@ public Boolean hasText()
}
return false;
}

@Kroll.method
public void setSelection(int start, int stop)
{
TiUIView v = getOrCreateView();
if (v != null) {
if (TiApplication.isUIThread()) {
((TiUIText)v).setSelection(start, stop);
return;
}
KrollDict args = new KrollDict();
args.put(TiC.PROPERTY_START, start);
args.put(TiC.PROPERTY_STOP, stop);
getMainHandler().obtainMessage(MSG_SET_SELECTION, args).sendToTarget();
}
}

public boolean handleMessage(Message msg)
{
switch (msg.what) {
case MSG_SET_SELECTION: {
TiUIView v = getOrCreateView();
if (v != null) {
Object argsObj = msg.obj;
if (argsObj instanceof KrollDict) {
KrollDict args = (KrollDict) argsObj;
((TiUIText)v).setSelection(args.getInt(TiC.PROPERTY_START), args.getInt(TiC.PROPERTY_STOP));
}
}
return true;
}

default: {
return super.handleMessage(msg);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.widget.TiUIText;
import android.app.Activity;
import android.os.Message;

@Kroll.proxy(creatableInModule=UIModule.class, propertyAccessors = {
TiC.PROPERTY_AUTOCAPITALIZATION,
Expand All @@ -37,6 +40,9 @@
})
public class TextFieldProxy extends TiViewProxy
{
private static final int MSG_FIRST_ID = TiViewProxy.MSG_LAST_ID + 1;
private static final int MSG_SET_SELECTION = MSG_FIRST_ID + 201;

public TextFieldProxy()
{
super();
Expand Down Expand Up @@ -71,4 +77,42 @@ public Boolean hasText()
}
return false;
}

@Kroll.method
public void setSelection(int start, int stop)
{
TiUIView v = getOrCreateView();
if (v != null) {
if (TiApplication.isUIThread()) {
((TiUIText)v).setSelection(start, stop);
return;
}
KrollDict args = new KrollDict();
args.put(TiC.PROPERTY_START, start);
args.put(TiC.PROPERTY_STOP, stop);
getMainHandler().obtainMessage(MSG_SET_SELECTION, args).sendToTarget();
}
}

public boolean handleMessage(Message msg)
{
switch (msg.what) {
case MSG_SET_SELECTION: {
TiUIView v = getOrCreateView();
if (v != null) {
Object argsObj = msg.obj;
if (argsObj instanceof KrollDict) {
KrollDict args = (KrollDict) argsObj;
((TiUIText)v).setSelection(args.getInt(TiC.PROPERTY_START), args.getInt(TiC.PROPERTY_STOP));
}
}
return true;
}

default: {
return super.handleMessage(msg);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ protected char[] getAcceptedChars() {
}
}

public void setSelection(int start, int end)
{
int textLength = tv.length();
if (start < 0 || start > textLength || end < 0 || end > textLength) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a better check. Start < 0 could default to start=0. End > textLength could default to end=textLength
Probably also good to check start<end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'start' doesn't have to be less than 'end'. setSelection(14,0) produces the same result as setSelection(0,14). If user specify invalid values for start or end, then the behavior is undefined. I think in this case it's better to print out a warning and do nothing instead of capping values and let end-developers think their values are correct.

Log.w(TAG, "Invalid range for text selection. Ignoring.");
return;
}
tv.setSelection(start, end);
}

public void handleReturnKeyType(int type)
{
switch(type) {
Expand Down
16 changes: 16 additions & 0 deletions apidoc/Titanium/UI/TextArea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ methods:
description: |
See also: [add](Titanium.UI.TextArea.add), <Titanium.UI.View.add>.
platforms: [iphone, ipad]

-name: setSelection
summary: Selects the text in range (start, end).
platforms: [android]
since: 2.2.0
parameters:
- name: start
summary: Start index for selection. Value ranges from 0 to the text's length.
type: Integer
- name: end
summary: End index for selection, not inclusive. Value ranges from 0 to the text's length.
type: Integer
description: |
Selects the text in range (start, end). If start equals end, no text will be selected,
and the cursor will move to the start position. End is not inclusive, meaning setSelection(0,0)
will not select the first character, but will move the cursor to the position before the first character.

properties:
- name: appearance
Expand Down
18 changes: 17 additions & 1 deletion apidoc/Titanium/UI/TextField.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ methods:
description: |
See also: [add](Titanium.UI.TextField.add), <Titanium.UI.View.add>.
platforms: [iphone, ipad]


-name: setSelection
summary: Selects the text in range (start, end).
platforms: [android]
since: 2.2.0
parameters:
- name: start
summary: Start index for selection. Value ranges from 0 to the text's length.
type: Integer
- name: end
summary: End index for selection, not inclusive. Value ranges from 0 to the text's length.
type: Integer
description: |
Selects the text in range (start, end). If start equals end, no text will be selected,
and the cursor will move to the start position. End is not inclusive, meaning setSelection(0,0)
will not select the first character, but will move the cursor to the position before the first character.

properties:
- name: appearance
summary: Determines the appearance of the keyboard displayed when this field is focused.
Expand Down