@@ -4,26 +4,35 @@ import { invoke } from '@tauri-apps/api/core';
44import SettingsCard from './SettingsCard' ;
55
66const SystemSettingsCard : React . FC = ( ) => {
7- const [ autostart , setAutostart ] = useState ( false ) ;
7+ // null = unknown / error reading state
8+ const [ autostart , setAutostart ] = useState < boolean | null > ( null ) ;
89 const [ loading , setLoading ] = useState ( true ) ;
10+ const [ pending , setPending ] = useState ( false ) ;
911
1012 useEffect ( ( ) => {
1113 invoke < boolean > ( 'is_autostart_enabled' )
1214 . then ( setAutostart )
13- . catch ( ( ) => setAutostart ( false ) )
15+ . catch ( ( ) => setAutostart ( null ) )
1416 . finally ( ( ) => setLoading ( false ) ) ;
1517 } , [ ] ) ;
1618
1719 const handleToggle = async ( ) => {
20+ if ( autostart === null ) return ;
1821 const next = ! autostart ;
22+ setPending ( true ) ;
1923 try {
2024 await invoke ( next ? 'enable_autostart' : 'disable_autostart' ) ;
2125 setAutostart ( next ) ;
2226 } catch ( err ) {
2327 console . error ( 'Failed to toggle autostart:' , err ) ;
28+ } finally {
29+ setPending ( false ) ;
2430 }
2531 } ;
2632
33+ const isDisabled = loading || pending || autostart === null ;
34+ const isChecked = autostart === true ;
35+
2736 return (
2837 < SettingsCard
2938 icon = { Monitor }
@@ -32,24 +41,28 @@ const SystemSettingsCard: React.FC = () => {
3241 >
3342 < div className = "flex items-center justify-between py-1" >
3443 < div >
35- < div className = "font-medium" > Launch at startup</ div >
36- < div className = "text-muted-foreground text-sm" >
44+ < div id = "autostart-label" className = "font-medium" >
45+ Launch at startup
46+ </ div >
47+ < div id = "autostart-desc" className = "text-muted-foreground text-sm" >
3748 Automatically start PictoPy when you log in. The window starts
3849 minimized to the system tray.
3950 </ div >
4051 </ div >
4152
4253 < button
4354 role = "switch"
44- aria-checked = { autostart }
45- disabled = { loading }
55+ aria-checked = { isChecked }
56+ aria-labelledby = "autostart-label"
57+ aria-describedby = "autostart-desc"
58+ disabled = { isDisabled }
4659 onClick = { handleToggle }
4760 className = { [
4861 'relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full' ,
4962 'transition-colors duration-200 ease-in-out' ,
5063 'focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2' ,
5164 'disabled:cursor-not-allowed disabled:opacity-50' ,
52- autostart
65+ isChecked
5366 ? 'bg-primary focus-visible:ring-primary'
5467 : 'bg-gray-200 focus-visible:ring-gray-500 dark:bg-gray-700' ,
5568 ] . join ( ' ' ) }
@@ -58,7 +71,7 @@ const SystemSettingsCard: React.FC = () => {
5871 className = { [
5972 'inline-block h-4 w-4 rounded-full bg-white shadow-md' ,
6073 'transition-transform duration-200 ease-in-out' ,
61- autostart ? 'translate-x-6' : 'translate-x-1' ,
74+ isChecked ? 'translate-x-6' : 'translate-x-1' ,
6275 ] . join ( ' ' ) }
6376 />
6477 </ button >
0 commit comments