Improved display of branches' names in branch context menu

This commit is contained in:
Abdelilah El Aissaoui 2022-02-24 23:07:28 +01:00
parent dc80cadc1b
commit 41ff6a57b8
5 changed files with 41 additions and 30 deletions

View File

@ -60,6 +60,11 @@ val Ref.isBranch: Boolean
return this is ObjectIdRef.PeeledNonTag
}
val Ref.isHead: Boolean
get() {
return this.name == Constants.HEAD
}
val Ref.isTag: Boolean
get() = this.name.startsWith(Constants.R_TAGS)

View File

@ -25,7 +25,9 @@ fun Branches(
branchesViewModel: BranchesViewModel,
) {
val branches by branchesViewModel.branches.collectAsState()
val currentBranch by branchesViewModel.currentBranch.collectAsState()
val currentBranchState = branchesViewModel.currentBranch.collectAsState()
val currentBranch = currentBranchState.value
val (mergeBranch, setMergeBranch) = remember { mutableStateOf<Ref?>(null) }
val (rebaseBranch, setRebaseBranch) = remember { mutableStateOf<Ref?>(null) }
@ -36,8 +38,8 @@ fun Branches(
itemContent = { branch ->
BranchLineEntry(
branch = branch,
currentBranchName = currentBranch,
isCurrentBranch = currentBranch == branch.name,
currentBranch = currentBranch,
isCurrentBranch = currentBranch?.name == branch.name,
onBranchClicked = { branchesViewModel.selectBranch(branch) },
onCheckoutBranch = { branchesViewModel.checkoutRef(branch) },
onMergeBranch = { setMergeBranch(branch) },
@ -49,18 +51,18 @@ fun Branches(
}
)
if (mergeBranch != null) {
if (mergeBranch != null && currentBranch != null) {
MergeDialog(
currentBranch,
currentBranchName = currentBranch.simpleName,
mergeBranchName = mergeBranch.name,
onReject = { setMergeBranch(null) },
onAccept = { ff -> branchesViewModel.mergeBranch(mergeBranch, ff) }
)
}
if (rebaseBranch != null) {
if (rebaseBranch != null && currentBranch != null) {
RebaseDialog(
currentBranch,
currentBranchName = currentBranch.simpleName,
rebaseBranchName = rebaseBranch.name,
onReject = { setRebaseBranch(null) },
onAccept = { branchesViewModel.rebaseBranch(rebaseBranch) }
@ -72,7 +74,7 @@ fun Branches(
@Composable
private fun BranchLineEntry(
branch: Ref,
currentBranchName: String,
currentBranch: Ref?,
isCurrentBranch: Boolean,
onBranchClicked: () -> Unit,
onCheckoutBranch: () -> Unit,
@ -86,7 +88,7 @@ private fun BranchLineEntry(
items = {
branchContextMenuItems(
branch = branch,
currentBranchName = currentBranchName,
currentBranch = currentBranch,
isCurrentBranch = isCurrentBranch,
isLocal = branch.isLocal,
onCheckoutBranch = onCheckoutBranch,

View File

@ -2,6 +2,8 @@ package app.ui.context_menu
import androidx.compose.foundation.ContextMenuItem
import androidx.compose.foundation.ExperimentalFoundationApi
import app.extensions.isBranch
import app.extensions.isHead
import app.extensions.simpleLogName
import org.eclipse.jgit.lib.Ref
@ -9,7 +11,7 @@ import org.eclipse.jgit.lib.Ref
fun branchContextMenuItems(
branch: Ref,
isCurrentBranch: Boolean,
currentBranchName: String,
currentBranch: Ref?,
isLocal: Boolean,
onCheckoutBranch: () -> Unit,
onMergeBranch: () -> Unit,
@ -26,18 +28,20 @@ fun branchContextMenuItems(
onClick = onCheckoutBranch
)
)
add(
ContextMenuItem(
label = "Merge branch",
onClick = onMergeBranch
if(currentBranch != null && !currentBranch.isHead) {
add(
ContextMenuItem(
label = "Merge branch",
onClick = onMergeBranch
)
)
)
add(
ContextMenuItem(
label = "Rebase branch",
onClick = onRebaseBranch
add(
ContextMenuItem(
label = "Rebase branch",
onClick = onRebaseBranch
)
)
)
}
}
if (isLocal && !isCurrentBranch) {
add(
@ -47,16 +51,16 @@ fun branchContextMenuItems(
)
)
}
if (!isLocal) {
if (!isLocal && currentBranch != null && !currentBranch.isHead) {
add(
ContextMenuItem(
label = "Push $currentBranchName to ${branch.simpleLogName}",
label = "Push ${currentBranch.simpleLogName} to ${branch.simpleLogName}",
onClick = onPushToRemoteBranch
)
)
add(
ContextMenuItem(
label = "Pull ${branch.simpleLogName} to $currentBranchName",
label = "Pull ${branch.simpleLogName} to ${currentBranch.simpleLogName}",
onClick = onPullFromRemoteBranch
)
)

View File

@ -636,7 +636,7 @@ fun CommitMessage(
BranchChip(
ref = ref,
color = nodeColor,
currentBranch = currentBranch?.name.orEmpty(),
currentBranch = currentBranch,
isCurrentBranch = ref.isSameBranch(currentBranch),
onCheckoutBranch = { onCheckoutRef(ref) },
onMergeBranch = { onMergeBranch(ref) },
@ -825,7 +825,7 @@ fun BranchChip(
modifier: Modifier = Modifier,
isCurrentBranch: Boolean = false,
ref: Ref,
currentBranch: String,
currentBranch: Ref?,
onCheckoutBranch: () -> Unit,
onMergeBranch: () -> Unit,
onDeleteBranch: () -> Unit,
@ -837,7 +837,7 @@ fun BranchChip(
val contextMenuItemsList = {
branchContextMenuItems(
branch = ref,
currentBranchName = currentBranch,
currentBranch = currentBranch,
isCurrentBranch = isCurrentBranch,
isLocal = ref.isLocal,
onCheckoutBranch = onCheckoutBranch,

View File

@ -18,17 +18,17 @@ class BranchesViewModel @Inject constructor(
val branches: StateFlow<List<Ref>>
get() = _branches
private val _currentBranch = MutableStateFlow<String>("")
val currentBranch: StateFlow<String>
private val _currentBranch = MutableStateFlow<Ref?>(null)
val currentBranch: StateFlow<Ref?>
get() = _currentBranch
suspend fun loadBranches(git: Git) {
_currentBranch.value = branchesManager.currentBranchRef(git)?.name ?: ""
_currentBranch.value = branchesManager.currentBranchRef(git)
val branchesList = branchesManager.getBranches(git)
// set selected branch as the first one always
val selectedBranch = branchesList.find { it.name == _currentBranch.value }
val selectedBranch = branchesList.find { it.name == _currentBranch.value?.name }
if (selectedBranch != null) {
branchesList.remove(selectedBranch)
branchesList.add(0, selectedBranch)