You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.2 KiB

  1. package org.gerweck.scalafx.util.control
  2. import language.implicitConversions
  3. import scalafx.geometry.Orientation.Horizontal
  4. import scalafx.geometry.Pos
  5. import scalafx.scene.Node
  6. import scalafx.scene.control.{ Label, Separator }
  7. import scalafx.scene.layout._
  8. import scalafx.scene.text._
  9. import org.gerweck.scalafx.util._
  10. import ControlPanel._
  11. object HorizontalControlPanel {
  12. def apply(controls: ControlPanelEntry*): Pane =
  13. new HBox {
  14. alignment = Pos.Center
  15. hgrow = Priority.Always
  16. spacing = 15
  17. children = controls map {
  18. case RegularControl(name, control) =>
  19. new HBox {
  20. spacing = 4
  21. hgrow = Priority.Always
  22. children = ControlPanel.controlRow(name, control)
  23. alignment = Pos.CenterRight
  24. }
  25. case SeparatorEntry =>
  26. Separators.vertical()
  27. }
  28. }
  29. }
  30. object VerticalControlPanel {
  31. def apply(controls: ControlPanelEntry*): Pane = new GridPane { gp =>
  32. hgap = 4
  33. vgap = 6
  34. for ((entry, i) <- controls.zipWithIndex) {
  35. entry match {
  36. case RegularControl(name, control) =>
  37. gp.addToRow(i, ControlPanel.controlRow(name, control): _*)
  38. case SeparatorEntry =>
  39. val sep = new Separator {
  40. orientation = Horizontal
  41. }
  42. gp.add(sep, 0, i, 2, 1)
  43. }
  44. }
  45. }
  46. }
  47. object ControlPanel {
  48. def apply(controls: ControlPanelEntry*): Pane = VerticalControlPanel(controls: _*)
  49. private[this] lazy val defaultFont = new Font(Font.default)
  50. private[this] lazy val labelsFont = defaultFont
  51. private[control] def controlRow(name: String, control: Node) = {
  52. val label = new Label(name + ':') {
  53. alignmentInParent = Pos.CenterRight
  54. font = labelsFont
  55. }
  56. Seq(label, control)
  57. }
  58. /* It's expected that you won't create these directly, but that you'll
  59. * mostly use the implicit magnets */
  60. sealed trait ControlPanelEntry
  61. case class RegularControl(label: String, control: Node) extends ControlPanelEntry
  62. case object SeparatorEntry extends ControlPanelEntry
  63. object ControlPanelEntry {
  64. implicit def pair2Regular(p: (String, Node)): RegularControl = RegularControl.tupled(p)
  65. implicit def sep2Sep(p: Separator): SeparatorEntry.type = SeparatorEntry
  66. }
  67. }