Skip to content

Commit

Permalink
Allow mirrord to be enabled or disabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
gememma committed Aug 20, 2024
1 parent bc43e0a commit 32330f1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
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 by using dropdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.metalbear.mirrord

import com.intellij.ide.BrowserUtil
import com.intellij.ide.DataManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.actionSystem.ex.ComboBoxAction
import com.intellij.openapi.application.WriteAction
Expand Down Expand Up @@ -33,6 +34,30 @@ class MirrordDropDown : ComboBoxAction(), DumbAware {
}
}

private class ToggleEnabledByDefault : AnAction("Enable/ Disable mirrord by Default") {
override fun actionPerformed(e: AnActionEvent) {
val newValue = !MirrordSettingsState.instance.mirrordState.enabledByDefault
MirrordSettingsState.instance.mirrordState.enabledByDefault = newValue
// popup containing new value of enabledByDefault
val service = e.project?.service<MirrordProjectService>() ?: return
val notification = if (newValue) {
service.notifier.notification(
"mirrord set to be enabled by default",
NotificationType.IDE_UPDATE
)
} else {
service.notifier.notification(
"mirrord set to be disabled by default",
NotificationType.IDE_UPDATE
)
}

notification.fire()

return
}
}

private class SelectActiveConfigAction : AnAction("Select Active Config") {
override fun actionPerformed(e: AnActionEvent) {
val service = e.project?.service<MirrordProjectService>() ?: return
Expand Down Expand Up @@ -130,6 +155,7 @@ class MirrordDropDown : ComboBoxAction(), DumbAware {
service.activeConfig?.let { add(ShowActiveConfigAction(it, project)) }
add(SelectActiveConfigAction())
add(SettingsAction())
add(ToggleEnabledByDefault())

if (!MirrordSettingsState.instance.mirrordState.operatorUsed) {
addSeparator("mirrord for Teams")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.metalbear.mirrord

import com.intellij.ide.ActivityTracker
import com.intellij.notification.NotificationType
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.intellij.openapi.util.IconLoader
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 +29,20 @@ 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()
project.service<MirrordProjectService>().notifier.notification(
"mirrord is enabled by default",
NotificationType.IDE_UPDATE
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.metalbear.mirrord

import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.modules
import com.intellij.openapi.startup.StartupActivity
import com.metalbear.mirrord.MirrordSettingsState.Companion.instance
import icons.MirrordIcons

@State(name = "MirrordSettingsState", storages = [Storage("mirrord.xml")])
open class MirrordSettingsState : PersistentStateComponent<MirrordSettingsState.MirrordState> {
Expand Down Expand Up @@ -49,6 +55,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 32330f1

Please sign in to comment.