Skip to content

Commit

Permalink
Allow mirrord to be enabled or disabled by default (#276)
Browse files Browse the repository at this point in the history
* Allow mirrord to be enabled or disabled by default

* Remove unused imports

* Switch from dropdown menu item to settings panel item

* Change settings text

Co-authored-by: Michał Smolarek <[email protected]>

---------

Co-authored-by: Michał Smolarek <[email protected]>
  • Loading branch information
gememma and Razz4780 authored Aug 20, 2024
1 parent bc43e0a commit ca730f9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/268.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the option for mirrord to be enabled by default on startup in settings
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.metalbear.mirrord

import com.intellij.ide.ActivityTracker
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.ToggleAction
import com.intellij.openapi.components.service
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.metalbear.mirrord.MirrordSettingsState.Companion.instance
import icons.MirrordIcons

class MirrordEnabler : ToggleAction(), DumbAware {
class MirrordEnabler : ToggleAction(), DumbAware, StartupActivity, StartupActivity.DumbAware {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT

override fun isSelected(e: AnActionEvent): Boolean {
Expand All @@ -23,7 +27,16 @@ class MirrordEnabler : ToggleAction(), DumbAware {
override fun update(e: AnActionEvent) {
e.presentation.isVisible = true
e.presentation.isEnabled = e.project != null
val state = e.project?.service<MirrordProjectService>()?.enabled ?: false
e.presentation.icon = if (state) MirrordIcons.enabled else MirrordIcons.disabled

super.update(e)
}

override fun runActivity(project: Project) {
if (instance.mirrordState.enabledByDefault) {
project.service<MirrordProjectService>().enabled = true
ActivityTracker.getInstance().inc()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MirrordSettingsComponent {
.associateWith { JBCheckBox(it.presentableName) }

private val usageBannerEnabled = JBCheckBox("Show usage banner on startup")
private val enabledOnStartup = JBCheckBox("Enable mirrord on startup")

private val mirrordVersionLabel = JBLabel("mirrord binary version:")
private val mirrordVersion = with(JBTextField("", 10)) {
Expand All @@ -41,6 +42,7 @@ class MirrordSettingsComponent {
val panel: JPanel = FormBuilder
.createFormBuilder()
.addComponent(usageBannerEnabled)
.addComponent(enabledOnStartup)
.addComponent(versionCheckEnabled)
.addSeparator()
.addComponent(autoUpdatePanel)
Expand All @@ -64,6 +66,12 @@ class MirrordSettingsComponent {
usageBannerEnabled.isSelected = value
}

var enabledOnStartupStatus: Boolean
get() = enabledOnStartup.isSelected
set(value) {
enabledOnStartup.isSelected = value
}

var versionCheckEnabledStatus: Boolean
get() = versionCheckEnabled.isSelected
set(newStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class MirrordSettingsConfigurable : Configurable {
(notificationsDisabledStatus != settings.disabledNotifications) ||
(usageBannerEnabledStatus != settings.showUsageBanner) ||
(autoUpdateEnabledStatus != settings.autoUpdate) ||
(mirrordVersionStatus != settings.mirrordVersion)
(mirrordVersionStatus != settings.mirrordVersion) ||
(enabledOnStartupStatus != settings.enabledByDefault)
}
}

Expand All @@ -31,6 +32,7 @@ class MirrordSettingsConfigurable : Configurable {
settings.showUsageBanner = usageBannerEnabledStatus
settings.autoUpdate = autoUpdateEnabledStatus
settings.mirrordVersion = mirrordVersionStatus
settings.enabledByDefault = enabledOnStartupStatus
}
}

Expand All @@ -42,6 +44,7 @@ class MirrordSettingsConfigurable : Configurable {
mirrordVersionStatus = settings.mirrordVersion
notificationsDisabledStatus = settings.disabledNotifications.orEmpty()
usageBannerEnabledStatus = settings.showUsageBanner
enabledOnStartupStatus = settings.enabledByDefault
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ open class MirrordSettingsState : PersistentStateComponent<MirrordSettingsState.
var showUsageBanner: Boolean = true
var runsCounter: Int = 0
var operatorUsed: Boolean = false
var enabledByDefault: Boolean = false

fun disableNotification(id: NotificationId) {
disabledNotifications = disabledNotifications.orEmpty() + id
Expand Down
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 @@ -42,6 +42,7 @@
<fileBasedIndex implementation="com.metalbear.mirrord.MirrordConfigIndex"/>

<postStartupActivity implementation="com.metalbear.mirrord.MirrordUsageBanner"/>
<postStartupActivity implementation="com.metalbear.mirrord.MirrordEnabler"/>
<backgroundPostStartupActivity implementation="com.metalbear.mirrord.MirrordBinaryManager$DownloadInitializer"/>
</extensions>

Expand Down

0 comments on commit ca730f9

Please sign in to comment.