diff --git a/advanced_ui_component/chip/interfaces/chip_v16.js b/advanced_ui_component/chip/interfaces/chip_v16.js index b96be7780a59151ff13d23091153dc5cb7026bdc..013daaa38389191507b3bc91298078744ff7eed4 100644 --- a/advanced_ui_component/chip/interfaces/chip_v16.js +++ b/advanced_ui_component/chip/interfaces/chip_v16.js @@ -48,6 +48,30 @@ export var AccessibilitySelectedType; const RESOURCE_TYPE_STRING = 10003; const RESOURCE_TYPE_FLOAT = 10002; const RESOURCE_TYPE_INTEGER = 10007; + +class LengthMetricsCache { + static _cache = new Map(); + static get(key, defaultValue) { + if (this._cache.has(key)) { + return this._cache.get(key); + } + try { + const res = { + id: -1, + type: 10002, + params: [key], + bundleName: '__harDefaultBundleName__', + moduleName: '__harDefaultModuleName__', + }; + const metrics = LengthMetrics.resource(res); + this._cache.set(key, metrics); + return metrics; + } catch (error) { + return defaultValue; + } + } +} + export function Chip(options, parent = null) { const __options__ = options; { @@ -277,38 +301,14 @@ export class ChipComponent extends ViewPU { }, defaultFontSize: 14, localizedNormalMargin: { - start: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_normal_text_margin'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), - end: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_normal_text_margin'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), + start: LengthMetricsCache.get('sys.float.chip_normal_text_margin', LengthMetrics.vp(6)), + end: LengthMetricsCache.get('sys.float.chip_normal_text_margin', LengthMetrics.vp(6)), top: LengthMetrics.vp(0), bottom: LengthMetrics.vp(0), }, localizedSmallMargin: { - start: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_small_text_margin'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), - end: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_small_text_margin'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), + start: LengthMetricsCache.get('sys.float.chip_small_text_margin', LengthMetrics.vp(4)), + end: LengthMetricsCache.get('sys.float.chip_small_text_margin', LengthMetrics.vp(4)), top: LengthMetrics.vp(0), bottom: LengthMetrics.vp(0), }, @@ -409,20 +409,8 @@ export class ChipComponent extends ViewPU { moduleName: '__harDefaultModuleName__', }, ], - normalSymbolFontSize: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_normal_icon_size'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }).value, - smallSymbolFontSize: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_small_icon_size'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }).value, + normalSymbolFontSize: LengthMetricsCache.get('sys.float.chip_normal_icon_size', LengthMetrics.vp(16)).value, + smallSymbolFontSize: LengthMetricsCache.get('sys.float.chip_small_icon_size', LengthMetrics.vp(16)).value, defaultEffect: -1, }, chipNode: { @@ -538,74 +526,26 @@ export class ChipComponent extends ViewPU { moduleName: '__harDefaultModuleName__', }, localizedNormalPadding: { - start: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_normal_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), - end: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_normal_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), + start: LengthMetricsCache.get('sys.float.chip_normal_text_padding', LengthMetrics.vp(16)), + end: LengthMetricsCache.get('sys.float.chip_normal_text_padding', LengthMetrics.vp(16)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4), }, localizedSmallPadding: { - start: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_small_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), - end: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_small_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), + start: LengthMetricsCache.get('sys.float.chip_small_text_padding', LengthMetrics.vp(12)), + end: LengthMetricsCache.get('sys.float.chip_small_text_padding', LengthMetrics.vp(12)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4), }, localizedActivatedNormalPadding: { - start: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_activated_normal_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), - end: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_activated_normal_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), + start: LengthMetricsCache.get('sys.float.chip_activated_normal_text_padding', LengthMetrics.vp(16)), + end: LengthMetricsCache.get('sys.float.chip_activated_normal_text_padding', LengthMetrics.vp(16)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4), }, localizedActivatedSmallPadding: { - start: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_activated_small_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), - end: LengthMetrics.resource({ - id: -1, - type: 10002, - params: ['sys.float.chip_activated_small_text_padding'], - bundleName: '__harDefaultBundleName__', - moduleName: '__harDefaultModuleName__', - }), + start: LengthMetricsCache.get('sys.float.chip_activated_small_text_padding', LengthMetrics.vp(12)), + end: LengthMetricsCache.get('sys.float.chip_activated_small_text_padding', LengthMetrics.vp(12)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4), }, diff --git a/advanced_ui_component/chip/source/chip_v16.ets b/advanced_ui_component/chip/source/chip_v16.ets index 626b15dd7b0aa14312ef8f70131de771c1299695..f49124275fc3f7ad2ce21d7d0e1c1747c98e15a3 100644 --- a/advanced_ui_component/chip/source/chip_v16.ets +++ b/advanced_ui_component/chip/source/chip_v16.ets @@ -225,6 +225,29 @@ interface ChipOptions { onClicked?: () => void } +class LengthMetricsCache { + private static _cache: Map = new Map(); + public static get(key: string, defaultValue: LengthMetrics): LengthMetrics { + if (LengthMetricsCache._cache.has(key)) { + return LengthMetricsCache._cache.get(key)!; + } + try { + const res: Resource = { + id: -1, + type: 10002, + params: [key], + bundleName: '__harDefaultBundleName__', + moduleName: '__harDefaultModuleName__', + }; + const metrics = LengthMetrics.resource(res); + LengthMetricsCache._cache.set(key, metrics); + return metrics; + } catch (error) { + return defaultValue; + } + } +} + @Builder export function Chip(options: ChipOptions) { ChipComponent({ @@ -293,14 +316,14 @@ export struct ChipComponent { }, defaultFontSize: 14, localizedNormalMargin: { - start: LengthMetrics.resource($r('sys.float.chip_normal_text_margin')), - end: LengthMetrics.resource($r('sys.float.chip_normal_text_margin')), + start: LengthMetricsCache.get('sys.float.chip_normal_text_margin', LengthMetrics.vp(6)), + end: LengthMetricsCache.get('sys.float.chip_normal_text_margin', LengthMetrics.vp(6)), top: LengthMetrics.vp(0), bottom: LengthMetrics.vp(0) }, localizedSmallMargin: { - start: LengthMetrics.resource($r('sys.float.chip_small_text_margin')), - end: LengthMetrics.resource($r('sys.float.chip_small_text_margin')), + start: LengthMetricsCache.get('sys.float.chip_small_text_margin', LengthMetrics.vp(4)), + end: LengthMetricsCache.get('sys.float.chip_small_text_margin', LengthMetrics.vp(4)), top: LengthMetrics.vp(0), bottom: LengthMetrics.vp(0), } @@ -325,8 +348,10 @@ export struct ChipComponent { defaultSymbol: { normalFontColor: [$r('sys.color.chip_usually_icon_color')], activatedFontColor: [$r('sys.color.chip_active_icon_color')], - normalSymbolFontSize: LengthMetrics.resource($r('sys.float.chip_normal_icon_size')).value as Length, - smallSymbolFontSize: LengthMetrics.resource($r('sys.float.chip_small_icon_size')).value as Length, + normalSymbolFontSize: + LengthMetricsCache.get('sys.float.chip_normal_icon_size', LengthMetrics.vp(16)).value as Length, + smallSymbolFontSize: + LengthMetricsCache.get('sys.float.chip_small_icon_size', LengthMetrics.vp(16)).value as Length, defaultEffect: -1, }, chipNode: { @@ -352,26 +377,26 @@ export struct ChipComponent { focusBtnScaleX: $r('sys.float.chip_focused_btn_scale'), focusBtnScaleY: $r('sys.float.chip_focused_btn_scale'), localizedNormalPadding: { - start: LengthMetrics.resource($r('sys.float.chip_normal_text_padding')), - end: LengthMetrics.resource($r('sys.float.chip_normal_text_padding')), + start: LengthMetricsCache.get('sys.float.chip_normal_text_padding', LengthMetrics.vp(16)), + end: LengthMetricsCache.get('sys.float.chip_normal_text_padding', LengthMetrics.vp(16)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4) }, localizedSmallPadding: { - start: LengthMetrics.resource($r('sys.float.chip_small_text_padding')), - end: LengthMetrics.resource($r('sys.float.chip_small_text_padding')), + start: LengthMetricsCache.get('sys.float.chip_small_text_padding', LengthMetrics.vp(12)), + end: LengthMetricsCache.get('sys.float.chip_small_text_padding', LengthMetrics.vp(12)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4) }, localizedActivatedNormalPadding: { - start: LengthMetrics.resource($r('sys.float.chip_activated_normal_text_padding')), - end: LengthMetrics.resource($r('sys.float.chip_activated_normal_text_padding')), + start: LengthMetricsCache.get('sys.float.chip_activated_normal_text_padding', LengthMetrics.vp(16)), + end: LengthMetricsCache.get('sys.float.chip_activated_normal_text_padding', LengthMetrics.vp(16)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4) }, localizedActivatedSmallPadding: { - start: LengthMetrics.resource($r('sys.float.chip_activated_small_text_padding')), - end: LengthMetrics.resource($r('sys.float.chip_activated_small_text_padding')), + start: LengthMetricsCache.get('sys.float.chip_activated_small_text_padding', LengthMetrics.vp(12)), + end: LengthMetricsCache.get('sys.float.chip_activated_small_text_padding', LengthMetrics.vp(12)), top: LengthMetrics.vp(4), bottom: LengthMetrics.vp(4) },