ProfileEditorDialog.xaml.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 #nullable enable 6 7 using System.Collections.ObjectModel; 8 using System.Linq; 9 using Microsoft.PowerToys.Settings.UI.Helpers; 10 using Microsoft.PowerToys.Settings.UI.Library; 11 using Microsoft.PowerToys.Settings.UI.ViewModels; 12 using Microsoft.UI.Xaml; 13 using Microsoft.UI.Xaml.Controls; 14 using PowerDisplay.Common.Models; 15 16 namespace Microsoft.PowerToys.Settings.UI.Views 17 { 18 /// <summary> 19 /// Dialog for creating/editing PowerDisplay profiles 20 /// </summary> 21 public sealed partial class ProfileEditorDialog : ContentDialog 22 { 23 public ProfileEditorViewModel ViewModel { get; private set; } 24 25 public PowerDisplayProfile? ResultProfile { get; private set; } 26 27 public ProfileEditorDialog(ObservableCollection<MonitorInfo> availableMonitors, string defaultName = "") 28 { 29 this.InitializeComponent(); 30 ViewModel = new ProfileEditorViewModel(availableMonitors, defaultName); 31 32 // Set localized strings for ContentDialog 33 var resourceLoader = ResourceLoaderInstance.ResourceLoader; 34 Title = resourceLoader.GetString("PowerDisplay_ProfileEditor_Title"); 35 PrimaryButtonText = resourceLoader.GetString("PowerDisplay_Dialog_Save"); 36 CloseButtonText = resourceLoader.GetString("PowerDisplay_Dialog_Cancel"); 37 } 38 39 private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) 40 { 41 if (ViewModel.CanSave) 42 { 43 ResultProfile = ViewModel.CreateProfile(); 44 } 45 } 46 47 private void ContentDialog_CloseButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) 48 { 49 ResultProfile = null; 50 } 51 52 /// <summary> 53 /// Pre-fill the dialog with existing profile data 54 /// </summary> 55 public void PreFillProfile(PowerDisplayProfile profile) 56 { 57 if (profile == null || ViewModel == null) 58 { 59 return; 60 } 61 62 // Set profile name 63 ViewModel.ProfileName = profile.Name; 64 65 // Pre-fill monitor settings from existing profile 66 foreach (var monitorSetting in profile.MonitorSettings) 67 { 68 var monitorItem = ViewModel.Monitors.FirstOrDefault(m => m.Monitor.Id == monitorSetting.MonitorId); 69 if (monitorItem != null) 70 { 71 monitorItem.IsSelected = true; 72 73 // Set brightness if included in profile 74 if (monitorSetting.Brightness.HasValue) 75 { 76 monitorItem.IncludeBrightness = true; 77 monitorItem.Brightness = monitorSetting.Brightness.Value; 78 } 79 80 // Set color temperature if included in profile 81 if (monitorSetting.ColorTemperatureVcp.HasValue) 82 { 83 monitorItem.IncludeColorTemperature = true; 84 monitorItem.ColorTemperature = monitorSetting.ColorTemperatureVcp.Value; 85 } 86 87 // Set contrast if included in profile 88 if (monitorSetting.Contrast.HasValue) 89 { 90 monitorItem.IncludeContrast = true; 91 monitorItem.Contrast = monitorSetting.Contrast.Value; 92 } 93 94 // Set volume if included in profile 95 if (monitorSetting.Volume.HasValue) 96 { 97 monitorItem.IncludeVolume = true; 98 monitorItem.Volume = monitorSetting.Volume.Value; 99 } 100 } 101 } 102 } 103 } 104 }