Disable the apply button if the input is invalid

This commit is contained in:
Fingercomp 2023-06-05 17:02:46 +07:00
parent a65c683529
commit bbfd85395a
No known key found for this signature in database
GPG Key ID: BBC71CEE45D86E37
3 changed files with 26 additions and 9 deletions

View File

@ -58,10 +58,13 @@ TextInputForeground = #333333
TextInputBorderError = #aa8888
TextInputBorderErrorFocused = #cc6666
ButtonBackground = #aaaaaa
ButtonBorder = #888888
ButtonForeground = #333333
ButtonConfirm = #336633
ButtonBackground = #aaaaaa
ButtonBorder = #888888
ButtonForeground = #333333
ButtonBackgroundDisabled = #333333
ButtonBorderDisabled = #666666
ButtonForegroundDisabled = #888888
ButtonConfirm = #336633
BottomDrawerBorder = #888888

View File

@ -11,26 +11,40 @@ import ocelot.desktop.util.DrawUtils
class Button extends Widget with ClickHandler with ClickSoundSource {
def text: String = ""
def onClick(): Unit = {}
def enabled: Boolean = true
override def receiveMouseEvents: Boolean = true
eventHandlers += {
case ClickEvent(MouseEvent.Button.Left, _) =>
case ClickEvent(MouseEvent.Button.Left, _) if enabled =>
onClick()
clickSoundSource.play()
}
override def minimumSize: Size2D = Size2D(24 + text.length * 8, 24)
override def maximumSize: Size2D = minimumSize
override def draw(g: Graphics): Unit = {
g.rect(bounds, ColorScheme("ButtonBackground"))
DrawUtils.ring(g, position.x, position.y, width, height, 2, ColorScheme("ButtonBorder"))
val (background, border, foreground) = if (enabled) (
ColorScheme("ButtonBackground"),
ColorScheme("ButtonBorder"),
ColorScheme("ButtonForeground")
) else (
ColorScheme("ButtonBackgroundDisabled"),
ColorScheme("ButtonBorderDisabled"),
ColorScheme("ButtonForegroundDisabled")
)
g.rect(bounds, background)
DrawUtils.ring(g, position.x, position.y, width, height, 2, border)
g.background = Color.Transparent
g.foreground = ColorScheme("ButtonForeground")
g.foreground = foreground
val textWidth = text.iterator.map(g.font.charWidth(_)).sum
g.text(position.x + ((width - textWidth) / 2).round, position.y + 4, text)
}

View File

@ -91,10 +91,10 @@ class ChangeSimulationSpeedDialog() extends ModalDialog {
override def onClick(): Unit = close()
}, Padding2D(right = 8))
// TODO: disable the button if tickInterval.isEmpty
children :+= new Button {
override def text: String = "Apply"
override def onClick(): Unit = confirm()
override def enabled: Boolean = tickInterval.nonEmpty
}
}
}, Padding2D.equal(16))