I am trying to add functionality in my app to allow a user to change the background theme (Light/Dark) of the app in a view called Settings. If I am changing the theme by using two different style sheets from Stylesheet classes, then how can I change which stylesheet the app uses during execution? Is there a simpler way to do this?
My code for these settings can be found below. Any improvements on the code are helpful as well:
class Settings(){
var BackGroundTheme: BackGroundThemeState = BackGroundThemeState.Light
}
enum class BackGroundThemeState{Light, Dark}
class SettingsController: Controller(){
private var settings = Settings()
fun changeTheme(state: BackGroundThemeState){
when(state){
Light -> settings.BackGroundTheme = Light
Dark -> settings.BackGroundTheme = Dark
}
when(settings.BackGroundTheme){
// Light -> do nothing for now
Dark -> importStylesheet(app.DarkThemeStyleSheet)
}
reloadStylesheetsOnFocus()
}
}
class SettingsView: View("Settings"){
val settings: SettingsController by inject()
private val toggleGroup = ToggleGroup()
override val root = vbox(){
alignment = Pos.BOTTOM_CENTER
setPrefSize(300.0, 200.0)
hbox(){
alignment = Pos.BASELINE_LEFT
vbox {
paddingTop = 10.0
paddingLeft = 30.0
paddingBottom = 90.0
label("Theme")
radiobutton("Light", toggleGroup){
isSelected = true
action {
settings.changeTheme(Light)
}
}
radiobutton("Dark", toggleGroup) {
action {
settings.changeTheme(Dark)
}
}
}
}
hbox {
alignment = Pos.BOTTOM_RIGHT
paddingRight = 15.0
paddingBottom = 10.0
button("OK"){
setPrefSize(70.0, 30.0)
action{
find(SettingsView::class).close()
}
}
}
}
}