Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/kotlin/com/coder/gateway/models/TokenSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.coder.gateway.models

/**
* Describes where a token came from.
*/
enum class TokenSource {
CONFIG, // Pulled from the Coder CLI config.
USER, // Input by the user.
QUERY, // From the Gateway link as a query parameter.
LAST_USED, // Last used token, either from storage or current run.
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.coder.gateway.views

import com.coder.gateway.CoderRemoteConnectionHandle
import com.coder.gateway.views.steps.CoderWizardView
import com.intellij.ui.components.panels.Wrapper
import com.intellij.util.ui.JBUI
import com.jetbrains.gateway.api.GatewayConnectorView
import javax.swing.JComponent

class CoderGatewayConnectorWizardWrapperView : GatewayConnectorView {
override val component: JComponent
get() = Wrapper(CoderGatewayConnectorWizardView()).apply { border = JBUI.Borders.empty() }
}
get() {
return Wrapper(CoderWizardView { params ->
CoderRemoteConnectionHandle().connect { params }
}).apply { border = JBUI.Borders.empty() }
}
}
48 changes: 48 additions & 0 deletions src/main/kotlin/com/coder/gateway/views/steps/CoderWizardStep.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.coder.gateway.views.steps

import com.intellij.openapi.Disposable
import com.intellij.openapi.ui.DialogPanel

sealed interface CoderWizardStep : Disposable {
val component: DialogPanel
var nextActionText: String
var previousActionText: String

/**
* Stop any background processes. Data will still be available.
*/
fun stop()
}

/**
* Run block with provided arguments after checking they are all non-null. This
* is to enforce non-null values and should be used to signify developer error.
*/
fun <A, Z> withoutNull(a: A?, block: (a: A) -> Z): Z {
if (a == null) {
throw Error("Unexpected null value")
}
return block(a)
}

/**
* Run block with provided arguments after checking they are all non-null. This
* is to enforce non-null values and should be used to signify developer error.
*/
fun <A, B, Z> withoutNull(a: A?, b: B?, block: (a: A, b: B) -> Z): Z {
if (a == null || b == null) {
throw Error("Unexpected null value")
}
return block(a, b)
}

/**
* Run block with provided arguments after checking they are all non-null. This
* is to enforce non-null values and should be used to signify developer error.
*/
fun <A, B, C, D, Z> withoutNull(a: A?, b: B?, c: C?, d: D?, block: (a: A, b: B, c: C, d: D) -> Z): Z {
if (a == null || b == null || c == null || d == null) {
throw Error("Unexpected null value")
}
return block(a, b, c, d)
}
108 changes: 108 additions & 0 deletions src/main/kotlin/com/coder/gateway/views/steps/CoderWizardView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.coder.gateway.views.steps

import com.intellij.openapi.Disposable
import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeScreenUIManager
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.RightGap
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import com.jetbrains.gateway.api.GatewayUI
import javax.swing.JButton

/**
* Wrapper around all wizard steps. This view takes you from configuring a URL
* to connecting to a workspace.
*/
class CoderWizardView(
private val onFinish: (data: Map<String, String>) -> Unit,
) : BorderLayoutPanel(), Disposable {
private lateinit var previousButton: JButton
private lateinit var nextButton: JButton

// These are not in a list because the types of one step lead into the types
// of the next and it would not be possible to do that with a generic type
// on a list. It could possibly be refactored to have steps point to their
// own next step which might be cleaner, but this works.
private val step1 = CoderWorkspacesStepView { nextButton.isEnabled = it }
private val step2 = CoderWorkspaceStepView { nextButton.isEnabled = it }
private var current: CoderWizardStep? = null

private val buttons = panel {
separator(background = WelcomeScreenUIManager.getSeparatorColor())
row {
label("").resizableColumn().align(AlignX.FILL).gap(RightGap.SMALL)
previousButton = button("") { previous() }
.align(AlignX.RIGHT).gap(RightGap.SMALL)
.applyToComponent { background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() }.component
nextButton = button("") { next() }
.align(AlignX.RIGHT)
.applyToComponent { background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() }.component
}
}.apply {
background = WelcomeScreenUIManager.getMainAssociatedComponentBackground()
border = JBUI.Borders.empty(0, 16)
}

init {
background = WelcomeScreenUIManager.getMainAssociatedComponentBackground()
addToBottom(buttons)
setStep(step1)
step1.init()
}

/**
* Replace the current step with the new one.
*/
private fun setStep(step: CoderWizardStep) {
current?.apply {
remove(component)
stop()
}
current = step
step.apply {
addToCenter(component.apply {
background = WelcomeScreenUIManager.getMainAssociatedComponentBackground()
border = JBUI.Borders.empty(0, 16)
})
nextButton.text = nextActionText
previousButton.text = previousActionText
nextButton.isEnabled = false
updateUI()
}
}

private fun previous() {
when(current) {
is CoderWorkspacesStepView -> {
GatewayUI.getInstance().reset()
dispose()
}
is CoderWorkspaceStepView -> {
setStep(step1)
step1.init()
}
null -> throw Error("Unexpected null step")
}
}

private fun next() {
when(current) {
is CoderWorkspacesStepView -> {
setStep(step2)
step2.init(step1.data())
}
is CoderWorkspaceStepView -> {
onFinish(step2.data())
GatewayUI.getInstance().reset()
dispose()
}
null -> throw Error("Unexpected null step")
}
}

override fun dispose() {
step1.dispose()
step2.dispose()
}
}
Loading