|
1 | | -function ToggleDarkMode { |
2 | | - $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" |
3 | | - |
4 | | - $appsMode = Get-ItemProperty -Path $regPath -Name "AppsUseLightTheme" |
5 | | - $systemMode = Get-ItemProperty -Path $regPath -Name "SystemUsesLightTheme" |
6 | | - |
7 | | - # Si está en modo claro, cambia ambos a oscuro |
8 | | - if ($appsMode.AppsUseLightTheme -eq 1 -or $systemMode.SystemUsesLightTheme -eq 1) { |
9 | | - Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 0 |
10 | | - Set-ItemProperty -Path $regPath -Name "SystemUsesLightTheme" -Value 0 |
11 | | - Write-Host "🖤 Cambiado a modo oscuro para apps y sistema" |
| 1 | +<# |
| 2 | +███████╗ █████╗ ██████╗ ██╗██████╗ ███████╗██╗ ██╗ ██╗██████╗ |
| 3 | +██╔════╝██╔══██╗██╔══██╗██║██╔══██╗██╔════╝██║ ██║ ██╔╝╚════██╗ |
| 4 | +█████╗ ███████║██████╔╝██║██║ ██║█████╗ ██║ ██║██╔╝ █████╔╝ |
| 5 | +██╔══╝ ██╔══██║██╔══██╗██║██║ ██║██╔══╝ ╚██╗ ██╔╝╚██╗ ╚═══██╗ |
| 6 | +██║ ██║ ██║██████╔╝██║██████╔╝███████╗ ╚████╔╝ ╚██╗██████╔╝ |
| 7 | +╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝╚═════╝ ╚══════╝ ╚═══╝ ╚═╝╚═════╝ |
| 8 | + 🧠 Automation CLI by: FabiDev<3 |
| 9 | +#> |
| 10 | + |
| 11 | +param( |
| 12 | + [ValidateSet('Dark','Light','Toggle')] |
| 13 | + [string]$Mode = 'Toggle', # <— al llamar el archivo sin args entra aquí |
| 14 | + [switch]$PassThru # ← útil para las pruebas |
| 15 | +) |
| 16 | + |
| 17 | +function Toggle-DarkMode { |
| 18 | + [CmdletBinding()] |
| 19 | + param( |
| 20 | + [ValidateSet('Dark','Light','Toggle')] |
| 21 | + [string]$Mode = 'Toggle', |
| 22 | + [switch]$PassThru |
| 23 | + ) |
| 24 | + |
| 25 | + $regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize' |
| 26 | + |
| 27 | + # Lee los valores actuales (0 = dark, 1 = light) |
| 28 | + $currentApps = (Get-ItemProperty -Path $regPath -Name AppsUseLightTheme).AppsUseLightTheme |
| 29 | + $currentSystem = (Get-ItemProperty -Path $regPath -Name SystemUsesLightTheme).SystemUsesLightTheme |
| 30 | + |
| 31 | + switch ($Mode) { |
| 32 | + 'Dark' { $new = 0 } |
| 33 | + 'Light' { $new = 1 } |
| 34 | + 'Toggle' { $new = if ($currentApps -eq 1 -or $currentSystem -eq 1) { 0 } else { 1 } } |
| 35 | + } |
| 36 | + |
| 37 | + # Solo escribe si hay cambio |
| 38 | + if ($currentApps -ne $new -or $currentSystem -ne $new) { |
| 39 | + Set-ItemProperty -Path $regPath -Name AppsUseLightTheme -Value $new |
| 40 | + Set-ItemProperty -Path $regPath -Name SystemUsesLightTheme -Value $new |
12 | 41 | } |
13 | | - else { |
14 | | - Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 1 |
15 | | - Set-ItemProperty -Path $regPath -Name "SystemUsesLightTheme" -Value 1 |
| 42 | + |
| 43 | + # Mensaje amigable |
| 44 | + if ($new -eq 0) { |
| 45 | + Write-Host "🖤 Cambiado a modo oscuro para apps y sistema" |
| 46 | + } else { |
16 | 47 | Write-Host "🤍 Cambiado a modo claro para apps y sistema" |
17 | 48 | } |
18 | 49 |
|
19 | | - ## Option Reinicia el explorador para aplicar los cambios |
20 | | - # Stop-Process -Name explorer -Force |
21 | | - # Start-Process explorer |
| 50 | + # Notifica al shell sin cerrar sesión (silencioso si falla en pruebas) |
| 51 | + try { |
| 52 | + if (-not ('NativeMethods' -as [type])) { |
| 53 | + Add-Type @" |
| 54 | +using System; |
| 55 | +using System.Runtime.InteropServices; |
| 56 | +public class NativeMethods { |
| 57 | + [DllImport("user32.dll", SetLastError=true)] |
| 58 | + public static extern IntPtr SendMessageTimeout( |
| 59 | + IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, |
| 60 | + uint fuFlags, uint uTimeout, out UIntPtr lpdwResult); |
| 61 | +} |
| 62 | +"@ |
| 63 | + } |
| 64 | + [UIntPtr]$dummy = [UIntPtr]::Zero |
| 65 | + [NativeMethods]::SendMessageTimeout([IntPtr]::Zero, 0x1A, |
| 66 | + [UIntPtr]::Zero, 'ImmersiveColorSet', 0x0000, 100, [ref]$dummy) | Out-Null |
| 67 | + } catch { |
| 68 | + # Ignorar en entornos sin GUI (p.ej., GitHub Actions) |
| 69 | + } |
| 70 | + |
| 71 | + if ($PassThru) { |
| 72 | + [PSCustomObject]@{ |
| 73 | + Mode = if ($new -eq 0) { 'Dark' } else { 'Light' } |
| 74 | + Timestamp = Get-Date |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +# ------------------------------------------------------------------ |
| 80 | +# Si este archivo se ejecuta **directamente** (no dot-sourced), llama |
| 81 | +# a la función con los parámetros recibidos. |
| 82 | +# ------------------------------------------------------------------ |
| 83 | +if ($MyInvocation.InvocationName -eq $MyInvocation.MyCommand.Path) { |
| 84 | + Toggle-DarkMode @PSBoundParameters |
| 85 | +} |
| 86 | + |
| 87 | +# Exporta la función si alguien dot-sourcó el archivo o lo importó |
| 88 | +if ($ExecutionContext.SessionState.Module) { |
| 89 | + Export-ModuleMember -Function Toggle-DarkMode |
22 | 90 | } |
23 | 91 |
|
24 | | -# Ejecutar la función |
25 | | -ToggleDarkMode |
|
0 commit comments