| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using CommunityToolkit.WinUI.Controls; |
| 4 | using Microsoft.UI.Xaml.Controls; |
| 5 | using Microsoft.UI.Xaml.Input; |
| 6 | using System.Diagnostics; |
| 7 | using System.Reflection; |
| 8 | using System.Runtime.InteropServices; |
| 9 | using System.Text; |
| 10 | using Windows.ApplicationModel; |
| 11 | using Windows.Storage.Pickers; |
| 12 | |
| 13 | namespace WslSettings.Helpers; |
| 14 | |
| 15 | public class RuntimeHelper |
| 16 | { |
| 17 | [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
| 18 | private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder? packageFullName); |
| 19 | |
| 20 | public static Windows.Foundation.IAsyncOperation<Windows.Storage.StorageFile> PickSingleFileAsync(List<string>? fileTypeFilters = null) |
| 21 | { |
| 22 | // Create a file picker |
| 23 | var openPicker = new FileOpenPicker(); |
| 24 | var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow); |
| 25 | |
| 26 | // Initialize the file picker with the window handle (HWND). |
| 27 | WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hWnd); |
| 28 | |
| 29 | fileTypeFilters ??= ["*"]; |
| 30 | |
| 31 | // Set options for your file picker |
| 32 | openPicker.ViewMode = PickerViewMode.List; |
| 33 | foreach (string fileTypeFilter in fileTypeFilters) |
| 34 | { |
| 35 | openPicker.FileTypeFilter.Add(fileTypeFilter); |
| 36 | } |
| 37 | |
| 38 | // Open the picker for the user to pick a file |
| 39 | return openPicker.PickSingleFileAsync(); |
| 40 | } |
| 41 | |
| 42 | public static void TryMoveFocusPreviousControl(Button? button) |
| 43 | { |
| 44 | if (button == null) |
| 45 | { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | FindNextElementOptions fneo = new() { SearchRoot = button.XamlRoot.Content }; |
| 50 | FocusManager.TryMoveFocus(FocusNavigationDirection.Previous, fneo); |
| 51 | } |
| 52 | |
| 53 | public static void SetupSettingsExpanderFocusManagement(Microsoft.UI.Xaml.FrameworkElement expander, Microsoft.UI.Xaml.Controls.Control firstFocusableElement) |
| 54 | { |
| 55 | if (expander is CommunityToolkit.WinUI.Controls.SettingsExpander settingsExpander) |
| 56 | { |
| 57 | settingsExpander.RegisterPropertyChangedCallback(CommunityToolkit.WinUI.Controls.SettingsExpander.IsExpandedProperty, (sender, dp) => |
| 58 | { |
| 59 | if (sender is CommunityToolkit.WinUI.Controls.SettingsExpander se && se.IsExpanded) |
| 60 | { |
| 61 | System.EventHandler<object>? layoutHandler = null; |
| 62 | layoutHandler = (s, e) => |
| 63 | { |
| 64 | se.LayoutUpdated -= layoutHandler; |
| 65 | firstFocusableElement.Focus(Microsoft.UI.Xaml.FocusState.Keyboard); |
| 66 | }; |
| 67 | |
| 68 | se.LayoutUpdated += layoutHandler; |
| 69 | } |
| 70 | }); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public static void SetupExpanderFocusManagementByName(Microsoft.UI.Xaml.FrameworkElement parent, string expanderName, string textBoxName) |
| 75 | { |
| 76 | var expander = parent.FindName(expanderName) as Microsoft.UI.Xaml.FrameworkElement; |
| 77 | var textBox = parent.FindName(textBoxName) as Microsoft.UI.Xaml.Controls.Control; |
| 78 | |
| 79 | Debug.Assert(expander != null, $"Expander '{expanderName}' not found"); |
| 80 | Debug.Assert(textBox != null, $"TextBox '{textBoxName}' not found"); |
| 81 | |
| 82 | if (expander != null && textBox != null) |
| 83 | { |
| 84 | SetupSettingsExpanderFocusManagement(expander, textBox); |
| 85 | } |
| 86 | } |
| 87 | } |