Skip to content

Commit 874383b

Browse files
bugfix and refactor
1. Fix issues with uid. 2. Implement #1486.
1 parent 2f9b19d commit 874383b

File tree

6 files changed

+74
-47
lines changed

6 files changed

+74
-47
lines changed

Plugins/Flow.Launcher.Plugin.Program/AddProgramSource.xaml.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
5555
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
5656
{
5757
string path = Directory.Text;
58+
bool modified = false;
5859
if (!System.IO.Directory.Exists(path))
5960
{
6061
System.Windows.MessageBox.Show(_context.API.GetTranslation("flowlauncher_plugin_program_invalid_path"));
@@ -65,18 +66,22 @@ private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
6566
if (!ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier.Equals(path, System.StringComparison.InvariantCultureIgnoreCase)))
6667
{
6768
var source = new ProgramSource(path);
68-
69+
modified = true;
6970
_settings.ProgramSources.Insert(0, source);
7071
ProgramSetting.ProgramSettingDisplayList.Add(source);
7172
}
7273
}
7374
else
7475
{
75-
_editing.Location = path;
76-
_editing.Enabled = Chkbox.IsChecked ?? true; // Fixme, need to add to disabled source if not custom source
76+
modified = _editing.Location != path || _editing.Enabled != Chkbox.IsChecked;
77+
if (modified)
78+
{
79+
_editing.SetLocation(path);
80+
_editing.Enabled = Chkbox.IsChecked ?? true;
81+
}
7782
}
7883

79-
DialogResult = true;
84+
DialogResult = modified;
8085
Close();
8186
}
8287
}

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private static void DisableProgram(IProgram programToDelete)
197197
_settings.DisabledProgramSources.Add(new DisabledProgramSource(programToDelete));
198198
var t1 = Task.Run(() =>
199199
{
200-
IndexWin32Programs();
200+
IndexUwpPrograms();
201201
_settings.LastIndexTime = DateTime.Today;
202202
});
203203
}
@@ -208,7 +208,7 @@ private static void DisableProgram(IProgram programToDelete)
208208
_settings.DisabledProgramSources.Add(new DisabledProgramSource(programToDelete));
209209
var t1 = Task.Run(() =>
210210
{
211-
IndexUwpPrograms();
211+
IndexWin32Programs();
212212
_settings.LastIndexTime = DateTime.Today;
213213
});
214214
}

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
2525
public class Win32 : IProgram, IEquatable<Win32>
2626
{
2727
public string Name { get; set; }
28-
public string UniqueIdentifier { get => _uid; set => _uid = value.ToLowerInvariant(); }
28+
public string UniqueIdentifier { get => _uid; set => _uid = value.ToLowerInvariant(); } // For path comparison
2929
public string IcoPath { get; set; }
3030
public string FullPath { get; set; }
3131
public string LnkResolvedPath { get; set; }

Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,31 @@ internal static void SetProgramSourcesStatus(List<ProgramSource> selectedProgram
5252

5353
internal static void StoreDisabledInSettings()
5454
{
55-
Main._settings.ProgramSources
56-
.Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && !x.Enabled))
57-
.ToList()
58-
.ForEach(t1 => t1.Enabled = false);
55+
// no need since using refernce now
56+
//Main._settings.ProgramSources
57+
// .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && !x.Enabled))
58+
// .ToList()
59+
// .ForEach(t1 => t1.Enabled = false);
5960

60-
ProgramSetting.ProgramSettingDisplayList
61+
// Disabled, not in DisabledProgramSources or ProgramSources
62+
var tmp = ProgramSetting.ProgramSettingDisplayList
6163
.Where(t1 => !t1.Enabled
62-
&& !Main._settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
63-
.ToList()
64-
.ForEach(x => Main._settings.DisabledProgramSources
65-
.Add(new DisabledProgramSource(x)));
64+
&& !Main._settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier)
65+
&& !Main._settings.ProgramSources.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
66+
.Select(x => new DisabledProgramSource(x));
67+
68+
Main._settings.DisabledProgramSources.AddRange(tmp);
6669
}
6770

6871
internal static void RemoveDisabledFromSettings()
6972
{
70-
Main._settings.ProgramSources
71-
.Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled))
72-
.ToList()
73-
.ForEach(t1 => t1.Enabled = true);
73+
//Main._settings.ProgramSources
74+
// .Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled))
75+
// .ToList()
76+
// .ForEach(t1 => t1.Enabled = true);
7477

7578
Main._settings.DisabledProgramSources
76-
.Where(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled))
77-
.ToList()
78-
.ForEach(x => Main._settings.DisabledProgramSources.Remove(x));
79+
.RemoveAll(t1 => ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && x.Enabled));
7980
}
8081

8182
internal static bool IsReindexRequired(this List<ProgramSource> selectedItems)

Plugins/Flow.Launcher.Plugin.Program/Views/Models/ProgramSource.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.IO;
3+
using System.Text.Json.Serialization;
4+
using System.Windows.Media.Imaging;
35
using Flow.Launcher.Plugin.Program.Programs;
46

57
namespace Flow.Launcher.Plugin.Program.Views.Models
@@ -16,17 +18,20 @@ public class ProgramSource
1618
{
1719
private string name;
1820

19-
public string Location { get; set; }
21+
public string Location { get; private set; }
2022
public string Name { get => name ?? new DirectoryInfo(Location).Name; set => name = value; }
2123
public bool Enabled { get; set; } = true;
2224

23-
/// <summary>
24-
/// Guaranteed lowercase.
25-
/// </summary>
26-
public string UniqueIdentifier { get => uid; set => uid = value.ToLowerInvariant(); }
27-
private string uid { get; set; }
25+
public string UniqueIdentifier { get; private set; }
2826

29-
public ProgramSource() { } // only for json deserialization
27+
[JsonConstructor]
28+
public ProgramSource(string name, string location, bool enabled, string uniqueIdentifier)
29+
{
30+
Location = location;
31+
this.name = name;
32+
Enabled = enabled;
33+
UniqueIdentifier = uniqueIdentifier;
34+
}
3035

3136
/// <summary>
3237
/// Add source by location
@@ -37,7 +42,7 @@ public ProgramSource(string location, bool enabled=true)
3742
{
3843
Location = location;
3944
Enabled = enabled;
40-
UniqueIdentifier = location;
45+
UniqueIdentifier = location.ToLowerInvariant(); // For path comparison
4146
}
4247

4348
public ProgramSource(ProgramSource source)
@@ -68,13 +73,21 @@ public bool Equals(IProgram program)
6873

6974
public override int GetHashCode()
7075
{
71-
return HashCode.Combine(uid);
76+
return HashCode.Combine(UniqueIdentifier);
77+
}
78+
79+
public void SetLocation(string value)
80+
{
81+
if (Location == value) return;
82+
Location = value;
83+
UniqueIdentifier = value.ToLowerInvariant(); // Update
7284
}
7385
}
7486

7587
public class DisabledProgramSource : ProgramSource
7688
{
77-
public DisabledProgramSource() { } // only for json deserialization
89+
[JsonConstructor]
90+
public DisabledProgramSource(string name, string location, bool enabled, string uniqueIdentifier) : base(name, location, enabled, uniqueIdentifier) { }
7891

7992
public DisabledProgramSource(string location) : base(location, false) { }
8093

Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,34 @@ private void DeleteProgramSources(List<ProgramSource> itemsToDelete)
149149
private void btnEditProgramSource_OnClick(object sender, RoutedEventArgs e)
150150
{
151151
var selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
152-
if (selectedProgramSource != null)
152+
EditProgramSource(selectedProgramSource);
153+
}
154+
155+
private void EditProgramSource(ProgramSource selectedProgramSource)
156+
{
157+
if (selectedProgramSource == null)
158+
{
159+
string msg = context.API.GetTranslation("flowlauncher_plugin_program_pls_select_program_source");
160+
MessageBox.Show(msg);
161+
}
162+
else
153163
{
154164
var add = new AddProgramSource(selectedProgramSource, _settings);
155165
if (add.ShowDialog() ?? false)
156166
{
167+
if (selectedProgramSource.Enabled)
168+
{
169+
ProgramSettingDisplay.SetProgramSourcesStatus(new List<ProgramSource> { selectedProgramSource }, true); // sync status in win32, uwp and disabled
170+
ProgramSettingDisplay.RemoveDisabledFromSettings();
171+
}
172+
else
173+
{
174+
ProgramSettingDisplay.SetProgramSourcesStatus(new List<ProgramSource> { selectedProgramSource }, false);
175+
ProgramSettingDisplay.StoreDisabledInSettings();
176+
}
157177
ReIndexing();
158178
}
159179
}
160-
else
161-
{
162-
string msg = context.API.GetTranslation("flowlauncher_plugin_program_pls_select_program_source");
163-
MessageBox.Show(msg);
164-
}
165180
}
166181

167182
private void btnReindex_Click(object sender, RoutedEventArgs e)
@@ -352,14 +367,7 @@ private void programSourceView_SelectionChanged(object sender, SelectionChangedE
352367
private void programSourceView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
353368
{
354369
var selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
355-
if (selectedProgramSource != null)
356-
{
357-
var add = new AddProgramSource(selectedProgramSource, _settings);
358-
if (add.ShowDialog() ?? false)
359-
{
360-
ReIndexing();
361-
}
362-
}
370+
EditProgramSource(selectedProgramSource);
363371
}
364372

365373
private bool IsAllItemsUserAdded(List<ProgramSource> items)

0 commit comments

Comments
 (0)