1/* 2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15import * as vscode from 'vscode'; 16import { WelcomeMod } from "../model/welcomemod"; 17import { IModel } from "../model/imodel"; 18import { IView } from "./iview"; 19import { 20 EVENT_ERROR, 21 EVENT_INFORMATION, 22 EVENT_PROGRESS, 23 EVENT_WARNING 24} from '../common/eventtype'; 25import { IController } from '../controller/icontroller'; 26import { doAsyncQuickPick, toastMsg } from '../common/widget'; 27import { CONFIRM_SELECT, HDF_FRAMEWORK, INPUT_INCONSISTENT, INPUT_NO_EMPTY, INPUT_NUMBER, INPUT_SERVICEID, NAPI_FRAMEWORK, SA_FRAMEWORK, SELECT_FRAMWORK, SELECT_VERSION } from '../common/constants'; 28import { Logger } from '../common/log'; 29export class WelcomeView extends IView { 30 name: string; 31 model: IModel; 32 controller: IController | undefined; 33 progress: vscode.Progress<{ message?: string; increment?: number; }> | undefined = undefined; 34 constructor() { 35 super(); 36 this.name = 'h2dtsview'; 37 this.model = WelcomeMod.getInstance(); 38 } 39 40 init(controller: IController): void { 41 this.controller = controller; 42 43 this.model.onEvent(EVENT_PROGRESS, (percent, info) => { 44 if (this.progress) { 45 this.progress.report({ increment: percent, message: info }) 46 } 47 }) 48 this.model.onEvent(EVENT_ERROR, (errno, errmsg) => { 49 vscode.window.showErrorMessage(errmsg); 50 }) 51 this.model.onEvent(EVENT_WARNING, (errno, errmsg) => { 52 vscode.window.showWarningMessage(errmsg); 53 }) 54 } 55 56 asyncCB = async (value: string) => { 57 // await vscode.window.showInputBox({ 58 // placeHolder: CONFIRM_SELECT, 59 // validateInput: (input) => { 60 // if (!input) { 61 // return INPUT_NO_EMPTY; 62 // } 63 // if (input !== value) { 64 // return INPUT_INCONSISTENT; 65 // } 66 // } 67 // }); 68 69 if (this.model) { 70 let welcomeMode = this.model as WelcomeMod; 71 welcomeMode.setGenType(value); 72 73 const options: vscode.OpenDialogOptions = { 74 canSelectFiles: true, 75 canSelectFolders: false, 76 canSelectMany: false, 77 defaultUri: vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0].uri : undefined, 78 openLabel: 'Select' 79 }; 80 81 vscode.window.showOpenDialog(options).then(fileUri => { 82 if (fileUri && fileUri[0]) { 83 const filePath = fileUri[0].fsPath; 84 Logger.getInstance().debug('Selected path:' + filePath); 85 vscode.window.withProgress({ 86 location: vscode.ProgressLocation.Notification, 87 title: 'Generating ...', 88 cancellable: false 89 }, async (progress) => { 90 this.progress = progress; 91 if (this.controller) { 92 this.controller.uri = fileUri[0]; 93 this.model.uri = fileUri[0]; 94 this.controller.start(); 95 } 96 }) 97 } 98 }); 99 } 100 } 101 102 showProgress(): void { 103 try { 104 doAsyncQuickPick( 105 [HDF_FRAMEWORK, SA_FRAMEWORK, NAPI_FRAMEWORK], 106 { placeHolder: SELECT_FRAMWORK }, 107 this.asyncCB 108 ) 109 } catch (error) { 110 let errmsg = this.name + " showProgress error: " + JSON.stringify(error); 111 toastMsg(EVENT_ERROR, errmsg); 112 } 113 } 114 115 showMsg(event: string, msg: string): void { 116 toastMsg(event, msg); 117 } 118}