Add LocalKeyEvents

This commit is contained in:
dizyaa 2023-01-27 23:50:10 +04:00
parent 7469404670
commit c1d122b3b7
2 changed files with 36 additions and 18 deletions

View File

@ -14,6 +14,8 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Density
@ -47,6 +49,19 @@ import javax.inject.Inject
private const val TAG = "App" private const val TAG = "App"
val LocalTabScope = compositionLocalOf { emptyTabInformation() } val LocalTabScope = compositionLocalOf { emptyTabInformation() }
val LocalWindowKeyEventKeysScope = compositionLocalOf { WindowEventKeys.Empty }
data class WindowEventKeys(
val isCtrlPressed: Boolean,
val isShiftPressed: Boolean,
) {
companion object {
val Empty = WindowEventKeys(
isCtrlPressed = false,
isShiftPressed = false,
)
}
}
class App { class App {
private val appComponent = DaggerAppComponent.create() private val appComponent = DaggerAppComponent.create()
@ -97,6 +112,9 @@ class App {
// Save window state for next time the Window is started // Save window state for next time the Window is started
appSettings.windowPlacement = windowState.placement.preferenceValue appSettings.windowPlacement = windowState.placement.preferenceValue
var eventKeys by remember {
mutableStateOf(WindowEventKeys.Empty)
}
if (isOpen) { if (isOpen) {
Window( Window(
@ -106,13 +124,20 @@ class App {
}, },
state = windowState, state = windowState,
icon = painterResource("logo.svg"), icon = painterResource("logo.svg"),
onKeyEvent = { event ->
eventKeys = WindowEventKeys(
isShiftPressed = event.isShiftPressed,
isCtrlPressed = event.isCtrlPressed,
)
false
}
) { ) {
val density = if (scale != -1f) { val density = if (scale != -1f) {
arrayOf(LocalDensity provides Density(scale, 1f)) arrayOf(LocalDensity provides Density(scale, 1f))
} else } else
emptyArray() emptyArray()
CompositionLocalProvider(values = density) { CompositionLocalProvider(LocalWindowKeyEventKeysScope provides eventKeys, *density) {
AppTheme( AppTheme(
selectedTheme = theme, selectedTheme = theme,
customTheme = customTheme, customTheme = customTheme,

View File

@ -26,9 +26,7 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.clipRect import androidx.compose.ui.graphics.drawscope.clipRect
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.key.KeyEventType import androidx.compose.ui.input.key.*
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.pointer.PointerIcon import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerHoverIcon import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
@ -37,6 +35,7 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.jetpackduba.gitnuro.LocalWindowKeyEventKeysScope
import com.jetpackduba.gitnuro.extensions.* import com.jetpackduba.gitnuro.extensions.*
import com.jetpackduba.gitnuro.git.graph.GraphCommitList import com.jetpackduba.gitnuro.git.graph.GraphCommitList
import com.jetpackduba.gitnuro.git.graph.GraphNode import com.jetpackduba.gitnuro.git.graph.GraphNode
@ -48,6 +47,7 @@ import com.jetpackduba.gitnuro.ui.SelectedItem
import com.jetpackduba.gitnuro.ui.components.AvatarImage import com.jetpackduba.gitnuro.ui.components.AvatarImage
import com.jetpackduba.gitnuro.ui.components.ScrollableLazyColumn import com.jetpackduba.gitnuro.ui.components.ScrollableLazyColumn
import com.jetpackduba.gitnuro.ui.components.gitnuroViewModel import com.jetpackduba.gitnuro.ui.components.gitnuroViewModel
import com.jetpackduba.gitnuro.ui.containCommit
import com.jetpackduba.gitnuro.ui.context_menu.* import com.jetpackduba.gitnuro.ui.context_menu.*
import com.jetpackduba.gitnuro.ui.dialogs.NewBranchDialog import com.jetpackduba.gitnuro.ui.dialogs.NewBranchDialog
import com.jetpackduba.gitnuro.ui.dialogs.NewTagDialog import com.jetpackduba.gitnuro.ui.dialogs.NewTagDialog
@ -99,12 +99,6 @@ fun Log(
val logStatus = logStatusState.value val logStatus = logStatusState.value
val showLogDialog by logViewModel.logDialog.collectAsState() val showLogDialog by logViewModel.logDialog.collectAsState()
val selectedCommit = if (selectedItem is SelectedItem.CommitBasedItem) {
selectedItem.revCommit
} else {
null
}
if (logStatus is LogStatus.Loaded) { if (logStatus is LogStatus.Loaded) {
val hasUncommitedChanges = logStatus.hasUncommitedChanges val hasUncommitedChanges = logStatus.hasUncommitedChanges
val commitList = logStatus.plotCommitList val commitList = logStatus.plotCommitList
@ -160,7 +154,6 @@ fun Log(
Box { Box {
GraphList( GraphList(
commitList = commitList, commitList = commitList,
selectedCommit = selectedCommit,
selectedItem = selectedItem, selectedItem = selectedItem,
repositoryState = repositoryState, repositoryState = repositoryState,
horizontalScrollState = horizontalScrollState, horizontalScrollState = horizontalScrollState,
@ -176,7 +169,6 @@ fun Log(
scrollState = verticalScrollState, scrollState = verticalScrollState,
hasUncommitedChanges = hasUncommitedChanges, hasUncommitedChanges = hasUncommitedChanges,
searchFilter = if (searchFilterValue is LogSearch.SearchResults) searchFilterValue.commits else null, searchFilter = if (searchFilterValue is LogSearch.SearchResults) searchFilterValue.commits else null,
selectedCommit = selectedCommit,
logStatus = logStatus, logStatus = logStatus,
repositoryState = repositoryState, repositoryState = repositoryState,
selectedItem = selectedItem, selectedItem = selectedItem,
@ -384,7 +376,6 @@ fun MessagesList(
scrollState: LazyListState, scrollState: LazyListState,
hasUncommitedChanges: Boolean, hasUncommitedChanges: Boolean,
searchFilter: List<GraphNode>?, searchFilter: List<GraphNode>?,
selectedCommit: RevCommit?,
logStatus: LogStatus.Loaded, logStatus: LogStatus.Loaded,
repositoryState: RepositoryState, repositoryState: RepositoryState,
selectedItem: SelectedItem, selectedItem: SelectedItem,
@ -396,6 +387,7 @@ fun MessagesList(
onShowLogDialog: (LogDialog) -> Unit, onShowLogDialog: (LogDialog) -> Unit,
graphWidth: Dp, graphWidth: Dp,
) { ) {
val keyScope = LocalWindowKeyEventKeysScope.current
ScrollableLazyColumn( ScrollableLazyColumn(
state = scrollState, state = scrollState,
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
@ -425,7 +417,7 @@ fun MessagesList(
graphWidth = graphWidth, graphWidth = graphWidth,
logViewModel = logViewModel, logViewModel = logViewModel,
graphNode = graphNode, graphNode = graphNode,
isSelected = selectedCommit?.name == graphNode.name, isSelected = selectedItem.containCommit(graphNode),
currentBranch = logStatus.currentBranch, currentBranch = logStatus.currentBranch,
matchesSearchFilter = searchFilter?.contains(graphNode), matchesSearchFilter = searchFilter?.contains(graphNode),
showCreateNewBranch = { onShowLogDialog(LogDialog.NewBranch(graphNode)) }, showCreateNewBranch = { onShowLogDialog(LogDialog.NewBranch(graphNode)) },
@ -434,7 +426,9 @@ fun MessagesList(
onMergeBranch = onMerge, onMergeBranch = onMerge,
onRebaseBranch = onRebase, onRebaseBranch = onRebase,
onRebaseInteractive = { logViewModel.rebaseInteractive(graphNode) }, onRebaseInteractive = { logViewModel.rebaseInteractive(graphNode) },
onRevCommitSelected = { logViewModel.selectLogLine(graphNode) }, onRevCommitSelected = {
logViewModel.selectLogLine(graphNode, keyScope.isCtrlPressed, keyScope.isShiftPressed)
},
) )
} }
@ -470,7 +464,6 @@ fun GraphList(
verticalScrollState: LazyListState, verticalScrollState: LazyListState,
graphWidth: Dp, graphWidth: Dp,
hasUncommitedChanges: Boolean, hasUncommitedChanges: Boolean,
selectedCommit: RevCommit?,
selectedItem: SelectedItem, selectedItem: SelectedItem,
commitsLimit: Int, commitsLimit: Int,
repositoryState: RepositoryState, repositoryState: RepositoryState,
@ -535,7 +528,7 @@ fun GraphList(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
plotCommit = graphNode, plotCommit = graphNode,
nodeColor = nodeColor, nodeColor = nodeColor,
isSelected = selectedCommit?.name == graphNode.name, isSelected = selectedItem.containCommit(graphNode),
) )
} }
} }
@ -787,7 +780,7 @@ fun CommitLine(
) { ) {
Box( Box(
modifier = Modifier modifier = Modifier
.fastClickable(graphNode, logViewModel) { onRevCommitSelected() } .clickable { onRevCommitSelected() }
.padding(start = graphWidth) .padding(start = graphWidth)
.height(LINE_HEIGHT.dp) .height(LINE_HEIGHT.dp)
.backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected) .backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected)