Skip to content

Commit

Permalink
feat: allow to choose theme per project
Browse files Browse the repository at this point in the history
  • Loading branch information
Azn9 committed Nov 30, 2023
1 parent 5f43b65 commit 34e8b58
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
package com.almightyalpaca.jetbrains.plugins.discord.icons.source

interface Theme {
object Default : Theme {
override val id: String
get() = "default"
override val name: String
get() = "Default"
override val description: String
get() = "The default theme"
override val applications: Map<String, Long>
get() = emptyMap()

override fun getIconSet(applicationName: String): IconSet? {
return null
}

}

val id: String
val name: String
val description: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ package com.almightyalpaca.jetbrains.plugins.discord.plugin.render

import com.almightyalpaca.jetbrains.plugins.discord.icons.source.IconSet
import com.almightyalpaca.jetbrains.plugins.discord.icons.source.Source
import com.almightyalpaca.jetbrains.plugins.discord.icons.source.Theme
import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.Data
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.options.types.SimpleValue
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.settings

class RenderContext(val source: Source, val data: Data, val mode: Renderer.Mode) {
val icons: IconSet? by lazy {
var themeValue = projectData?.projectSettings?.theme ?: settings.theme
if (themeValue.getValue() == "default") {
themeValue = settings.theme
}

source.getThemesOrNull()
?.get(settings.theme.getValue())
?.get(themeValue.getValue())
?.getIconSet(settings.applicationType.getValue().applicationName)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.almightyalpaca.jetbrains.plugins.discord.plugin.settings
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.options.OptionHolder
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.options.types.BooleanValue
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.options.types.StringValue
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.options.types.ThemeValue
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.values.ProjectShowValue
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.service
Expand All @@ -38,4 +39,6 @@ interface ProjectSettings : PersistentStateComponent<Element>, OptionHolder {
val nameOverrideText: StringValue

val description: StringValue

val theme: ThemeValue?
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import javax.swing.JPanel
import javax.swing.plaf.basic.BasicComboBoxRenderer
import kotlin.coroutines.CoroutineContext

class ThemeDialog(private val themes: ThemeMap, private val initialValue: String?) : DialogWrapper(null, true, IdeModalityType.IDE), CoroutineScope {
class ThemeDialog(private val themes: ThemeMap, private val initialValue: String?, private val showDefault: Boolean = false) : DialogWrapper(null, true, IdeModalityType.IDE), CoroutineScope {
private val parentJob: Job = SupervisorJob()

override val coroutineContext: CoroutineContext
Expand Down Expand Up @@ -78,9 +78,15 @@ class ThemeDialog(private val themes: ThemeMap, private val initialValue: String
return this
}
}
field = JComboBox(themes.values.toTypedArray()).apply box@{

var themeValues = themes.values.toTypedArray()
if (showDefault) {
themeValues += Theme.Default
}

field = JComboBox(themeValues).apply box@{
this@box.renderer = renderer
selectedItem = themes[initialValue]
selectedItem = themes[initialValue] ?: (if (showDefault) Theme.Default else themes.values.first())
}

add(field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ class ProjectSettingsImpl(override val project: Project) : ProjectSettings, Pers
override val nameOverrideText by nameOverrideToggle.option.text("", "")

override val description by text("Project description", "")

override val theme by themeChooser("Project theme", "The theme to use for this project", true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import javax.swing.JComponent
import javax.swing.JPanel
import kotlin.reflect.KProperty

fun OptionCreator<in ThemeValue>.themeChooser(text: String, description: String? = null) =
OptionProviderImpl(this, ThemeOption(text, description))
fun OptionCreator<in ThemeValue>.themeChooser(text: String, description: String? = null, showDefault: Boolean = false) =
OptionProviderImpl(this, ThemeOption(text, description, showDefault))

class ThemeOption(text: String, val description: String?) : Option<ThemeValue>(text), ThemeValue.Provider {
class ThemeOption(text: String, val description: String?, val showDefault: Boolean) : Option<ThemeValue>(text), ThemeValue.Provider {
private val source: Source = sourceService.source

private val listeners = mutableListOf<(ThemeValue) -> Unit>()
Expand All @@ -64,7 +64,7 @@ class ThemeOption(text: String, val description: String?) : Option<ThemeValue>(t
val themes = this@ThemeOption.source.getThemesOrNull()

if (themes != null) {
val dialog = ThemeDialog(themes, componentValue)
val dialog = ThemeDialog(themes, componentValue, showDefault)
val result = dialog.showAndGet()

if (result) {
Expand Down
4 changes: 0 additions & 4 deletions plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
<depends optional="true" config-file="git-extension.xml">Git4Idea</depends>

<applicationListeners>
<listener
class="com.almightyalpaca.jetbrains.plugins.discord.plugin.time.TimeProjectManagerListener"
topic="com.intellij.openapi.project.ProjectManagerListener"/>

<listener
class="com.almightyalpaca.jetbrains.plugins.discord.plugin.time.TimeFileEditorManagerListener"
topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"/>
Expand Down

0 comments on commit 34e8b58

Please sign in to comment.