Skip to content

Commit 38e5dff

Browse files
Merge pull request #92 from AkshayAshokCode/#30
#30 Add Option to Enable/Disable Notification Sounds
2 parents ebab14f + 98e1b57 commit 38e5dff

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.akshayashokcode.devfocus.services.settings
2+
3+
import com.intellij.openapi.components.PersistentStateComponent
4+
import com.intellij.openapi.components.Service
5+
import com.intellij.openapi.components.State
6+
import com.intellij.openapi.components.Storage
7+
8+
@Service(Service.Level.APP)
9+
@State(
10+
name = "DevFocusSettings",
11+
storages = [Storage("DevFocusSettings.xml")]
12+
)
13+
class DevFocusSettingsState :
14+
PersistentStateComponent<DevFocusSettingsState.SettingsState> {
15+
16+
data class SettingsState(
17+
var soundEnabled: Boolean = true
18+
)
19+
20+
private var state = SettingsState()
21+
22+
override fun getState(): SettingsState = state
23+
24+
override fun loadState(state: SettingsState) {
25+
this.state = state
26+
}
27+
28+
var soundEnabled: Boolean
29+
get() = state.soundEnabled
30+
set(value) {
31+
state.soundEnabled = value
32+
}
33+
}

src/main/kotlin/com/github/akshayashokcode/devfocus/toolWindow/PomodoroToolWindowPanel.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import com.github.akshayashokcode.devfocus.model.PomodoroSettings
55
import com.github.akshayashokcode.devfocus.services.pomodoro.PomodoroTimerService
66
import com.github.akshayashokcode.devfocus.ui.components.CircularTimerPanel
77
import com.github.akshayashokcode.devfocus.ui.components.SessionIndicatorPanel
8+
import com.github.akshayashokcode.devfocus.ui.settings.PomodoroSettingsDialog
89
import com.github.akshayashokcode.devfocus.ui.settings.PomodoroSettingsPanel
10+
import com.intellij.icons.AllIcons
911
import com.intellij.openapi.Disposable
1012
import com.intellij.openapi.project.Project
1113
import com.intellij.ui.components.JBPanel
@@ -31,6 +33,14 @@ class PomodoroToolWindowPanel(private val project: Project) : JBPanel<JBPanel<*>
3133
selectedItem = PomodoroMode.CLASSIC
3234
}
3335

36+
// Setting button
37+
private val settingsButton = JButton(AllIcons.General.GearPlain).apply {
38+
toolTipText = "Settings"
39+
isBorderPainted = false
40+
isContentAreaFilled = false
41+
isFocusPainted = false
42+
}
43+
3444
// Info label showing current mode settings
3545
private val infoLabel = JLabel("📊 25 min work • 5 min break").apply {
3646
horizontalAlignment = SwingConstants.CENTER
@@ -94,6 +104,7 @@ class PomodoroToolWindowPanel(private val project: Project) : JBPanel<JBPanel<*>
94104
val topPanel = JPanel(BorderLayout(5, 5)).apply {
95105
border = BorderFactory.createEmptyBorder(10, 10, 5, 10)
96106
add(modeComboBox, BorderLayout.CENTER)
107+
add(settingsButton, BorderLayout.EAST)
97108
}
98109

99110
// Info panel
@@ -158,6 +169,10 @@ class PomodoroToolWindowPanel(private val project: Project) : JBPanel<JBPanel<*>
158169
}
159170
updateSettingsPanelVisibility()
160171
}
172+
173+
settingsButton.addActionListener {
174+
PomodoroSettingsDialog(project).show()
175+
}
161176
}
162177

163178
private fun updateSettingsPanelVisibility() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.akshayashokcode.devfocus.ui.settings
2+
3+
import com.github.akshayashokcode.devfocus.services.settings.DevFocusSettingsState
4+
import com.intellij.openapi.application.ApplicationManager
5+
import com.intellij.openapi.project.Project
6+
import com.intellij.openapi.ui.DialogWrapper
7+
import java.awt.BorderLayout
8+
import javax.swing.BorderFactory
9+
import javax.swing.JCheckBox
10+
import javax.swing.JComponent
11+
import javax.swing.JPanel
12+
13+
class PomodoroSettingsDialog(
14+
project: Project
15+
) : DialogWrapper(project) {
16+
17+
private val settings =
18+
ApplicationManager.getApplication()
19+
.getService(DevFocusSettingsState::class.java)
20+
21+
private val soundCheckbox =
22+
JCheckBox(
23+
"Notification sound",
24+
settings.soundEnabled
25+
)
26+
27+
init {
28+
title = "DevFocus Settings"
29+
init()
30+
}
31+
32+
override fun createCenterPanel(): JComponent {
33+
34+
return JPanel(BorderLayout()).apply {
35+
border = BorderFactory.createEmptyBorder(16, 16, 16, 16)
36+
37+
add(soundCheckbox, BorderLayout.NORTH)
38+
}
39+
}
40+
41+
override fun doOKAction() {
42+
43+
settings.soundEnabled = soundCheckbox.isSelected
44+
45+
super.doOKAction()
46+
}
47+
}

src/main/kotlin/com/github/akshayashokcode/devfocus/util/SoundPlayer.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.akshayashokcode.devfocus.util
22

3+
import com.github.akshayashokcode.devfocus.services.settings.DevFocusSettingsState
4+
import com.intellij.openapi.application.ApplicationManager
35
import java.io.BufferedInputStream
46
import javax.sound.sampled.AudioSystem
57
import javax.sound.sampled.Clip
@@ -11,6 +13,12 @@ object SoundPlayer {
1113
fun play(soundFileName: String) {
1214

1315
try {
16+
val settings = ApplicationManager.getApplication()
17+
.getService(DevFocusSettingsState::class.java)
18+
19+
if (!settings.soundEnabled) {
20+
return
21+
}
1422

1523
val resourceStream = SoundPlayer::class.java
1624
.getResourceAsStream("/sounds/$soundFileName")

0 commit comments

Comments
 (0)