Skip to content

Commit e23eb94

Browse files
committed
allow progress bar customisation via plugin
1 parent b62ab1d commit e23eb94

File tree

5 files changed

+88
-65
lines changed

5 files changed

+88
-65
lines changed

Flow.Launcher.Plugin/Result.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ public ValueTask<bool> ExecuteAsync(ActionContext context)
198198
return AsyncAction?.Invoke(context) ?? ValueTask.FromResult(Action?.Invoke(context) ?? false);
199199
}
200200

201+
/// <summary>
202+
/// Progress bar display. Providing an int value between 0-100 will trigger the progress bar to be displayed on the result
203+
/// </summary>
201204
public int? ProgressBar { get; set; }
205+
206+
/// <summary>
207+
/// Optionally set the color of the progress bar
208+
/// </summary>
209+
/// <default>#26a0da (blue)</default>
210+
public string ProgressBarColor { get; set; } = "#26a0da";
202211
}
203212
}

Flow.Launcher/Converters/ProgressForegroundConverter.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

Flow.Launcher/ResultListBox.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
<Grid.Resources>
4747
<converter:HighlightTextConverter x:Key="HighlightTextConverter" />
4848
<converter:OrdinalConverter x:Key="OrdinalConverter" />
49-
<converter:ProgressForegroundConverter x:Key="ProgressForegroundConverter" />
5049
<converter:OpenResultHotkeyVisibilityConverter x:Key="OpenResultHotkeyVisibilityConverter" />
5150
</Grid.Resources>
5251
<Grid.ColumnDefinitions>
@@ -114,7 +113,7 @@
114113
</Grid.RowDefinitions>
115114
<ProgressBar
116115
x:Name="progressbarResult"
117-
Foreground="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Value, Converter={StaticResource ProgressForegroundConverter}}"
116+
Foreground="{Binding Result.ProgressBarColor}"
118117
Style="{DynamicResource ProgressBarResult}"
119118
Value="{Binding Result.ProgressBar}" />
120119
<TextBlock

Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,53 @@ internal static Result CreateFolderResult(string title, string subtitle, string
7777
};
7878
}
7979

80+
internal static Result CreateDriveSpaceDisplayResult(string path, bool windowsIndexed = false)
81+
{
82+
var progressBarColor = "#26a0da";
83+
int? progressValue = null;
84+
var title = string.Empty; // hide title when use progress bar,
85+
var driveLetter = path.Substring(0, 1).ToUpper();
86+
var driveName = driveLetter + ":\\";
87+
DriveInfo drv = new DriveInfo(driveLetter);
88+
var subtitle = toReadableSize(drv.AvailableFreeSpace, 2) + " free of " + toReadableSize(drv.TotalSize, 2);
89+
double UsingSize = (Convert.ToDouble(drv.TotalSize) - Convert.ToDouble(drv.AvailableFreeSpace)) / Convert.ToDouble(drv.TotalSize) * 100;
90+
91+
progressValue = Convert.ToInt32(UsingSize);
92+
93+
if (progressValue >= 90)
94+
progressBarColor = "#da2626";
95+
96+
return new Result
97+
{
98+
Title = title,
99+
SubTitle = subtitle,
100+
AutoCompleteText = GetPathWithActionKeyword(path, ResultType.Folder),
101+
IcoPath = path,
102+
Score = 500,
103+
ProgressBar = progressValue,
104+
ProgressBarColor = progressBarColor,
105+
Action = c =>
106+
{
107+
Context.API.OpenDirectory(path);
108+
return true;
109+
},
110+
TitleToolTip = path,
111+
SubTitleToolTip = path,
112+
ContextData = new SearchResult
113+
{
114+
Type = ResultType.Folder,
115+
FullPath = path,
116+
ShowIndexState = true,
117+
WindowsIndexed = windowsIndexed
118+
}
119+
};
120+
}
121+
80122
private static string toReadableSize(long pDrvSize, int pi)
81123
{
82124
int mok = 0;
83125
double drvSize = pDrvSize;
84126
string Space = "Byte";
85-
string returnStr = "";
86127

87128
while (drvSize > 1024.0)
88129
{
@@ -91,25 +132,33 @@ private static string toReadableSize(long pDrvSize, int pi)
91132
}
92133

93134
if (mok == 1)
94-
Space = "KB";
135+
Space = "KB";
95136
else if (mok == 2)
96137
Space = " MB";
97138
else if (mok == 3)
98139
Space = " GB";
99140
else if (mok == 4)
100141
Space = " TB";
101142

143+
var returnStr = string.Format("{0}{1}", Convert.ToInt32(drvSize), Space);
102144
if (mok != 0)
103-
if (pi == 1)
104-
returnStr = string.Format("{0:F1}{1}", drvSize, Space);
105-
else if (pi == 2)
106-
returnStr = string.Format("{0:F2}{1}", drvSize, Space);
107-
else if (pi == 3)
108-
returnStr = string.Format("{0:F3}{1}", drvSize, Space);
109-
else
110-
returnStr = string.Format("{0}{1}", Convert.ToInt32(drvSize), Space);
111-
else
112-
returnStr = string.Format("{0}{1}", Convert.ToInt32(drvSize), Space);
145+
{
146+
switch (pi)
147+
{
148+
case 1:
149+
returnStr = string.Format("{0:F1}{1}", drvSize, Space);
150+
break;
151+
case 2:
152+
returnStr = string.Format("{0:F2}{1}", drvSize, Space);
153+
break;
154+
case 3:
155+
returnStr = string.Format("{0:F3}{1}", drvSize, Space);
156+
break;
157+
default:
158+
returnStr = string.Format("{0}{1}", Convert.ToInt32(drvSize), Space);
159+
break;
160+
}
161+
}
113162

114163
return returnStr;
115164
}
@@ -123,28 +172,19 @@ internal static Result CreateOpenCurrentFolderResult(string path, bool windowsIn
123172
Path.DirectorySeparatorChar
124173
}, StringSplitOptions.None).Last();
125174

126-
var title = "Open current directory";
127-
128-
if (retrievedDirectoryPath != path)
129-
title = "Open " + folderName;
130-
131-
var subtitleFolderName = folderName;
132-
var subtitle = $"Use > to search within {subtitleFolderName}, " +
133-
$"* to search for file extensions or >* to combine both searches.";
134-
135-
int? progressBar = null;
136175
if (retrievedDirectoryPath.EndsWith(":\\"))
137176
{
138-
title = ""; // hide title when use progress bar,
139177
var driveLetter = path.Substring(0, 1).ToUpper();
140178
folderName = driveLetter + " drive";
141-
var driveName = driveLetter + ":\\";
142-
DriveInfo drv = new DriveInfo(driveLetter);
143-
subtitle = toReadableSize(drv.AvailableFreeSpace, 2) + " free of " + toReadableSize(drv.TotalSize, 2);
144-
double UsingSize = ((Convert.ToDouble(drv.TotalSize) - Convert.ToDouble(drv.AvailableFreeSpace)) / Convert.ToDouble(drv.TotalSize) * 100);
145-
progressBar = Convert.ToInt32(UsingSize);
146179
}
147180

181+
var title = "Open current directory";
182+
183+
if (retrievedDirectoryPath != path)
184+
title = "Open " + folderName;
185+
186+
187+
var subtitleFolderName = folderName;
148188

149189
// ie. max characters can be displayed without subtitle cutting off: "Program Files (x86)"
150190
if (folderName.Length > 19)
@@ -153,11 +193,11 @@ internal static Result CreateOpenCurrentFolderResult(string path, bool windowsIn
153193
return new Result
154194
{
155195
Title = title,
156-
SubTitle = subtitle,
196+
SubTitle = $"Use > to search within {subtitleFolderName}, " +
197+
$"* to search for file extensions or >* to combine both searches.",
157198
AutoCompleteText = GetPathWithActionKeyword(retrievedDirectoryPath, ResultType.Folder),
158199
IcoPath = retrievedDirectoryPath,
159200
Score = 500,
160-
ProgressBar = progressBar,
161201
Action = c =>
162202
{
163203
Context.API.OpenDirectory(retrievedDirectoryPath);

Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,14 @@ public async Task<List<Result>> PathSearchAsync(Query query, CancellationToken t
123123

124124
var useIndexSearch = UseWindowsIndexForDirectorySearch(locationPath);
125125

126-
results.Add(ResultManager.CreateOpenCurrentFolderResult(locationPath, useIndexSearch));
126+
if (locationPath.EndsWith(":\\"))
127+
{
128+
results.Add(ResultManager.CreateDriveSpaceDisplayResult(locationPath, useIndexSearch));
129+
}
130+
else
131+
{
132+
results.Add(ResultManager.CreateOpenCurrentFolderResult(locationPath, useIndexSearch));
133+
}
127134

128135
token.ThrowIfCancellationRequested();
129136

0 commit comments

Comments
 (0)