-
-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathThemes.psm1
More file actions
145 lines (121 loc) · 4.86 KB
/
Themes.psm1
File metadata and controls
145 lines (121 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Set-StrictMode -Version 3.0
$windir = [Environment]::GetFolderPath('Windows')
function Stop-ThemeProcesses {
Get-Process 'SystemSettings', 'control' -EA 0 | Stop-Process -Force -EA 0
}
function Set-Theme {
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Path
)
$themeItem = Get-Item -Path $Path -ErrorAction SilentlyContinue
if (($null -eq $themeItem) -or ($themeItem.Extension -ne '.theme')) {
throw "'$Path' is not a valid path to a theme file."
}
function Set-ThemeUsingExplorer {
Write-Warning "Failed to apply theme using COM, falling back to launching file..."
Stop-ThemeProcesses
Start-Process -FilePath explorer -ArgumentList $Path
Start-Sleep 10
}
Add-Type @'
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
public static class ThemeManagerAPI
{
public static void ApplyTheme(string themeFilePath)
{
IThemeManager themeManager = new ThemeManagerClass();
themeManager.ApplyTheme(themeFilePath);
}
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("D23CC733-5522-406D-8DFB-B3CF5EF52A71")]
[ComImport]
public interface ITheme
{
}
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("0646EBBE-C1B7-4045-8FD0-FFD65D3FC792")]
[ComImport]
public interface IThemeManager
{
[DispId(1610678272)]
ITheme CurrentTheme { get; }
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void ApplyTheme([MarshalAs(UnmanagedType.BStr)] string themeFilePath);
}
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
[Guid("C04B329E-5823-4415-9C93-BA44688947B0")]
[ClassInterface(ClassInterfaceType.None)]
[ComImport]
public class ThemeManagerClass : IThemeManager
{
[DispId(1610678272)]
public virtual extern ITheme CurrentTheme { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] get; }
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern void ApplyTheme([MarshalAs(UnmanagedType.BStr)] string themeFilePath);
}
}
'@
try {
[ThemeManagerAPI]::ApplyTheme($Path)
} catch {
Set-ThemeUsingExplorer
}
Stop-ThemeProcesses
}
function Set-ThemeMRU {
if ([System.Environment]::OSVersion.Version.Build -ge 22000) {
Stop-ThemeProcesses
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes" -Name "ThemeMRU" -Value "$((@(
"atlas-v0.4.x-dark.theme",
"atlas-v0.4.x-light.theme",
"atlas-v0.5.x-dark.theme",
"atlas-v0.5.x-light.theme",
"dark.theme",
"aero.theme"
) | ForEach-Object { "$windir\resources\Themes\$_" }) -join ';');" -Type String -Force
}
}
# Credit: https://superuser.com/a/1343640
function Set-LockscreenImage {
param (
[ValidateNotNullOrEmpty()]
[string]$Path = "$([Environment]::GetFolderPath('Windows'))\AtlasModules\Wallpapers\lockscreen_dark.png"
)
if (!(Test-Path $Path)) {
throw "Path ('$Path') for lockscreen not found."
}
$newImagePath = [System.IO.Path]::GetTempPath() + (New-Guid).Guid + [System.IO.Path]::GetExtension($Path)
Copy-Item $Path $newImagePath
# setup WinRT namespaces
Add-Type -AssemblyName System.Runtime.WindowsRuntime
[Windows.System.UserProfile.LockScreen, Windows.System.UserProfile, ContentType = WindowsRuntime] | Out-Null
# setup async
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {
$_.Name -eq 'AsTask' -and
$_.GetParameters().Count -eq 1 -and
$_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'
})[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
Function AwaitAction($WinRtAction) {
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, @($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
# make image object
[Windows.Storage.StorageFile, Windows.Storage, ContentType = WindowsRuntime] | Out-Null
$image = Await ([Windows.Storage.StorageFile]::GetFileFromPathAsync($newImagePath)) ([Windows.Storage.StorageFile])
# execute
AwaitAction ([Windows.System.UserProfile.LockScreen]::SetImageFileAsync($image))
# cleanup
Remove-Item $newImagePath
}
Export-ModuleMember -Function Set-Theme, Set-ThemeMRU, Set-LockscreenImage