From b962c62209c535232bb0925fb0ec0bcbb6a6a214 Mon Sep 17 00:00:00 2001 From: Sarah Gerweck Date: Wed, 15 Jun 2016 05:42:35 -0700 Subject: [PATCH] Add control-panel builders I've used these in a few projects now, so might as well put them in a common library where they can get properly enhanced. --- .../scalafx/util/control/ControlPanel.scala | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main/scala/org/gerweck/scalafx/util/control/ControlPanel.scala diff --git a/src/main/scala/org/gerweck/scalafx/util/control/ControlPanel.scala b/src/main/scala/org/gerweck/scalafx/util/control/ControlPanel.scala new file mode 100644 index 0000000..29893e2 --- /dev/null +++ b/src/main/scala/org/gerweck/scalafx/util/control/ControlPanel.scala @@ -0,0 +1,83 @@ +package org.gerweck.scalafx.util.control + +import language.implicitConversions + +import scalafx.geometry.Orientation.Horizontal +import scalafx.geometry.Pos +import scalafx.scene.Node +import scalafx.scene.control.{ Label, Separator } +import scalafx.scene.layout._ +import scalafx.scene.text._ + +import org.gerweck.scalafx.util._ + +import ControlPanel._ + +object HorizontalControlPanel { + def apply(controls: ControlPanelEntry*): Pane = + new HBox { + alignment = Pos.Center + hgrow = Priority.Always + spacing = 15 + children = controls map { + case RegularControl(name, control) => + new HBox { + spacing = 4 + hgrow = Priority.Always + children = ControlPanel.controlRow(name, control) + alignment = Pos.CenterRight + } + case SeparatorEntry => + Separators.vertical + } + } +} + +object VerticalControlPanel { + def apply(controls: ControlPanelEntry*): Pane = new GridPane { gp => + hgap = 4 + vgap = 6 + for ((entry, i) <- controls.zipWithIndex) { + entry match { + case RegularControl(name, control) => + gp.addToRow(i, ControlPanel.controlRow(name, control): _*) + + case SeparatorEntry => + val sep = new Separator { + orientation = Horizontal + } + gp.add(sep, 0, i, 2, 1) + } + } + } +} + +object ControlPanel { + def apply(controls: ControlPanelEntry*): Pane = VerticalControlPanel(controls: _*) + + private[this] lazy val defaultFont = new Font(Font.default) + private[this] lazy val boldFont = { + Font(defaultFont.family, FontWeight.Bold, defaultFont.size) + } + + private[this] lazy val labelsFont = defaultFont + + private[control] def controlRow(name: String, control: Node) = { + val label = new Label(name + ':') { + alignmentInParent = Pos.CenterRight + font = labelsFont + } + Seq(label, control) + } + + /* It's expected that you won't create these directly, but that you'll + * mostly use the implicit magnets */ + sealed trait ControlPanelEntry + case class RegularControl(label: String, control: Node) extends ControlPanelEntry + case object SeparatorEntry extends ControlPanelEntry + + object ControlPanelEntry { + implicit def pair2Regular(p: (String, Node)): RegularControl = RegularControl.tupled(p) + implicit def sep2Sep(p: Separator): SeparatorEntry.type = SeparatorEntry + } +}