From a5edb0b421afaaa34290dc3ada73da6bca3c556a Mon Sep 17 00:00:00 2001 From: fragosti Date: Tue, 23 Oct 2018 15:54:18 -0700 Subject: polish: improve scaling significantly --- .../instant/src/components/asset_amount_input.tsx | 3 +- .../src/components/scaling_amount_input.tsx | 7 +-- packages/instant/src/components/scaling_input.tsx | 68 +++++++++++++++------- 3 files changed, 50 insertions(+), 28 deletions(-) (limited to 'packages/instant') diff --git a/packages/instant/src/components/asset_amount_input.tsx b/packages/instant/src/components/asset_amount_input.tsx index bfa857f0e..8cbf2b37a 100644 --- a/packages/instant/src/components/asset_amount_input.tsx +++ b/packages/instant/src/components/asset_amount_input.tsx @@ -39,8 +39,7 @@ export class AssetAmountInput extends React.Component void; @@ -23,11 +22,11 @@ export class ScalingAmountInput extends React.Component onFontSizeChange: util.boundNoop, }; public render(): React.ReactNode { - const { startWidthCh, endWidthCh, fontColor, maxFontSizePx, value, onFontSizeChange } = this.props; + const { textLengthThreshold, fontColor, maxFontSizePx, value, onFontSizeChange } = this.props; return ( { public static defaultProps = { onChange: util.boundNoop, onFontSizeChange: util.boundNoop, - maxLength: 9, + maxLength: 8, + scalingSettings: defaultScalingSettings, }; public state = { - fixedWidthInPxIfExists: undefined, + inputWidthPxAtPhaseChange: undefined, }; private readonly _inputRef = React.createRef(); - public static getPhase(endWidthCh: number, value: string): ScalingInputPhase { - if (value.length <= endWidthCh) { + public static getPhase(textLengthThreshold: number, value: string): ScalingInputPhase { + if (value.length <= textLengthThreshold) { return ScalingInputPhase.FixedFontSize; } return ScalingInputPhase.ScalingFontSize; } public static getPhaseFromProps(props: ScalingInputProps): ScalingInputPhase { - const { value, endWidthCh } = props; - return ScalingInput.getPhase(endWidthCh, value); + const { value, textLengthThreshold } = props; + return ScalingInput.getPhase(textLengthThreshold, value); } public static calculateFontSize( - endWidthCh: number, + textLengthThreshold: number, maxFontSizePx: number, phase: ScalingInputPhase, value: string, + percentageToReduceFontSizePerCharacter: number, ): number { if (phase !== ScalingInputPhase.ScalingFontSize) { return maxFontSizePx; } - const charactersOverMax = value.length - endWidthCh; - const scalingFactor = (1 - percentageToReduceByPerCharacter) ** charactersOverMax; + const charactersOverMax = value.length - textLengthThreshold; + const scalingFactor = (1 - percentageToReduceFontSizePerCharacter) ** charactersOverMax; const fontSize = scalingFactor * maxFontSizePx; return fontSize; } public static calculateFontSizeFromProps(props: ScalingInputProps, phase: ScalingInputPhase): number { - const { endWidthCh, value, maxFontSizePx } = props; - return ScalingInput.calculateFontSize(endWidthCh, maxFontSizePx, phase, value); + const { textLengthThreshold, value, maxFontSizePx, scalingSettings } = props; + return ScalingInput.calculateFontSize( + textLengthThreshold, + maxFontSizePx, + phase, + value, + scalingSettings.percentageToReduceFontSizePerCharacter, + ); } public getSnapshotBeforeUpdate(): ScalingInputSnapshot { return { @@ -87,13 +106,13 @@ export class ScalingInput extends React.Component { - const { value, endWidthCh } = this.props; + const { value, textLengthThreshold, scalingSettings } = this.props; if (_.isEmpty(value)) { return `${this.props.emptyInputWidthCh}ch`; } - if (!_.isUndefined(this.state.fixedWidthInPxIfExists)) { - return `${this.state.fixedWidthInPxIfExists}px`; - } switch (phase) { case ScalingInputPhase.FixedFontSize: return `${value.length}ch`; case ScalingInputPhase.ScalingFontSize: - return `${endWidthCh}ch`; + const { inputWidthPxAtPhaseChange } = this.state; + if (!_.isUndefined(inputWidthPxAtPhaseChange)) { + const charactersOverMax = value.length - textLengthThreshold; + const scalingFactor = + (scalingSettings.percentageToIncreaseWidthPerCharacter + 1) ** charactersOverMax; + const width = scalingFactor * inputWidthPxAtPhaseChange; + return `${width}px`; + } + return `${textLengthThreshold}ch`; default: return '1ch'; } -- cgit v1.2.3