From 62e71ef3a5d8165178cb028fae248518aa4b2b6d Mon Sep 17 00:00:00 2001 From: ryoii Date: Mon, 30 Mar 2020 20:17:23 +0800 Subject: [PATCH] Graphical logout bot --- .../controller/MiraiGraphicalUIController.kt | 10 +++++++ .../graphical/stylesheet/PrimaryStyleSheet.kt | 16 +++++++++-- .../mamoe/mirai/console/graphical/util/SVG.kt | 15 ++++++++++ .../console/graphical/view/PrimaryView.kt | 28 +++++++++++++++++-- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/util/SVG.kt 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 793db0a96..dba5bc9e7 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 @@ -5,6 +5,7 @@ import javafx.collections.ObservableList import javafx.stage.Modality import javafx.stage.StageStyle import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import net.mamoe.mirai.Bot import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.CommandManager.runCommand @@ -42,6 +43,15 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI { CommandManager.runCommand(ConsoleCommandSender, "/login $qq $psd") } + fun logout(qq: Long) { + cache.remove(qq)?.apply { + botList.remove(this) + if (botProperty.value != null && bot.isActive) { + bot.close() + } + } + } + fun sendCommand(command: String) = runCommand(ConsoleCommandSender, command) override fun pushLog(identity: Long, message: String) = Platform.runLater { diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/stylesheet/PrimaryStyleSheet.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/stylesheet/PrimaryStyleSheet.kt index 58ded3be4..adbec3b80 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/stylesheet/PrimaryStyleSheet.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/stylesheet/PrimaryStyleSheet.kt @@ -69,9 +69,21 @@ class PrimaryStyleSheet : BaseStyleSheet() { backgroundColor += c(100, 100, 100, 0.4) backgroundRadius += box(5.px) - textFill = c(fontColor) - fontWeight = FontWeight.BOLD + label { + textFill = c(fontColor) + fontWeight = FontWeight.BOLD + } + button { + opacity = 0.0 + backgroundRadius += box(10.px) + backgroundColor += c(fontColor, 0.1) + cursor = Cursor.HAND + + and(hover) { + opacity = 1.0 + } + } } } } diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/util/SVG.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/util/SVG.kt new file mode 100644 index 000000000..442a18188 --- /dev/null +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/util/SVG.kt @@ -0,0 +1,15 @@ +package net.mamoe.mirai.console.graphical.util + +import com.jfoenix.svg.SVGGlyph +import javafx.scene.paint.Color + +class SVG { + companion object { + var close = SVGGlyph( + 0, + "CLOSE", + "M810 274l-238 238 238 238-60 60-238-238-238 238-60-60 238-238-238-238 60-60 238 238 238-238z", + Color.WHITE + ).apply { setSize(8.0, 8.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 ebaaefe04..8399373c5 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 @@ -5,18 +5,21 @@ import com.jfoenix.controls.JFXListCell import javafx.collections.ObservableList import javafx.geometry.Insets import javafx.geometry.Pos +import javafx.scene.control.Alert +import javafx.scene.control.ButtonType import javafx.scene.control.Tab import javafx.scene.control.TabPane import javafx.scene.image.Image import javafx.scene.input.KeyCode +import javafx.scene.layout.Priority import javafx.stage.FileChooser import kotlinx.coroutines.runBlocking import net.mamoe.mirai.console.graphical.controller.MiraiGraphicalUIController import net.mamoe.mirai.console.graphical.model.BotModel +import net.mamoe.mirai.console.graphical.util.* import net.mamoe.mirai.console.graphical.util.jfxButton import net.mamoe.mirai.console.graphical.util.jfxListView import net.mamoe.mirai.console.graphical.util.jfxTabPane -import net.mamoe.mirai.console.graphical.util.myButtonBar import tornadofx.* class PrimaryView : View() { @@ -72,8 +75,27 @@ class PrimaryView : View() { override fun updateItem(item: BotModel?, empty: Boolean) { super.updateItem(item, empty) if (item != null && !empty) { - graphic = null - text = item.uin.toString() + graphic = hbox { + + alignment = Pos.CENTER_LEFT + + label(item.uin.toString()) + pane { + hgrow = Priority.ALWAYS + } + jfxButton(graphic = SVG.close) { + buttonType = JFXButton.ButtonType.FLAT + tooltip("退出登录") + }.action { + alert(Alert.AlertType.CONFIRMATION, "${item.uin}将会退出登录,是否确认") { + if (it == ButtonType.OK) { + tab?.close() + controller.logout(item.uin) + } + } + } + } + text = "" } else { graphic = null text = ""