Browse Source

Merge pull request #5462 from ssaket/inputText

select all on focus in InputText
David Catuhe 6 years ago
parent
commit
e7e83f6dde
2 changed files with 36 additions and 15 deletions
  1. 1 1
      dist/preview release/what's new.md
  2. 35 14
      gui/src/2D/controls/inputText.ts

+ 1 - 1
dist/preview release/what's new.md

@@ -29,7 +29,7 @@
 - Added support for performing operations like select all, text highlight, delete selected in `inputText` ([Saket Saurabh](https://github.com/ssaket))
 - Added support for performing operations like select all, text highlight, delete selected in `inputText` ([Saket Saurabh](https://github.com/ssaket))
 - Added `inputText.onTextCopyObservable`, `inputText.onTextCutObservable` and `inputText.onTextPasteObservable` to inputText ([Saket Saurabh](https://github.com/ssaket))
 - Added `inputText.onTextCopyObservable`, `inputText.onTextCutObservable` and `inputText.onTextPasteObservable` to inputText ([Saket Saurabh](https://github.com/ssaket))
 - Added `AdvancedDynamicTexture.onClipboardObservable` to observe for clipboard events in AdvancedDynamicTexture([Saket Saurabh](https://github.com/ssaket))
 - Added `AdvancedDynamicTexture.onClipboardObservable` to observe for clipboard events in AdvancedDynamicTexture([Saket Saurabh](https://github.com/ssaket))
-
+- Added `inputText.onFocusSelectAll` to allow complete selection of text on focus event.([Saket Saurabh](https://github.com/ssaket))
 
 
 ### Core Engine
 ### Core Engine
 
 

+ 35 - 14
gui/src/2D/controls/inputText.ts

@@ -34,6 +34,7 @@ export class InputText extends Control implements IFocusableControl {
     private _highlightedText = "";
     private _highlightedText = "";
     private _startHighlightIndex = 0;
     private _startHighlightIndex = 0;
     private _endHighlightIndex = 0;
     private _endHighlightIndex = 0;
+    private _onFocusSelectAll = false;
     private _onClipboardObserver: Nullable<Observer<ClipboardInfo>>;
     private _onClipboardObserver: Nullable<Observer<ClipboardInfo>>;
     private _onPointerDblTapObserver: Nullable<Observer<PointerInfo>>;
     private _onPointerDblTapObserver: Nullable<Observer<PointerInfo>>;
 
 
@@ -80,7 +81,7 @@ export class InputText extends Control implements IFocusableControl {
         }
         }
     }
     }
 
 
-    /** Gets and sets the text highlighter transparency; default: 0.4 */
+    /** Gets or sets the text highlighter transparency; default: 0.4 */
     public get highligherOpacity(): number {
     public get highligherOpacity(): number {
         return this._highligherOpacity;
         return this._highligherOpacity;
     }
     }
@@ -92,8 +93,21 @@ export class InputText extends Control implements IFocusableControl {
         this._highligherOpacity = value;
         this._highligherOpacity = value;
         this._markAsDirty();
         this._markAsDirty();
     }
     }
+    /** Gets or sets a boolean indicating whether to select complete text by default on input focus */
+    public get onFocusSelectAll(): boolean {
+        return this._onFocusSelectAll;
+    }
+
+    public set onFocusSelectAll(value: boolean) {
+        if (this._onFocusSelectAll === value) {
+            return;
+        }
+
+        this._onFocusSelectAll = value;
+        this._markAsDirty();
+    }
 
 
-    /** Gets and sets the text hightlight color */
+    /** Gets or sets the text hightlight color */
     public get textHighlightColor(): string {
     public get textHighlightColor(): string {
         return this._textHighlightColor;
         return this._textHighlightColor;
     }
     }
@@ -368,6 +382,10 @@ export class InputText extends Control implements IFocusableControl {
             });
             });
         }
         }
 
 
+        if (this._onFocusSelectAll) {
+            this._selectAllText();
+        }
+
     }
     }
 
 
     protected _getTypeName(): string {
     protected _getTypeName(): string {
@@ -395,19 +413,8 @@ export class InputText extends Control implements IFocusableControl {
 
 
         //select all
         //select all
         if (evt && (evt.ctrlKey || evt.metaKey) && keyCode === 65) {
         if (evt && (evt.ctrlKey || evt.metaKey) && keyCode === 65) {
-
-            this._blinkIsEven = false;
-            this._isTextHighlightOn = true;
+            this._selectAllText();
             evt.preventDefault();
             evt.preventDefault();
-
-            //if already highlighted pass
-            if (this._highlightedText) {
-                return;
-            }
-
-            this._startHighlightIndex = 0;
-            this._endHighlightIndex = this._text.length;
-            this._cursorOffset = 0;
             return;
             return;
         }
         }
         // Specific cases
         // Specific cases
@@ -560,6 +567,20 @@ export class InputText extends Control implements IFocusableControl {
         this._isTextHighlightOn = true;
         this._isTextHighlightOn = true;
         this._blinkIsEven = false;
         this._blinkIsEven = false;
     }
     }
+    /** @hidden */
+    private _selectAllText() {
+        this._blinkIsEven = false;
+        this._isTextHighlightOn = true;
+
+        //if already highlighted pass
+        if (this._highlightedText) {
+            return;
+        }
+
+        this._startHighlightIndex = 0;
+        this._endHighlightIndex = this._text.length;
+        this._cursorOffset = 0;
+    }
 
 
     /**
     /**
      * Handles the keyboard event
      * Handles the keyboard event