From f23ae340a715475d0822f44b572c2399d5f5ddb1 Mon Sep 17 00:00:00 2001 From: terrymacdonald Date: Fri, 24 Jul 2020 13:11:42 +1200 Subject: [PATCH] Fixed DisplayProfileForm profile selection logic Profile selection logic was running functions multiple times which was resulting in a lot of extra delay while using the DisplayProfileFOrm. These changes streamline it and make it work a lot faster! --- HeliosPlus.Shared/ProfileItem.cs | 4 +- HeliosPlus.Shared/ProfileRepository.cs | 12 ++ HeliosPlus/ShortcutRepository.cs | 6 +- HeliosPlus/UIForms/DisplayProfileForm.cs | 153 ++++++----------------- HeliosPlus/UIForms/ShortcutForm.cs | 3 +- 5 files changed, 53 insertions(+), 125 deletions(-) diff --git a/HeliosPlus.Shared/ProfileItem.cs b/HeliosPlus.Shared/ProfileItem.cs index 4a68eff..7b339fa 100644 --- a/HeliosPlus.Shared/ProfileItem.cs +++ b/HeliosPlus.Shared/ProfileItem.cs @@ -96,7 +96,7 @@ namespace HeliosPlus.Shared #endregion public ProfileItem() { - try + /*try { NvAPIWrapper.NVIDIA.Initialize(); } @@ -104,7 +104,7 @@ namespace HeliosPlus.Shared { Console.WriteLine($"ShortcutItem/Instansiation exception: {ex.Message}: {ex.InnerException}"); // ignored - } + }*/ } diff --git a/HeliosPlus.Shared/ProfileRepository.cs b/HeliosPlus.Shared/ProfileRepository.cs index 309c235..847886b 100644 --- a/HeliosPlus.Shared/ProfileRepository.cs +++ b/HeliosPlus.Shared/ProfileRepository.cs @@ -50,6 +50,18 @@ namespace HeliosPlus.Shared #region Class Constructors static ProfileRepository() { + + // Initialise the the NVIDIA NvAPIWrapper + try + { + NvAPIWrapper.NVIDIA.Initialize(); + } + catch (Exception ex) + { + Console.WriteLine($"ShortcutItem/Instansiation exception: {ex.Message}: {ex.InnerException}"); + // ignored + } + // Load the Profiles from storage LoadProfiles(); } diff --git a/HeliosPlus/ShortcutRepository.cs b/HeliosPlus/ShortcutRepository.cs index 6f5c941..5b8b958 100644 --- a/HeliosPlus/ShortcutRepository.cs +++ b/HeliosPlus/ShortcutRepository.cs @@ -679,7 +679,7 @@ namespace HeliosPlus } - private static bool ApplyProfile(ProfileItem profile) + public static bool ApplyProfile(ProfileItem profile) { // If we're already on the wanted profile then no need to change! if (ProfileRepository.IsActiveProfile(profile)) @@ -716,7 +716,7 @@ namespace HeliosPlus } } - public static void ApplyTopology(ProfileItem profile) + private static void ApplyTopology(ProfileItem profile) { Debug.Print("ShortcutRepository.ApplyTopology()"); if (profile == null) @@ -757,7 +757,7 @@ namespace HeliosPlus } } - public static void ApplyPathInfo(ProfileItem profile) + private static void ApplyPathInfo(ProfileItem profile) { Debug.Print("ShortcutRepository.ApplyPathInfo()"); if (profile == null) diff --git a/HeliosPlus/UIForms/DisplayProfileForm.cs b/HeliosPlus/UIForms/DisplayProfileForm.cs index 0878b2f..caae2cc 100644 --- a/HeliosPlus/UIForms/DisplayProfileForm.cs +++ b/HeliosPlus/UIForms/DisplayProfileForm.cs @@ -33,6 +33,9 @@ namespace HeliosPlus.UIForms private void Apply_Click(object sender, EventArgs e) { + if (!(_selectedProfile is ProfileItem)) + return; + if (!_selectedProfile.IsPossible) { MessageBox.Show(this, Language.This_profile_is_currently_impossible_to_apply, @@ -42,34 +45,8 @@ namespace HeliosPlus.UIForms return; } - // Need to move this logic to the Shortcut Repository or the Profile Repository. - /*IDictionary actions = dv_profile.Profile.applyProfileActions(); - IDictionary messages = dv_profile.Profile.applyProfileMsgs(); - List sequence = dv_profile.Profile.applyProfileSequence(); - - if (new ApplyingChangesForm( - () => { - Task.Factory.StartNew(() => - { - System.Threading.Thread.Sleep(2000); - actions[sequence[0]](); - }, TaskCreationOptions.LongRunning); - }, 3, 30, 5, messages[sequence[0]] - ).ShowDialog(this) != DialogResult.Cancel) - { - for (int i = 1; i < sequence.Count; i++) - { - new ApplyingChangesForm( - () => - { - Task.Factory.StartNew(() => actions[sequence[i]](), TaskCreationOptions.LongRunning); - }, 0, 30, 5, messages[sequence[i]]).ShowDialog(this); - } - // nothing to do - Console.WriteLine("Applying profile " + _selectedProfile.Name); - } - - Activate();*/ + // Apply the Profile + ShortcutRepository.ApplyProfile(_selectedProfile); } @@ -83,6 +60,9 @@ namespace HeliosPlus.UIForms private void Delete_Click(object sender, EventArgs e) { + if (!(_selectedProfile is ProfileItem)) + 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; @@ -128,53 +108,28 @@ namespace HeliosPlus.UIForms 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(); - if (ProfileRepository.ProfileCount > 0) + // Empty the imageListView + ilv_saved_profiles.Items.Clear(); + + // Fill it back up with the Profiles we have + foreach (ProfileItem profile in ProfileRepository.AllProfiles) { - ImageListViewItem newItem = null; - bool foundCurrentProfileInLoadedProfiles = false; - foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) - { - bool thisLoadedProfileIsAlreadyHere = (from item in ilv_saved_profiles.Items where item.Text == loadedProfile.Name select item.Text).Any(); - if (!thisLoadedProfileIsAlreadyHere) - { - //loadedProfile.SaveProfileImageToCache(); - //newItem = new ImageListViewItem(loadedProfile.SavedProfileCacheFilename, loadedProfile.Name); - //newItem = new ImageListViewItem(loadedProfile, loadedProfile.Name); - newItem = new ImageListViewItem(loadedProfile, loadedProfile.Name); - //ilv_saved_profiles.Items.Add(newItem); - ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); - } + // Create a new ImageListViewItem from the profile + newItem = new ImageListViewItem(profile, profile.Name); - if (ProfileRepository.CurrentProfile.Equals(loadedProfile)) - { - // We have already saved the selected profile! - // so we need to show the selected profile - ChangeSelectedProfile(loadedProfile); - foundCurrentProfileInLoadedProfiles = true; - } - } + // Select it if its the selectedProfile + if (_selectedProfile is ProfileItem && _selectedProfile.Equals(profile)) + newItem.Selected = true; - // If we get to the end of the loaded profiles and haven't - // found a matching profile, then we need to show the current - // Profile - if (!foundCurrentProfileInLoadedProfiles) - ChangeSelectedProfile(ProfileRepository.CurrentProfile); + // Add it to the list! + ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); - // Check if we were loading a profile to edit - // If so, select that instead of all that other stuff above! - if (_profileToLoad != null) - ChangeSelectedProfile(_profileToLoad); - - } - else - { - // If there are no profiles at all then we are starting from scratch! - // Show the profile in the DV window - // Use the current profile name in the label and the save name - ChangeSelectedProfile(ProfileRepository.CurrentProfile); } // Restart updating the saved_profiles listview @@ -190,10 +145,10 @@ namespace HeliosPlus.UIForms // 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 + /*// Reload the profiles in case we swapped to another program to change it ProfileRepository.UpdateCurrentProfile(); // Refresh the Profile UI - RefreshDisplayProfileUI(); + RefreshDisplayProfileUI();*/ } private void DisplayProfileForm_Load(object sender, EventArgs e) @@ -202,6 +157,8 @@ namespace HeliosPlus.UIForms //_savedProfiles = ProfileRepository.AllProfiles; // Update the Current Profile ProfileRepository.UpdateCurrentProfile(); + // Change to the current selected Profile + ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); // Refresh the Profile UI RefreshDisplayProfileUI(); } @@ -209,7 +166,6 @@ namespace HeliosPlus.UIForms private void ChangeSelectedProfile(ProfileItem profile) { - // And we need to update the actual selected profile too! _selectedProfile = profile; @@ -219,7 +175,7 @@ namespace HeliosPlus.UIForms // And update the save/rename textbox txt_profile_save_name.Text = _selectedProfile.Name; - if (ProfileRepository.AllProfiles.Contains(_selectedProfile)) + if (ProfileRepository.ContainsProfile(_selectedProfile)) { // we already have the profile stored _saveOrRenameMode = "rename"; @@ -232,7 +188,10 @@ namespace HeliosPlus.UIForms else { lbl_profile_shown_subtitle.Text = ""; - btn_apply.Visible = true; + if (ProfileRepository.IsActiveProfile(_selectedProfile)) + btn_apply.Visible = false; + else + btn_apply.Visible = true; } } else @@ -244,40 +203,8 @@ namespace HeliosPlus.UIForms btn_apply.Visible = false; } - - /*if (ProfileRepository.CurrentProfile.Equals(_selectedProfile)) - { - if (ProfileRepository.AllProfiles.Contains(_selectedProfile)) - { - _saveOrRenameMode = "rename"; - btn_save_or_rename.Text = "Rename To"; - lbl_profile_shown_subtitle.Text = "(Current Display Profile in use)"; - } - else - { - _saveOrRenameMode = "save"; - btn_save_or_rename.Text = "Save As"; - lbl_profile_shown_subtitle.Text = "(Current Display Profile in use - UNSAVED)"; - } - btn_apply.Visible = false; - } - else - { - _saveOrRenameMode = "rename"; - btn_save_or_rename.Text = "Rename To"; - if (!_selectedProfile.IsPossible) - { - lbl_profile_shown_subtitle.Text = "(Display Profile is not valid so cannot be used)"; - btn_apply.Visible = false; - } - else - { - lbl_profile_shown_subtitle.Text = ""; - btn_apply.Visible = true; - } - }*/ // Refresh the image list view - RefreshImageListView(profile); + //RefreshImageListView(profile); // And finally show the profile in the display view dv_profile.Profile = profile; @@ -285,18 +212,6 @@ namespace HeliosPlus.UIForms } - private void RefreshImageListView(ProfileItem profile) - { - ilv_saved_profiles.ClearSelection(); - IEnumerable matchingImageListViewItems = (from item in ilv_saved_profiles.Items where item.Text == profile.Name select item); - if (matchingImageListViewItems.Any()) - { - matchingImageListViewItems.First().Selected = true; - matchingImageListViewItems.First().Focused = true; - } - - } - private void btn_save_as_Click(object sender, EventArgs e) { @@ -408,6 +323,8 @@ namespace HeliosPlus.UIForms { // Reload the profiles in case we swapped to another program to change it ProfileRepository.UpdateCurrentProfile(); + // Change to the current selected Profile + ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); // Refresh the Profile UI RefreshDisplayProfileUI(); } diff --git a/HeliosPlus/UIForms/ShortcutForm.cs b/HeliosPlus/UIForms/ShortcutForm.cs index fd8f8bb..c4c9d8a 100644 --- a/HeliosPlus/UIForms/ShortcutForm.cs +++ b/HeliosPlus/UIForms/ShortcutForm.cs @@ -19,7 +19,6 @@ namespace HeliosPlus.UIForms public partial class ShortcutForm : Form { - List _allSteamGames; private ProfileAdaptor _profileAdaptor; //private List _loadedProfiles = new List(); private ProfileItem _profileToUse= null; @@ -827,7 +826,7 @@ namespace HeliosPlus.UIForms if (lv_games.SelectedItems.Count > 0) { txt_game_name.Text = lv_games.SelectedItems[0].Text; - foreach (SteamGame game in _allSteamGames) + foreach (SteamGame game in SteamLibrary.AllInstalledGames) { if (game.GameName == txt_game_name.Text) {