From 81d295498b4c548b9d4bffc048b03616264d820d Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 20 Oct 2022 16:15:56 +0800 Subject: [PATCH] Move ProgramSource definition --- .../AddProgramSource.xaml.cs | 4 +- .../Flow.Launcher.Plugin.Program/Settings.cs | 21 +--- .../Views/Commands/ProgramSettingDisplay.cs | 4 +- .../Views/Models/ProgramSource.cs | 102 +++++++++++++++++- .../Views/ProgramSetting.xaml.cs | 6 +- 5 files changed, 108 insertions(+), 29 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs b/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs index 045d363b3..25bc3d670 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs @@ -12,7 +12,7 @@ namespace Flow.Launcher.Plugin.Program public partial class AddProgramSource { private PluginInitContext _context; - private Settings.ProgramSource _editing; + private ProgramSource _editing; private Settings _settings; public AddProgramSource(PluginInitContext context, Settings settings) @@ -23,7 +23,7 @@ namespace Flow.Launcher.Plugin.Program Directory.Focus(); } - public AddProgramSource(Settings.ProgramSource edit, Settings settings) + public AddProgramSource(ProgramSource edit, Settings settings) { _editing = edit; _settings = settings; diff --git a/Plugins/Flow.Launcher.Plugin.Program/Settings.cs b/Plugins/Flow.Launcher.Plugin.Program/Settings.cs index d97ddd993..af5b7bc7d 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Settings.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Settings.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using Flow.Launcher.Plugin.Program.Views.Models; namespace Flow.Launcher.Plugin.Program { @@ -24,25 +25,5 @@ namespace Flow.Launcher.Plugin.Program internal const string Explorer = "explorer"; internal const string ExplorerArgs = "%s"; - - /// - /// Contains user added folder location contents as well as all user disabled applications - /// - /// - /// Win32 class applications set UniqueIdentifier using their full file path - /// UWP class applications set UniqueIdentifier using their Application User Model ID - /// Custom user added program sources set UniqueIdentifier using their location - /// - public class ProgramSource - { - private string name; - - public string Location { get; set; } - public string Name { get => name ?? new DirectoryInfo(Location).Name; set => name = value; } - public bool Enabled { get; set; } = true; - public string UniqueIdentifier { get; set; } - } - - public class DisabledProgramSource : ProgramSource { } } } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs b/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs index 3129b2b99..5be38a7b3 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs @@ -9,7 +9,7 @@ namespace Flow.Launcher.Plugin.Program.Views.Commands { internal static class ProgramSettingDisplay { - internal static List LoadProgramSources(this List programSources) + internal static List LoadProgramSources(this List programSources) { var list = new List(); @@ -108,7 +108,7 @@ namespace Flow.Launcher.Plugin.Program.Views.Commands .ToList() .ForEach(x => Main._settings.DisabledProgramSources .Add( - new Settings.DisabledProgramSource + new DisabledProgramSource { Name = x.Name, Location = x.Location, diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs b/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs index 8e3184ff7..e42552696 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs @@ -1,5 +1,103 @@ - +using System; +using System.IO; +using Flow.Launcher.Plugin.Program.Programs; + namespace Flow.Launcher.Plugin.Program.Views.Models { - public class ProgramSource : Settings.ProgramSource { } + /// + /// Contains user added folder location contents as well as all user disabled applications + /// + /// + /// Win32 class applications set UniqueIdentifier using their full file path + /// UWP class applications set UniqueIdentifier using their Application User Model ID + /// Custom user added program sources set UniqueIdentifier using their location + /// + public class ProgramSource + { + private string name; + + public string Location; + public string Name { get => name ?? new DirectoryInfo(Location).Name; set => name = value; } + public bool Enabled { get; set; } = true; + private string uid { get; set; } + + /// + /// Guranteed lowercase. + /// + public string UniqueIdentifier { get => uid; set => uid = value.ToLowerInvariant(); } + + public ProgramSource() + { + } + + /// + /// Custom user added source. + /// + /// + /// + public ProgramSource(string location, bool enabled) + { + Location = location; + Enabled = enabled; + UniqueIdentifier = location; + } + + public ProgramSource(ProgramSource source) + { + Location = source.Location; + Name = source.Name; + Enabled = source.Enabled; + UniqueIdentifier = source.UniqueIdentifier; + } + + public ProgramSource(IProgram source) + { + Location = source.Location; + Name = source.Name; + Enabled = source.Enabled; + UniqueIdentifier = source.UniqueIdentifier; + } + + public override bool Equals(object obj) + { + return obj is ProgramSource other && other.UniqueIdentifier == this.UniqueIdentifier; + } + + public bool Equals(IProgram program) + { + return program != null && program.UniqueIdentifier == this.UniqueIdentifier; + } + + public static bool operator ==(ProgramSource a, ProgramSource b) + { + return a is not null && a.Equals(b); + } + + public static bool operator !=(ProgramSource a, ProgramSource b) + { + return !(a == b); + } + + public override int GetHashCode() + { + return HashCode.Combine(uid); + } + } + + public class DisabledProgramSource : ProgramSource + { + public DisabledProgramSource() { } + + public DisabledProgramSource(string location) : base(location, false) { } + + public DisabledProgramSource(ProgramSource source) : base(source) + { + Enabled = false; + } + + public DisabledProgramSource(IProgram program) : base(program) + { + Enabled = false; + } + } } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs index 1a31e8c28..0c4892abc 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -147,7 +147,7 @@ namespace Flow.Launcher.Plugin.Program.Views private void btnEditProgramSource_OnClick(object sender, RoutedEventArgs e) { - var selectedProgramSource = programSourceView.SelectedItem as Settings.ProgramSource; + var selectedProgramSource = programSourceView.SelectedItem as ProgramSource; if (selectedProgramSource != null) { var add = new AddProgramSource(selectedProgramSource, _settings); @@ -360,4 +360,4 @@ namespace Flow.Launcher.Plugin.Program.Views } } } -} \ No newline at end of file +}