Paul Nogas
02/26/2021, 4:35 PMjim
02/26/2021, 4:49 PMdarkModeViewModel.toggleDarkMode()
from within a composable function? That means every time you recompose, you want to flip to/from dark mode, which of course is going to cause a recomposition, and then cause it to flip again, and keep flipping forever.Button(onClick={darkModeViewModel.toggleDarkMode()})
Paul Nogas
02/26/2021, 6:23 PMjim
02/26/2021, 6:38 PMfor
loops or exceptions
when you were taking your very first programming class, it was probably a bit mind bending to start thinking the way a computer thinks. The entire Android team was fighting me for months when I first proposed Compose, until it finally clicked for all of them and now they all love it.
You can't perform a side-effect in a composable function, and calling toggleDarkMode()
is a side effect. You MUST perform the side effect in an event-handler callback of some sort. If you want to toggle the mode from a menu bar, the menu bar must have an operation that allows you to flip the toggle. If you want to do it from a keyboard shortcut, you must listen for the key event and flip the toggle in the key event handler. But you can't call toggleDarkMode
from the composable function because the composable function gets called every time you recompose and the toggle operation is going to trigger a recompose.Paul Nogas
02/26/2021, 7:19 PMJFileChooser(System.getProperty("user.home")).apply { showOpenDialog(AppWindowAmbient.current!!.window)
result = selectedFile
}
and now I have
JFileChooser(System.getProperty("user.home")).apply { showOpenDialog(null)
result = selectedFile
}
TheMrCodes
02/27/2021, 9:18 AMData into UI
just one-Way and so if you like to trigger an Event (a Callback) by user interaction you have to provide it as the data (or specifically as a parameter) of your composable