status-desktop/ui/app/AppLayouts/Wallet/addaccount/panels/DerivationPathInput/Element.qml

62 lines
1.7 KiB
QML
Raw Normal View History

feat(wallet): implement DerivationPathInput control The enforcing of the derivation path editing rules is done in a structured way by handling all the changes on the array of `Element` stored in d.elements and then recomposing the HTML string to be displayed after every change. Main limitation is the workaround in `onTextChanged` that regenerates the text in order to dismiss foreign characters introduced by pasting which I couldn't find a way to disable without disabling also the ability to copy content to clipboard. Highlights: - Implement DerivationPathInput control that intercepts the modifiable keyboard events in order to edit the visible TextEdit.text while respecting the requirements of the derivation path editing - Implement a JS Controller that handles the logic of the decomposing and recomposing the derivation path string - Add anew StatusQ with the TextEdit basic look and feel to be used in DerivationPathInput control without duplicating the style - Allow passing modifiable events that are not generating characters in order to allow copy to clipboard - Disable add account when control is in error state - Limit to maximum 5 elements in the derivation path Testing: - Integrate the control with StoryBook for a quick preview of the control - Add unit tests for the Controller basic functionality and regression for the main control Item - Removed forcing x64 architecture on apple arm64 hardware from the storybook build configuration Note: initially the implementation was suppose to be simple parse the derivation path string edit elements and format it. However, I could not find a quick way fix the circular dependency issue between editing the text and reformatting it. The solution was to use a one way from the structured data to the formatted string which complicates the implementation logic. Closes: #9890
2023-03-15 22:34:48 +00:00
import QtQuick 2.15
QtObject {
required property string content
required property int startIndex
// Non-inclusive
required property int endIndex
required property int contentType
property bool isFrozen: false
enum ContentType {
Number,
Separator,
Base
}
function length() {
return endIndex - startIndex
}
function isHardened() {
return contentType === Element.ContentType.Separator && (content[0] === "'" || content[0] === "")
}
/// Returns NaN if not a number
function number() {
return (contentType === Element.ContentType.Number && /^\d+$/.test(content)) ? parseInt(content, 10) : NaN
}
function isNumber() {
return contentType === Element.ContentType.Number
}
function validateNumber() {
return contentType !== Element.ContentType.Number || !isNaN(number())
}
function isSeparator() {
return contentType === Element.ContentType.Separator
}
function isEmptyNumber() {
return contentType === Element.ContentType.Number && content.length === 0
}
function isBase() {
return contentType === Element.ContentType.Base
}
/// Compares for incomplete typed separators
function isSimilar(other) {
return contentType === other.contentType
&& (contentType === Element.ContentType.Number
? number() === other.number()
: (isHardened() === other.isHardened()))
}
/// Compares
function isSame(other) {
return contentType === other.contentType && content === other.content && startIndex == other.startIndex && endIndex == other.endIndex
}
}