Clicking on a ref now selects the log's commit

This commit is contained in:
Abdelilah El Aissaoui 2021-12-12 19:38:11 +01:00
parent 2d32b77a69
commit 912397b257
6 changed files with 47 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import app.AppStateManager
import app.app.ErrorsManager import app.app.ErrorsManager
import app.app.newErrorNow import app.app.newErrorNow
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.transport.RemoteConfig import org.eclipse.jgit.transport.RemoteConfig
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
@ -350,6 +351,10 @@ class GitManager @Inject constructor(
fun clone(directory: File, url: String) = managerScope.launch { fun clone(directory: File, url: String) = managerScope.launch {
remoteOperationsManager.clone(directory, url) remoteOperationsManager.clone(directory, url)
} }
fun findCommit(objectId: ObjectId): RevCommit {
return safeGit.repository.parseCommit(objectId)
}
} }

View File

@ -33,7 +33,11 @@ import app.ui.context_menu.branchContextMenuItems
import app.ui.dialogs.MergeDialog import app.ui.dialogs.MergeDialog
@Composable @Composable
fun Branches(gitManager: GitManager) { fun Branches(
gitManager: GitManager,
onBranchClicked: (Ref) -> Unit,
) {
val branches by gitManager.branches.collectAsState() val branches by gitManager.branches.collectAsState()
val currentBranch by gitManager.currentBranch.collectAsState() val currentBranch by gitManager.currentBranch.collectAsState()
val (mergeBranch, setMergeBranch) = remember { mutableStateOf<Ref?>(null) } val (mergeBranch, setMergeBranch) = remember { mutableStateOf<Ref?>(null) }
@ -53,6 +57,7 @@ fun Branches(gitManager: GitManager) {
BranchRow( BranchRow(
branch = branch, branch = branch,
isCurrentBranch = currentBranch == branch.name, isCurrentBranch = currentBranch == branch.name,
onBranchClicked = { onBranchClicked(branch) },
onCheckoutBranch = { gitManager.checkoutRef(branch) }, onCheckoutBranch = { gitManager.checkoutRef(branch) },
onMergeBranch = { setMergeBranch(branch) }, onMergeBranch = { setMergeBranch(branch) },
onDeleteBranch = { gitManager.deleteBranch(branch) }, onDeleteBranch = { gitManager.deleteBranch(branch) },
@ -77,6 +82,7 @@ fun Branches(gitManager: GitManager) {
private fun BranchRow( private fun BranchRow(
branch: Ref, branch: Ref,
isCurrentBranch: Boolean, isCurrentBranch: Boolean,
onBranchClicked: () -> Unit,
onCheckoutBranch: () -> Unit, onCheckoutBranch: () -> Unit,
onMergeBranch: () -> Unit, onMergeBranch: () -> Unit,
onDeleteBranch: () -> Unit, onDeleteBranch: () -> Unit,
@ -96,6 +102,7 @@ private fun BranchRow(
text = branch.simpleName, text = branch.simpleName,
iconResourcePath = "branch.svg", iconResourcePath = "branch.svg",
bold = isCurrentBranch, bold = isCurrentBranch,
onClick = onBranchClicked
) { ) {
if (isCurrentBranch) { if (isCurrentBranch) {
Icon( Icon(

View File

@ -71,9 +71,21 @@ fun RepositoryOpenPage(gitManager: GitManager) {
.weight(0.15f) .weight(0.15f)
.fillMaxHeight() .fillMaxHeight()
) { ) {
Branches(gitManager = gitManager) Branches(
gitManager = gitManager,
onBranchClicked = {
val commit = gitManager.findCommit(it.objectId)
setSelectedItem(SelectedItem.Ref(commit))
}
)
Remotes(gitManager = gitManager) Remotes(gitManager = gitManager)
Tags(gitManager = gitManager) Tags(
gitManager = gitManager,
onTagClicked = {
val commit = gitManager.findCommit(it.objectId)
setSelectedItem(SelectedItem.Ref(commit))
}
)
Stashes( Stashes(
gitManager = gitManager, gitManager = gitManager,
onStashSelected = { stash -> onStashSelected = { stash ->
@ -154,6 +166,7 @@ sealed class SelectedItem {
object None : SelectedItem() object None : SelectedItem()
object UncommitedChanges : SelectedItem() object UncommitedChanges : SelectedItem()
sealed class CommitBasedItem(val revCommit: RevCommit) : SelectedItem() sealed class CommitBasedItem(val revCommit: RevCommit) : SelectedItem()
class Ref(revCommit: RevCommit) : CommitBasedItem(revCommit)
class Commit(revCommit: RevCommit) : CommitBasedItem(revCommit) class Commit(revCommit: RevCommit) : CommitBasedItem(revCommit)
class Stash(revCommit: RevCommit) : CommitBasedItem(revCommit) class Stash(revCommit: RevCommit) : CommitBasedItem(revCommit)
} }

View File

@ -19,7 +19,10 @@ import app.ui.context_menu.tagContextMenuItems
import org.eclipse.jgit.lib.Ref import org.eclipse.jgit.lib.Ref
@Composable @Composable
fun Tags(gitManager: GitManager) { fun Tags(
gitManager: GitManager,
onTagClicked: (Ref) -> Unit,
) {
val tagsState = gitManager.tags.collectAsState() val tagsState = gitManager.tags.collectAsState()
val tags = tagsState.value val tags = tagsState.value
@ -39,6 +42,7 @@ fun Tags(gitManager: GitManager) {
items(items = tags) { tag -> items(items = tags) { tag ->
TagRow( TagRow(
tag = tag, tag = tag,
onTagClicked = { onTagClicked(tag) },
onCheckoutTag = { gitManager.checkoutRef(tag) }, onCheckoutTag = { gitManager.checkoutRef(tag) },
onDeleteTag = { gitManager.deleteTag(tag) } onDeleteTag = { gitManager.deleteTag(tag) }
) )
@ -53,6 +57,7 @@ fun Tags(gitManager: GitManager) {
@Composable @Composable
private fun TagRow( private fun TagRow(
tag: Ref, tag: Ref,
onTagClicked: () -> Unit,
onCheckoutTag: () -> Unit, onCheckoutTag: () -> Unit,
onDeleteTag: () -> Unit, onDeleteTag: () -> Unit,
) { ) {
@ -67,6 +72,7 @@ private fun TagRow(
SideMenuSubentry( SideMenuSubentry(
text = tag.simpleName, text = tag.simpleName,
iconResourcePath = "tag.svg", iconResourcePath = "tag.svg",
onClick = onTagClicked,
) )
} }
} }

View File

@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollbarAdapter import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -14,10 +15,9 @@ import androidx.compose.ui.Modifier
@Composable @Composable
fun ScrollableLazyColumn( fun ScrollableLazyColumn(
modifier: Modifier, modifier: Modifier,
state: LazyListState = rememberLazyListState(),
content: LazyListScope.() -> Unit content: LazyListScope.() -> Unit
) { ) {
val state = rememberLazyListState()
Box( Box(
modifier = modifier, modifier = modifier,
) { ) {

View File

@ -10,6 +10,7 @@ import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon import androidx.compose.material.Icon
@ -77,14 +78,21 @@ fun Log(
val showLogDialog = remember { mutableStateOf<LogDialog>(LogDialog.None) } val showLogDialog = remember { mutableStateOf<LogDialog>(LogDialog.None) }
val selectedCommit = if (selectedItem is SelectedItem.Commit) { val selectedCommit = if (selectedItem is SelectedItem.CommitBasedItem) {
selectedItem.revCommit selectedItem.revCommit
} else { } else {
null null
} }
if (logStatus is LogStatus.Loaded) { if (logStatus is LogStatus.Loaded) {
val commitList = logStatus.plotCommitList val commitList = logStatus.plotCommitList
val scrollState = rememberLazyListState()
LaunchedEffect(selectedCommit) {
if(selectedItem is SelectedItem.Ref)
scrollState.scrollToItem(commitList.indexOfFirst { it.name == selectedCommit?.name })
}
LogDialogs( LogDialogs(
gitManager, gitManager,
@ -111,6 +119,7 @@ fun Log(
weightMod = weightMod, weightMod = weightMod,
) )
ScrollableLazyColumn( ScrollableLazyColumn(
state = scrollState,
modifier = Modifier modifier = Modifier
.background(MaterialTheme.colors.background) .background(MaterialTheme.colors.background)
.fillMaxSize(), .fillMaxSize(),