All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marid.fx.action.ActionExtensions.kt Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
package org.marid.fx.action

import javafx.beans.property.SimpleStringProperty
import javafx.scene.control.*
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import org.marid.fx.control.ToolButton
import org.marid.fx.extensions.mapObject

fun String?.icon(size: Int): Image? = this?.let { Image(it, size.toDouble(), size.toDouble(), false, true) }

fun  C.configure(action: Fx): C = this
  .apply { tooltipProperty().bind(action.description.mapObject { it?.let(::Tooltip) }) }

fun  L.configure(action: Fx, size: Int = 24): L = this
  .apply { (this as Control).configure(action) }
  .apply { textProperty().bind(action.text) }
  .apply { graphicProperty().bind(action.icon.mapObject { it?.let { ImageView(it.icon(size)) } }) }

fun  B.configure(action: Fx, size: Int = 24): B = this
  .apply { (this as Labeled).configure(action, size) }
  .apply { onActionProperty().bind(action.handler) }

fun  B.configure(action: Fx, size: Int = 24): B = this
  .apply { (this as ButtonBase).configure(action, size) }
  .apply { selectedProperty().bindBidirectional(action.selected) }

fun  B.configure(action: Fx, size: Int = 24): B = this
  .apply { (this as Button).configure(action, size) }
  .apply { textProperty().bind(SimpleStringProperty()) }
  .apply { tooltipProperty().bind(action.text.mapObject { it?.let(::Tooltip) }) }

fun  M.configure(action: Fx, size: Int = 20): M = this
  .apply { textProperty().bind(action.text) }
  .apply { graphicProperty().bind(action.icon.mapObject { it?.let { ImageView(it.icon(size)) } }) }
  .apply { onActionProperty().bind(action.handler) }
  .apply { acceleratorProperty().bind(action.accelerator) }

fun  M.configure(action: Fx, size: Int = 20): M = this
  .apply { (this as MenuItem).configure(action, size) }
  .apply { selectedProperty().bindBidirectional(action.selected) }

fun  M.configure(action: Fx, size: Int = 20): M = this
  .apply { textProperty().bind(action.text) }
  .apply { graphicProperty().bind(action.icon.mapObject { it?.let { ImageView(it.icon(size)) } }) }

fun  T.configure(action: Fx, size: Int = 18): T = this
  .apply { textProperty().bind(action.text) }
  .apply { graphicProperty().bind(action.icon.mapObject { it?.let { ImageView(it.icon(size)) } }) }
  .apply { tooltipProperty().bind(action.description.mapObject { it?.let(::Tooltip) }) }

val Fx.button get() = Button().configure(this)
val Fx.toolButton get() = ToolButton().configure(this)
val Fx.label get() = Label().configure(this)
val Fx.menuItem get() = MenuItem().configure(this)
val Fx.checkMenuItem get() = CheckMenuItem().configure(this)
val Fx.toggleButton get() = ToggleButton().configure(this)