Skip to content

Commit

Permalink
add key down Ctrl-Enter to fire the current request
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Dec 17, 2023
1 parent 3f610bf commit 5143c48
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -58,7 +57,6 @@ import com.sunnychung.application.multiplatform.hellohttp.document.RequestsDI
import com.sunnychung.application.multiplatform.hellohttp.document.ResponsesDI
import com.sunnychung.application.multiplatform.hellohttp.extension.toCurlCommand
import com.sunnychung.application.multiplatform.hellohttp.extension.toGrpcurlCommand
import com.sunnychung.application.multiplatform.hellohttp.network.ConnectionStatus
import com.sunnychung.application.multiplatform.hellohttp.model.ColourTheme
import com.sunnychung.application.multiplatform.hellohttp.model.Environment
import com.sunnychung.application.multiplatform.hellohttp.model.MoveDirection
Expand All @@ -69,6 +67,7 @@ import com.sunnychung.application.multiplatform.hellohttp.model.TreeFolder
import com.sunnychung.application.multiplatform.hellohttp.model.TreeRequest
import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestTemplate
import com.sunnychung.application.multiplatform.hellohttp.model.UserResponse
import com.sunnychung.application.multiplatform.hellohttp.network.ConnectionStatus
import com.sunnychung.application.multiplatform.hellohttp.util.let
import com.sunnychung.application.multiplatform.hellohttp.util.log
import com.sunnychung.application.multiplatform.hellohttp.util.replaceIf
Expand Down Expand Up @@ -496,6 +495,16 @@ fun AppContentView() {
first(minSize = 200.dp) {
val requestEditorModifier = Modifier.fillMaxWidth()
request?.let { requestNonNull ->
fun onClickSendOrConnect() {
networkClientManager.fireRequest(
request = requestNonNull,
requestExampleId = selectedRequestExampleId!!,
environment = selectedEnvironment,
projectId = selectedProject!!.id,
subprojectId = selectedSubproject!!.id
)
}

RequestEditorView(
modifier = requestEditorModifier,
request = requestNonNull,
Expand All @@ -510,13 +519,7 @@ fun AppContentView() {
updateResponseView()
},
onClickSend = {
networkClientManager.fireRequest(
request = requestNonNull,
requestExampleId = selectedRequestExampleId!!,
environment = selectedEnvironment,
projectId = selectedProject!!.id,
subprojectId = selectedSubproject!!.id
)
onClickSendOrConnect()
},
onClickCancel = {
networkClientManager.cancel(selectedRequestExampleId!!)
Expand Down Expand Up @@ -560,13 +563,7 @@ fun AppContentView() {
},
connectionStatus = selectedRequestExampleId?.let { networkClientManager.getStatusByRequestExampleId(it) } ?: ConnectionStatus.DISCONNECTED ,
onClickConnect = {
networkClientManager.fireRequest(
request = requestNonNull,
requestExampleId = selectedRequestExampleId!!,
environment = selectedEnvironment,
projectId = selectedProject!!.id,
subprojectId = selectedSubproject!!.id
)
onClickSendOrConnect()
},
onClickDisconnect = {
networkClientManager.cancel(selectedRequestExampleId!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isAltPressed
import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.isMetaPressed
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
Expand All @@ -62,6 +68,8 @@ import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestExamp
import com.sunnychung.application.multiplatform.hellohttp.model.UserRequestTemplate
import com.sunnychung.application.multiplatform.hellohttp.network.ConnectionStatus
import com.sunnychung.application.multiplatform.hellohttp.network.hostFromUrl
import com.sunnychung.application.multiplatform.hellohttp.platform.MacOS
import com.sunnychung.application.multiplatform.hellohttp.platform.currentOS
import com.sunnychung.application.multiplatform.hellohttp.util.copyWithChange
import com.sunnychung.application.multiplatform.hellohttp.util.copyWithIndexedChange
import com.sunnychung.application.multiplatform.hellohttp.util.copyWithRemovedIndex
Expand Down Expand Up @@ -131,9 +139,29 @@ fun RequestEditorView(
)
var selectedPayloadExampleId by rememberLast(request.id) { mutableStateOf(request.payloadExamples?.firstOrNull()?.id) }

val isEnableSendButton = when (connectionStatus.isConnectionActive()) {
true -> true
false -> when (request.application) {
ProtocolApplication.Grpc -> currentGrpcMethod != null
else -> true
}
}

log.d { "RequestEditorView recompose $request" }

Column(modifier = modifier) {
Column(modifier = modifier
.onKeyEvent { e ->
if (isEnableSendButton && e.type == KeyEventType.KeyDown && e.key == Key.Enter && !e.isAltPressed && !e.isShiftPressed) {
val currentOS = currentOS()
if ( (currentOS != MacOS && e.isCtrlPressed && !e.isMetaPressed) ||
(currentOS == MacOS && !e.isCtrlPressed && e.isMetaPressed) ) {
onClickSend()
return@onKeyEvent true
}
}
false
}
) {
Row(
modifier = Modifier
.background(color = colors.backgroundInputField)
Expand Down Expand Up @@ -230,13 +258,6 @@ fun RequestEditorView(
ProtocolApplication.Grpc -> currentGrpcMethod?.isClientStreaming != true
else -> true
}
val isEnableButton = when (connectionStatus.isConnectionActive()) {
true -> true
false -> when (request.application) {
ProtocolApplication.Grpc -> currentGrpcMethod != null
else -> true
}
}
val dropdownItems: List<String> = when (request.application) {
ProtocolApplication.WebSocket -> emptyList()
ProtocolApplication.Graphql -> if (isOneOffRequest) listOf("Copy as cURL command") else emptyList()
Expand All @@ -254,7 +275,7 @@ fun RequestEditorView(
) {
Box(modifier = Modifier.fillMaxHeight().weight(1f)
.run {
if (isEnableButton) {
if (isEnableSendButton) {
clickable {
if (!connectionStatus.isConnectionActive()) {
onClickSend()
Expand All @@ -270,7 +291,7 @@ fun RequestEditorView(
) {
AppText(
text = label,
color = if (isEnableButton) colors.primary else colors.disabled,
color = if (isEnableSendButton) colors.primary else colors.disabled,
fontSize = fonts.buttonFontSize,
textAlign = TextAlign.Center,
modifier = Modifier.align(Alignment.Center)
Expand Down

0 comments on commit 5143c48

Please sign in to comment.