Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AOT] clean up AOT issue in Settings.UI #36559

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/settings-ui/Settings.UI/Helpers/ActionMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Microsoft.PowerToys.Settings.UI.Helpers
{
public sealed class ActionMessage
{
[JsonPropertyName("action")]
public SettingsAction Action { get; set; }

public static ActionMessage Create(string actionName)
{
return new ActionMessage
{
Action = new SettingsAction
{
PublishedDate = new SettingsGeneral
{
ActionName = actionName,
},
},
};
}
}

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Those are just a define for one simple struct")]
public sealed class SettingsAction
{
[JsonPropertyName("general")]
public SettingsGeneral PublishedDate { get; set; }
}

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Those are just a define for one simple struct")]
public sealed class SettingsGeneral
{
[JsonPropertyName("action_name")]
public string ActionName { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/settings-ui/Settings.UI/Helpers/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.PowerToys.Settings.UI.Helpers
{
internal sealed class AsyncCommand : ICommand
internal sealed partial class AsyncCommand : ICommand
{
private readonly Func<Task> _execute;
private readonly Func<bool> _canExecute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IndexedItem(T item, int index)
}

#pragma warning disable SA1402 // File may only contain a single type
public class IndexedObservableCollection<T> : ObservableCollection<IndexedItem<T>>
public partial class IndexedObservableCollection<T> : ObservableCollection<IndexedItem<T>>
#pragma warning restore SA1402 // File may only contain a single type
{
public IndexedObservableCollection(IEnumerable<T> items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal struct INPUT

internal static int Size
{
get { return Marshal.SizeOf(typeof(INPUT)); }
get { return Marshal.SizeOf<INPUT>(); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is INPUT blittable? If so you should just do sizeof(INPUT). Same for all other Marshal.SizeOf uses in this PR.

}
}

Expand Down
29 changes: 29 additions & 0 deletions src/settings-ui/Settings.UI/Helpers/PowerToysReleaseInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Microsoft.PowerToys.Settings.UI.Helpers
{
// Contains information for a release. Used to deserialize release JSON info from GitHub.
public sealed class PowerToysReleaseInfo
{
[JsonPropertyName("published_at")]
public DateTimeOffset PublishedDate { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("tag_name")]
public string TagName { get; set; }

[JsonPropertyName("body")]
public string ReleaseNotes { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/settings-ui/Settings.UI/Helpers/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.PowerToys.Settings.UI.Helpers
{
public class RelayCommand : ICommand
public partial class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
Expand All @@ -33,7 +33,7 @@ public RelayCommand(Action execute, Func<bool> canExecute)
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "abstract T and abstract")]
public class RelayCommand<T> : ICommand
public partial class RelayCommand<T> : ICommand
{
private readonly Action<T> execute;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library;

namespace Microsoft.PowerToys.Settings.UI.Helpers;

[JsonSerializable(typeof(WINDOWPLACEMENT))]
[JsonSerializable(typeof(AdvancedPasteSettings))]
[JsonSerializable(typeof(Dictionary<string, List<string>>))]
[JsonSerializable(typeof(AlwaysOnTopSettings))]
[JsonSerializable(typeof(ColorPickerSettings))]
[JsonSerializable(typeof(CropAndLockSettings))]
[JsonSerializable(typeof(FileLocksmithSettings))]
[JsonSerializable(typeof(MeasureToolSettings))]
[JsonSerializable(typeof(MouseWithoutBordersSettings))]
[JsonSerializable(typeof(NewPlusSettings))]
[JsonSerializable(typeof(PeekSettings))]
[JsonSerializable(typeof(PowerLauncherSettings))]
[JsonSerializable(typeof(PowerOcrSettings))]
[JsonSerializable(typeof(RegistryPreviewSettings))]
[JsonSerializable(typeof(VideoConferenceSettings))]
[JsonSerializable(typeof(WorkspacesSettings))]
[JsonSerializable(typeof(IList<PowerToysReleaseInfo>))]
[JsonSerializable(typeof(ActionMessage))]
public sealed partial class SettingsUIJsonSerializerContext : JsonSerializerContext
{
}
6 changes: 3 additions & 3 deletions src/settings-ui/Settings.UI/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static WINDOWPLACEMENT DeserializePlacementOrDefault(IntPtr handle)
try
{
var json = File.ReadAllText(_placementPath);
var placement = JsonSerializer.Deserialize<WINDOWPLACEMENT>(json);
var placement = JsonSerializer.Deserialize<WINDOWPLACEMENT>(json, SettingsUIJsonSerializerContext.Default.WINDOWPLACEMENT);

placement.Length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
placement.Length = Marshal.SizeOf<WINDOWPLACEMENT>();
placement.Flags = 0;
placement.ShowCmd = (placement.ShowCmd == NativeMethods.SW_SHOWMAXIMIZED) ? NativeMethods.SW_SHOWMAXIMIZED : NativeMethods.SW_SHOWNORMAL;
return placement;
Expand All @@ -40,7 +40,7 @@ public static void SerializePlacement(IntPtr handle)
_ = NativeMethods.GetWindowPlacement(handle, out var placement);
try
{
var json = JsonSerializer.Serialize(placement);
var json = JsonSerializer.Serialize(placement, SettingsUIJsonSerializerContext.Default.WINDOWPLACEMENT);
File.WriteAllText(_placementPath, json);
}
catch (Exception)
Expand Down
2 changes: 1 addition & 1 deletion src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void OnLaunchedToGetSetting(string[] cmdArgs)

try
{
var requestedSettings = JsonSerializer.Deserialize<Dictionary<string, List<string>>>(File.ReadAllText(ipcFileName));
var requestedSettings = JsonSerializer.Deserialize<Dictionary<string, List<string>>>(File.ReadAllText(ipcFileName), SettingsUIJsonSerializerContext.Default.DictionaryStringListString);
File.WriteAllText(ipcFileName, GetSettingCommandLineCommand.Execute(requestedSettings));
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,6 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
{
public sealed partial class OobeWhatsNew : Page
{
// Contains information for a release. Used to deserialize release JSON info from GitHub.
private sealed class PowerToysReleaseInfo
{
[JsonPropertyName("published_at")]
public DateTimeOffset PublishedDate { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("tag_name")]
public string TagName { get; set; }

[JsonPropertyName("body")]
public string ReleaseNotes { get; set; }
}

public OobePowerToysModule ViewModel { get; set; }

public bool ShowDataDiagnosticsInfoBar => GetShowDataDiagnosticsInfoBar();
Expand Down Expand Up @@ -111,7 +95,7 @@ private static async Task<string> GetReleaseNotesMarkdown()
// https://docs.github.com/rest/overview/resources-in-the-rest-api#user-agent-required
getReleaseInfoClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "PowerToys");
releaseNotesJSON = await getReleaseInfoClient.GetStringAsync("https://api.github.com/repos/microsoft/PowerToys/releases");
IList<PowerToysReleaseInfo> releases = JsonSerializer.Deserialize<IList<PowerToysReleaseInfo>>(releaseNotesJSON);
IList<PowerToysReleaseInfo> releases = JsonSerializer.Deserialize<IList<PowerToysReleaseInfo>>(releaseNotesJSON, SettingsUIJsonSerializerContext.Default.IListPowerToysReleaseInfo);

// Get the latest releases
var latestReleases = releases.OrderByDescending(release => release.PublishedDate).Take(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Timers;

using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
Expand All @@ -23,7 +24,7 @@

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class AdvancedPasteViewModel : Observable, IDisposable
public partial class AdvancedPasteViewModel : Observable, IDisposable
{
private static readonly HashSet<string> WarnHotkeys = ["Ctrl + V", "Ctrl + Shift + V"];

Expand Down Expand Up @@ -387,7 +388,7 @@ private void NotifySettingsChanged()
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
AdvancedPasteSettings.ModuleName,
JsonSerializer.Serialize(_advancedPasteSettings)));
JsonSerializer.Serialize(_advancedPasteSettings, SettingsUIJsonSerializerContext.Default.AdvancedPasteSettings)));
}

public void RefreshEnabledState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

using Common.UI;
using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class AlwaysOnTopViewModel : Observable
public partial class AlwaysOnTopViewModel : Observable
{
private ISettingsUtils SettingsUtils { get; set; }

Expand Down Expand Up @@ -132,7 +133,7 @@ public HotkeySettings Hotkey
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
AlwaysOnTopSettings.ModuleName,
JsonSerializer.Serialize(Settings)));
JsonSerializer.Serialize(Settings, SettingsUIJsonSerializerContext.Default.AlwaysOnTopSettings)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/settings-ui/Settings.UI/ViewModels/AwakeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class AwakeViewModel : Observable
public partial class AwakeViewModel : Observable
{
public AwakeViewModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class CmdNotFoundViewModel : Observable
public partial class CmdNotFoundViewModel : Observable
{
public ButtonClickCommand CheckRequirementsEventHandler => new ButtonClickCommand(CheckCommandNotFoundRequirements);

Expand All @@ -38,10 +38,7 @@ public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().Location;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
return AppContext.BaseDirectory;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

using global::PowerToys.GPOWrapper;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Enumerations;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class ColorPickerViewModel : Observable, IDisposable
public partial class ColorPickerViewModel : Observable, IDisposable
{
private bool disposedValue;

Expand Down Expand Up @@ -362,7 +363,7 @@ private void NotifySettingsChanged()
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
ColorPickerSettings.ModuleName,
JsonSerializer.Serialize(_colorPickerSettings)));
JsonSerializer.Serialize(_colorPickerSettings, SettingsUIJsonSerializerContext.Default.ColorPickerSettings)));
}

public void RefreshEnabledState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.PowerToys.Settings.UI.ViewModels.Commands
{
public class ButtonClickCommand : ICommand
public partial class ButtonClickCommand : ICommand
{
private readonly Action _execute;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using System.Text.Json;

using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class CropAndLockViewModel : Observable
public partial class CropAndLockViewModel : Observable
{
private ISettingsUtils SettingsUtils { get; set; }

Expand Down Expand Up @@ -122,7 +123,7 @@ public HotkeySettings ReparentActivationShortcut
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
CropAndLockSettings.ModuleName,
JsonSerializer.Serialize(Settings)));
JsonSerializer.Serialize(Settings, SettingsUIJsonSerializerContext.Default.CropAndLockSettings)));
}
}
}
Expand Down Expand Up @@ -153,7 +154,7 @@ public HotkeySettings ThumbnailActivationShortcut
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
CropAndLockSettings.ModuleName,
JsonSerializer.Serialize(Settings)));
JsonSerializer.Serialize(Settings, SettingsUIJsonSerializerContext.Default.CropAndLockSettings)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class DashboardListItem : INotifyPropertyChanged
public partial class DashboardListItem : INotifyPropertyChanged
{
private bool _visible;
private bool _isEnabled;
Expand Down
Loading
Loading