| 1 |
package org.iwakura.larcjb.widget |
| 2 |
|
| 3 |
import com.intellij.openapi.project.Project |
| 4 |
import com.intellij.openapi.util.Disposer |
| 5 |
import com.intellij.openapi.vcs.ProjectLevelVcsManager |
| 6 |
import com.intellij.openapi.vfs.VirtualFile |
| 7 |
import com.intellij.openapi.wm.StatusBar |
| 8 |
import com.intellij.openapi.wm.StatusBarWidget |
| 9 |
import com.intellij.openapi.wm.StatusBarWidgetFactory |
| 10 |
import com.intellij.util.Consumer |
| 11 |
import org.iwakura.larcjb.LarcExecutable |
| 12 |
import org.iwakura.larcjb.LarcVcs |
| 13 |
import java.awt.event.MouseEvent |
| 14 |
|
| 15 |
/** |
| 16 |
* Status bar widget showing current Larc branch. |
| 17 |
*/ |
| 18 |
class LarcBranchWidgetFactory : StatusBarWidgetFactory { |
| 19 |
|
| 20 |
override fun getId(): String = WIDGET_ID |
| 21 |
|
| 22 |
override fun getDisplayName(): String = "Larc Branch" |
| 23 |
|
| 24 |
override fun isAvailable(project: Project): Boolean { |
| 25 |
return ProjectLevelVcsManager.getInstance(project) |
| 26 |
.allVcsRoots |
| 27 |
.any { it.vcs?.name == LarcVcs.NAME } |
| 28 |
} |
| 29 |
|
| 30 |
override fun createWidget(project: Project): StatusBarWidget { |
| 31 |
return LarcBranchWidget(project) |
| 32 |
} |
| 33 |
|
| 34 |
override fun canBeEnabledOn(statusBar: StatusBar): Boolean = true |
| 35 |
|
| 36 |
companion object { |
| 37 |
const val WIDGET_ID = "LarcBranchWidget" |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
class LarcBranchWidget(private val project: Project) : StatusBarWidget, StatusBarWidget.TextPresentation { |
| 42 |
|
| 43 |
private var statusBar: StatusBar? = null |
| 44 |
private var currentBranch: String? = null |
| 45 |
|
| 46 |
override fun ID(): String = LarcBranchWidgetFactory.WIDGET_ID |
| 47 |
|
| 48 |
override fun getPresentation(): StatusBarWidget.WidgetPresentation = this |
| 49 |
|
| 50 |
override fun install(statusBar: StatusBar) { |
| 51 |
this.statusBar = statusBar |
| 52 |
updateBranch() |
| 53 |
} |
| 54 |
|
| 55 |
override fun dispose() { |
| 56 |
statusBar = null |
| 57 |
} |
| 58 |
|
| 59 |
override fun getText(): String { |
| 60 |
return currentBranch?.let { "Larc: $it" } ?: "" |
| 61 |
} |
| 62 |
|
| 63 |
override fun getTooltipText(): String = "Current Larc branch" |
| 64 |
|
| 65 |
override fun getAlignment(): Float = 0f |
| 66 |
|
| 67 |
override fun getClickConsumer(): Consumer<MouseEvent>? { |
| 68 |
return Consumer { |
| 69 |
/* TODO(kroot): show branch selector popup */ |
| 70 |
updateBranch() |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
fun updateBranch() { |
| 75 |
val roots = getLarcRoots() |
| 76 |
if (roots.isEmpty()) { |
| 77 |
currentBranch = null |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
val executable = LarcExecutable(project) |
| 82 |
currentBranch = executable.currentBranch(roots.first()) |
| 83 |
|
| 84 |
statusBar?.updateWidget(ID()) |
| 85 |
} |
| 86 |
|
| 87 |
private fun getLarcRoots(): List<VirtualFile> { |
| 88 |
return ProjectLevelVcsManager.getInstance(project) |
| 89 |
.allVcsRoots |
| 90 |
.filter { it.vcs?.name == LarcVcs.NAME } |
| 91 |
.mapNotNull { it.path } |
| 92 |
} |
| 93 |
} |
| 94 |
|