Plugin Center view

This commit is contained in:
ryoii 2020-04-03 17:15:12 +08:00
parent bd2ce8f771
commit 7d9ffb8c71
6 changed files with 168 additions and 11 deletions

View File

@ -57,7 +57,9 @@ dependencies {
api(group = "com.jfoenix", name = "jfoenix", version = "9.0.8")
testApi(project(":mirai-console"))
testApi(kotlinx("coroutines-core", Versions.Kotlin.coroutines))
testApi(group = "org.yaml", name = "snakeyaml", version = "1.25")
testApi("net.mamoe:mirai-core-jvm:${Versions.Mirai.core}")
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {

View File

@ -125,6 +125,29 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
}
}
fun checkLatest(plugin: PluginModel) {
pluginList.forEach {
if (it.name == plugin.name && it.author == plugin.author) {
if (plugin.version > it.version) {
it.latest = false
return
}
}
}
}
/**
* return `true` when command is ambiguous
*/
fun checkAmbiguous(plugin: PluginModel) : Boolean {
plugin.insight?.commands?.forEach { name ->
CommandManager.commands.forEach {
if (name == it.name) return true
}
} ?: return false
return false
}
}
class GraphicalLoginSolver : LoginSolver() {

View File

@ -3,6 +3,7 @@ package net.mamoe.mirai.console.graphical.model
import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleStringProperty
import net.mamoe.mirai.console.center.PluginCenter
import net.mamoe.mirai.console.plugins.PluginDescription
import tornadofx.getValue
import tornadofx.setValue
@ -11,7 +12,8 @@ class PluginModel(
val name: String,
val version: String,
val author: String,
val description: String
val description: String,
var insight: PluginCenter.PluginInsight? = null
) : RecursiveTreeObject<PluginModel>() {
constructor(plugin: PluginDescription) : this(plugin.name, plugin.version, plugin.author, plugin.info)
@ -22,4 +24,7 @@ class PluginModel(
val enabledProperty = SimpleBooleanProperty(this, "enabledProperty")
var enabled by enabledProperty
val latestProperty = SimpleBooleanProperty(this, "latestProperty", true)
var latest by latestProperty
}

View File

@ -0,0 +1,126 @@
package net.mamoe.mirai.console.graphical.view
import com.jfoenix.controls.JFXTreeTableColumn
import javafx.collections.ObservableList
import javafx.geometry.Pos
import javafx.scene.control.TreeTableCell
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.console.center.CuiPluginCenter
import net.mamoe.mirai.console.graphical.controller.MiraiGraphicalUIController
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 tornadofx.*
class PluginsCenterView : View() {
private val controller = find<MiraiGraphicalUIController>()
private val center = CuiPluginCenter
private val plugins: ObservableList<PluginModel> by lazy(::fetch)
override val root = jfxTreeTableView(plugins) {
addStylesheet(PluginViewStyleSheet::class)
isShowRoot = false
columns.addAll(
JFXTreeTableColumn<PluginModel, String>("插件名").apply {
prefWidthProperty().bind(this@jfxTreeTableView.widthProperty().multiply(0.1))
setCellValueFactory {
return@setCellValueFactory it.value.value.nameProperty
}
},
JFXTreeTableColumn<PluginModel, String>("版本").apply {
prefWidthProperty().bind(this@jfxTreeTableView.widthProperty().multiply(0.1))
setCellValueFactory {
return@setCellValueFactory it.value.value.versionProperty
}
},
JFXTreeTableColumn<PluginModel, String>("作者").apply {
prefWidthProperty().bind(this@jfxTreeTableView.widthProperty().multiply(0.1))
setCellValueFactory {
return@setCellValueFactory it.value.value.authorProperty
}
},
JFXTreeTableColumn<PluginModel, String>("介绍").apply {
prefWidthProperty().bind(this@jfxTreeTableView.widthProperty().multiply(0.48))
setCellValueFactory {
return@setCellValueFactory it.value.value.descriptionProperty
}
},
JFXTreeTableColumn<PluginModel, PluginModel>("操作").apply {
prefWidthProperty().bind(this@jfxTreeTableView.widthProperty().multiply(0.2))
setCellValueFactory { return@setCellValueFactory it.value.valueProperty() }
setCellFactory {
return@setCellFactory object : TreeTableCell<PluginModel, PluginModel>() {
override fun updateItem(item: PluginModel?, empty: Boolean) {
if (item != null && !empty) {
graphic = hbox {
spacing = 15.0
alignment = Pos.CENTER
jfxButton("详情") {
action { detail(item) }
}
jfxButton("下载") {
action { download(item) }
}
}
text = ""
} else {
graphic = null
text = ""
}
}
}
}
}
)
}
private fun fetch(): ObservableList<PluginModel> =
runAsync {
val ret = observableListOf<PluginModel>()
runBlocking {
var page = 1
while (true) {
val map = center.fetchPlugin(page++)
if (map.isEmpty()) return@runBlocking
map.forEach {
ret.add(
PluginModel(
it.value.name,
it.value.version,
it.value.author,
it.value.description,
it.value
)
)
}
}
}
return@runAsync ret
}.get()
private fun detail(pluginModel: PluginModel) {
//to show pluginModel.insight
}
private fun download(pluginModel: PluginModel) {
// controller.checkAmbiguous(pluginModel)
// do nothing
}
}

View File

@ -120,6 +120,8 @@ class PrimaryView : View() {
fixedTab("Plugins").content = find<PluginsView>().root
fixedTab("Plugins Center").content = find<PluginsCenterView>().root
fixedTab("Settings").content = find<SettingsView>().root
fixedTab("Login").content = find<LoginView>().root

View File

@ -1,6 +1,5 @@
package net.mamoe.mirai.console.center
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
@ -24,20 +23,20 @@ object CuiPluginCenter: PluginCenter{
if(plugins == null){
refresh()
}
if(it > plugins!!.size()){
if(it >= plugins!!.size()){
return@forEach
}
val info = plugins!![it]
with(info.asJsonObject){
map[this.get("name").asString] = PluginCenter.PluginInsight(
this.get("name").asString,
this.get("version").asString,
this.get("core").asString,
this.get("console").asString,
this.get("author").asString,
this.get("contact").asString,
this.get("tags").asJsonArray.map { it.asString },
this.get("commands").asJsonArray.map { it.asString }
this.get("name")?.asString ?: "",
this.get("version")?.asString ?: "",
this.get("core")?.asString ?: "",
this.get("console")?.asString ?: "",
this.get("author")?.asString ?: "",
this.get("contact")?.asString ?: "",
this.get("tags")?.asJsonArray?.map { it.asString } ?: arrayListOf(),
this.get("commands")?.asJsonArray?.map { it.asString } ?: arrayListOf()
)
}
}