Skip to content

Commit c81dd32

Browse files
authored
Merge pull request #1365 from onesounds/CheckfreeSpace
Display Drive Space
2 parents 05044ae + e23eb94 commit c81dd32

File tree

5 files changed

+126
-3
lines changed

5 files changed

+126
-3
lines changed

Flow.Launcher.Plugin/Result.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,16 @@ public ValueTask<bool> ExecuteAsync(ActionContext context)
197197
{
198198
return AsyncAction?.Invoke(context) ?? ValueTask.FromResult(Action?.Invoke(context) ?? false);
199199
}
200+
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>
204+
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";
200211
}
201212
}

Flow.Launcher/ResultListBox.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@
111111
<RowDefinition />
112112
<RowDefinition x:Name="SubTitleRowDefinition" Height="Auto" />
113113
</Grid.RowDefinitions>
114-
114+
<ProgressBar
115+
x:Name="progressbarResult"
116+
Foreground="{Binding Result.ProgressBarColor}"
117+
Style="{DynamicResource ProgressBarResult}"
118+
Value="{Binding Result.ProgressBar}" />
115119
<TextBlock
116120
x:Name="Title"
117121
VerticalAlignment="Center"

Flow.Launcher/Themes/Base.xaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@
8181

8282

8383
<!-- Item Style -->
84+
<Style x:Key="ProgressBarResult" TargetType="{x:Type ProgressBar}">
85+
<Setter Property="Height" Value="18" />
86+
<Setter Property="Margin" Value="0,0,0,4" />
87+
<Setter Property="VerticalAlignment" Value="Center" />
88+
<Setter Property="Maximum" Value="100" />
89+
<Setter Property="Minimum" Value="0" />
90+
<Setter Property="Visibility" Value="Visible" />
91+
<Setter Property="Foreground" Value="#26a0da " />
92+
<Style.Triggers>
93+
<DataTrigger Binding="{Binding Result.ProgressBar}" Value="{x:Null}">
94+
<Setter Property="Visibility" Value="Collapsed" />
95+
</DataTrigger>
96+
</Style.Triggers>
97+
</Style>
98+
8499
<Style x:Key="BaseItemTitleStyle" TargetType="{x:Type TextBlock}">
85100
<Setter Property="Foreground" Value="#FFFFF8" />
86101
<Setter Property="FontSize" Value="16" />

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,92 @@ 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+
122+
private static string toReadableSize(long pDrvSize, int pi)
123+
{
124+
int mok = 0;
125+
double drvSize = pDrvSize;
126+
string Space = "Byte";
127+
128+
while (drvSize > 1024.0)
129+
{
130+
drvSize /= 1024.0;
131+
mok++;
132+
}
133+
134+
if (mok == 1)
135+
Space = "KB";
136+
else if (mok == 2)
137+
Space = " MB";
138+
else if (mok == 3)
139+
Space = " GB";
140+
else if (mok == 4)
141+
Space = " TB";
142+
143+
var returnStr = string.Format("{0}{1}", Convert.ToInt32(drvSize), Space);
144+
if (mok != 0)
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+
}
162+
163+
return returnStr;
164+
}
165+
80166
internal static Result CreateOpenCurrentFolderResult(string path, bool windowsIndexed = false)
81167
{
82168
var retrievedDirectoryPath = FilesFolders.ReturnPreviousDirectoryIfIncompleteString(path);
@@ -208,4 +294,4 @@ public enum ResultType
208294
Folder,
209295
File
210296
}
211-
}
297+
}

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)