From 874383bd076d8dd2f3a0e5be965f07eb003e87fd Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Tue, 25 Oct 2022 17:39:01 +0800 Subject: [PATCH] bugfix and refactor 1. Fix issues with uid. 2. Implement #1486. --- .../AddProgramSource.xaml.cs | 13 ++++--- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 4 +-- .../Programs/Win32.cs | 2 +- .../Views/Commands/ProgramSettingDisplay.cs | 33 ++++++++--------- .../Views/Models/ProgramSource.cs | 33 +++++++++++------ .../Views/ProgramSetting.xaml.cs | 36 +++++++++++-------- 6 files changed, 74 insertions(+), 47 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs b/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs index 1d2740cd7..1445cdb7b 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs @@ -55,6 +55,7 @@ namespace Flow.Launcher.Plugin.Program private void BtnAdd_OnClick(object sender, RoutedEventArgs e) { string path = Directory.Text; + bool modified = false; if (!System.IO.Directory.Exists(path)) { System.Windows.MessageBox.Show(_context.API.GetTranslation("flowlauncher_plugin_program_invalid_path")); @@ -65,18 +66,22 @@ namespace Flow.Launcher.Plugin.Program if (!ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier.Equals(path, System.StringComparison.InvariantCultureIgnoreCase))) { var source = new ProgramSource(path); - + modified = true; _settings.ProgramSources.Insert(0, source); ProgramSetting.ProgramSettingDisplayList.Add(source); } } else { - _editing.Location = path; - _editing.Enabled = Chkbox.IsChecked ?? true; // Fixme, need to add to disabled source if not custom source + modified = _editing.Location != path || _editing.Enabled != Chkbox.IsChecked; + if (modified) + { + _editing.SetLocation(path); + _editing.Enabled = Chkbox.IsChecked ?? true; + } } - DialogResult = true; + DialogResult = modified; Close(); } } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index bf1014c44..6786a2258 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -197,7 +197,7 @@ namespace Flow.Launcher.Plugin.Program _settings.DisabledProgramSources.Add(new DisabledProgramSource(programToDelete)); var t1 = Task.Run(() => { - IndexWin32Programs(); + IndexUwpPrograms(); _settings.LastIndexTime = DateTime.Today; }); } @@ -208,7 +208,7 @@ namespace Flow.Launcher.Plugin.Program _settings.DisabledProgramSources.Add(new DisabledProgramSource(programToDelete)); var t1 = Task.Run(() => { - IndexUwpPrograms(); + IndexWin32Programs(); _settings.LastIndexTime = DateTime.Today; }); } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs b/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs index 2c7b0c3ff..b5969affc 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs @@ -25,7 +25,7 @@ namespace Flow.Launcher.Plugin.Program.Programs public class Win32 : IProgram, IEquatable { public string Name { get; set; } - public string UniqueIdentifier { get => _uid; set => _uid = value.ToLowerInvariant(); } + public string UniqueIdentifier { get => _uid; set => _uid = value.ToLowerInvariant(); } // For path comparison public string IcoPath { get; set; } public string FullPath { get; set; } public string LnkResolvedPath { get; set; } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs b/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs index 4ebca8cf3..efa7900b0 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs @@ -52,30 +52,31 @@ namespace Flow.Launcher.Plugin.Program.Views.Commands internal static void StoreDisabledInSettings() { - Main._settings.ProgramSources - .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && !x.Enabled)) - .ToList() - .ForEach(t1 => t1.Enabled = false); + // no need since using refernce now + //Main._settings.ProgramSources + // .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && !x.Enabled)) + // .ToList() + // .ForEach(t1 => t1.Enabled = false); - ProgramSetting.ProgramSettingDisplayList + // Disabled, not in DisabledProgramSources or ProgramSources + var tmp = ProgramSetting.ProgramSettingDisplayList .Where(t1 => !t1.Enabled - && !Main._settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier)) - .ToList() - .ForEach(x => Main._settings.DisabledProgramSources - .Add(new DisabledProgramSource(x))); + && !Main._settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier) + && !Main._settings.ProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier)) + .Select(x => new DisabledProgramSource(x)); + + Main._settings.DisabledProgramSources.AddRange(tmp); } internal static void RemoveDisabledFromSettings() { - Main._settings.ProgramSources - .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled)) - .ToList() - .ForEach(t1 => t1.Enabled = true); + //Main._settings.ProgramSources + // .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled)) + // .ToList() + // .ForEach(t1 => t1.Enabled = true); Main._settings.DisabledProgramSources - .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled)) - .ToList() - .ForEach(x => Main._settings.DisabledProgramSources.Remove(x)); + .RemoveAll(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled)); } internal static bool IsReindexRequired(this List selectedItems) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs b/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs index 5deb6b396..8efa3ef6d 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs @@ -1,5 +1,7 @@ using System; using System.IO; +using System.Text.Json.Serialization; +using System.Windows.Media.Imaging; using Flow.Launcher.Plugin.Program.Programs; namespace Flow.Launcher.Plugin.Program.Views.Models @@ -16,17 +18,20 @@ namespace Flow.Launcher.Plugin.Program.Views.Models { private string name; - public string Location { get; set; } + public string Location { get; private set; } public string Name { get => name ?? new DirectoryInfo(Location).Name; set => name = value; } public bool Enabled { get; set; } = true; - /// - /// Guaranteed lowercase. - /// - public string UniqueIdentifier { get => uid; set => uid = value.ToLowerInvariant(); } - private string uid { get; set; } + public string UniqueIdentifier { get; private set; } - public ProgramSource() { } // only for json deserialization + [JsonConstructor] + public ProgramSource(string name, string location, bool enabled, string uniqueIdentifier) + { + Location = location; + this.name = name; + Enabled = enabled; + UniqueIdentifier = uniqueIdentifier; + } /// /// Add source by location @@ -37,7 +42,7 @@ namespace Flow.Launcher.Plugin.Program.Views.Models { Location = location; Enabled = enabled; - UniqueIdentifier = location; + UniqueIdentifier = location.ToLowerInvariant(); // For path comparison } public ProgramSource(ProgramSource source) @@ -68,13 +73,21 @@ namespace Flow.Launcher.Plugin.Program.Views.Models public override int GetHashCode() { - return HashCode.Combine(uid); + return HashCode.Combine(UniqueIdentifier); + } + + public void SetLocation(string value) + { + if (Location == value) return; + Location = value; + UniqueIdentifier = value.ToLowerInvariant(); // Update } } public class DisabledProgramSource : ProgramSource { - public DisabledProgramSource() { } // only for json deserialization + [JsonConstructor] + public DisabledProgramSource(string name, string location, bool enabled, string uniqueIdentifier) : base(name, location, enabled, uniqueIdentifier) { } public DisabledProgramSource(string location) : base(location, false) { } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs index c78c4fa9a..77d29fab1 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs @@ -149,19 +149,34 @@ namespace Flow.Launcher.Plugin.Program.Views private void btnEditProgramSource_OnClick(object sender, RoutedEventArgs e) { var selectedProgramSource = programSourceView.SelectedItem as ProgramSource; - if (selectedProgramSource != null) + EditProgramSource(selectedProgramSource); + } + + private void EditProgramSource(ProgramSource selectedProgramSource) + { + if (selectedProgramSource == null) + { + string msg = context.API.GetTranslation("flowlauncher_plugin_program_pls_select_program_source"); + MessageBox.Show(msg); + } + else { var add = new AddProgramSource(selectedProgramSource, _settings); if (add.ShowDialog() ?? false) { + if (selectedProgramSource.Enabled) + { + ProgramSettingDisplay.SetProgramSourcesStatus(new List { selectedProgramSource }, true); // sync status in win32, uwp and disabled + ProgramSettingDisplay.RemoveDisabledFromSettings(); + } + else + { + ProgramSettingDisplay.SetProgramSourcesStatus(new List { selectedProgramSource }, false); + ProgramSettingDisplay.StoreDisabledInSettings(); + } ReIndexing(); } } - else - { - string msg = context.API.GetTranslation("flowlauncher_plugin_program_pls_select_program_source"); - MessageBox.Show(msg); - } } private void btnReindex_Click(object sender, RoutedEventArgs e) @@ -352,14 +367,7 @@ namespace Flow.Launcher.Plugin.Program.Views private void programSourceView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { var selectedProgramSource = programSourceView.SelectedItem as ProgramSource; - if (selectedProgramSource != null) - { - var add = new AddProgramSource(selectedProgramSource, _settings); - if (add.ShowDialog() ?? false) - { - ReIndexing(); - } - } + EditProgramSource(selectedProgramSource); } private bool IsAllItemsUserAdded(List items)