Skip to content

Commit

Permalink
Add defaultLabel to select
Browse files Browse the repository at this point in the history
  • Loading branch information
JD557 committed May 5, 2024
1 parent bdeb5cf commit ad1e4db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ trait Components:
else skin.renderButton(area, label, itemStatus)

/** Select box component. Returns the index value currently selected inside a PanelState.
*
* It also allows one to define a default label if the value is invalid.
* This can be particularly to keep track if a user has selected no value, by setting the
* initial value to an invalid sentinel value (e.g. -1).
*
* @param labels text labels for each value
* @param undefinedFirstValue if true, the value 0 will not show on the options, acting as a default undefined value
* @param defaultLabel label to print if the value doesn't match any of the labels.
*/
final def select(
id: ItemId,
labels: Vector[String],
undefinedFirstValue: Boolean = false,
defaultLabel: String = "",
skin: SelectSkin = SelectSkin.default()
): ComponentWithValue[PanelState[Int]] =
new ComponentWithValue[PanelState[Int]]:
Expand All @@ -132,17 +136,16 @@ trait Components:
val selectBoxArea = skin.selectBoxArea(area)
val itemStatus = UiContext.registerItem(id, area)
value.modifyIf(itemStatus.selected)(_.open)
skin.renderSelectBox(area, value.get.value, labels, itemStatus)
skin.renderSelectBox(area, value.get.value, labels, defaultLabel, itemStatus)
if (value.get.isOpen)
value.modifyIf(!itemStatus.selected)(_.close)
val selectableLabels = labels.drop(if (undefinedFirstValue) 1 else 0)
Primitives.onTop:
selectableLabels.zipWithIndex
labels.zipWithIndex
.foreach: (label, idx) =>
val selectOptionArea = skin.selectOptionArea(area, idx)
val optionStatus = UiContext.registerItem(id |> idx, selectOptionArea)
skin.renderSelectOption(area, idx, selectableLabels, optionStatus)
if (optionStatus.active) value := PanelState.closed(if (undefinedFirstValue) idx + 1 else idx)
skin.renderSelectOption(area, idx, labels, optionStatus)
if (optionStatus.active) value := PanelState.closed(idx)

/** Slider component. Returns the current position of the slider, between min and max.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ trait SelectSkin:
def allocateArea(allocator: LayoutAllocator.AreaAllocator, labels: Vector[String]): Rect

def selectBoxArea(area: Rect): Rect
def renderSelectBox(area: Rect, value: Int, labels: Vector[String], itemStatus: UiContext.ItemStatus)(using
def renderSelectBox(
area: Rect,
value: Int,
labels: Vector[String],
defaultLabel: String,
itemStatus: UiContext.ItemStatus
)(using
uiContext: UiContext
): Unit

Expand All @@ -32,11 +38,17 @@ object SelectSkin extends DefaultSkin:
def selectBoxArea(area: Rect): Rect =
area

def renderSelectBox(area: Rect, value: Int, labels: Vector[String], itemStatus: UiContext.ItemStatus)(using
def renderSelectBox(
area: Rect,
value: Int,
labels: Vector[String],
defaultLabel: String,
itemStatus: UiContext.ItemStatus
)(using
uiContext: UiContext
): Unit =
val selectBoxArea = this.selectBoxArea(area)
val selectedLabel = labels.applyOrElse(value, _ => "")
val selectedLabel = labels.applyOrElse(value, _ => defaultLabel)
itemStatus match
case UiContext.ItemStatus(_, _, true, _) | UiContext.ItemStatus(_, true, _, _) =>
rectangle(selectBoxArea, colorScheme.primaryHighlight)
Expand Down

0 comments on commit ad1e4db

Please sign in to comment.