Skip to content

Commit

Permalink
Controlled inputs for settings + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Apr 18, 2024
1 parent dc8a6ca commit a8a3ae8
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"database:allow-reset",
"default-view:allow-set",
"shell-extension:allow-show-item-in-folder",
"sleepblocker:allow-enable",
"sleepblocker:allow-disable"
],
"platforms": ["linux", "macOS", "windows"]
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/plugins/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum DefaultView {
pub struct Config {
pub theme: String,
pub audio_volume: f32,
pub audio_playback_rate: f32,
pub audio_playback_rate: Option<f32>,
pub audio_output_device: String,
pub audio_muted: bool,
pub audio_shuffle: bool,
Expand All @@ -67,7 +67,7 @@ impl Config {
Config {
theme: "__system".to_owned(),
audio_volume: 1.0,
audio_playback_rate: 1.0,
audio_playback_rate: Some(1.0),
audio_output_device: "default".to_owned(),
audio_muted: false,
audio_shuffle: false,
Expand Down
4 changes: 2 additions & 2 deletions src/components/AudioOutputSelect/AudioOutputSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logAndNotifyError } from '../../lib/utils';
import * as Setting from '../Setting/Setting';

type Props = {
defaultValue: string;
value: string;
onChange: (deviceID: string) => void;
} & Setting.InputProps;

Expand Down Expand Up @@ -73,7 +73,7 @@ export default function AudioOutputSelect(props: Props) {
<Setting.Select
{...selectProps}
key="devicesOk" // avoid default value problems
defaultValue={props.defaultValue}
value={props.value}
onChange={setAudioOutputDevice}
>
{devices.map((device) => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/SettingCheckbox/SettingCheckbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import styles from './SettingCheckbox.module.css';
type Props = {
title: string;
slug: string;
defaultValue: boolean;
onClick: (value: boolean) => void;
value: boolean;
onChange: (value: boolean) => void;
description?: string;
};

Expand All @@ -17,8 +17,8 @@ export default function CheckboxSetting(props: Props) {
<input
id={`setting-${slug}`}
type="checkbox"
onClick={(e) => props.onClick(e.currentTarget.checked)}
defaultChecked={props.defaultValue}
onChange={(e) => props.onChange(e.currentTarget.checked)}
checked={props.value}
/>
<Setting.Label htmlFor={slug} noMargin>
{title}
Expand Down
2 changes: 1 addition & 1 deletion src/generated/typings/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import type { Repeat } from "./Repeat";
import type { SortBy } from "./SortBy";
import type { SortOrder } from "./SortOrder";

export interface Config { theme: string, audio_volume: number, audio_playback_rate: number, audio_output_device: string, audio_muted: boolean, audio_shuffle: boolean, audio_repeat: Repeat, default_view: DefaultView, library_sort_by: SortBy, library_sort_order: SortOrder, library_folders: Array<string>, sleepblocker: boolean, auto_update_checker: boolean, minimize_to_tray: boolean, notifications: boolean, track_view_density: string, }
export interface Config { theme: string, audio_volume: number, audio_playback_rate: number | null, audio_output_device: string, audio_muted: boolean, audio_shuffle: boolean, audio_repeat: Repeat, default_view: DefaultView, library_sort_by: SortBy, library_sort_order: SortOrder, library_folders: Array<string>, sleepblocker: boolean, auto_update_checker: boolean, minimize_to_tray: boolean, notifications: boolean, track_view_density: string, }
2 changes: 1 addition & 1 deletion src/lib/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Player {

export default new Player({
volume: config.getInitial('audio_volume'),
playbackRate: config.getInitial('audio_playback_rate'),
playbackRate: config.getInitial('audio_playback_rate') ?? 1,
audioOutputDevice: config.getInitial('audio_output_device'),
muted: config.getInitial('audio_muted'),
});
10 changes: 8 additions & 2 deletions src/stores/SettingsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { themes } from '../lib/themes';
import { logAndNotifyError } from '../lib/utils';
import type { Theme } from '../types/museeks';

import router from '../views/router';
import useToastsStore from './useToastsStore';

interface UpdateCheckOptions {
Expand Down Expand Up @@ -52,6 +53,7 @@ const setTracksDensity = async (
density: Config['track_view_density'],
): Promise<void> => {
await config.set('track_view_density', density);
router.revalidate();
};

/**
Expand Down Expand Up @@ -137,10 +139,11 @@ const check = async (): Promise<void> => {
*/
const toggleSleepBlocker = async (value: boolean): Promise<void> => {
if (value === true) {
invoke('plugin:sleepblocker|enable');
await invoke('plugin:sleepblocker|enable');
} else {
invoke('plugin:sleepblocker|disable');
await invoke('plugin:sleepblocker|disable');
}
router.revalidate();
};

/**
Expand All @@ -150,20 +153,23 @@ const setDefaultView = async (defaultView: DefaultView): Promise<void> => {
await invoke('plugin:default-view|set', {
defaultView,
});
router.revalidate();
};

/**
* Toggle update check on startup
*/
const toggleAutoUpdateChecker = async (value: boolean): Promise<void> => {
await config.set('auto_update_checker', value);
router.revalidate();
};

/**
* Toggle native notifications display
*/
const toggleDisplayNotifications = async (value: boolean): Promise<void> => {
await config.set('notifications', value);
router.revalidate();
};

// Should we use something else to harmonize between zustand and non-store APIs?
Expand Down
15 changes: 10 additions & 5 deletions src/stores/usePlayerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ const usePlayerStore = createPlayerStore<PlayerState>((set, get) => ({

const newTrack = queue[newQueueCursor];

// tslint:disable-next-line
if (newTrack !== undefined) {
player.setTrack(newTrack);
await player.play();
Expand Down Expand Up @@ -334,18 +333,22 @@ const usePlayerStore = createPlayerStore<PlayerState>((set, get) => ({
else player.unmute();

await config.set('audio_muted', muted);
router.revalidate();
},

/**
* Set audio's playback rate
*/
setPlaybackRate: async (value) => {
if (value >= 0.5 && value <= 5) {
// if in allowed range
player.setPlaybackRate(value);

// if in allowed range
if (!Number.isNaN(value) && value >= 0.5 && value <= 5) {
await config.set('audio_playback_rate', value);
player.setPlaybackRate(value);
} else {
await config.set('audio_playback_rate', null);
player.setPlaybackRate(1.0);
}
router.revalidate();
},

/**
Expand All @@ -356,6 +359,7 @@ const usePlayerStore = createPlayerStore<PlayerState>((set, get) => ({
try {
await player.setOutputDevice(deviceID);
await config.set('audio_output_device', deviceID);
router.revalidate();
} catch (err) {
logAndNotifyError(err);
}
Expand Down Expand Up @@ -568,4 +572,5 @@ function createPlayerStore<T extends PlayerState>(store: StateCreator<T>) {
*/
const saveVolume = debounce(async (volume: number) => {
await config.set('audio_volume', volume);
router.revalidate();
}, 500);
4 changes: 2 additions & 2 deletions src/views/ViewSettingsAbout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export default function ViewSettingsAbout() {
<CheckboxSetting
slug="update"
title="Automatically check for updates"
defaultValue={config.auto_update_checker}
onClick={SettingsAPI.toggleAutoUpdateChecker}
value={config.auto_update_checker}
onChange={SettingsAPI.toggleAutoUpdateChecker}
/>
<div>
<Button
Expand Down
4 changes: 2 additions & 2 deletions src/views/ViewSettingsAudio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function ViewSettingsAudio() {
description="Increase the playback rate: a value of 2 will play your music at a 2x
speed"
id="setting-playbackrate"
defaultValue={config.audio_playback_rate}
value={config.audio_playback_rate ?? ''}
onChange={setPlaybackRate}
type="number"
min="0.5"
Expand All @@ -40,7 +40,7 @@ export default function ViewSettingsAudio() {
label="Audio output"
description="Advanced: set a custom audio output device."
id="setting-playbackrate"
defaultValue={config.audio_output_device}
value={config.audio_output_device}
onChange={playerAPI.setOutputDevice}
/>
</Setting.Section>
Expand Down
14 changes: 7 additions & 7 deletions src/views/ViewSettingsUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function ViewSettingsUI() {
label="Theme"
description="Change the appearance of the interface"
id="setting-theme"
defaultValue={config.theme}
value={config.theme}
onChange={onThemeChange}
disabled // Issue in Tauri where we cannot easily detect system-wide preferences
>
Expand All @@ -59,7 +59,7 @@ export default function ViewSettingsUI() {
label="Tracks density"
description="Change the default view when starting the application"
id="setting-tracks-density"
defaultValue={config.track_view_density}
value={config.track_view_density}
onChange={onTracksDensityChange}
>
<option value="normal">Normal (default)</option>
Expand All @@ -69,7 +69,7 @@ export default function ViewSettingsUI() {
<Setting.Section>
<Setting.Select
label="Default view"
defaultValue={config.theme}
value={config.theme}
description="Change the default view when starting the application"
id="setting-default-view"
onChange={onDefaultViewChange}
Expand All @@ -83,17 +83,17 @@ export default function ViewSettingsUI() {
slug="native-notifications"
title="Display Notifications"
description="Send notifications when the playing track changes"
defaultValue={config.notifications}
onClick={SettingsAPI.toggleDisplayNotifications}
value={config.notifications}
onChange={SettingsAPI.toggleDisplayNotifications}
/>
</Setting.Section>
<Setting.Section>
<CheckboxSetting
slug="sleepmode"
title="Sleep mode blocker"
description="Prevent the computer from going into sleep mode when playing"
defaultValue={config.sleepblocker}
onClick={SettingsAPI.toggleSleepBlocker}
value={config.sleepblocker}
onChange={SettingsAPI.toggleSleepBlocker}
/>
</Setting.Section>
</div>
Expand Down

0 comments on commit a8a3ae8

Please sign in to comment.