用tornadofx框架做了个简易浏览器
import javafx.concurrent.Worker
import javafx.geometry.Orientation
import javafx.scene.control.ListCell
import javafx.scene.effect.Effect
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.layout.BorderStrokeStyle.SOLID
import javafx.scene.layout.Priority
import javafx.scene.paint.Color
import javafx.scene.text.FontSmoothingType
import javafx.scene.web.WebHistory
import javafx.scene.web.WebView
import javafx.stage.FileChooser
import tornadofx.*
import java.net.MalformedURLException
class MyApp : App(WebviewView::class, Mystyle::class)
class WebviewView : View("webview") {
val homePageUrl = "http://www.dogedoge.com"
val pageUrl = stringProperty()
var wv = webview {
prefWidth = 470.0
engine.userAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36"
// isContextMenuEnabled=false
// fontScale=1.2
// zoom=1.2
fontSmoothingType = FontSmoothingType.GRAY
}
val back = booleanProperty(false)
val forward = booleanProperty(false)
val history0 = objectProperty<WebHistory.Entry>()
val history = wv.engine.history
override val root = borderpane {
addStageIcon(Image("tornado-fx-logo.png"))
setPrefSize(800.0, 700.0)
top = hbox(4) {
paddingBottom = 10.0
label("History:"){
effect
}
textfield(pageUrl) {
hgrow = Priority.ALWAYS
action {
wv.engine.load(text)
}
}
button("Refresh") {
action {
wv.engine.reload()
}
}
button("Go") {
action {
wv.engine.load(pageUrl.value)
}
}
button("Home") {
action {
wv.engine.load(homePageUrl)
}
}
button("Open") {
action {
val selectedFile = chooseFile("Open Web Content", arrayOf(FileChooser.ExtensionFilter("HTML Files", "*.html", "*.htm")))
if (selectedFile.isNotEmpty()) {
try {
wv.engine.load(selectedFile.first().toURI().toURL().toExternalForm())
} catch (e: MalformedURLException) {
e.printStackTrace()
}
}
}
}
menubutton("Options") {
checkmenuitem("Enable Context Menu") {
isSelected = true
wv.contextMenuEnabledProperty().bind(selectedProperty())
}
menu("Font Scale") {
textProperty().bind(stringProperty("Font Scale ").concat(wv.fontScaleProperty().multiply(100.0)).concat("%"))
item("Normal") {
action {
wv.fontScale = 1.0
}
}
item("10% Bigger") {
action {
wv.fontScale += 0.1
}
}
item("10% Smaller") {
action {
wv.fontScale -= 0.1
}
}
}
menu("Font Smoothing") {
togglegroup {
radiomenuitem("GRAY") {
isSelected = true
action {
wv.fontSmoothingType = FontSmoothingType.GRAY
}
}
radiomenuitem("LCD") {
action {
wv.fontSmoothingType = FontSmoothingType.LCD
}
}
}
}
menu("Zoom") {
textProperty().bind(stringProperty("Zoom ").concat(wv.zoomProperty().multiply(100.0)).concat("%"))
item("Normal") {
action {
wv.zoom = 1.0
}
}
item("10% Bigger") {
action {
wv.zoom += 0.1
}
}
item("10% Smaller") {
action {
wv.zoom -= 0.1
}
}
}
checkmenuitem("Enable JavaScript") {
isSelected = true
wv.engine.javaScriptEnabledProperty().bind(selectedProperty())
}
}
button("Back") {
enableWhen(back)
action {
history.go(-1)
}
}
button("Forward") {
enableWhen(forward)
action {
history.go(1)
}
}
label("History:")
combobox(history0, history.entries) {
prefWidth = 150.0
setCellFactory {
object : ListCell<WebHistory.Entry>() {
public override fun updateItem(item: WebHistory.Entry?, empty: Boolean) {
super.updateItem(item, empty)
if (empty) {
text = null
graphic = null
} else {
val pageTitle = item?.title
text = pageTitle
}
}
}
}
setOnAction {
val currentIndex = history.currentIndex
val selectedEntry = value
val selectedIndex = items.indexOf(selectedEntry)
val offset = selectedIndex - currentIndex
history.go(offset)
}
}
}
center {
add(wv)
run { wv.engine.load(homePageUrl) }
wv.engine.loadWorker.stateProperty().addListener { _, _, newState ->
if (newState == Worker.State.SUCCEEDED)
title = wv.engine.title
}
wv.engine.locationProperty().addListener { _, _, newValue ->
pageUrl.value = newValue
}
// wv.engine.titleProperty().addListener { _, _, newValue ->
// title = newValue
// }
history.currentIndexProperty().addListener { _, _, newValue ->
val currentIndex = newValue.toInt()
back.value = currentIndex > 0
forward.value = currentIndex < history.entries.size
}
}
}
}
class Mystyle : Stylesheet() {
init {
root {
padding = box(10.px)
borderWidth += box(2.px)
borderInsets += box(5.px)
borderRadius += box(5.px)
// borderColor += box(Color.BLUE)
// borderStyle += SOLID
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。