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 0aaa4501a..d1c0921b3 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 @@ -10,6 +10,7 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.CommandManager.runCommand import net.mamoe.mirai.console.command.ConsoleCommandSender +import net.mamoe.mirai.console.graphical.event.ReloadEvent import net.mamoe.mirai.console.graphical.model.* import net.mamoe.mirai.console.graphical.view.dialog.InputDialog import net.mamoe.mirai.console.graphical.view.dialog.VerificationCodeFragment @@ -35,8 +36,16 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI { private val consoleInfo = ConsoleInfo() - private val sdf by lazy { - SimpleDateFormat("HH:mm:ss") + private val sdf by lazy { SimpleDateFormat("HH:mm:ss") } + + init { + // 监听插件重载事件,以重新从console获取插件列表 + subscribe { + pluginList.clear() + + // 不能直接赋值,pluginList已经被bind,不能更换对象 + pluginList.addAll(getPluginsFromConsole()) + } } fun login(qq: String, psd: String) { @@ -148,6 +157,15 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI { return false } + fun reloadPlugins() { + + with(PluginManager) { + disablePlugins() + loadPlugins() + } + + fire(ReloadEvent) // 广播插件重载事件 + } } class GraphicalLoginSolver : LoginSolver() { diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/event/ReloadEvent.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/event/ReloadEvent.kt new file mode 100644 index 000000000..48f0f616c --- /dev/null +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/event/ReloadEvent.kt @@ -0,0 +1,5 @@ +package net.mamoe.mirai.console.graphical.event + +import tornadofx.FXEvent + +object ReloadEvent : FXEvent() \ No newline at end of file diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsCenterView.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsCenterView.kt index b7513a054..cc246af51 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsCenterView.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsCenterView.kt @@ -1,28 +1,52 @@ package net.mamoe.mirai.console.graphical.view import com.jfoenix.controls.JFXTreeTableColumn +import javafx.application.Platform import javafx.collections.ObservableList import javafx.geometry.Pos +import javafx.scene.control.Button import javafx.scene.control.TreeTableCell import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.center.PluginCenter import net.mamoe.mirai.console.graphical.controller.MiraiGraphicalUIController +import net.mamoe.mirai.console.graphical.event.ReloadEvent import net.mamoe.mirai.console.graphical.model.PluginModel import net.mamoe.mirai.console.graphical.stylesheet.PluginViewStyleSheet import net.mamoe.mirai.console.graphical.util.jfxButton import net.mamoe.mirai.console.graphical.util.jfxTreeTableView +import net.mamoe.mirai.console.graphical.view.dialog.PluginDetailFragment +import net.mamoe.mirai.console.plugins.PluginManager import tornadofx.* class PluginsCenterView : View() { private val controller = find() private val center = PluginCenter.Default - private val plugins: ObservableList by lazy(::fetch) + private val plugins: ObservableList = observableListOf() + + init { + // 监听插件重载,情况插件列表,重新载入。 + // 同时把页面刷新,按键的listener也初始化 + subscribe { plugins.clear() } + } override val root = jfxTreeTableView(plugins) { addStylesheet(PluginViewStyleSheet::class) + placeholder = button("从崔云获取插件列表") { + action { + isDisable = true + runAsync { + fetch() + }.ui { + plugins.addAll(it) + isDisable = false + } + } + } + isShowRoot = false columns.addAll( JFXTreeTableColumn("插件名").apply { @@ -73,7 +97,7 @@ class PluginsCenterView : View() { jfxButton("下载") { - action { download(item) } + action { download(item, this) } } } @@ -90,39 +114,62 @@ class PluginsCenterView : View() { } - private fun fetch(): ObservableList = - runAsync { - val ret = observableListOf() - runBlocking { - var page = 1 - while (true) { - val map = center.fetchPlugin(page++) - if (map.isEmpty()) return@runBlocking - map.forEach { - with(PluginModel( + private fun fetch(): List = mutableListOf().apply { + runBlocking { + var page = 1 + while (true) { + val map = center.fetchPlugin(page++) + if (map.isEmpty()) return@runBlocking + map.forEach { + with( + PluginModel( it.value.name, it.value.version, it.value.author, it.value.description, it.value - )) { - ret.add(this) - controller.checkUpdate(this) - controller.checkAmbiguous(this) - } + ) + ) { + add(this) + controller.checkUpdate(this) + controller.checkAmbiguous(this) } } } - return@runAsync ret - }.get() - - private fun detail(pluginModel: PluginModel) { - //to show pluginModel.insight + } } - private fun download(pluginModel: PluginModel) { - // controller.checkAmbiguous(pluginModel) - // do nothing + private fun detail(pluginModel: PluginModel) { + runAsync { + runBlocking { center.findPlugin(pluginModel.name) } + }.ui { + it?.apply { + PluginDetailFragment(this).openModal() + } + } + } + + private fun download(pluginModel: PluginModel, button: Button) { + button.isDisable = true + button.text = "连接中..." + runAsync { + runBlocking { + center.downloadPlugin(pluginModel.name) { + // download process + Platform.runLater { + button.text = "$it%" + } + } + } + }.ui { + with(button) { + isDisable = false + text = "重载插件" + setOnAction { + controller.reloadPlugins() + } + } + } } } \ No newline at end of file diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsView.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsView.kt index 2753ad9cf..4d3e335d0 100644 --- a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsView.kt +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/PluginsView.kt @@ -3,6 +3,7 @@ package net.mamoe.mirai.console.graphical.view import com.jfoenix.controls.JFXTreeTableColumn import javafx.scene.control.TreeTableCell import net.mamoe.mirai.console.graphical.controller.MiraiGraphicalUIController +import net.mamoe.mirai.console.graphical.event.ReloadEvent import net.mamoe.mirai.console.graphical.model.PluginModel import net.mamoe.mirai.console.graphical.stylesheet.PluginViewStyleSheet import net.mamoe.mirai.console.graphical.util.jfxButton diff --git a/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/dialog/PluginDetailFragment.kt b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/dialog/PluginDetailFragment.kt new file mode 100644 index 000000000..334027d91 --- /dev/null +++ b/mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/view/dialog/PluginDetailFragment.kt @@ -0,0 +1,74 @@ +package net.mamoe.mirai.console.graphical.view.dialog + +import javafx.geometry.Insets +import net.mamoe.mirai.console.center.PluginCenter +import net.mamoe.mirai.console.graphical.util.jfxTextfield +import tornadofx.Fragment +import tornadofx.form +import tornadofx.vbox + +class PluginDetailFragment(info: PluginCenter.PluginInfo) : Fragment() { + + + override val root = vbox { + + prefWidth = 450.0 + padding = Insets(25.0) + spacing = 25.0 + + jfxTextfield(info.name) { + promptText = "插件名" + isLabelFloat = true + } + + jfxTextfield(info.version) { + promptText = "版本号" + isLabelFloat = true + } + + jfxTextfield(info.coreVersion) { + promptText = "Mirai核心版本" + isLabelFloat = true + } + + jfxTextfield(info.consoleVersion) { + promptText = "Mirai控制台版本" + isLabelFloat = true + } + + jfxTextfield(info.tags.joinToString(",")) { + promptText = "标签" + isLabelFloat = true + } + + jfxTextfield(info.author) { + promptText = "作者" + isLabelFloat = true + } + + jfxTextfield(info.description) { + promptText = "描述" + isLabelFloat = true + } + + jfxTextfield(info.usage) { + promptText = "使用方法" + isLabelFloat = true + } + + jfxTextfield(info.vcs) { + promptText = "仓库地址" + isLabelFloat = true + } + + jfxTextfield(info.commands.joinToString("\n\n")) { + promptText = "命令" + isLabelFloat = true + } + + jfxTextfield(info.changeLog.joinToString("\n\n")) { + promptText = "修改日志" + isLabelFloat = true + } + } +} \ No newline at end of file