/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 2022 Martin Grimme <martin.grimme@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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.
*******************************************************************************/
require "shellfish/ui";
/**
* Element representing a single-line text entry box.
*
* @memberof ui
* @name TextEntry
* @class
* @extends html.Box
*
* @property {bool} clearButton - (default: `false`) If `true`, shows a button to clear the text.
* @property {string} horizontalAlignment - (default: `"left"`) Horizontal text alignment: `left|center|right`
* @property {string} text - (default: `""`) The current text.
* @property {bool} password - (default: `false`) Whether this is a password entry, i.e. the actual text is not displayed.
* @property {string} pattern - (default: `".*"`) A regular expression for limiting the allowed text input, e.g. `"[0-9]*"` would allow entering numbers only.
*/
MouseBox {
id: box
property text: ""
property password: false
property pattern: ".*"
property horizontalAlignment: "left"
property clearButton: false
property active: false
property selectRange: input.selectRange
width: theme.itemWidthLarge
height: theme.itemHeightSmall
opacity: enabled && ancestorsEnabled ? 1 : 0.6
color: theme.contentBackgroundColor
borderColor: theme.borderColor
borderWidth: 1
borderRadius: theme.borderRadius
layout: "center-row"
onActiveChanged: () =>
{
if (active && ! input.focus)
{
input.focus = true;
}
}
onTextChanged: () =>
{
if (text !== input.text)
{
input.text = text;
}
}
onPointerDown: ev => { ev.accepted = true; }
Box {
id: flashBox
position: "free"
fillWidth: true
fillHeight: true
color: "red"
opacity: 0
}
TextInput {
id: input
fillWidth: true
marginLeft: theme.paddingSmall
marginRight: theme.paddingSmall
horizontalAlignment: box.horizontalAlignment
fontSize: theme.fontSizeMedium
color: theme.primaryColor
selectionBackgroundColor: theme.highlightBackgroundColor
selectionColor: theme.highlightColor
password: box.password
pattern: box.pattern
canFocus: true
onTextChanged: { box.text = text; }
onReject: { flashAnimation.start(); }
onFocusChanged: () =>
{
if (focus)
{
box.active = true;
selectRange(0, text.length);
}
else
{
box.active = false;
}
}
}
MouseBox {
visible: box.clearButton
width: theme.itemWidthSmall
fillHeight: true
layout: "center"
Label {
color: theme.secondaryColor
text: "[icon:core-keyboard_backspace]"
}
onClick: ev =>
{
ev.accepted = true;
input.text = "";
}
}
NumberAnimation {
id: flashAnimation
from: 1
to: 0
duration: 300
easing: "InOutQuad"
onNext: {
flashBox.opacity = arguments[0];
}
}
FocusIndicator { visible: input.focus }
}