Skip to content

Commit

Permalink
feat: store muted status in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
vjousse committed Oct 4, 2024
1 parent 65c48eb commit e290a68
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 49 deletions.
3 changes: 2 additions & 1 deletion src-elm/Json.elm
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ configDecoder =
(Decode.field "minimize_to_tray" Decode.bool)
(Decode.field "minimize_to_tray_on_close" Decode.bool)
in
Decode.map6 (<|)
Decode.map7 (<|)
fieldSet0
(Decode.field "muted" Decode.bool)
(Decode.field "pomodoro_duration" Decode.int)
(Decode.field "short_break_duration" Decode.int)
(Decode.field "theme" Decode.string)
Expand Down
69 changes: 28 additions & 41 deletions src-elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type alias Model =
, currentState : CurrentState
, currentTime : Seconds
, drawerOpen : Bool
, muted : Bool
, pomodoroState : Maybe RustState
, sessionStatus : SessionStatus
, settingTab : SettingTab
Expand Down Expand Up @@ -69,6 +68,7 @@ type alias Flags =
, maxRoundNumber : Int
, minimizeToTray : Bool
, minimizeToTrayOnClose : Bool
, muted : Bool
, pomodoroDuration : Seconds
, shortBreakDuration : Seconds
, theme : String
Expand Down Expand Up @@ -108,6 +108,7 @@ init flags =
, maxRoundNumber = flags.maxRoundNumber
, minimizeToTray = flags.minimizeToTray
, minimizeToTrayOnClose = flags.minimizeToTrayOnClose
, muted = flags.muted
, pomodoroDuration = flags.pomodoroDuration
, shortBreakDuration = flags.shortBreakDuration
, theme = flags.theme
Expand All @@ -120,7 +121,6 @@ init flags =
, currentState = currentState
, currentTime = flags.pomodoroDuration
, drawerOpen = False
, muted = False
, pomodoroState = Nothing
, sessionStatus = NotStarted
, settingTab = TimerTab
Expand Down Expand Up @@ -229,38 +229,35 @@ getNextRoundInfo model =


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
update msg ({ config } as model) =
case msg of
ChangeSettingConfig settingConfig ->
let
settingsConfig =
model.config

newSettingsConfig =
case settingConfig of
AlwaysOnTop ->
{ settingsConfig | alwaysOnTop = not settingsConfig.alwaysOnTop }
{ config | alwaysOnTop = not config.alwaysOnTop }

AutoStartBreakTimer ->
{ settingsConfig | autoStartBreakTimer = not settingsConfig.autoStartBreakTimer }
{ config | autoStartBreakTimer = not config.autoStartBreakTimer }

AutoStartWorkTimer ->
{ settingsConfig | autoStartWorkTimer = not settingsConfig.autoStartWorkTimer }
{ config | autoStartWorkTimer = not config.autoStartWorkTimer }

TickSoundsDuringWork ->
{ settingsConfig | tickSoundsDuringWork = not settingsConfig.tickSoundsDuringWork }
{ config | tickSoundsDuringWork = not config.tickSoundsDuringWork }

TickSoundsDuringBreak ->
{ settingsConfig | tickSoundsDuringBreak = not settingsConfig.tickSoundsDuringBreak }
{ config | tickSoundsDuringBreak = not config.tickSoundsDuringBreak }

DesktopNotifications ->
{ settingsConfig | desktopNotifications = not settingsConfig.desktopNotifications }
{ config | desktopNotifications = not config.desktopNotifications }

MinimizeToTray ->
{ settingsConfig | minimizeToTray = not settingsConfig.minimizeToTray }
{ config | minimizeToTray = not config.minimizeToTray }

MinimizeToTrayOnClose ->
{ settingsConfig | minimizeToTrayOnClose = not settingsConfig.minimizeToTrayOnClose }
{ config | minimizeToTrayOnClose = not config.minimizeToTrayOnClose }
in
( { model | config = newSettingsConfig }, updateConfig newSettingsConfig )

Expand All @@ -275,9 +272,6 @@ update msg model =
newState =
{ currentState | color = fromRGBToCSSHex <| colorForSessionType model.currentSessionType theme }

config =
model.config

newConfig =
{ config
| theme = theme.name |> String.toLower
Expand Down Expand Up @@ -319,18 +313,18 @@ update msg model =
NoOp ->
( model, Cmd.none )

ProcessExternalMessage (RustConfigAndThemesMsg { config, themes }) ->
ProcessExternalMessage (RustConfigAndThemesMsg c) ->
let
updatedThemes =
themes
c.themes
|> ListWithCurrent.fromList
|> ListWithCurrent.setCurrentByPredicate (\t -> (t.name |> String.toLower) == config.theme)
|> ListWithCurrent.setCurrentByPredicate (\t -> (t.name |> String.toLower) == c.config.theme)

newThemes =
case ListWithCurrent.getCurrent updatedThemes of
Just theme ->
-- We found a theme with the same name than in the config: everything's fine
if (theme.name |> String.toLower) == (config.theme |> String.toLower) then
if (theme.name |> String.toLower) == (c.config.theme |> String.toLower) then
updatedThemes

else
Expand All @@ -342,7 +336,7 @@ update msg model =

newModel =
{ model
| config = config
| config = c.config
, sessionStatus = NotStarted
, themes = newThemes
, currentTime =
Expand Down Expand Up @@ -402,9 +396,6 @@ update msg model =

ResetSettings ->
let
config =
model.config

newConfig =
{ config
| pomodoroDuration = defaults.pomodoroDuration
Expand Down Expand Up @@ -458,7 +449,7 @@ update msg model =
in
( { nextModel | currentState = currentState }
, Cmd.batch
[ if model.muted then
[ if model.config.muted then
Cmd.none

else
Expand Down Expand Up @@ -562,7 +553,7 @@ update msg model =
}
, Cmd.batch
[ updateCurrentState currentState
, if nextModel.muted then
, if nextModel.config.muted then
Cmd.none

else
Expand All @@ -581,9 +572,6 @@ update msg model =

ToggleDrawer ->
let
config =
model.config

newConfig =
{ config
| -- Avoid having impossible states
Expand Down Expand Up @@ -623,17 +611,17 @@ update msg model =
ToggleMute ->
let
newVolume =
if model.muted then
if config.muted then
model.volume

else
0

nextModel =
{ model | muted = not model.muted, volume = newVolume }
newConfig =
{ config | muted = not config.muted }
in
( nextModel
, setVolume newVolume
( { model | volume = newVolume, config = newConfig }
, Cmd.batch [ setVolume newVolume, updateConfig newConfig ]
)

TogglePlayStatus ->
Expand Down Expand Up @@ -696,9 +684,6 @@ update msg model =

Just stringValue ->
stringValue

config =
model.config
in
case settingType of
FocusTime ->
Expand Down Expand Up @@ -803,11 +788,13 @@ update msg model =

Just v ->
toFloat v / 100

newConfig =
{ config | muted = newVolume <= 0 }
in
( { model
| volume = newVolume
, muted =
newVolume <= 0
, config = newConfig
}
, setVolume newVolume
)
Expand Down Expand Up @@ -1053,7 +1040,7 @@ footerView model =
]
]
, div [ class "icon-wrapper", class "icon-wrapper--double--right", id "toggle-mute", onClick ToggleMute, title "Mute" ]
[ if model.muted == False then
[ if model.config.muted == False then
svg
[ SvgAttr.version "1.2"
, SvgAttr.id "Layer_1"
Expand Down
1 change: 1 addition & 0 deletions src-elm/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type alias Config =
, maxRoundNumber : Int
, minimizeToTray : Bool
, minimizeToTrayOnClose : Bool
, muted : Bool
, pomodoroDuration : Seconds
, shortBreakDuration : Seconds
, theme : String
Expand Down
14 changes: 9 additions & 5 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct Config {
max_round_number: u16,
minimize_to_tray: bool,
minimize_to_tray_on_close: bool,
#[serde(default)]
muted: bool,
pomodoro_duration: u16,
short_break_duration: u16,
#[serde(default = "default_theme")]
Expand Down Expand Up @@ -207,6 +209,7 @@ impl Default for Config {
max_round_number: 4u16,
minimize_to_tray: true,
minimize_to_tray_on_close: true,
muted: false,
pomodoro_duration: 25 * 60,
short_break_duration: 5 * 60,
theme: "pomotroid".to_string(),
Expand Down Expand Up @@ -442,19 +445,20 @@ fn get_themes_for_directory(themes_path: PathBuf) -> Vec<PathBuf> {
themes_paths_bufs
}

// @FIX: handle mute on Elm side
fn should_play_tick_sound(config: &Config, pomodoro: &Pomodoro) -> bool {
match (
pomodoro.current_session.status,
pomodoro.current_session.session_type,
config.tick_sounds_during_work,
config.tick_sounds_during_break,
config.muted,
) {
// No tick sound configured
(_, _, false, false) => false,
(SessionStatus::Running, SessionType::Focus, true, _) => true,
(SessionStatus::Running, SessionType::LongBreak, _, true) => true,
(SessionStatus::Running, SessionType::ShortBreak, _, true) => true,
(_, _, _, _, true) => false,
(_, _, false, false, _) => false,
(SessionStatus::Running, SessionType::Focus, true, _, _) => true,
(SessionStatus::Running, SessionType::LongBreak, _, true, _) => true,
(SessionStatus::Running, SessionType::ShortBreak, _, true, _) => true,
_ => false,
}
}
Expand Down
7 changes: 5 additions & 2 deletions src-ts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type ElmState = {
color: string;
percentage: number;
paused: boolean;
playTick: boolean;
};

type Notification = {
Expand Down Expand Up @@ -75,6 +74,7 @@ type ElmConfig = {
maxRoundNumber: number;
minimizeToTray: boolean;
minimizeToTrayOnClose: boolean;
muted: boolean;
pomodoroDuration: number;
shortBreakDuration: number;
theme: string;
Expand All @@ -91,6 +91,7 @@ type RustConfig = {
max_round_number: number;
minimize_to_tray: boolean;
minimize_to_tray_on_close: boolean;
muted: boolean;
pomodoro_duration: number;
short_break_duration: number;
theme: string;
Expand All @@ -109,6 +110,7 @@ let rustConfig: RustConfig = {
max_round_number: 4,
minimize_to_tray: true,
minimize_to_tray_on_close: true,
muted: false,
pomodoro_duration: 1500,
short_break_duration: 300,
theme: "pomodorolm",
Expand All @@ -130,6 +132,7 @@ app = Elm.Main.init({
maxRoundNumber: rustConfig.max_round_number,
minimizeToTray: rustConfig.minimize_to_tray,
minimizeToTrayOnClose: rustConfig.minimize_to_tray_on_close,
muted: rustConfig.muted,
pomodoroDuration: rustConfig.pomodoro_duration,
shortBreakDuration: rustConfig.short_break_duration,
theme: rustConfig.theme,
Expand Down Expand Up @@ -186,6 +189,7 @@ app.ports.updateConfig.subscribe(function (config: ElmConfig) {
max_round_number: config.maxRoundNumber,
minimize_to_tray: config.minimizeToTray,
minimize_to_tray_on_close: config.minimizeToTrayOnClose,
muted: config.muted,
pomodoro_duration: config.pomodoroDuration,
short_break_duration: config.shortBreakDuration,
theme: config.theme,
Expand All @@ -196,7 +200,6 @@ app.ports.updateConfig.subscribe(function (config: ElmConfig) {
});

app.ports.updateCurrentState.subscribe(function (state: ElmState) {
invoke("update_play_tick", { playTick: state.playTick });
invoke("change_icon", {
red: hexToRgb(state.color)?.r,
green: hexToRgb(state.color)?.g,
Expand Down

0 comments on commit e290a68

Please sign in to comment.