123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ciif (!("finalizeConstruction" in ViewPU.prototype)) { 1723b3eb3cSopenharmony_ci Reflect.set(ViewPU.prototype, "finalizeConstruction", () => { }); 1823b3eb3cSopenharmony_ci} 1923b3eb3cSopenharmony_ciconst EMPTY_STRING = ''; 2023b3eb3cSopenharmony_ciconst MAX_PROGRESS = 100; 2123b3eb3cSopenharmony_ciconst MAX_PERCENTAGE = '100%'; 2223b3eb3cSopenharmony_ciconst MIN_PERCENTAGE = '0%'; 2323b3eb3cSopenharmony_ciconst TEXT_OPACITY = 0.4; 2423b3eb3cSopenharmony_ciconst BUTTON_NORMARL_WIDTH = 44; 2523b3eb3cSopenharmony_ciconst BUTTON_NORMARL_HEIGHT = 28; 2623b3eb3cSopenharmony_ciconst BUTTON_BORDER_RADIUS = 14; 2723b3eb3cSopenharmony_ciconst TEXT_ENABLE = 1.0; 2823b3eb3cSopenharmony_ciconst PROGRESS_BUTTON_PROGRESS_KEY = 'progress_button_progress_key'; 2923b3eb3cSopenharmony_ciconst PROGRESS_BUTTON_PRIMARY_FONT_KEY = 'progress_button_primary_font_key'; 3023b3eb3cSopenharmony_ciconst PROGRESS_BUTTON_CONTAINER_BACKGROUND_COLOR_KEY = 'progress_button_container_background_color_key'; 3123b3eb3cSopenharmony_ciconst PROGRESS_BUTTON_EMPHASIZE_SECONDARY_BUTTON_KEY = 'progress_button_emphasize_secondary_button_key'; 3223b3eb3cSopenharmony_ciexport class ProgressButton extends ViewPU { 3323b3eb3cSopenharmony_ci constructor(k1, l1, m1, n1 = -1, o1 = undefined, p1) { 3423b3eb3cSopenharmony_ci super(k1, m1, n1, p1); 3523b3eb3cSopenharmony_ci if (typeof o1 === "function") { 3623b3eb3cSopenharmony_ci this.paramsGenerator_ = o1; 3723b3eb3cSopenharmony_ci } 3823b3eb3cSopenharmony_ci this.__progress = new SynchedPropertySimpleOneWayPU(l1.progress, this, "progress"); 3923b3eb3cSopenharmony_ci this.__textProgress = new ObservedPropertySimplePU(EMPTY_STRING, this, "textProgress"); 4023b3eb3cSopenharmony_ci this.__content = new SynchedPropertySimpleOneWayPU(l1.content, this, "content"); 4123b3eb3cSopenharmony_ci this.__isLoading = new ObservedPropertySimplePU(false, this, "isLoading"); 4223b3eb3cSopenharmony_ci this.progressButtonWidth = BUTTON_NORMARL_WIDTH; 4323b3eb3cSopenharmony_ci this.clickCallback = () => { }; 4423b3eb3cSopenharmony_ci this.__enable = new SynchedPropertySimpleOneWayPU(l1.enable, this, "enable"); 4523b3eb3cSopenharmony_ci this.__progressColor = new ObservedPropertyObjectPU('#330A59F7', this, "progressColor"); 4623b3eb3cSopenharmony_ci this.__containerBorderColor = new ObservedPropertyObjectPU('#330A59F7', this, "containerBorderColor"); 4723b3eb3cSopenharmony_ci this.__containerBackgroundColor = new ObservedPropertyObjectPU({ "id": -1, "type": 10001, 4823b3eb3cSopenharmony_ci params: ['sys.color.ohos_id_color_foreground_contrary'], 4923b3eb3cSopenharmony_ci "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }, this, "containerBackgroundColor"); 5023b3eb3cSopenharmony_ci this.__textHeight = new ObservedPropertyObjectPU(BUTTON_NORMARL_HEIGHT, this, "textHeight"); 5123b3eb3cSopenharmony_ci this.__buttonBorderRadius = new ObservedPropertySimplePU(BUTTON_BORDER_RADIUS, this, "buttonBorderRadius"); 5223b3eb3cSopenharmony_ci this.setInitiallyProvidedValue(l1); 5323b3eb3cSopenharmony_ci this.declareWatch("progress", this.getProgressContext); 5423b3eb3cSopenharmony_ci this.declareWatch("isLoading", this.getLoadingProgress); 5523b3eb3cSopenharmony_ci this.finalizeConstruction(); 5623b3eb3cSopenharmony_ci } 5723b3eb3cSopenharmony_ci setInitiallyProvidedValue(j1) { 5823b3eb3cSopenharmony_ci if (j1.textProgress !== undefined) { 5923b3eb3cSopenharmony_ci this.textProgress = j1.textProgress; 6023b3eb3cSopenharmony_ci } 6123b3eb3cSopenharmony_ci if (j1.content === undefined) { 6223b3eb3cSopenharmony_ci this.__content.set(EMPTY_STRING); 6323b3eb3cSopenharmony_ci } 6423b3eb3cSopenharmony_ci if (j1.isLoading !== undefined) { 6523b3eb3cSopenharmony_ci this.isLoading = j1.isLoading; 6623b3eb3cSopenharmony_ci } 6723b3eb3cSopenharmony_ci if (j1.progressButtonWidth !== undefined) { 6823b3eb3cSopenharmony_ci this.progressButtonWidth = j1.progressButtonWidth; 6923b3eb3cSopenharmony_ci } 7023b3eb3cSopenharmony_ci if (j1.clickCallback !== undefined) { 7123b3eb3cSopenharmony_ci this.clickCallback = j1.clickCallback; 7223b3eb3cSopenharmony_ci } 7323b3eb3cSopenharmony_ci if (j1.enable === undefined) { 7423b3eb3cSopenharmony_ci this.__enable.set(true); 7523b3eb3cSopenharmony_ci } 7623b3eb3cSopenharmony_ci if (j1.progressColor !== undefined) { 7723b3eb3cSopenharmony_ci this.progressColor = j1.progressColor; 7823b3eb3cSopenharmony_ci } 7923b3eb3cSopenharmony_ci if (j1.containerBorderColor !== undefined) { 8023b3eb3cSopenharmony_ci this.containerBorderColor = j1.containerBorderColor; 8123b3eb3cSopenharmony_ci } 8223b3eb3cSopenharmony_ci if (j1.containerBackgroundColor !== undefined) { 8323b3eb3cSopenharmony_ci this.containerBackgroundColor = j1.containerBackgroundColor; 8423b3eb3cSopenharmony_ci } 8523b3eb3cSopenharmony_ci if (j1.textHeight !== undefined) { 8623b3eb3cSopenharmony_ci this.textHeight = j1.textHeight; 8723b3eb3cSopenharmony_ci } 8823b3eb3cSopenharmony_ci if (j1.buttonBorderRadius !== undefined) { 8923b3eb3cSopenharmony_ci this.buttonBorderRadius = j1.buttonBorderRadius; 9023b3eb3cSopenharmony_ci } 9123b3eb3cSopenharmony_ci } 9223b3eb3cSopenharmony_ci updateStateVars(i1) { 9323b3eb3cSopenharmony_ci this.__progress.reset(i1.progress); 9423b3eb3cSopenharmony_ci this.__content.reset(i1.content); 9523b3eb3cSopenharmony_ci this.__enable.reset(i1.enable); 9623b3eb3cSopenharmony_ci } 9723b3eb3cSopenharmony_ci purgeVariableDependenciesOnElmtId(h1) { 9823b3eb3cSopenharmony_ci this.__progress.purgeDependencyOnElmtId(h1); 9923b3eb3cSopenharmony_ci this.__textProgress.purgeDependencyOnElmtId(h1); 10023b3eb3cSopenharmony_ci this.__content.purgeDependencyOnElmtId(h1); 10123b3eb3cSopenharmony_ci this.__isLoading.purgeDependencyOnElmtId(h1); 10223b3eb3cSopenharmony_ci this.__enable.purgeDependencyOnElmtId(h1); 10323b3eb3cSopenharmony_ci this.__progressColor.purgeDependencyOnElmtId(h1); 10423b3eb3cSopenharmony_ci this.__containerBorderColor.purgeDependencyOnElmtId(h1); 10523b3eb3cSopenharmony_ci this.__containerBackgroundColor.purgeDependencyOnElmtId(h1); 10623b3eb3cSopenharmony_ci this.__textHeight.purgeDependencyOnElmtId(h1); 10723b3eb3cSopenharmony_ci this.__buttonBorderRadius.purgeDependencyOnElmtId(h1); 10823b3eb3cSopenharmony_ci } 10923b3eb3cSopenharmony_ci aboutToBeDeleted() { 11023b3eb3cSopenharmony_ci this.__progress.aboutToBeDeleted(); 11123b3eb3cSopenharmony_ci this.__textProgress.aboutToBeDeleted(); 11223b3eb3cSopenharmony_ci this.__content.aboutToBeDeleted(); 11323b3eb3cSopenharmony_ci this.__isLoading.aboutToBeDeleted(); 11423b3eb3cSopenharmony_ci this.__enable.aboutToBeDeleted(); 11523b3eb3cSopenharmony_ci this.__progressColor.aboutToBeDeleted(); 11623b3eb3cSopenharmony_ci this.__containerBorderColor.aboutToBeDeleted(); 11723b3eb3cSopenharmony_ci this.__containerBackgroundColor.aboutToBeDeleted(); 11823b3eb3cSopenharmony_ci this.__textHeight.aboutToBeDeleted(); 11923b3eb3cSopenharmony_ci this.__buttonBorderRadius.aboutToBeDeleted(); 12023b3eb3cSopenharmony_ci SubscriberManager.Get().delete(this.id__()); 12123b3eb3cSopenharmony_ci this.aboutToBeDeletedInternal(); 12223b3eb3cSopenharmony_ci } 12323b3eb3cSopenharmony_ci get progress() { 12423b3eb3cSopenharmony_ci return this.__progress.get(); 12523b3eb3cSopenharmony_ci } 12623b3eb3cSopenharmony_ci set progress(g1) { 12723b3eb3cSopenharmony_ci this.__progress.set(g1); 12823b3eb3cSopenharmony_ci } 12923b3eb3cSopenharmony_ci get textProgress() { 13023b3eb3cSopenharmony_ci return this.__textProgress.get(); 13123b3eb3cSopenharmony_ci } 13223b3eb3cSopenharmony_ci set textProgress(f1) { 13323b3eb3cSopenharmony_ci this.__textProgress.set(f1); 13423b3eb3cSopenharmony_ci } 13523b3eb3cSopenharmony_ci get content() { 13623b3eb3cSopenharmony_ci return this.__content.get(); 13723b3eb3cSopenharmony_ci } 13823b3eb3cSopenharmony_ci set content(e1) { 13923b3eb3cSopenharmony_ci this.__content.set(e1); 14023b3eb3cSopenharmony_ci } 14123b3eb3cSopenharmony_ci get isLoading() { 14223b3eb3cSopenharmony_ci return this.__isLoading.get(); 14323b3eb3cSopenharmony_ci } 14423b3eb3cSopenharmony_ci set isLoading(d1) { 14523b3eb3cSopenharmony_ci this.__isLoading.set(d1); 14623b3eb3cSopenharmony_ci } 14723b3eb3cSopenharmony_ci get enable() { 14823b3eb3cSopenharmony_ci return this.__enable.get(); 14923b3eb3cSopenharmony_ci } 15023b3eb3cSopenharmony_ci set enable(c1) { 15123b3eb3cSopenharmony_ci this.__enable.set(c1); 15223b3eb3cSopenharmony_ci } 15323b3eb3cSopenharmony_ci get progressColor() { 15423b3eb3cSopenharmony_ci return this.__progressColor.get(); 15523b3eb3cSopenharmony_ci } 15623b3eb3cSopenharmony_ci set progressColor(b1) { 15723b3eb3cSopenharmony_ci this.__progressColor.set(b1); 15823b3eb3cSopenharmony_ci } 15923b3eb3cSopenharmony_ci get containerBorderColor() { 16023b3eb3cSopenharmony_ci return this.__containerBorderColor.get(); 16123b3eb3cSopenharmony_ci } 16223b3eb3cSopenharmony_ci set containerBorderColor(a1) { 16323b3eb3cSopenharmony_ci this.__containerBorderColor.set(a1); 16423b3eb3cSopenharmony_ci } 16523b3eb3cSopenharmony_ci get containerBackgroundColor() { 16623b3eb3cSopenharmony_ci return this.__containerBackgroundColor.get(); 16723b3eb3cSopenharmony_ci } 16823b3eb3cSopenharmony_ci set containerBackgroundColor(z) { 16923b3eb3cSopenharmony_ci this.__containerBackgroundColor.set(z); 17023b3eb3cSopenharmony_ci } 17123b3eb3cSopenharmony_ci get textHeight() { 17223b3eb3cSopenharmony_ci return this.__textHeight.get(); 17323b3eb3cSopenharmony_ci } 17423b3eb3cSopenharmony_ci set textHeight(y) { 17523b3eb3cSopenharmony_ci this.__textHeight.set(y); 17623b3eb3cSopenharmony_ci } 17723b3eb3cSopenharmony_ci get buttonBorderRadius() { 17823b3eb3cSopenharmony_ci return this.__buttonBorderRadius.get(); 17923b3eb3cSopenharmony_ci } 18023b3eb3cSopenharmony_ci set buttonBorderRadius(x) { 18123b3eb3cSopenharmony_ci this.__buttonBorderRadius.set(x); 18223b3eb3cSopenharmony_ci } 18323b3eb3cSopenharmony_ci onWillApplyTheme(w) { 18423b3eb3cSopenharmony_ci this.progressColor = w.colors.compEmphasizeSecondary; 18523b3eb3cSopenharmony_ci this.containerBorderColor = w.colors.compEmphasizeSecondary; 18623b3eb3cSopenharmony_ci this.containerBackgroundColor = w.colors.iconOnFourth; 18723b3eb3cSopenharmony_ci } 18823b3eb3cSopenharmony_ci getButtonProgress() { 18923b3eb3cSopenharmony_ci if (this.progress < 0) { 19023b3eb3cSopenharmony_ci return 0; 19123b3eb3cSopenharmony_ci } 19223b3eb3cSopenharmony_ci else if (this.progress > MAX_PROGRESS) { 19323b3eb3cSopenharmony_ci return MAX_PROGRESS; 19423b3eb3cSopenharmony_ci } 19523b3eb3cSopenharmony_ci return this.progress; 19623b3eb3cSopenharmony_ci } 19723b3eb3cSopenharmony_ci getProgressContext() { 19823b3eb3cSopenharmony_ci if (this.progress < 0) { 19923b3eb3cSopenharmony_ci this.isLoading = false; 20023b3eb3cSopenharmony_ci this.textProgress = MIN_PERCENTAGE; 20123b3eb3cSopenharmony_ci } 20223b3eb3cSopenharmony_ci else if (this.progress >= MAX_PROGRESS) { 20323b3eb3cSopenharmony_ci this.isLoading = false; 20423b3eb3cSopenharmony_ci this.textProgress = MAX_PERCENTAGE; 20523b3eb3cSopenharmony_ci } 20623b3eb3cSopenharmony_ci else { 20723b3eb3cSopenharmony_ci this.isLoading = true; 20823b3eb3cSopenharmony_ci this.textProgress = Math.floor(this.progress / MAX_PROGRESS * MAX_PROGRESS).toString() + '%'; 20923b3eb3cSopenharmony_ci } 21023b3eb3cSopenharmony_ci } 21123b3eb3cSopenharmony_ci getLoadingProgress() { 21223b3eb3cSopenharmony_ci if (this.isLoading) { 21323b3eb3cSopenharmony_ci if (this.progress < 0) { 21423b3eb3cSopenharmony_ci this.textProgress = MIN_PERCENTAGE; 21523b3eb3cSopenharmony_ci } 21623b3eb3cSopenharmony_ci else if (this.progress >= MAX_PROGRESS) { 21723b3eb3cSopenharmony_ci this.textProgress = MAX_PERCENTAGE; 21823b3eb3cSopenharmony_ci } 21923b3eb3cSopenharmony_ci else { 22023b3eb3cSopenharmony_ci this.textProgress = Math.floor(this.progress / MAX_PROGRESS * MAX_PROGRESS).toString() + '%'; 22123b3eb3cSopenharmony_ci } 22223b3eb3cSopenharmony_ci } 22323b3eb3cSopenharmony_ci } 22423b3eb3cSopenharmony_ci initialRender() { 22523b3eb3cSopenharmony_ci this.observeComponentCreation2((t, u) => { 22623b3eb3cSopenharmony_ci Button.createWithChild(); 22723b3eb3cSopenharmony_ci Button.borderRadius(this.buttonBorderRadius); 22823b3eb3cSopenharmony_ci Button.clip(false); 22923b3eb3cSopenharmony_ci Button.hoverEffect(HoverEffect.None); 23023b3eb3cSopenharmony_ci Button.key(PROGRESS_BUTTON_EMPHASIZE_SECONDARY_BUTTON_KEY); 23123b3eb3cSopenharmony_ci Button.backgroundColor(ObservedObject.GetRawObject(this.containerBackgroundColor)); 23223b3eb3cSopenharmony_ci Button.constraintSize({ minWidth: 44 }); 23323b3eb3cSopenharmony_ci Button.padding({ top: 0, bottom: 0 }); 23423b3eb3cSopenharmony_ci Button.width((!this.progressButtonWidth || this.progressButtonWidth < BUTTON_NORMARL_WIDTH) ? 23523b3eb3cSopenharmony_ci BUTTON_NORMARL_WIDTH : this.progressButtonWidth); 23623b3eb3cSopenharmony_ci Button.stateEffect(this.enable); 23723b3eb3cSopenharmony_ci Button.onClick(() => { 23823b3eb3cSopenharmony_ci if (!this.enable) { 23923b3eb3cSopenharmony_ci return; 24023b3eb3cSopenharmony_ci } 24123b3eb3cSopenharmony_ci if (this.progress < MAX_PROGRESS) { 24223b3eb3cSopenharmony_ci this.isLoading = !this.isLoading; 24323b3eb3cSopenharmony_ci } 24423b3eb3cSopenharmony_ci this.clickCallback && this.clickCallback(); 24523b3eb3cSopenharmony_ci }); 24623b3eb3cSopenharmony_ci }, Button); 24723b3eb3cSopenharmony_ci this.observeComponentCreation2((r, s) => { 24823b3eb3cSopenharmony_ci Stack.create(); 24923b3eb3cSopenharmony_ci }, Stack); 25023b3eb3cSopenharmony_ci this.observeComponentCreation2((p, q) => { 25123b3eb3cSopenharmony_ci Progress.create({ value: this.getButtonProgress(), total: MAX_PROGRESS, 25223b3eb3cSopenharmony_ci style: ProgressStyle.Capsule }); 25323b3eb3cSopenharmony_ci Progress.height(ObservedObject.GetRawObject(this.textHeight)); 25423b3eb3cSopenharmony_ci Progress.constraintSize({ minHeight: BUTTON_NORMARL_HEIGHT }); 25523b3eb3cSopenharmony_ci Progress.borderRadius(this.buttonBorderRadius); 25623b3eb3cSopenharmony_ci Progress.width('100%'); 25723b3eb3cSopenharmony_ci Progress.hoverEffect(HoverEffect.None); 25823b3eb3cSopenharmony_ci Progress.clip(false); 25923b3eb3cSopenharmony_ci Progress.enabled(this.enable); 26023b3eb3cSopenharmony_ci Progress.key(PROGRESS_BUTTON_PROGRESS_KEY); 26123b3eb3cSopenharmony_ci Progress.color(ObservedObject.GetRawObject(this.progressColor)); 26223b3eb3cSopenharmony_ci }, Progress); 26323b3eb3cSopenharmony_ci this.observeComponentCreation2((n, o) => { 26423b3eb3cSopenharmony_ci Row.create(); 26523b3eb3cSopenharmony_ci Row.constraintSize({ minHeight: BUTTON_NORMARL_HEIGHT }); 26623b3eb3cSopenharmony_ci }, Row); 26723b3eb3cSopenharmony_ci this.observeComponentCreation2((i, j) => { 26823b3eb3cSopenharmony_ci Text.create(this.isLoading ? this.textProgress : this.content); 26923b3eb3cSopenharmony_ci Text.fontSize({ "id": -1, "type": 10002, params: ['sys.float.ohos_id_text_size_button3'], 27023b3eb3cSopenharmony_ci "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }); 27123b3eb3cSopenharmony_ci Text.fontWeight(FontWeight.Medium); 27223b3eb3cSopenharmony_ci Text.key(PROGRESS_BUTTON_PRIMARY_FONT_KEY); 27323b3eb3cSopenharmony_ci Text.maxLines(1); 27423b3eb3cSopenharmony_ci Text.textOverflow({ overflow: TextOverflow.Ellipsis }); 27523b3eb3cSopenharmony_ci Text.padding({ top: 4, left: 8, right: 8, bottom: 4 }); 27623b3eb3cSopenharmony_ci Text.opacity(this.enable ? TEXT_ENABLE : TEXT_OPACITY); 27723b3eb3cSopenharmony_ci Text.onAreaChange((l, m) => { 27823b3eb3cSopenharmony_ci if (!m.height || m.height === this.textHeight) { 27923b3eb3cSopenharmony_ci return; 28023b3eb3cSopenharmony_ci } 28123b3eb3cSopenharmony_ci this.textHeight = m.height > BUTTON_NORMARL_HEIGHT ? m.height : BUTTON_NORMARL_HEIGHT; 28223b3eb3cSopenharmony_ci this.buttonBorderRadius = Number(this.textHeight) / 2; 28323b3eb3cSopenharmony_ci }); 28423b3eb3cSopenharmony_ci }, Text); 28523b3eb3cSopenharmony_ci Text.pop(); 28623b3eb3cSopenharmony_ci Row.pop(); 28723b3eb3cSopenharmony_ci this.observeComponentCreation2((g, h) => { 28823b3eb3cSopenharmony_ci Row.create(); 28923b3eb3cSopenharmony_ci Row.key(PROGRESS_BUTTON_CONTAINER_BACKGROUND_COLOR_KEY); 29023b3eb3cSopenharmony_ci Row.backgroundColor(Color.Transparent); 29123b3eb3cSopenharmony_ci Row.border({ width: 1, color: this.containerBorderColor }); 29223b3eb3cSopenharmony_ci Row.height(ObservedObject.GetRawObject(this.textHeight)); 29323b3eb3cSopenharmony_ci Row.constraintSize({ minHeight: BUTTON_NORMARL_HEIGHT }); 29423b3eb3cSopenharmony_ci Row.borderRadius(this.buttonBorderRadius); 29523b3eb3cSopenharmony_ci Row.width('100%'); 29623b3eb3cSopenharmony_ci }, Row); 29723b3eb3cSopenharmony_ci Row.pop(); 29823b3eb3cSopenharmony_ci Stack.pop(); 29923b3eb3cSopenharmony_ci Button.pop(); 30023b3eb3cSopenharmony_ci } 30123b3eb3cSopenharmony_ci rerender() { 30223b3eb3cSopenharmony_ci this.updateDirtyElements(); 30323b3eb3cSopenharmony_ci } 30423b3eb3cSopenharmony_ci} 30523b3eb3cSopenharmony_ci 30623b3eb3cSopenharmony_ciexport default { ProgressButton };