From be202a6e1ad878cbd39f4f1f88d8909a434d1ac6 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Mon, 21 Aug 2017 13:40:16 +0800 Subject: [PATCH] Log Window refined --- .../main/kotlin/org/jackhuang/hmcl/Main.kt | 6 ++- .../org/jackhuang/hmcl/game/LauncherHelper.kt | 12 +++-- .../org/jackhuang/hmcl/setting/Config.kt | 4 +- .../org/jackhuang/hmcl/setting/Settings.kt | 8 ++++ .../hmcl/setting/SettingsConstants.kt | 5 +++ .../org/jackhuang/hmcl/ui/Controllers.kt | 4 ++ .../kotlin/org/jackhuang/hmcl/ui/LogWindow.kt | 12 ++++- .../org/jackhuang/hmcl/ui/SettingsPage.kt | 23 ++++++++++ .../hmcl/ui/construct/FontComboBox.kt | 45 +++++++++++++++++++ .../main/resources/assets/fxml/setting.fxml | 11 +++-- .../resources/assets/log-window-content.html | 28 ++++++++++++ .../kotlin/org/jackhuang/hmcl/util/Logging.kt | 10 ++--- 12 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/construct/FontComboBox.kt create mode 100644 HMCL/src/main/resources/assets/log-window-content.html diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/Main.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/Main.kt index b8e0fbd35..3e05f48c3 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/Main.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/Main.kt @@ -85,8 +85,12 @@ class Main : Application() { fun getMinecraftDirectory(): File = getWorkingDirectory("minecraft") fun stop() = runOnUiThread { - Controllers.stage.close() + stopWithoutJavaFXPlatform() Platform.exit() + } + + fun stopWithoutJavaFXPlatform() = runOnUiThread { + Controllers.stage.close() Scheduler.shutdown() } diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt index 0ac9f9eaa..08738729c 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt @@ -17,6 +17,7 @@ */ package org.jackhuang.hmcl.game +import javafx.application.Platform import org.jackhuang.hmcl.Main import org.jackhuang.hmcl.auth.AuthInfo import org.jackhuang.hmcl.auth.AuthenticationException @@ -169,9 +170,14 @@ object LauncherHelper { private fun checkExit(launcherVisibility: LauncherVisibility) { when (launcherVisibility) { LauncherVisibility.HIDE_AND_REOPEN -> runOnUiThread { Controllers.stage.show() } - LauncherVisibility.KEEP -> {} - LauncherVisibility.CLOSE -> {} - LauncherVisibility.HIDE -> Main.stop() + LauncherVisibility.KEEP -> { /* no operations here. */ } + LauncherVisibility.CLOSE -> { throw Error("Never get to here") } + LauncherVisibility.HIDE -> runOnUiThread { + // Shut down the platform when user closed log window. + Platform.setImplicitExit(true) + // If we use Main.stop(), log window will be halt immediately. + Main.stopWithoutJavaFXPlatform() + } } } diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Config.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Config.kt index 6d0c6d496..d9dfc112a 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Config.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Config.kt @@ -115,13 +115,13 @@ class Config { Settings.save() } @SerializedName("fontFamily") - var fontFamily: String? = null + var fontFamily: String? = "Consolas" set(value) { field = value Settings.save() } @SerializedName("fontSize") - var fontSize: Int = 12 + var fontSize: Double = 12.0 set(value) { field = value Settings.save() diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Settings.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Settings.kt index fcf98b73d..3244faec8 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Settings.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/Settings.kt @@ -19,6 +19,7 @@ package org.jackhuang.hmcl.setting import com.google.gson.GsonBuilder import javafx.beans.InvalidationListener +import javafx.scene.text.Font import java.io.IOException import org.jackhuang.hmcl.Main import org.jackhuang.hmcl.download.BMCLAPIDownloadProvider @@ -192,6 +193,13 @@ object Settings { init { loadProxy() } + var font: Font + get() = Font.font(SETTINGS.fontFamily, SETTINGS.fontSize) + set(value) { + SETTINGS.fontFamily = value.family + SETTINGS.fontSize = value.size + } + var downloadProvider: DownloadProvider get() = when (SETTINGS.downloadtype) { 0 -> MojangDownloadProvider diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/SettingsConstants.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/SettingsConstants.kt index 87ded3e22..4533a747e 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/SettingsConstants.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/SettingsConstants.kt @@ -17,6 +17,7 @@ */ package org.jackhuang.hmcl.setting +import javafx.scene.text.Font import org.jackhuang.hmcl.auth.Account import org.jackhuang.hmcl.auth.AccountFactory import org.jackhuang.hmcl.auth.OfflineAccount @@ -101,4 +102,8 @@ object Locales { DEFAULT -> "def" else -> throw IllegalArgumentException("Unknown argument: " + supportedLocale) } +} + +object Fonts { + val FONTS = Font.getFamilies() } \ No newline at end of file diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/Controllers.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/Controllers.kt index 565523514..3061c5694 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/Controllers.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/Controllers.kt @@ -20,6 +20,7 @@ package org.jackhuang.hmcl.ui import com.jfoenix.controls.JFXDialog import javafx.scene.Node import javafx.scene.Scene +import javafx.scene.image.Image import javafx.scene.layout.Region import javafx.stage.Stage import org.jackhuang.hmcl.Main @@ -51,6 +52,9 @@ object Controllers { stage.maxWidth = 800.0 stage.maxHeight = 480.0 stage.minHeight = 480.0 + + stage.icons += Image("/assets/img/icon.png") + stage.title = Main.TITLE } fun dialog(content: Region): JFXDialog { diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt index db4b21332..e9567ecc9 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt @@ -23,7 +23,10 @@ import javafx.scene.layout.StackPane import javafx.scene.web.WebEngine import javafx.scene.web.WebView import javafx.stage.Stage +import org.jackhuang.hmcl.i18n +import org.jackhuang.hmcl.setting.Settings import org.jackhuang.hmcl.util.Log4jLevel +import org.jackhuang.hmcl.util.readFullyAsString import org.w3c.dom.Document import org.w3c.dom.Node @@ -38,20 +41,25 @@ class LogWindow : Stage() { init { scene = Scene(rootPane, 800.0, 480.0) + title = i18n("logwindow.title") + + contentPane.onScroll engine = contentPane.engine - engine.loadContent("") + engine.loadContent(javaClass.getResourceAsStream("/assets/log-window-content.html").readFullyAsString().replace("\${FONT}", "${Settings.font.size}px \"${Settings.font.family}\"")) engine.loadWorker.stateProperty().addListener { _, _, newValue -> if (newValue == Worker.State.SUCCEEDED) { document = engine.document body = document.getElementsByTagName("body").item(0) } } + } fun logLine(line: String, level: Log4jLevel) { body.appendChild(contentPane.engine.document.createElement("div").apply { - setAttribute("style", "color: #${level.color.toString().substring(2)};") + setAttribute("style", "background-color: #${level.color.toString().substring(2)};") textContent = line }) + engine.executeScript("scrollToBottom()") } } \ No newline at end of file diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/SettingsPage.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/SettingsPage.kt index 6a2d49882..b2960fb6e 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/SettingsPage.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/SettingsPage.kt @@ -25,12 +25,15 @@ import javafx.collections.FXCollections import javafx.fxml.FXML import javafx.scene.control.Label import javafx.scene.layout.StackPane +import javafx.scene.text.Font import org.jackhuang.hmcl.i18n import org.jackhuang.hmcl.setting.DownloadProviders import org.jackhuang.hmcl.setting.Locales import org.jackhuang.hmcl.setting.Proxies import org.jackhuang.hmcl.setting.Settings import org.jackhuang.hmcl.ui.construct.FileItem +import org.jackhuang.hmcl.ui.construct.FontComboBox +import org.jackhuang.hmcl.ui.construct.Validator import org.jackhuang.hmcl.ui.wizard.DecoratorPage class SettingsPage : StackPane(), DecoratorPage { @@ -41,10 +44,13 @@ class SettingsPage : StackPane(), DecoratorPage { @FXML lateinit var txtProxyUsername: JFXTextField @FXML lateinit var txtProxyPassword: JFXTextField @FXML lateinit var cboProxyType: JFXComboBox<*> + @FXML lateinit var cboFont: FontComboBox @FXML lateinit var cboLanguage: JFXComboBox<*> @FXML lateinit var cboDownloadSource: JFXComboBox<*> @FXML lateinit var fileCommonLocation: FileItem @FXML lateinit var fileBackgroundLocation: FileItem + @FXML lateinit var lblDisplay: Label + @FXML lateinit var txtFontSize: JFXTextField init { loadFXML("/assets/fxml/setting.fxml") @@ -77,6 +83,23 @@ class SettingsPage : StackPane(), DecoratorPage { Settings.downloadProvider = DownloadProviders.getDownloadProvider(newValue.toInt()) } + cboFont.selectionModel.select(Settings.font.family) + cboFont.valueProperty().addListener { _, _, newValue -> + val font = Font.font(newValue, Settings.font.size) + Settings.font = font + lblDisplay.style = "-fx-font: ${Settings.font.size} \"${font.family}\";" + } + txtFontSize.text = Settings.font.size.toString() + txtFontSize.validators += Validator { it.toDoubleOrNull() != null } + txtFontSize.textProperty().addListener { _, _, newValue -> + if (txtFontSize.validate()) { + val font = Font.font(Settings.font.family, newValue.toDouble()) + Settings.font = font + lblDisplay.style = "-fx-font: ${font.size} \"${Settings.font.family}\";" + } + } + lblDisplay.style = "-fx-font: ${Settings.font.size} \"${Settings.font.family}\";" + val list = FXCollections.observableArrayList