fun loadCustomIcon()

in idea-plugin/src/main/java/com/jetbrains/ide/streamdeck/customization/CustomActionsSchema.kt [65:96]


fun loadCustomIcon(path: String): Icon {
  val independentPath = FileUtil.toSystemIndependentName(path)

  val lastDotIndex = independentPath.lastIndexOf('.')
  val rawUrl: String
  val ext: String
  if (lastDotIndex == -1) {
    rawUrl = independentPath
    ext = "svg"
  }
  else {
    rawUrl = independentPath.substring(0, lastDotIndex)
    ext = independentPath.substring(lastDotIndex + 1)
  }

  val possibleSuffixes = listOf("@2x_dark", "_dark@2x", "_dark", "@2x")
  val adjustedUrl = possibleSuffixes.firstOrNull { rawUrl.endsWith(it) }?.let { rawUrl.removeSuffix(it) } ?: rawUrl
  try {
    return doLoadCustomIcon("$adjustedUrl.$ext")
  }
  catch (t: Throwable) {
    // In Light theme we do not fall back on dark icon, so if the original provided path ends with '_dark'
    // and there is no icon file without '_dark' suffix, we will fail.
    // And in this case, we just need to load the file chosen by the user.
    if (rawUrl == adjustedUrl) {
      throw t
    }
    else {
      return doLoadCustomIcon("$rawUrl.$ext")
    }
  }
}