Skip to content

Commit

Permalink
refactor Focus ToolWindow to not use Register that is Internal in 2022.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DevSrSouza committed May 2, 2022
1 parent 3c30ff7 commit e1d7e7f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package br.com.devsrsouza.intellij.dropboxfocus.extensions

import androidx.compose.ui.awt.ComposePanel
import br.com.devsrsouza.intellij.dropboxfocus.services.FocusGradleSettingsReader
import br.com.devsrsouza.intellij.dropboxfocus.services.FocusService
import br.com.devsrsouza.intellij.dropboxfocus.ui.FocusSelection
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import java.awt.Dimension

class FocusToolWindowFactory : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
ApplicationManager.getApplication().invokeLater {
toolWindow.component.add(
ComposePanel().apply {
val focusService = project.service<FocusService>()
preferredSize = Dimension(300, 300)
setContent {
FocusSelection(
currentFocusGradleSettingsState = project.service<FocusGradleSettingsReader>()
.focusGradleSettings,
isLoadingState = focusService.focusOperationState,
syncGradle = focusService::syncGradle,
selectModuleToFocus = { focusGradleSettings, focusModule ->
focusService.focusOn(focusGradleSettings, focusModule.gradleModulePath)
}
)
}
}
)
}
}

override fun isApplicable(project: Project): Boolean {
return true
}

override fun shouldBeAvailable(project: Project): Boolean {
return project.service<FocusGradleSettingsReader>().focusGradleSettings.value != null
}
}
Original file line number Diff line number Diff line change
@@ -1,77 +1,34 @@
package br.com.devsrsouza.intellij.dropboxfocus.services

import androidx.compose.ui.awt.ComposePanel
import br.com.devsrsouza.intellij.dropboxfocus.ui.FocusSelection
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.RegisterToolWindowTask
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowAnchor
import com.intellij.openapi.wm.ToolWindowManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.swing.Swing
import java.awt.Dimension
import org.jetbrains.kotlin.idea.util.application.invokeLater

@Service
class FocusToolWindowService(private val project: Project) : Disposable {
private val job = Job()
private var toolWindow: ToolWindow? = null

fun init() {
GlobalScope.launch(Dispatchers.Swing + job) {
project.service<FocusGradleSettingsReader>().focusGradleSettings.collect {
createOrReload(it)
}
}
}

private fun createOrReload(focusGradleSettings: FocusGradleSettings?) {
ToolWindowManager.getInstance(project).invokeLater {
if (toolWindow == null && focusGradleSettings != null) {
toolWindow = registerToolWindow()
}
GlobalScope.launch(Dispatchers.Swing + job) {
project.service<FocusGradleSettingsReader>().focusGradleSettings.collect {
val toolWindow = ToolWindowManager.getInstance(project)
.getToolWindow("Focus")

if (toolWindow != null) {
if (toolWindow!!.isAvailable && focusGradleSettings == null) {
toolWindow!!.remove()
toolWindow = null
toolWindow?.isAvailable = it != null
}
}
}
}

private fun registerToolWindow(): ToolWindow {
return ToolWindowManager.getInstance(project).registerToolWindow(
RegisterToolWindowTask(
id = "Focus",
anchor = ToolWindowAnchor.LEFT,
canCloseContent = false,
component = ComposePanel().apply {
val focusService = project.service<FocusService>()
preferredSize = Dimension(300, 300)
setContent {
FocusSelection(
currentFocusGradleSettingsState = project.service<FocusGradleSettingsReader>()
.focusGradleSettings,
isLoadingState = focusService.focusOperationState,
syncGradle = focusService::syncGradle,
selectModuleToFocus = { focusGradleSettings, focusModule ->
focusService.focusOn(focusGradleSettings, focusModule.gradleModulePath)
}
)
}
},
sideTool = true,
)
)
}

override fun dispose() {
job.cancel()
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
instance="br.com.devsrsouza.intellij.dropboxfocus.services.FocusConfigurable" />

<projectService serviceImplementation="br.com.devsrsouza.intellij.dropboxfocus.services.FocusSettings" />

<toolWindow factoryClass="br.com.devsrsouza.intellij.dropboxfocus.extensions.FocusToolWindowFactory" id="Focus"
anchor="left" canCloseContents="false" secondary="true"/>
</extensions>

<applicationListeners>
Expand Down

0 comments on commit e1d7e7f

Please sign in to comment.