<commit_before>//******************************************************************************
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

#import "AssertARCEnabled.h"
#import <Starboard.h>
#import <StubReturn.h>

#import <CoreGraphics/CGContext.h>
#import "CGContextInternal.h"

#import <UIKit/UIView.h>
#import <UIKit/UIControl.h>
#import <Foundation/NSTimer.h>
#import <UIKit/UIViewController.h>
#import <Foundation/NSNotificationCenter.h>
#import <UIKit/UIFont.h>
#import <UIKit/UIColor.h>
#import <UIKit/UITextField.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIImageView.h>
#import <UIKit/UITableViewCell.h>
#import "NSMutableString+Internal.h"

#import <UWP/WindowsUIXamlControls.h>

#import "XamlUtilities.h"

static const wchar_t* TAG = L"UITextField";

NSString* const UITextFieldTextDidBeginEditingNotification = @"UITextFieldTextDidBeginEditingNotification";
NSString* const UITextFieldTextDidChangeNotification = @"UITextFieldTextDidChangeNotification";
NSString* const UITextFieldTextDidEndEditingNotification = @"UITextFieldTextDidEndEditingNotification";

// This is a hidden view control used to steal focus typically when a text field OSK (on screen keyboard) is dismissed.
@interface _UIHiddenButtonView : UIView
@end

@implementation _UIHiddenButtonView
@end

@implementation UITextField {
    StrongId<NSString> _text;
    StrongId<NSString> _placeHolder;
    StrongId<UIFont> _font;
    StrongId<UIColor> _textColor;
    StrongId<UIColor> _backgroundColor;
    UITextAlignment _alignment;
    UITextBorderStyle _borderStyle;
    BOOL _secureTextMode;
    BOOL _isFirstResponder;

    // backing keyboard Behavior
    UITextAutocapitalizationType _autoCapitalizatonType;
    UIKeyboardType _keyboardType;
    UITextAutocorrectionType _autoCorrectionType;
    UITextSpellCheckingType _spellCheckingType;
    BOOL _enablesReturnKeyAutomatically;

    // backing xaml textbox and passwordBox
    WXCTextBox* _textBox;
    WXCPasswordBox* _passwordBox;

    // lock use to access the properties
    StrongId<NSRecursiveLock> _secureModeLock;

    // dummy control to steal the focus
    StrongId<WXCButton> _dummyButton;
    StrongId<_UIHiddenButtonView> _hiddenView;
}

//
// Properties Accessing the Text Attributes
//
/**
 @Status Interoperable
*/
- (void)setText:(NSString*)text {
    if (![_text isEqualToString:text]) {
        if (text != nil) {
            _text = [text copy];
        } else {
            _text = nil;
        }

        [_secureModeLock lock];
        if (_secureTextMode) {
            _passwordBox.password = _text;
        } else {
            _textBox.text = _text;
        }
        [_secureModeLock unlock];
    }
}

/**<commit_msg>UITextField setText: should move caret position of underlying TextBox to end.<commit_after>//******************************************************************************
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

#import "AssertARCEnabled.h"
#import <Starboard.h>
#import <StubReturn.h>

#import <CoreGraphics/CGContext.h>
#import "CGContextInternal.h"

#import <UIKit/UIView.h>
#import <UIKit/UIControl.h>
#import <Foundation/NSTimer.h>
#import <UIKit/UIViewController.h>
#import <Foundation/NSNotificationCenter.h>
#import <UIKit/UIFont.h>
#import <UIKit/UIColor.h>
#import <UIKit/UITextField.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIImageView.h>
#import <UIKit/UITableViewCell.h>
#import "NSMutableString+Internal.h"

#import <UWP/WindowsUIXamlControls.h>

#import "XamlUtilities.h"

static const wchar_t* TAG = L"UITextField";

NSString* const UITextFieldTextDidBeginEditingNotification = @"UITextFieldTextDidBeginEditingNotification";
NSString* const UITextFieldTextDidChangeNotification = @"UITextFieldTextDidChangeNotification";
NSString* const UITextFieldTextDidEndEditingNotification = @"UITextFieldTextDidEndEditingNotification";

// This is a hidden view control used to steal focus typically when a text field OSK (on screen keyboard) is dismissed.
@interface _UIHiddenButtonView : UIView
@end

@implementation _UIHiddenButtonView
@end

@implementation UITextField {
    StrongId<NSString> _text;
    StrongId<NSString> _placeHolder;
    StrongId<UIFont> _font;
    StrongId<UIColor> _textColor;
    StrongId<UIColor> _backgroundColor;
    UITextAlignment _alignment;
    UITextBorderStyle _borderStyle;
    BOOL _secureTextMode;
    BOOL _isFirstResponder;

    // backing keyboard Behavior
    UITextAutocapitalizationType _autoCapitalizatonType;
    UIKeyboardType _keyboardType;
    UITextAutocorrectionType _autoCorrectionType;
    UITextSpellCheckingType _spellCheckingType;
    BOOL _enablesReturnKeyAutomatically;

    // backing xaml textbox and passwordBox
    WXCTextBox* _textBox;
    WXCPasswordBox* _passwordBox;

    // lock use to access the properties
    StrongId<NSRecursiveLock> _secureModeLock;

    // dummy control to steal the focus
    StrongId<WXCButton> _dummyButton;
    StrongId<_UIHiddenButtonView> _hiddenView;
}

//
// Properties Accessing the Text Attributes
//
/**
 @Status Interoperable
*/
- (void)setText:(NSString*)text {
    if (![_text isEqualToString:text]) {
        if (text != nil) {
            _text = [text copy];
        } else {
            _text = nil;
        }

        [_secureModeLock lock];
        if (_secureTextMode) {
            _passwordBox.password = _text;
        } else {
            _textBox.text = _text;
            // Ensure caret at end of field in case we programmatically
            // gain focus (becomeFirstResponder) after the text is set:
            _textBox.selectionStart = [_text length];
            _textBox.selectionLength = 0;
        }
        [_secureModeLock unlock];
    }
}

/**
{'fraction_non_alphanumeric': 0.12976770863206194, 'fraction_numerical': 0.0012905075996558647, 'mean_word_length': 4.1024140453547915, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7635547', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1980, 'n_words': 569}