Skip to content

Impl: basic icon downloader #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
### Added
- warning system when plugin might not be compatible with Coder REST API
- a `Create workspace` button which links to Coder's templates page
- workspace icons

### Changed
- redesigned the information&warning banner. Messages can now include hyperlinks

### Fixed
- outdated Coder CLI binaries are cleaned up
- workspace status color style: running workspaces are green, failed ones should be red, everything else is gray
- typos in plugin description

## 2.1.2 - 2022-11-23

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Follow](https://img.shields.io/twitter/follow/CoderHQ?label=%40CoderHQ&style=soc
[![Coder Gateway Plugin Build](https://github.com/coder/coder-jetbrains/actions/workflows/build.yml/badge.svg)](https://github.com/coder/coder-jetbrains/actions/workflows/build.yml)

<!-- Plugin description -->
**Coder Gateway** connects your Jetbrains IDE to your [Coder Workspaces](https://coder.com/docs/coder-oss/latest/workspaces) so that you can develop from anywhere.
**Coder Gateway** connects your JetBrains IDE to your [Coder Workspaces](https://coder.com/docs/coder-oss/latest/workspaces) so that you can develop from anywhere.

**Manage less**

Expand Down
46 changes: 38 additions & 8 deletions src/main/kotlin/com/coder/gateway/icons/CoderIcons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,53 @@ import com.intellij.openapi.util.IconLoader
object CoderIcons {
val LOGO = IconLoader.getIcon("coder_logo.svg", javaClass)
val LOGO_16 = IconLoader.getIcon("coder_logo_16.svg", javaClass)
val LOGO_52 = IconLoader.getIcon("coder_logo_52.svg", javaClass)

val OPEN_TERMINAL = IconLoader.getIcon("open_terminal.svg", javaClass)

val CREATE = IconLoader.getIcon("create.svg", javaClass)
val RUN = IconLoader.getIcon("run.svg", javaClass)
val STOP = IconLoader.getIcon("stop.svg", javaClass)
val UPDATE = IconLoader.getIcon("update.svg", javaClass)
val DELETE = IconLoader.getIcon("delete.svg", javaClass)

val WINDOWS = IconLoader.getIcon("windows.svg", javaClass)
val MACOS = IconLoader.getIcon("macOS.svg", javaClass)
val LINUX = IconLoader.getIcon("linux.svg", javaClass)
val UNKNOWN = IconLoader.getIcon("unknown.svg", javaClass)

val GREEN_CIRCLE = IconLoader.getIcon("green_circle.svg", javaClass)
val GRAY_CIRCLE = IconLoader.getIcon("gray_circle.svg", javaClass)
val RED_CIRCLE = IconLoader.getIcon("red_circle.svg", javaClass)
val ZERO = IconLoader.getIcon("0.svg", javaClass)
val ONE = IconLoader.getIcon("1.svg", javaClass)
val TWO = IconLoader.getIcon("2.svg", javaClass)
val THREE = IconLoader.getIcon("3.svg", javaClass)
val FOUR = IconLoader.getIcon("4.svg", javaClass)
val FIVE = IconLoader.getIcon("5.svg", javaClass)
val SIX = IconLoader.getIcon("6.svg", javaClass)
val SEVEN = IconLoader.getIcon("7.svg", javaClass)
val EIGHT = IconLoader.getIcon("8.svg", javaClass)
val NINE = IconLoader.getIcon("9.svg", javaClass)

val A = IconLoader.getIcon("a.svg", javaClass)
val B = IconLoader.getIcon("b.svg", javaClass)
val C = IconLoader.getIcon("c.svg", javaClass)
val D = IconLoader.getIcon("d.svg", javaClass)
val E = IconLoader.getIcon("e.svg", javaClass)
val F = IconLoader.getIcon("f.svg", javaClass)
val G = IconLoader.getIcon("g.svg", javaClass)
val H = IconLoader.getIcon("h.svg", javaClass)
val I = IconLoader.getIcon("i.svg", javaClass)
val J = IconLoader.getIcon("j.svg", javaClass)
val K = IconLoader.getIcon("k.svg", javaClass)
val L = IconLoader.getIcon("l.svg", javaClass)
val M = IconLoader.getIcon("m.svg", javaClass)
val N = IconLoader.getIcon("n.svg", javaClass)
val O = IconLoader.getIcon("o.svg", javaClass)
val P = IconLoader.getIcon("p.svg", javaClass)
val Q = IconLoader.getIcon("q.svg", javaClass)
val R = IconLoader.getIcon("r.svg", javaClass)
val S = IconLoader.getIcon("s.svg", javaClass)
val T = IconLoader.getIcon("t.svg", javaClass)
val U = IconLoader.getIcon("u.svg", javaClass)
val V = IconLoader.getIcon("v.svg", javaClass)
val W = IconLoader.getIcon("w.svg", javaClass)
val X = IconLoader.getIcon("x.svg", javaClass)
val Y = IconLoader.getIcon("y.svg", javaClass)
val Z = IconLoader.getIcon("z.svg", javaClass)

val DELETE = IconLoader.getIcon("delete.svg", javaClass)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import com.coder.gateway.sdk.Arch
import com.coder.gateway.sdk.OS
import com.coder.gateway.sdk.v2.models.WorkspaceTransition
import java.util.UUID
import javax.swing.Icon

data class WorkspaceAgentModel(
val workspaceID: UUID,
val workspaceName: String,
val name: String,
val templateID: UUID,
val templateName: String,
val templateIcon: Icon,
val status: WorkspaceVersionStatus,
val agentStatus: WorkspaceAgentStatus,
val lastBuildTransition: WorkspaceTransition,
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/com/coder/gateway/sdk/CoderRestClientService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import java.util.UUID

@Service(Service.Level.APP)
class CoderRestClientService {
private lateinit var httpClient: OkHttpClient
private lateinit var retroRestClient: CoderV2RestFacade
private lateinit var sessionToken: String
lateinit var coderURL: URL
Expand All @@ -43,15 +44,14 @@ class CoderRestClientService {
.registerTypeAdapter(Instant::class.java, InstantConverter())
.setPrettyPrinting()
.create()
httpClient = OkHttpClient.Builder()
.addInterceptor { it.proceed(it.request().newBuilder().addHeader("Coder-Session-Token", token).build()) }
.addInterceptor(HttpLoggingInterceptor().apply { setLevel(HttpLoggingInterceptor.Level.BASIC) })
.build()

retroRestClient = Retrofit.Builder()
.baseUrl(url.toString())
.client(
OkHttpClient.Builder()
.addInterceptor { it.proceed(it.request().newBuilder().addHeader("Coder-Session-Token", token).build()) }
.addInterceptor(HttpLoggingInterceptor().apply { setLevel(HttpLoggingInterceptor.Level.BASIC) })
.build()
)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(CoderV2RestFacade::class.java)
Expand Down
80 changes: 80 additions & 0 deletions src/main/kotlin/com/coder/gateway/sdk/TemplateIconDownloader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.coder.gateway.sdk

import com.coder.gateway.icons.CoderIcons
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.ui.scale.ScaleContext
import com.intellij.util.ImageLoader
import com.intellij.util.ui.ImageUtil
import java.net.URL
import javax.swing.Icon
import javax.swing.ImageIcon

@Service(Service.Level.APP)
class TemplateIconDownloader {
private val coderClient: CoderRestClientService = service()

fun load(path: String, templateName: String): Icon {
var url: URL? = null
if (path.startsWith("http")) {
url = path.toURL()
} else if (path.contains(coderClient.coderURL.host)) {
url = coderClient.coderURL.withPath(path)
}

if (url != null) {
var img = ImageLoader.loadFromUrl(url)
if (img != null) {
if (ImageUtil.getRealHeight(img) > 32 && ImageUtil.getRealWidth(img) > 32) {
img = ImageUtil.resize(img, 32, ScaleContext.create())
}
return ImageIcon(img)
}
}

return iconForChar(templateName.lowercase().first())
}

private fun iconForChar(c: Char) = when (c) {
'0' -> CoderIcons.ZERO
'1' -> CoderIcons.ONE
'2' -> CoderIcons.TWO
'3' -> CoderIcons.THREE
'4' -> CoderIcons.FOUR
'5' -> CoderIcons.FIVE
'6' -> CoderIcons.SIX
'7' -> CoderIcons.SEVEN
'8' -> CoderIcons.EIGHT
'9' -> CoderIcons.NINE

'a' -> CoderIcons.A
'b' -> CoderIcons.B
'c' -> CoderIcons.C
'd' -> CoderIcons.D
'e' -> CoderIcons.E
'f' -> CoderIcons.F
'g' -> CoderIcons.G
'h' -> CoderIcons.H
'i' -> CoderIcons.I
'j' -> CoderIcons.J
'k' -> CoderIcons.K
'l' -> CoderIcons.L
'm' -> CoderIcons.M
'n' -> CoderIcons.N
'o' -> CoderIcons.O
'p' -> CoderIcons.P
'q' -> CoderIcons.Q
'r' -> CoderIcons.R
's' -> CoderIcons.S
't' -> CoderIcons.T
'u' -> CoderIcons.U
'v' -> CoderIcons.V
'w' -> CoderIcons.W
'x' -> CoderIcons.X
'y' -> CoderIcons.Y
'z' -> CoderIcons.Z

else -> CoderIcons.UNKNOWN
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.coder.gateway.sdk.CoderCLIManager
import com.coder.gateway.sdk.CoderRestClientService
import com.coder.gateway.sdk.CoderSemVer
import com.coder.gateway.sdk.OS
import com.coder.gateway.sdk.TemplateIconDownloader
import com.coder.gateway.sdk.ex.AuthenticationResponseException
import com.coder.gateway.sdk.ex.TemplateResponseException
import com.coder.gateway.sdk.ex.WorkspaceResponseException
Expand Down Expand Up @@ -83,6 +84,8 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
private val cs = CoroutineScope(Dispatchers.Main)
private var localWizardModel = CoderWorkspacesWizardModel()
private val coderClient: CoderRestClientService = service()
private val iconDownloader: TemplateIconDownloader = service()

private val appPropertiesService: PropertiesComponent = service()

private var tfUrl: JTextField? = null
Expand Down Expand Up @@ -448,6 +451,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
this.name,
this.templateID,
this.templateName,
iconDownloader.load(this@agentModels.templateIcon, this.name),
WorkspaceVersionStatus.from(this),
WorkspaceAgentStatus.from(this),
this.latestBuild.transition,
Expand All @@ -466,6 +470,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
workspaceWithAgentName,
this.templateID,
this.templateName,
iconDownloader.load(this@agentModels.templateIcon, workspaceWithAgentName),
WorkspaceVersionStatus.from(this),
WorkspaceAgentStatus.from(this),
this.latestBuild.transition,
Expand All @@ -484,6 +489,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
this.name,
this.templateID,
this.templateName,
iconDownloader.load(this@agentModels.templateIcon, this.name),
WorkspaceVersionStatus.from(this),
WorkspaceAgentStatus.from(this),
this.latestBuild.transition,
Expand Down Expand Up @@ -550,15 +556,16 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
}

override fun getIcon(value: String, table: JTable?, row: Int): Icon {
return when (OS.from(value)) {
OS.LINUX -> CoderIcons.LINUX
OS.WINDOWS -> CoderIcons.WINDOWS
OS.MAC -> CoderIcons.MACOS
else -> CoderIcons.UNKNOWN
}
return item?.templateIcon ?: CoderIcons.UNKNOWN
}

override fun isCenterAlignment() = true

override fun getTableCellRendererComponent(table: JTable?, value: Any?, selected: Boolean, focus: Boolean, row: Int, column: Int): Component {
return super.getTableCellRendererComponent(table, value, selected, focus, row, column).apply {
border = JBUI.Borders.empty(10, 10)
}
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/9.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="com.coder.gateway.sdk.CoderRestClientService"></applicationService>
<applicationService serviceImplementation="com.coder.gateway.sdk.TemplateIconDownloader"></applicationService>
<applicationService serviceImplementation="com.coder.gateway.services.CoderRecentWorkspaceConnectionsService"></applicationService>
</extensions>
<extensions defaultExtensionNs="com.jetbrains">
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/a.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/b.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/c.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 0 additions & 15 deletions src/main/resources/coder_logo_52.svg

This file was deleted.

Loading