@@ -2,7 +2,12 @@ package processing.app.gradle
22
33import androidx.compose.runtime.mutableStateListOf
44import androidx.compose.runtime.mutableStateOf
5+ import androidx.compose.runtime.neverEqualPolicy
56import androidx.compose.ui.awt.ComposePanel
7+ import kotlinx.coroutines.CoroutineScope
8+ import kotlinx.coroutines.Dispatchers
9+ import kotlinx.coroutines.delay
10+ import kotlinx.coroutines.launch
611import processing.app.Language.text
712import processing.app.Mode
813import processing.app.Preferences
@@ -30,7 +35,7 @@ class GradleService(
3035 val editor : Editor ? ,
3136) {
3237 val active = mutableStateOf(Preferences .getBoolean(" run.use_gradle" ))
33- var sketch = mutableStateOf<Sketch ?>(null )
38+ var sketch = mutableStateOf<Sketch ?>(null , neverEqualPolicy() )
3439 val jobs = mutableStateListOf<GradleJob >()
3540 val workingDir = createTempDirectory()
3641
@@ -52,7 +57,7 @@ class GradleService(
5257
5358 val job = GradleJob (
5459 tasks = tasks,
55- workingDir = workingDir,
60+ workingDir = workingDir,
5661 sketch = sketch.value ? : throw IllegalStateException (" Sketch is not set" ),
5762 editor = editor
5863 )
@@ -64,10 +69,47 @@ class GradleService(
6469 jobs.forEach(GradleJob ::cancel)
6570 }
6671
72+ private val scope = CoroutineScope (Dispatchers .IO )
73+
74+ /*
75+ Watch the sketch folder for changes and start a build job when the sketch is modified
76+ This need to be done properly to use hooks in the future but right now this is the simplest way to do it
77+ */
78+ init {
79+ scope.launch {
80+ var path = " "
81+ var modified = false
82+ var sketched: Sketch ? = null
83+ while (true ){
84+ sketch.value?.let { sketch ->
85+ if (sketch.folder.absolutePath != path){
86+ path = sketch.folder.absolutePath
87+ if (sketched == sketch){
88+ // The same sketch has its folder changed, trigger updates downstream from the service
89+ this @GradleService.sketch.value = sketch
90+ }else {
91+ sketched = sketch
92+ }
93+ startJob(" build" )
94+ }
95+ if (sketch.isModified != modified){
96+ modified = sketch.isModified
97+ if (! modified){
98+ // If the sketch is no longer modified, start the build job, aka build on save
99+ startJob(" build" )
100+ }
101+ }
102+ }
103+
104+
105+ delay(100 )
106+ }
107+ }
108+ }
109+
67110 // Hooks for java to interact with the Gradle service since mutableStateOf is not accessible in java
68111 fun setSketch (sketch : Sketch ){
69112 this .sketch.value = sketch
70- startJob(" build" )
71113 }
72114 fun getEnabled (): Boolean {
73115 return active.value
0 commit comments