From 6952443776487b0f31358d59fdc0e8eafaef663c Mon Sep 17 00:00:00 2001 From: ryoii Date: Thu, 19 Mar 2020 22:39:54 +0800 Subject: [PATCH 1/5] Remove println --- .../console/graphical/controller/MiraiGraphicalUIController.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt index c4af15f35..3675c5847 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt @@ -34,8 +34,6 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI { override fun pushLog(identity: Long, message: String) = Platform.runLater { fun ObservableList<*>.trim() { - println(size) - println(settingModel.item.maxLongNum) if (size > settingModel.item.maxLongNum) { this.removeAt(0) } From 5ece8e95f9fbe62d3ce8868197b553d531348275 Mon Sep 17 00:00:00 2001 From: ryoii Date: Fri, 20 Mar 2020 00:08:44 +0800 Subject: [PATCH 2/5] Graphic `VerificationCode` --- .../controller/MiraiGraphicalUIController.kt | 25 +++++++++++++++++- .../graphical/model/VerificationCodeModel.kt | 15 ++++++++--- .../view/VerificationCodeFragment.kt | 26 +++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt index 3675c5847..2cab97a46 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt @@ -3,12 +3,15 @@ package net.mamoe.mirai.console.graphical.controller import javafx.application.Platform import javafx.collections.ObservableList import javafx.stage.Modality +import javafx.stage.StageStyle +import kotlinx.coroutines.delay import net.mamoe.mirai.Bot import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.graphical.model.* import net.mamoe.mirai.console.graphical.view.VerificationCodeFragment import net.mamoe.mirai.console.plugins.PluginManager import net.mamoe.mirai.console.utils.MiraiConsoleUI +import net.mamoe.mirai.network.WrongPasswordException import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.SimpleLogger.LogPriority import tornadofx.* @@ -97,7 +100,27 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI { class GraphicalLoginSolver : LoginSolver() { override suspend fun onSolvePicCaptcha(bot: Bot, data: ByteArray): String? { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + val code = VerificationCodeModel(VerificationCode(data)) + + // 界面需要运行在主线程 + Platform.runLater { + find(Scope(code)).openModal( + stageStyle = StageStyle.UNDECORATED, + escapeClosesWindow = false, + modality = Modality.NONE, + resizable = false, + block = true + ) + } + + // 阻塞协程直到验证码已经输入 + while (code.isDirty || code.code.value == null) { + delay(1000) + if (code.code.value === VerificationCodeFragment.MAGIC_KEY) { + throw WrongPasswordException("取消登录") + } + } + return code.code.value } diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/model/VerificationCodeModel.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/model/VerificationCodeModel.kt index 2eeadb35f..fe97f848f 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/model/VerificationCodeModel.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/model/VerificationCodeModel.kt @@ -1,17 +1,26 @@ package net.mamoe.mirai.console.graphical.model +import javafx.beans.property.SimpleObjectProperty import javafx.beans.property.SimpleStringProperty import tornadofx.ItemViewModel import tornadofx.getValue import tornadofx.setValue -class VerificationCode { - val codeProperty = SimpleStringProperty("") - var code: String by codeProperty +class VerificationCode(data: ByteArray = ByteArray(0)) { + val codeProperty = SimpleStringProperty(null) + var code: String? by codeProperty + + val dataProperty: SimpleObjectProperty = SimpleObjectProperty() + val data: ByteArray by dataProperty + + init { + dataProperty.set(data) + } } class VerificationCodeModel(code: VerificationCode) : ItemViewModel(code) { constructor() : this(VerificationCode()) val code = bind(VerificationCode::codeProperty) + val data = bind(VerificationCode::dataProperty) } \ No newline at end of file diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/VerificationCodeFragment.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/VerificationCodeFragment.kt index aaabb90e1..e23b26abe 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/VerificationCodeFragment.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/VerificationCodeFragment.kt @@ -1,16 +1,38 @@ package net.mamoe.mirai.console.graphical.view +import javafx.scene.image.Image +import net.mamoe.mirai.console.graphical.model.VerificationCodeModel import tornadofx.* +import java.io.ByteArrayInputStream class VerificationCodeFragment : Fragment() { + companion object { + val MAGIC_KEY = String("CANCEL".toByteArray()) + } + + val code = find() + override val root = vbox { - //TODO: 显示验证码 + // 显示验证码 + imageview(Image(ByteArrayInputStream(code.data.value))) form { fieldset { field("验证码") { - textfield() + textfield(code.code) + } + } + + buttonbar { + button("提交").action { + code.commit() + this@VerificationCodeFragment.close() + } + button("取消").action { + code.code.value = MAGIC_KEY + code.commit() + this@VerificationCodeFragment.close() } } } From 4b17c58de0485b2f10937824e2a58584b47cbf19 Mon Sep 17 00:00:00 2001 From: ryoii Date: Fri, 20 Mar 2020 23:45:24 +0800 Subject: [PATCH 3/5] Closable tab --- .../console/graphical/view/PrimaryView.kt | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt index 883c007fa..dc0262224 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt @@ -16,6 +16,7 @@ import tornadofx.* class PrimaryView : View() { private val controller = find() + private lateinit var mainTabPane : TabPane override val root = borderpane { @@ -71,15 +72,22 @@ class PrimaryView : View() { center = jfxTabPane { - tab("Login").content = find().root + logTab("Main", controller.mainLog) tab("Plugins").content = find().root tab("Settings").content = find().root - logTab("Main", controller.mainLog) + tab("Login").content = find().root + + mainTabPane = this } } + + fun Tab.select() = apply { + if (!mainTabPane.tabs.contains(this)) mainTabPane.tabs.add(this) + mainTabPane.selectionModel.select(this) + } } private fun TabPane.logTab( @@ -87,13 +95,21 @@ private fun TabPane.logTab( logs: ObservableList, op: Tab.() -> Unit = {} ) = tab(text) { - listview(logs) { - fitToParentSize() - cellFormat { - graphic = label(it) { - maxWidthProperty().bind(this@listview.widthProperty()) - isWrapText = true + vbox { + buttonbar { + button("导出日志").action { } + button("关闭").action { close() } + } + + listview(logs) { + + fitToParentSize() + cellFormat { + graphic = label(it) { + maxWidthProperty().bind(this@listview.widthProperty()) + isWrapText = true + } } } } From c196e36f9b563c6adadfb70224c25967161bdda5 Mon Sep 17 00:00:00 2001 From: ryoii Date: Sat, 21 Mar 2020 00:22:03 +0800 Subject: [PATCH 4/5] Closable tab and export logs --- .../console/graphical/view/PrimaryView.kt | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt index dc0262224..d60ded2d8 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt @@ -6,6 +6,7 @@ import javafx.scene.control.Tab import javafx.scene.control.TabPane import javafx.scene.image.Image import javafx.scene.input.KeyCode +import javafx.stage.FileChooser import kotlinx.coroutines.runBlocking import net.mamoe.mirai.console.graphical.controller.MiraiGraphicalUIController import net.mamoe.mirai.console.graphical.model.BotModel @@ -16,7 +17,7 @@ import tornadofx.* class PrimaryView : View() { private val controller = find() - private lateinit var mainTabPane : TabPane + private lateinit var mainTabPane: TabPane override val root = borderpane { @@ -72,7 +73,7 @@ class PrimaryView : View() { center = jfxTabPane { - logTab("Main", controller.mainLog) + logTab("Main", controller.mainLog, closeable = false) tab("Plugins").content = find().root @@ -93,13 +94,35 @@ class PrimaryView : View() { private fun TabPane.logTab( text: String? = null, logs: ObservableList, + closeable: Boolean = true, op: Tab.() -> Unit = {} ) = tab(text) { vbox { buttonbar { - button("导出日志").action { } - button("关闭").action { close() } + + button("导出日志").action { + val path = chooseFile( + "选择保存路径", + arrayOf(FileChooser.ExtensionFilter("日志", "txt")), + FileChooserMode.Save + ) { + initialFileName = "$text.txt" + } + runAsyncWithOverlay { + path.firstOrNull()?.run { + if (!exists()) createNewFile() + writer().use { + logs.forEach { log -> it.appendln(log) } + } + true + } ?: false + }.ui {// isSucceed: Boolean -> + // notify something + } + + if (closeable) button("关闭").action { close() } + } } listview(logs) { From 0d9b14e27b9e988dbed828a5ae18eae16e89f7da Mon Sep 17 00:00:00 2001 From: ryoii Date: Sat, 21 Mar 2020 00:33:56 +0800 Subject: [PATCH 5/5] Set ui size --- .../net/mamoe/mirai/console/graphical/view/Decorator.kt | 5 ++++- .../net/mamoe/mirai/console/graphical/view/PrimaryView.kt | 3 --- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/Decorator.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/Decorator.kt index 5531abe04..ec2c9e12f 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/Decorator.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/Decorator.kt @@ -5,5 +5,8 @@ import tornadofx.View class Decorator : View() { - override val root = JFXDecorator(primaryStage, find().root) + override val root = JFXDecorator(primaryStage, find().root).apply { + prefWidth = 1000.0 + prefHeight = 650.0 + } } \ No newline at end of file diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt index d60ded2d8..c858aea81 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PrimaryView.kt @@ -21,9 +21,6 @@ class PrimaryView : View() { override val root = borderpane { - prefWidth = 1000.0 - prefHeight = 650.0 - left = vbox { imageview(Image(PrimaryView::class.java.classLoader.getResourceAsStream("logo.png")))