mirror of
https://github.com/terrymacdonald/DisplayMagician.git
synced 2024-08-30 18:32:20 +00:00
Moved RunProfile to ProfileRepository
It makes WAY more sense here.
This commit is contained in:
parent
5eea1f0a95
commit
293ca14730
@ -24,13 +24,6 @@ using DisplayMagicianShared.Windows;
|
|||||||
|
|
||||||
namespace DisplayMagician {
|
namespace DisplayMagician {
|
||||||
|
|
||||||
public enum ApplyProfileResult
|
|
||||||
{
|
|
||||||
Successful,
|
|
||||||
Cancelled,
|
|
||||||
Error
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
internal static string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician");
|
internal static string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician");
|
||||||
@ -295,10 +288,7 @@ namespace DisplayMagician {
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Lookup the profile
|
RunProfile(argumentProfile.Value);
|
||||||
ProfileItem profileToUse = ProfileRepository.AllProfiles.Where(p => p.UUID.Equals(argumentProfile.Value)).First();
|
|
||||||
|
|
||||||
ApplyProfile(profileToUse);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -568,73 +558,17 @@ namespace DisplayMagician {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyProfile lives here so that the UI works.
|
public static void RunProfile(string profileName)
|
||||||
public static ApplyProfileResult ApplyProfile(ProfileItem profile)
|
|
||||||
{
|
{
|
||||||
logger.Trace($"Program/ApplyProfile: Starting");
|
logger.Trace($"Program/RunProfile: Starting");
|
||||||
NVIDIAProfileItem nvidiaProfile = null;
|
|
||||||
AMDProfileItem amdProfile = null;
|
|
||||||
WinProfileItem winProfile = null;
|
|
||||||
|
|
||||||
if (profile == null)
|
// Lookup the profile
|
||||||
{
|
ProfileItem profileToUse = ProfileRepository.AllProfiles.Where(p => p.UUID.Equals(profileName)).First();
|
||||||
logger.Debug($"Program/ApplyProfile: The supplied profile is null! Can't be used.");
|
logger.Trace($"Program/RunProfile: Found profile called {profileName} and now starting to apply the profile");
|
||||||
return ApplyProfileResult.Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
ProfileRepository.ApplyProfile(profileToUse);
|
||||||
{
|
|
||||||
// We try to swap profiles. The profiles have checking logic in them
|
|
||||||
if (profile is NVIDIAProfileItem)
|
|
||||||
{
|
|
||||||
logger.Trace($"Program/ApplyProfile: Profile is an NVIDIA Profile, so changing type to NVIDIAProfileItem");
|
|
||||||
nvidiaProfile = (NVIDIAProfileItem)profile;
|
|
||||||
if (!nvidiaProfile.SetActive())
|
|
||||||
{
|
|
||||||
logger.Error($"Program/ApplyProfile: Error applying the NVIDIA Profile!");
|
|
||||||
return ApplyProfileResult.Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (profile is AMDProfileItem)
|
|
||||||
{
|
|
||||||
logger.Trace($"Program/ApplyProfile: Profile is an AMD Profile, so changing type to AMDProfileItem");
|
|
||||||
amdProfile = (AMDProfileItem)profile;
|
|
||||||
if (!amdProfile.SetActive())
|
|
||||||
{
|
|
||||||
logger.Error($"Program/ApplyProfile: Error applying the AMD Profile!");
|
|
||||||
return ApplyProfileResult.Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (profile is WinProfileItem)
|
|
||||||
{
|
|
||||||
logger.Trace($"Program/ApplyProfile: Profile is a Windows CCD Profile, so changing type to WinProfileItem");
|
|
||||||
winProfile = (WinProfileItem)profile;
|
|
||||||
if (!winProfile.SetActive())
|
|
||||||
{
|
|
||||||
// Somehow return that this profile topology didn't apply
|
|
||||||
throw new ApplyTopologyException("Program/ApplyProfile: amdApplyProfileTask: Error applying the AMD Profile!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.Trace($"Program/ApplyProfile: Profile type is not one that is supported by DisplayMagician, so returning an ApplyProfileResult error");
|
|
||||||
return ApplyProfileResult.Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"ProfileRepository/ApplyTopology exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
|
|
||||||
{
|
|
||||||
logger.Debug($"Program/ApplyProfile: Failed to complete changing the Windows Display layout");
|
|
||||||
return ApplyProfileResult.Error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ProfileRepository.UpdateActiveProfile();
|
|
||||||
|
|
||||||
return ApplyProfileResult.Successful;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool LoadGamesInBackground()
|
public static bool LoadGamesInBackground()
|
||||||
{
|
{
|
||||||
@ -867,33 +801,6 @@ namespace DisplayMagician {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class ApplyTopologyException : Exception
|
|
||||||
{
|
|
||||||
public ApplyTopologyException()
|
|
||||||
{ }
|
|
||||||
|
|
||||||
public ApplyTopologyException(string message) : base(message)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
public ApplyTopologyException(string message, Exception innerException) : base(message, innerException)
|
|
||||||
{ }
|
|
||||||
public ApplyTopologyException(SerializationInfo info, StreamingContext context) : base(info, context)
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ApplyPathInfoException : Exception
|
|
||||||
{
|
|
||||||
public ApplyPathInfoException()
|
|
||||||
{ }
|
|
||||||
|
|
||||||
public ApplyPathInfoException(string message) : base(message)
|
|
||||||
{ }
|
|
||||||
public ApplyPathInfoException(string message, Exception innerException) : base(message, innerException)
|
|
||||||
{ }
|
|
||||||
public ApplyPathInfoException(SerializationInfo info, StreamingContext context) : base(info, context)
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class LoadingInstalledGamesException : Exception
|
public class LoadingInstalledGamesException : Exception
|
||||||
{
|
{
|
||||||
public LoadingInstalledGamesException()
|
public LoadingInstalledGamesException()
|
||||||
|
@ -573,7 +573,7 @@ namespace DisplayMagician
|
|||||||
{
|
{
|
||||||
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {rollbackProfile.Name} profile.");
|
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {rollbackProfile.Name} profile.");
|
||||||
// Apply the Profile!
|
// Apply the Profile!
|
||||||
ApplyProfileResult result = Program.ApplyProfile(shortcutToUse.ProfileToUse);
|
ApplyProfileResult result = ProfileRepository.ApplyProfile(shortcutToUse.ProfileToUse);
|
||||||
if (result == ApplyProfileResult.Error)
|
if (result == ApplyProfileResult.Error)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"ERROR - Cannot apply '{shortcutToUse.ProfileToUse.Name}' Display Profile");
|
Console.WriteLine($"ERROR - Cannot apply '{shortcutToUse.ProfileToUse.Name}' Display Profile");
|
||||||
@ -1724,7 +1724,7 @@ namespace DisplayMagician
|
|||||||
{
|
{
|
||||||
logger.Debug($"ShortcutRepository/RunShortcut: Rolling back display profile to {rollbackProfile.Name}");
|
logger.Debug($"ShortcutRepository/RunShortcut: Rolling back display profile to {rollbackProfile.Name}");
|
||||||
|
|
||||||
ApplyProfileResult result = Program.ApplyProfile(rollbackProfile);
|
ApplyProfileResult result = ProfileRepository.ApplyProfile(rollbackProfile);
|
||||||
|
|
||||||
if (result == ApplyProfileResult.Error)
|
if (result == ApplyProfileResult.Error)
|
||||||
{
|
{
|
||||||
|
@ -404,7 +404,7 @@ namespace DisplayMagician.UIForms
|
|||||||
|
|
||||||
// Run the shortcut if it's still there
|
// Run the shortcut if it's still there
|
||||||
if (profileToRun != null)
|
if (profileToRun != null)
|
||||||
Program.ApplyProfile(profileToRun);
|
ProfileRepository.ApplyProfile(profileToRun);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -652,7 +652,7 @@ namespace DisplayMagician.UIForms
|
|||||||
string displayProfileUUID = e.Name;
|
string displayProfileUUID = e.Name;
|
||||||
ProfileItem chosenProfile = ProfileRepository.GetProfile(displayProfileUUID);
|
ProfileItem chosenProfile = ProfileRepository.GetProfile(displayProfileUUID);
|
||||||
if (chosenProfile is ProfileItem)
|
if (chosenProfile is ProfileItem)
|
||||||
Program.ApplyProfile(chosenProfile);
|
ProfileRepository.ApplyProfile(chosenProfile);
|
||||||
}
|
}
|
||||||
else if (hotkeyShortcuts.Contains(e.Name))
|
else if (hotkeyShortcuts.Contains(e.Name))
|
||||||
{
|
{
|
||||||
|
@ -57,6 +57,7 @@
|
|||||||
<Compile Include="NVIDIA\NVAPI.cs" />
|
<Compile Include="NVIDIA\NVAPI.cs" />
|
||||||
<Compile Include="NVIDIA\NVIDIALibrary.cs" />
|
<Compile Include="NVIDIA\NVIDIALibrary.cs" />
|
||||||
<Compile Include="NVIDIA\NVIDIAProfileItem.cs" />
|
<Compile Include="NVIDIA\NVIDIAProfileItem.cs" />
|
||||||
|
<Compile Include="NVIDIA\PInvokeDelegateFactory.cs" />
|
||||||
<Compile Include="SharedLogger.cs" />
|
<Compile Include="SharedLogger.cs" />
|
||||||
<Compile Include="Properties\Resources.Designer.cs">
|
<Compile Include="Properties\Resources.Designer.cs">
|
||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
|
@ -1086,7 +1086,7 @@ namespace DisplayMagicianShared.NVIDIA
|
|||||||
// Now, we have the current HDR settings, and the existing HDR settings, so we go through and we attempt to set each display color settings
|
// Now, we have the current HDR settings, and the existing HDR settings, so we go through and we attempt to set each display color settings
|
||||||
foreach (var wantedHdrColorData in displayConfig.HdrConfig.HdrColorData)
|
foreach (var wantedHdrColorData in displayConfig.HdrConfig.HdrColorData)
|
||||||
{
|
{
|
||||||
// If we have HDR settings for the display, then attempt to set them
|
// If we have HDR settings stored for the display, then attempt to set them
|
||||||
if (currentDisplayConfig.HdrConfig.HdrColorData.ContainsKey(wantedHdrColorData.Key))
|
if (currentDisplayConfig.HdrConfig.HdrColorData.ContainsKey(wantedHdrColorData.Key))
|
||||||
{
|
{
|
||||||
// Now we set the HDR colour settings of the display
|
// Now we set the HDR colour settings of the display
|
||||||
|
@ -10,6 +10,7 @@ using System.Text.RegularExpressions;
|
|||||||
using DisplayMagicianShared.AMD;
|
using DisplayMagicianShared.AMD;
|
||||||
using DisplayMagicianShared.NVIDIA;
|
using DisplayMagicianShared.NVIDIA;
|
||||||
using DisplayMagicianShared.Windows;
|
using DisplayMagicianShared.Windows;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace DisplayMagicianShared
|
namespace DisplayMagicianShared
|
||||||
{
|
{
|
||||||
@ -24,6 +25,13 @@ namespace DisplayMagicianShared
|
|||||||
AMD = 2,
|
AMD = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum ApplyProfileResult
|
||||||
|
{
|
||||||
|
Successful,
|
||||||
|
Cancelled,
|
||||||
|
Error
|
||||||
|
}
|
||||||
|
|
||||||
public static class ProfileRepository
|
public static class ProfileRepository
|
||||||
{
|
{
|
||||||
#region Class Variables
|
#region Class Variables
|
||||||
@ -973,6 +981,74 @@ namespace DisplayMagicianShared
|
|||||||
return uncheckedFilename;
|
return uncheckedFilename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ApplyProfile lives here so that the UI works.
|
||||||
|
public static ApplyProfileResult ApplyProfile(ProfileItem profile)
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Trace($"Program/ApplyProfile: Starting");
|
||||||
|
NVIDIAProfileItem nvidiaProfile = null;
|
||||||
|
AMDProfileItem amdProfile = null;
|
||||||
|
WinProfileItem winProfile = null;
|
||||||
|
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Debug($"Program/ApplyProfile: The supplied profile is null! Can't be used.");
|
||||||
|
return ApplyProfileResult.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// We try to swap profiles. The profiles have checking logic in them
|
||||||
|
if (profile is NVIDIAProfileItem)
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Trace($"Program/ApplyProfile: Profile is an NVIDIA Profile, so changing type to NVIDIAProfileItem");
|
||||||
|
nvidiaProfile = (NVIDIAProfileItem)profile;
|
||||||
|
if (!nvidiaProfile.SetActive())
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Error($"Program/ApplyProfile: Error applying the NVIDIA Profile!");
|
||||||
|
return ApplyProfileResult.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (profile is AMDProfileItem)
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Trace($"Program/ApplyProfile: Profile is an AMD Profile, so changing type to AMDProfileItem");
|
||||||
|
amdProfile = (AMDProfileItem)profile;
|
||||||
|
if (!amdProfile.SetActive())
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Error($"Program/ApplyProfile: Error applying the AMD Profile!");
|
||||||
|
return ApplyProfileResult.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (profile is WinProfileItem)
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Trace($"Program/ApplyProfile: Profile is a Windows CCD Profile, so changing type to WinProfileItem");
|
||||||
|
winProfile = (WinProfileItem)profile;
|
||||||
|
if (!winProfile.SetActive())
|
||||||
|
{
|
||||||
|
// Somehow return that this profile topology didn't apply
|
||||||
|
throw new ApplyTopologyException("Program/ApplyProfile: amdApplyProfileTask: Error applying the AMD Profile!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Trace($"Program/ApplyProfile: Profile type is not one that is supported by DisplayMagician, so returning an ApplyProfileResult error");
|
||||||
|
return ApplyProfileResult.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"ProfileRepository/ApplyTopology exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
|
||||||
|
{
|
||||||
|
SharedLogger.logger.Debug($"Program/ApplyProfile: Failed to complete changing the Windows Display layout");
|
||||||
|
return ApplyProfileResult.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ProfileRepository.UpdateActiveProfile();
|
||||||
|
|
||||||
|
return ApplyProfileResult.Successful;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -990,5 +1066,18 @@ namespace DisplayMagicianShared
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class ApplyTopologyException : Exception
|
||||||
|
{
|
||||||
|
public ApplyTopologyException()
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public ApplyTopologyException(string message) : base(message)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public ApplyTopologyException(string message, Exception innerException) : base(message, innerException)
|
||||||
|
{ }
|
||||||
|
public ApplyTopologyException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
564
displaymagician/uiforms/displayprofileform.cs
Normal file
564
displaymagician/uiforms/displayprofileform.cs
Normal file
@ -0,0 +1,564 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using DisplayMagician.Resources;
|
||||||
|
using DisplayMagicianShared;
|
||||||
|
using Manina.Windows.Forms;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
//using WK.Libraries.HotkeyListenerNS;
|
||||||
|
using NHotkey.WindowsForms;
|
||||||
|
using NHotkey;
|
||||||
|
|
||||||
|
namespace DisplayMagician.UIForms
|
||||||
|
{
|
||||||
|
internal partial class DisplayProfileForm : Form
|
||||||
|
{
|
||||||
|
private ProfileItem _selectedProfile;
|
||||||
|
//private List<ProfileItem> _savedProfiles = new List<ProfileItem>();
|
||||||
|
private string _saveOrRenameMode = "save";
|
||||||
|
//private static bool _inDialog = false;
|
||||||
|
private static ProfileItem _profileToLoad = null;
|
||||||
|
private ProfileAdaptor _profileAdaptor = new ProfileAdaptor();
|
||||||
|
//public static Dictionary<string, bool> profileValidity = new Dictionary<string, bool>();
|
||||||
|
|
||||||
|
public DisplayProfileForm()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
this.AcceptButton = this.btn_save_or_rename;
|
||||||
|
ilv_saved_profiles.MultiSelect = false;
|
||||||
|
ilv_saved_profiles.ThumbnailSize = new Size(100, 100);
|
||||||
|
ilv_saved_profiles.AllowDrag = false;
|
||||||
|
ilv_saved_profiles.AllowDrop = false;
|
||||||
|
ilv_saved_profiles.SetRenderer(new ProfileILVRenderer());
|
||||||
|
}
|
||||||
|
|
||||||
|
public DisplayProfileForm(ProfileItem profileToLoad) : this()
|
||||||
|
{
|
||||||
|
_profileToLoad = profileToLoad;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Apply_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_selectedProfile == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!_selectedProfile.IsPossible)
|
||||||
|
{
|
||||||
|
MessageBox.Show(this, Language.This_profile_is_currently_impossible_to_apply,
|
||||||
|
Language.Apply_Profile,
|
||||||
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the Profile
|
||||||
|
if (ProfileRepository.ApplyProfile(_selectedProfile) == ApplyProfileResult.Successful)
|
||||||
|
{
|
||||||
|
ChangeSelectedProfile(_selectedProfile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Exit_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
DialogResult = DialogResult.Cancel;
|
||||||
|
this.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Delete_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_selectedProfile == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (MessageBox.Show($"Are you sure you want to delete the '{_selectedProfile.Name}' Display Profile?", $"Delete '{_selectedProfile.Name}' Display Profile?", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// remove the profile from the imagelistview
|
||||||
|
int currentIlvIndex = ilv_saved_profiles.SelectedItems[0].Index;
|
||||||
|
ilv_saved_profiles.Items.RemoveAt(currentIlvIndex);
|
||||||
|
|
||||||
|
// Remove the Profile
|
||||||
|
ProfileRepository.RemoveProfile(_selectedProfile);
|
||||||
|
_selectedProfile = null;
|
||||||
|
|
||||||
|
// If the imageview isn't empty
|
||||||
|
if (ilv_saved_profiles.Items.Count > 0)
|
||||||
|
{
|
||||||
|
// set the new selected profile as the next one in the imagelistview
|
||||||
|
// or the new end one if we deleted the last one before
|
||||||
|
int ilvItemToSelect = currentIlvIndex;
|
||||||
|
if (ilv_saved_profiles.Items.Count < currentIlvIndex + 1)
|
||||||
|
ilvItemToSelect = ilv_saved_profiles.Items.Count - 1;
|
||||||
|
|
||||||
|
// Set the nearest profile image as selected
|
||||||
|
ilv_saved_profiles.Items[ilvItemToSelect].Selected = true;
|
||||||
|
|
||||||
|
// select the
|
||||||
|
foreach (ProfileItem newSelectedProfile in ProfileRepository.AllProfiles)
|
||||||
|
{
|
||||||
|
if (newSelectedProfile.UUID.Equals(ilv_saved_profiles.Items[ilvItemToSelect].EquipmentModel))
|
||||||
|
{
|
||||||
|
ChangeSelectedProfile(newSelectedProfile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We now only have an unsaved current profile, and no saved ones
|
||||||
|
// So we need to change the mode
|
||||||
|
ChangeSelectedProfile(ProfileRepository.CurrentProfile);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Save_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
//DialogResult = DialogResult.None;
|
||||||
|
|
||||||
|
// Only do something if there is a shortcut selected
|
||||||
|
if (_selectedProfile != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
// if shortcut is not valid then ask if the user
|
||||||
|
// really wants to save it to desktop
|
||||||
|
if (!_selectedProfile.IsPossible)
|
||||||
|
{
|
||||||
|
// We ask the user of they still want to save the desktop shortcut
|
||||||
|
if (MessageBox.Show($"The '{_selectedProfile.Name}' Desktop Profile isn't possible to use right now so a desktop shortcut wouldn't work until your Displays are changed to match the profile. Has your hardware or screen layout changed from when the Display Profile was made? Do you still want to save the desktop shortcut?", $"Still save the '{_selectedProfile.Name}' Display Profile?", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Set the profile save folder to the Desktop as that's where people will want it most likely
|
||||||
|
dialog_save.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
|
||||||
|
// Try to set up some sensible suggestions for the profile name
|
||||||
|
dialog_save.FileName = _selectedProfile.Name;
|
||||||
|
|
||||||
|
// Show the Save Profile window
|
||||||
|
if (dialog_save.ShowDialog(this) == DialogResult.OK)
|
||||||
|
{
|
||||||
|
if (_selectedProfile.CreateShortcut(dialog_save.FileName))
|
||||||
|
{
|
||||||
|
MessageBox.Show(
|
||||||
|
String.Format(Language.Shortcut_placed_successfully, dialog_save.FileName),
|
||||||
|
Language.Shortcut,
|
||||||
|
MessageBoxButtons.OK,
|
||||||
|
MessageBoxIcon.Information);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show(
|
||||||
|
Language.Failed_to_create_the_shortcut_Unexpected_exception_occurred,
|
||||||
|
Language.Shortcut,
|
||||||
|
MessageBoxButtons.OK,
|
||||||
|
MessageBoxIcon.Exclamation);
|
||||||
|
}
|
||||||
|
|
||||||
|
dialog_save.FileName = string.Empty;
|
||||||
|
//DialogResult = DialogResult.OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show(ex.Message, Language.Shortcut, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void RefreshDisplayProfileUI()
|
||||||
|
{
|
||||||
|
|
||||||
|
ImageListViewItem newItem = null;
|
||||||
|
|
||||||
|
// Temporarily stop updating the saved_profiles listview
|
||||||
|
// To stop the display showing all sorts of changes happening
|
||||||
|
ilv_saved_profiles.SuspendLayout();
|
||||||
|
|
||||||
|
// Figure out if anything is selected at the moment
|
||||||
|
// and if it is save it to reselect it after the refresh
|
||||||
|
// We only take the first as there is only one thing selected at a time
|
||||||
|
/*string lastSelectedItemName = "";
|
||||||
|
if (ilv_saved_profiles.SelectedItems.Count > 0)
|
||||||
|
lastSelectedItemName = ilv_saved_profiles.SelectedItems[0].Text;
|
||||||
|
*/
|
||||||
|
// Empty the imageListView
|
||||||
|
ilv_saved_profiles.Items.Clear();
|
||||||
|
|
||||||
|
//IOrderedEnumerable<ProfileItem> orderedProfiles = ProfileRepository.AllProfiles.OrderBy(p => p.Name);
|
||||||
|
|
||||||
|
// Check if the last selected profile is still in the list of profiles
|
||||||
|
//bool lastSelectedItemStillThere = (from profile in orderedProfiles select profile.Name).Contains(lastSelectedItemName);
|
||||||
|
|
||||||
|
// Fill it back up with the Profiles we have
|
||||||
|
foreach (ProfileItem profile in ProfileRepository.AllProfiles.OrderBy(p => p.Name))
|
||||||
|
{
|
||||||
|
// Create a new ImageListViewItem from the profile
|
||||||
|
newItem = new ImageListViewItem(profile, profile.Name);
|
||||||
|
|
||||||
|
// if the item was removed from the list during this
|
||||||
|
// list refresh, then we select this profile only if it
|
||||||
|
// is the currently used Profile
|
||||||
|
if (_selectedProfile is ProfileItem && _selectedProfile.Equals(profile))
|
||||||
|
newItem.Selected = true;
|
||||||
|
|
||||||
|
|
||||||
|
//ProfileRepository.ProfileValidityLookup[profile.Name] = profile.IsPossible;
|
||||||
|
|
||||||
|
// Add it to the list!
|
||||||
|
ilv_saved_profiles.Items.Add(newItem, _profileAdaptor);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restart updating the saved_profiles listview
|
||||||
|
ilv_saved_profiles.ResumeLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*private void DisplayProfileForm_Activated(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
// We handle the UI updating in DisplayProfileForm_Activated so that
|
||||||
|
// the app will check for changes to the current profile when the
|
||||||
|
// user clicks back to this app. This is designed to allow people to
|
||||||
|
// alter their Windows Display settings then come back to our app
|
||||||
|
// and the app will automatically recognise that things have changed.
|
||||||
|
|
||||||
|
// Reload the profiles in case we swapped to another program to change it
|
||||||
|
ChangeSelectedProfile(ProfileRepository.CurrentProfile);
|
||||||
|
// Refresh the Profile UI
|
||||||
|
RefreshDisplayProfileUI();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
private void DisplayProfileForm_Load(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Refresh the profiles to see whats valid
|
||||||
|
ProfileRepository.IsPossibleRefresh();
|
||||||
|
|
||||||
|
// Update the Current Profile
|
||||||
|
ProfileRepository.UpdateActiveProfile();
|
||||||
|
|
||||||
|
ChangeSelectedProfile(ProfileRepository.CurrentProfile);
|
||||||
|
|
||||||
|
// Refresh the Profile UI
|
||||||
|
RefreshDisplayProfileUI();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void ChangeSelectedProfile(ProfileItem profile)
|
||||||
|
{
|
||||||
|
// And we need to update the actual selected profile too!
|
||||||
|
_selectedProfile = profile;
|
||||||
|
|
||||||
|
// We also need to load the saved profile name to show the user
|
||||||
|
lbl_profile_shown.Text = _selectedProfile.Name;
|
||||||
|
|
||||||
|
// And show the logo for the driver
|
||||||
|
if (_selectedProfile.VideoMode == VIDEO_MODE.NVIDIA)
|
||||||
|
{
|
||||||
|
pbLogo.Image = PickBitmapBasedOnBgColour(BackColor, Properties.Resources.nvidiablack, Properties.Resources.nvidiawhite);
|
||||||
|
}
|
||||||
|
else if (_selectedProfile.VideoMode == VIDEO_MODE.AMD)
|
||||||
|
{
|
||||||
|
pbLogo.Image = PickBitmapBasedOnBgColour(BackColor, Properties.Resources.amdblack, Properties.Resources.amdwhite);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pbLogo.Image = PickBitmapBasedOnBgColour(BackColor, Properties.Resources.winblack, Properties.Resources.winwhite);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And update the save/rename textbox
|
||||||
|
txt_profile_save_name.Text = _selectedProfile.Name;
|
||||||
|
|
||||||
|
if (ProfileRepository.ContainsProfile(profile))
|
||||||
|
{
|
||||||
|
// we already have the profile stored
|
||||||
|
_saveOrRenameMode = "rename";
|
||||||
|
btn_save_or_rename.Text = "Rename To";
|
||||||
|
lbl_save_profile.Visible = false;
|
||||||
|
if (!_selectedProfile.IsPossible)
|
||||||
|
{
|
||||||
|
lbl_profile_shown_subtitle.Text = "This Display Profile can't be used as not all Displays are connected.";
|
||||||
|
btn_apply.Visible = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ProfileRepository.IsActiveProfile(_selectedProfile))
|
||||||
|
{
|
||||||
|
btn_apply.Visible = false;
|
||||||
|
lbl_profile_shown_subtitle.Text = "This is the Display Profile currently in use.";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
btn_apply.Visible = true;
|
||||||
|
lbl_profile_shown_subtitle.Text = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// we don't have the profile stored yet
|
||||||
|
_saveOrRenameMode = "save";
|
||||||
|
btn_save_or_rename.Text = "Save As";
|
||||||
|
lbl_profile_shown_subtitle.Text = "The current Display configuration hasn't been saved as a Display Profile yet.";
|
||||||
|
btn_apply.Visible = false;
|
||||||
|
lbl_save_profile.Visible = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the Hotkey Label text
|
||||||
|
UpdateHotkeyLabel(_selectedProfile.Hotkey);
|
||||||
|
|
||||||
|
// Refresh the image list view
|
||||||
|
//RefreshImageListView(profile);
|
||||||
|
|
||||||
|
// And finally refresh the profile in the display view
|
||||||
|
dv_profile.Profile = profile;
|
||||||
|
dv_profile.Refresh();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void btn_save_as_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Check the name is valid
|
||||||
|
if (!Program.IsValidFilename(txt_profile_save_name.Text))
|
||||||
|
{
|
||||||
|
MessageBox.Show("The profile name cannot contain the following characters:" + Path.GetInvalidFileNameChars(), "Invalid characters in profile name", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check we're not already using the name
|
||||||
|
foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles)
|
||||||
|
{
|
||||||
|
//if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
if (savedProfile.Name.Equals(txt_profile_save_name.Text))
|
||||||
|
{
|
||||||
|
MessageBox.Show("Sorry, each saved display profile needs a unique name.", "Profile name already exists", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're saving the current profile as a new item
|
||||||
|
// then we'll be in "save" mode
|
||||||
|
if (_saveOrRenameMode == "save")
|
||||||
|
{
|
||||||
|
// We're in 'save' mode!
|
||||||
|
|
||||||
|
// Check we're not already saving this profile
|
||||||
|
foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles)
|
||||||
|
{
|
||||||
|
//if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
if (savedProfile.Equals(_selectedProfile))
|
||||||
|
{
|
||||||
|
MessageBox.Show($"Sorry, this display profile was already saved as '{savedProfile.Name}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// So we've already passed the check that says this profile is unique
|
||||||
|
|
||||||
|
// Update the name just to make sure we record it if the user changed it
|
||||||
|
_selectedProfile.Name = txt_profile_save_name.Text;
|
||||||
|
|
||||||
|
// Add the current profile to the list of profiles so it gets saved
|
||||||
|
ProfileRepository.AddProfile(_selectedProfile);
|
||||||
|
|
||||||
|
// Also update the imagelistview so that we can see the new profile we just saved
|
||||||
|
|
||||||
|
// Load the currentProfile image into the imagelistview
|
||||||
|
//ImageListViewItem newItem = new ImageListViewItem(_selectedProfile.SavedProfileCacheFilename, _selectedProfile.Name);
|
||||||
|
ImageListViewItem newItem = new ImageListViewItem(_selectedProfile, _selectedProfile.Name)
|
||||||
|
{
|
||||||
|
Selected = true
|
||||||
|
};
|
||||||
|
//ilv_saved_profiles.Items.Add(newItem);
|
||||||
|
ilv_saved_profiles.Items.Add(newItem, _profileAdaptor);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We're in 'rename' mode!
|
||||||
|
// Check the name is the same, and if so do nothing
|
||||||
|
if (_selectedProfile.Name.Equals(txt_profile_save_name.Text))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lets save the old names for usage next
|
||||||
|
string oldProfileName = _selectedProfile.Name;
|
||||||
|
|
||||||
|
// Lets rename the selectedProfile to the new name
|
||||||
|
ProfileRepository.RenameProfile(_selectedProfile, txt_profile_save_name.Text);
|
||||||
|
|
||||||
|
// Lets rename the entry in the imagelistview to the new name
|
||||||
|
foreach (ImageListViewItem myItem in ilv_saved_profiles.Items)
|
||||||
|
{
|
||||||
|
if (myItem.Text == oldProfileName)
|
||||||
|
{
|
||||||
|
myItem.Text = txt_profile_save_name.Text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lets update the rest of the profile screen too
|
||||||
|
lbl_profile_shown.Text = txt_profile_save_name.Text;
|
||||||
|
|
||||||
|
// And we also need to go through the any Shortcuts that use the profile and rename them too!
|
||||||
|
ShortcutRepository.RenameShortcutProfile(_selectedProfile);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangeSelectedProfile(_selectedProfile);
|
||||||
|
|
||||||
|
// now update the profiles image listview
|
||||||
|
RefreshDisplayProfileUI();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ilv_saved_profiles_ItemClick(object sender, ItemClickEventArgs e)
|
||||||
|
{
|
||||||
|
foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles)
|
||||||
|
{
|
||||||
|
if (savedProfile.Name == e.Item.Text)
|
||||||
|
{
|
||||||
|
ChangeSelectedProfile(savedProfile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void btn_view_current_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
// Reload the profiles in case we swapped to another program to change it
|
||||||
|
ProfileRepository.GetActiveProfile();
|
||||||
|
// Refresh the profiles to see whats valid
|
||||||
|
ProfileRepository.IsPossibleRefresh();
|
||||||
|
// Change to the current selected Profile
|
||||||
|
ChangeSelectedProfile(ProfileRepository.GetActiveProfile());
|
||||||
|
// Refresh the Profile UI
|
||||||
|
RefreshDisplayProfileUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void txt_profile_save_name_KeyDown(object sender, KeyEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.KeyCode.Equals(Keys.Enter))
|
||||||
|
{
|
||||||
|
MessageBox.Show("Click works!", "Click works", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void WndProc(ref Message m)
|
||||||
|
{
|
||||||
|
const int WM_DISPLAYCHANGE = 0x007E;
|
||||||
|
|
||||||
|
switch (m.Msg)
|
||||||
|
{
|
||||||
|
case WM_DISPLAYCHANGE:
|
||||||
|
ProfileRepository.UpdateActiveProfile();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
base.WndProc(ref m);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ilv_saved_profiles_ItemHover(object sender, ItemHoverEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.Item != null)
|
||||||
|
{
|
||||||
|
tt_selected.SetToolTip(ilv_saved_profiles, e.Item.Text);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tt_selected.RemoveAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap PickBitmapBasedOnBgColour(Color bgColour, Bitmap lightBitmap, Bitmap darkBitmap)
|
||||||
|
{
|
||||||
|
if ((bgColour.R * 0.299 + bgColour.G * 0.587 + bgColour.B * 0.114) > 186)
|
||||||
|
{
|
||||||
|
return darkBitmap;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return lightBitmap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void btn_hotkey_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Keys testHotkey;
|
||||||
|
if (_selectedProfile.Hotkey != Keys.None)
|
||||||
|
testHotkey = _selectedProfile.Hotkey;
|
||||||
|
else
|
||||||
|
testHotkey = Keys.None;
|
||||||
|
string hotkeyHeading = $"Choose a '{_selectedProfile.Name}' Display Profile Hotkey";
|
||||||
|
string hotkeyDescription = $"Choose a Hotkey (a keyboard shortcut) so that you can apply this" + Environment.NewLine +
|
||||||
|
"Display Profile using your keyboard. This must be a Hotkey that" + Environment.NewLine +
|
||||||
|
"is unique across all your applications otherwise DisplayMagician" + Environment.NewLine +
|
||||||
|
"might not see it.";
|
||||||
|
HotkeyForm displayHotkeyForm = new HotkeyForm(testHotkey,hotkeyHeading, hotkeyDescription);
|
||||||
|
//ilv_saved_shortcuts.SuspendLayout();
|
||||||
|
//Program.HotkeyListener.SuspendOn(displayHotkeyForm);
|
||||||
|
displayHotkeyForm.ShowDialog(this);
|
||||||
|
if (displayHotkeyForm.DialogResult == DialogResult.OK)
|
||||||
|
{
|
||||||
|
// now we save the Hotkey
|
||||||
|
_selectedProfile.Hotkey = displayHotkeyForm.Hotkey;
|
||||||
|
// And cause this has changed within a Profile we need to save all the profiles
|
||||||
|
ProfileRepository.SaveProfiles();
|
||||||
|
// And if we get back and this is a Hotkey with a value, we need to show that in the UI
|
||||||
|
UpdateHotkeyLabel(_selectedProfile.Hotkey);
|
||||||
|
if (displayHotkeyForm.Hotkey == Keys.None)
|
||||||
|
// Remove the Hotkey if it needs to be removed
|
||||||
|
HotkeyManager.Current.Remove(_selectedProfile.UUID);
|
||||||
|
else
|
||||||
|
// And then apply the Hotkey now
|
||||||
|
HotkeyManager.Current.AddOrReplace(_selectedProfile.UUID, _selectedProfile.Hotkey, OnWindowHotkeyPressed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void lbl_hotkey_assigned_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
btn_hotkey.PerformClick();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateHotkeyLabel (Keys myHotkey)
|
||||||
|
{
|
||||||
|
// And if we get back and this is a Hotkey with a value, we need to show that in the UI
|
||||||
|
if (myHotkey != Keys.None)
|
||||||
|
{
|
||||||
|
KeysConverter kc = new KeysConverter();
|
||||||
|
|
||||||
|
lbl_hotkey_assigned.Text = "Hotkey: " + kc.ConvertToString(myHotkey);
|
||||||
|
lbl_hotkey_assigned.Visible = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lbl_hotkey_assigned.Text = "Hotkey: None";
|
||||||
|
lbl_hotkey_assigned.Visible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnWindowHotkeyPressed(object sender, HotkeyEventArgs e)
|
||||||
|
{
|
||||||
|
if (ProfileRepository.ContainsProfile(e.Name))
|
||||||
|
{
|
||||||
|
string displayProfileUUID = e.Name;
|
||||||
|
ProfileItem chosenProfile = ProfileRepository.GetProfile(displayProfileUUID);
|
||||||
|
if (chosenProfile is ProfileItem)
|
||||||
|
ProfileRepository.ApplyProfile(chosenProfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user