-
Notifications
You must be signed in to change notification settings - Fork 791
Adding text input command #137
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
Changes from 2 commits
3ada41c
12a1e2f
018d61e
30e7cc6
2227089
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # Copyright (c) 2016, Facebook, Inc. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. An additional grant | ||
| # of patent rights can be found in the PATENTS file in the same directory. | ||
|
|
||
| import os | ||
|
|
||
| import lldb | ||
| import fblldbbase as fb | ||
| import fblldbviewhelpers as viewHelpers | ||
|
|
||
| ACCESSIBILITY_ID = 0 | ||
| REPLACEMENT_TEXT = 1 | ||
| INPUT_TEXT = 0 | ||
|
|
||
|
|
||
| def lldbcommands(): | ||
| return [ | ||
| FBInputTexByAccessibilityIdCommand(), | ||
| FBInputTexToFirstResponderCommand(), | ||
| ] | ||
|
|
||
|
|
||
| class FBInputTexByAccessibilityIdCommand(fb.FBCommand): | ||
| def name(self): | ||
| return 'setxt' | ||
|
|
||
| def description(self): | ||
| return 'Set text on text on a view by accessibility id.' | ||
|
|
||
| def args(self): | ||
| return [ | ||
| fb.FBCommandArgument(arg='accessibilityId', type='string', help='The accessibility ID of the input view.'), | ||
| fb.FBCommandArgument(arg='replacementText', type='string', help='The text to set.') | ||
| ] | ||
|
|
||
| def run(self, arguments, options): | ||
| self.findView(rootView(), arguments[ACCESSIBILITY_ID], arguments[REPLACEMENT_TEXT]) | ||
|
|
||
| def findView(self, view, searchIdentifier, replacementText): | ||
| views = subviewsOfView(view) | ||
| for index in range(0, viewsCount(views)): | ||
| subview = subviewAtIndex(views, index) | ||
| self.findView(subview, searchIdentifier, replacementText) | ||
| else: | ||
| identifier = accessibilityIdentifier(view) | ||
| if isEqualToString(identifier, searchIdentifier): | ||
| setTextInView(view, replacementText) | ||
|
|
||
|
|
||
| class FBInputTexToFirstResponderCommand(fb.FBCommand): | ||
| def name(self): | ||
| return 'input' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can has verb? Ideally I would call this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose input because this acts like someone inputing text into a text field/text view which has the cursor in it. I could change this to I went with terse commands based on whats currently there in lldb and chisel. Again, no hard opinion on what these should be called.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, thanks. How about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had started to write this in the accessibility command file. But I changed after thinking about the purpose of the command. It does use accessibility, but it's not the primary point of the commands.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed up |
||
|
|
||
| def description(self): | ||
| return 'Input text into text field or text view that is first responder.' | ||
|
|
||
| def args(self): | ||
| return [ | ||
| fb.FBCommandArgument(arg='inputText', type='string', help='The text to input.') | ||
| ] | ||
|
|
||
| def run(self, arguments, options): | ||
| self.findFirstResponder(rootView(), arguments[INPUT_TEXT]) | ||
|
|
||
| def findFirstResponder(self, view, replacementText): | ||
| views = subviewsOfView(view) | ||
| for index in range(0, viewsCount(views)): | ||
| subview = subviewAtIndex(views, index) | ||
| self.findFirstResponder(subview, replacementText) | ||
| else: | ||
| if isFirstResponder(view): | ||
| setTextInView(view, replacementText) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My python is clearly lacking, how does this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that's exactly it. My python isn't that great either, only found out about this while writing this code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't handle any first responder that has subviews. Is there a reason for that? I'm not aware of anything that prevents a view with subviews from being first responder.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This command is intended to be used after selecting a text field/text view, making it first responder, then setting the text on it. With that in mind, if anything else is first responder at the time this command is run it will either do nothing, or set the text property on it. I took the happy path for this, but I could put checks in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just looking back over the code, this branch of code is only executed when a view has no subviews.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Right, and I'm wondering about cases where some text input view has internal subviews. There's no guarantee that that a text input view will always have no subviews. I think the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, no issue. Pushed it up. |
||
|
|
||
|
|
||
| # Some helpers | ||
| def rootView(): | ||
| return fb.evaluateObjectExpression('[[UIApplication sharedApplication] keyWindow]') | ||
|
|
||
| def subviewsOfView(view): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(subviews)]' % view): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do all of these check that the selector exists first?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was one instance where an unrecognised selector erred out, so I decided to be defensive and guard them. I'm happy to change this if there's a strong opinion. But there is at least one which caused a stuff to fail, can't remember off hand though.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In chisel this idiom isn't common. I'd prefer we only guard if there's a reason for it, and even better if we could add a comment why that reason is.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been trying to replicate the error I had and haven't been able to. I'll push up changes with these checks taken out. |
||
| return fb.evaluateObjectExpression('[%s subviews]' % view) | ||
|
|
||
| def subviewAtIndex(views, index): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(objectAtIndex:)]' % views): | ||
| return fb.evaluateObjectExpression('[%s objectAtIndex:%i]' % (views, index)) | ||
|
|
||
| def viewsCount(views): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(count)]' % views): | ||
| return int(fb.evaluateExpression('(int)[%s count]' % views)) | ||
|
|
||
| def accessibilityIdentifier(view): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(accessibilityIdentifier)]' % view): | ||
| return fb.evaluateObjectExpression('[%s accessibilityIdentifier]' % view) | ||
|
|
||
| def isEqualToString(identifier, needle): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(isEqualToString:)]' % identifier): | ||
| return fb.evaluateBooleanExpression('[%s isEqualToString:@"%s"]' % (identifier, needle)) | ||
|
|
||
| def setTextInView(view, text): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(setText:)]' % view): | ||
| fb.evaluateObjectExpression('[%s setText:@"%s"]' % (view, text)) | ||
| viewHelpers.flushCoreAnimationTransaction() | ||
|
|
||
| def isFirstResponder(view): | ||
| if fb.evaluateBooleanExpression('[%s respondsToSelector:@selector(isFirstResponder)]' % view): | ||
| return fb.evaluateBooleanExpression('[%s isFirstResponder]' % view) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the mnemonics behind this name? What does
xtmean?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just short for text. As per the comment bellow, I based the name on other examples of short hand commands.