import {Button, ComboBox, hot, PlainTextEdit, Text, View, Window} from "@nodegui/react-nodegui"; import React from "react"; import {AlignmentFlag, QIcon} from "@nodegui/nodegui"; import nodeguiIcon from "../assets/nodegui.jpg"; import {RNComboBox} from "@nodegui/react-nodegui/dist/components/ComboBox/RNComboBox"; import {RNText} from "@nodegui/react-nodegui/dist/components/Text/RNText"; import tencentTmt from "./tencent"; import {RNPlainTextEdit} from "@nodegui/react-nodegui/dist/components/PlainTextEdit/RNPlainTextEdit"; import baiduTranslate from "./baidu" import Translate from "./components/Translate"; class App extends React.Component { private getComboBoxHandler: (key: ("sourceIndex" | "targetIndex")) => (index: number) => void; private sourceLang: Array<{text:string,lang:string}>; private lang: { [key: string]: string }; private sourceRef: React.RefObject; private targetRef:React.RefObject; private labelRef: React.RefObject; private sourceTextRef: React.RefObject; private tencentTextRef: React.RefObject; private baiduTextRef: React.RefObject; constructor(props: any) { super(props); this.state = { additionalButtons: [], size: {height: 300, width: 900}, allow:false, } this.sourceRef=React.createRef() this.targetRef=React.createRef() this.labelRef=React.createRef() this.sourceTextRef=React.createRef() this.tencentTextRef=React.createRef() this.baiduTextRef=React.createRef() this.getComboBoxHandler = (key: 'sourceIndex' | 'targetIndex') => { return (index: number) => { let ref if(key==='sourceIndex'){ ref=this.targetRef }else{ ref=this.sourceRef } if (ref.current?.currentIndex() === index) { for(let langIndex in this.sourceLang){ if(+langIndex!==index){ ref.current?.setCurrentIndex(+langIndex) break } } } // console.debug(`${key}选中${this.sourceLang[index].text}`) } } this.lang = { 'en': '英语', 'zh': '中文' } this.sourceLang = [] for (let key in this.lang) { this.sourceLang.push({text: this.lang[key],lang:key}) } } componentDidMount() { this.labelRef.current?.setAlignment(AlignmentFlag.AlignCenter) } render() { const winIcon = new QIcon(nodeguiIcon); const comboHandler = { currentIndexChanged: this.getComboBoxHandler("sourceIndex"), } const targetComboHandler = { currentIndexChanged: this.getComboBoxHandler("targetIndex") } const sourceHandler={ textChanged:()=>{ let flag=(this.sourceTextRef.current?.toPlainText().length||0)>0 this.setState({allow:flag}) } } const translateHandler={ clicked:()=>{ let source=this.sourceTextRef.current?.toPlainText()||'' let target=this.sourceLang[this.targetRef.current?.currentIndex()||0].lang tencentTmt(source,target) .then(res=>{ console.info(res); this.tencentTextRef.current?.setPlainText(res.TargetText) }).catch(err=>console.error("error", err)) baiduTranslate(source,target).then((res: { text: () => any; })=>res.text()).then((res: string)=>{ console.info(res) let r=JSON.parse(res) this.baiduTextRef.current?.setPlainText(r["trans_result"][0]["dst"]) }).catch((err: any)=>console.error("error", err)) } } return ( ); } } const styleSheet = ` #rootView{ height: '100%'; flex-direction:'column'; } QPushButton{ width:300; color:red; font-size:20px; background-color:gray; border-radius:10px; } QPushButton:pressed{ background-color:gray; } QPushButton:enabled{ background-color: white; } ` export default hot(App);