Skip to content

Commit 0b3bced

Browse files
Add Toolbar to ToolWindow
The ethersync tool window provides now buttons to start and stop the ethersync daemon (see #4).
1 parent 7baefa1 commit 0b3bced

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ build/
44
!gradle/wrapper/gradle-wrapper.jar
55
!**/src/main/**/build/
66
!**/src/test/**/build/
7+
kls_database.db
78

89
### IntelliJ IDEA ###
910
.idea/modules.xml

src/main/kotlin/io/github/ethersync/EthersyncService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ interface EthersyncService {
44

55
fun start(peer: String?)
66

7+
fun shutdown()
8+
79
fun startWithCustomCommandLine(commandLine: String)
810
}

src/main/kotlin/io/github/ethersync/EthersyncServiceImpl.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.intellij.execution.process.ColoredProcessHandler
55
import com.intellij.execution.process.ProcessEvent
66
import com.intellij.execution.process.ProcessListener
77
import com.intellij.execution.ui.ConsoleView
8+
import com.intellij.openapi.application.EDT
89
import com.intellij.openapi.components.Service
910
import com.intellij.openapi.diagnostic.logger
1011
import com.intellij.openapi.editor.EditorFactory
@@ -15,7 +16,6 @@ import com.intellij.openapi.fileEditor.TextEditor
1516
import com.intellij.openapi.project.Project
1617
import com.intellij.openapi.project.ProjectManager
1718
import com.intellij.openapi.project.ProjectManagerListener
18-
import com.intellij.openapi.rd.util.withUiContext
1919
import com.intellij.openapi.util.Key
2020
import com.intellij.openapi.vfs.VirtualFile
2121
import com.intellij.openapi.wm.ToolWindowManager
@@ -27,7 +27,9 @@ import io.github.ethersync.settings.AppSettings
2727
import io.github.ethersync.sync.Changetracker
2828
import io.github.ethersync.sync.Cursortracker
2929
import kotlinx.coroutines.CoroutineScope
30+
import kotlinx.coroutines.Dispatchers
3031
import kotlinx.coroutines.launch
32+
import kotlinx.coroutines.withContext
3133
import org.eclipse.lsp4j.jsonrpc.Launcher
3234
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
3335
import java.io.BufferedReader
@@ -99,14 +101,18 @@ class EthersyncServiceImpl(
99101

100102
ProjectManager.getInstance().addProjectManagerListener(project, object: ProjectManagerListener {
101103
override fun projectClosingBeforeSave(project: Project) {
102-
cs.launch {
103-
shutdown()
104-
}
104+
shutdown()
105105
}
106106
})
107107
}
108108

109-
suspend fun shutdown() {
109+
override fun shutdown() {
110+
cs.launch {
111+
shutdownImpl()
112+
}
113+
}
114+
115+
private suspend fun shutdownImpl() {
110116
clientProcess?.let {
111117
it.destroy()
112118
it.awaitExit()
@@ -165,14 +171,14 @@ class EthersyncServiceImpl(
165171
}
166172

167173
cs.launch {
168-
shutdown()
174+
shutdownImpl()
169175

170176
if (!ethersyncDirectory.exists()) {
171177
LOG.debug("Creating ethersync directory")
172178
ethersyncDirectory.mkdir()
173179
}
174180

175-
withUiContext {
181+
withContext(Dispatchers.EDT) {
176182
daemonProcess = ColoredProcessHandler(cmd)
177183

178184
daemonProcess!!.addProcessListener(object : ProcessListener {
@@ -183,15 +189,13 @@ class EthersyncServiceImpl(
183189
}
184190

185191
override fun processTerminated(event: ProcessEvent) {
186-
cs.launch {
187-
shutdown()
188-
}
192+
shutdown()
189193
}
190194
})
191195

192196

193197
val tw = ToolWindowManager.getInstance(project).getToolWindow("ethersync")!!
194-
val console = tw.contentManager.findContent("Daemon")!!.component
198+
val console = tw.contentManager.findContent("Daemon")!!.component
195199
if (console is ConsoleView) {
196200
console.attachToProcess(daemonProcess!!)
197201
}
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
package io.github.ethersync
22

33
import com.intellij.execution.filters.TextConsoleBuilderFactory
4+
import com.intellij.openapi.actionSystem.ActionGroup
5+
import com.intellij.openapi.actionSystem.ActionManager
6+
import com.intellij.openapi.actionSystem.AnAction
7+
import com.intellij.openapi.actionSystem.AnActionEvent
8+
import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl
49
import com.intellij.openapi.project.Project
510
import com.intellij.openapi.wm.ToolWindow
611
import com.intellij.openapi.wm.ToolWindowFactory
712
import com.intellij.ui.content.ContentFactory
13+
import java.awt.BorderLayout
14+
import javax.swing.BoxLayout
15+
import javax.swing.JPanel
16+
817

918
class EthersyncToolWindowFactory : ToolWindowFactory {
1019

1120
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
12-
val console = TextConsoleBuilderFactory.getInstance()
21+
val panel = JPanel()
22+
panel.setLayout(BorderLayout())
23+
24+
val console = TextConsoleBuilderFactory.getInstance()
1325
.createBuilder(project)
1426
.console
1527

16-
val content = ContentFactory.getInstance()
17-
.createContent(console.component, "Daemon", true)
28+
panel.add(console.component, BorderLayout.CENTER)
29+
30+
val actionToolBar = ActionManager.getInstance().createActionToolbar("", object : ActionGroup() {
31+
override fun getChildren(p0: AnActionEvent?): Array<AnAction> {
32+
return arrayOf(StartEthersyncDaemonAction(), StopEthersyncDaemonAction())
33+
}
34+
}, true)
35+
36+
panel.add(
37+
actionToolBar.component, BorderLayout.NORTH)
1838

19-
toolWindow.contentManager.addContent(content)
39+
toolWindow.contentManager.addContent(ContentFactory.getInstance()
40+
.createContent(panel, "Daemon", true))
2041
}
2142
}

src/main/kotlin/io/github/ethersync/StartEthersyncDaemonAction.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.github.ethersync
22

3+
import com.intellij.icons.AllIcons
34
import com.intellij.openapi.actionSystem.AnAction
45
import com.intellij.openapi.actionSystem.AnActionEvent
56
import com.intellij.openapi.components.service
67
import com.intellij.openapi.ui.Messages
78

8-
class StartEthersyncDaemonAction : AnAction() {
9+
class StartEthersyncDaemonAction : AnAction("Connect to peer", "Connect to a running ethersync daemon or start a new peer",
10+
AllIcons.CodeWithMe.CwmInvite) {
911

1012
override fun actionPerformed(e: AnActionEvent) {
1113
val project = e.project ?: return
12-
1314
val address = Messages.showInputDialog(
1415
project,
1516
"Provide ethersync peer address. Leave empty if you want to host a new session.",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.ethersync
2+
3+
import com.intellij.icons.AllIcons
4+
import com.intellij.openapi.actionSystem.AnAction
5+
import com.intellij.openapi.actionSystem.AnActionEvent
6+
import com.intellij.openapi.components.service
7+
8+
class StopEthersyncDaemonAction : AnAction("Stpo daemon", "Stop running ethersync daemon", AllIcons.Run.Stop) {
9+
override fun actionPerformed(e: AnActionEvent) {
10+
val project = e.project ?: return
11+
12+
val service = project.service<EthersyncService>()
13+
service.shutdown()
14+
}
15+
}

0 commit comments

Comments
 (0)