forked from Flow-Launcher/Flow.Launcher
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreviewPanel.xaml.cs
More file actions
130 lines (108 loc) · 4.44 KB
/
PreviewPanel.xaml.cs
File metadata and controls
130 lines (108 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Flow.Launcher.Plugin.Explorer.Search;
namespace Flow.Launcher.Plugin.Explorer.Views;
#nullable enable
public partial class PreviewPanel : UserControl, INotifyPropertyChanged
{
private string FilePath { get; }
public string FileSize { get; } = "";
public string CreatedAt { get; } = "";
public string LastModifiedAt { get; } = "";
private ImageSource _previewImage = new BitmapImage();
private Settings Settings { get; }
public ImageSource PreviewImage
{
get => _previewImage;
private set
{
_previewImage = value;
OnPropertyChanged();
}
}
public Visibility FileSizeVisibility => Settings.ShowFileSizeInPreviewPanel
? Visibility.Visible
: Visibility.Collapsed;
public Visibility CreatedAtVisibility => Settings.ShowCreatedDateInPreviewPanel
? Visibility.Visible
: Visibility.Collapsed;
public Visibility LastModifiedAtVisibility => Settings.ShowModifiedDateInPreviewPanel
? Visibility.Visible
: Visibility.Collapsed;
public Visibility FileInfoVisibility =>
Settings.ShowFileSizeInPreviewPanel ||
Settings.ShowCreatedDateInPreviewPanel ||
Settings.ShowModifiedDateInPreviewPanel
? Visibility.Visible
: Visibility.Collapsed;
public PreviewPanel(Settings settings, string filePath)
{
InitializeComponent();
Settings = settings;
FilePath = filePath;
if (Settings.ShowFileSizeInPreviewPanel)
{
var fileSize = new FileInfo(filePath).Length;
FileSize = ResultManager.ToReadableSize(fileSize, 2);
}
if (Settings.ShowCreatedDateInPreviewPanel)
{
DateTime createdDate = File.GetCreationTime(filePath);
string formattedDate = createdDate.ToString(
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
CultureInfo.CurrentCulture
);
string result = formattedDate;
if (Settings.ShowFileAgeInPreviewPanel) result = $"{GetFileAge(createdDate)} - {formattedDate}";
CreatedAt = result;
}
if (Settings.ShowModifiedDateInPreviewPanel)
{
DateTime lastModifiedDate = File.GetLastWriteTime(filePath);
string formattedDate = lastModifiedDate.ToString(
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
CultureInfo.CurrentCulture
);
string result = formattedDate;
if (Settings.ShowFileAgeInPreviewPanel) result = $"{GetFileAge(lastModifiedDate)} - {formattedDate}";
LastModifiedAt = result;
}
_ = LoadImageAsync();
}
private async Task LoadImageAsync()
{
PreviewImage = await Main.Context.API.LoadImageAsync(FilePath, true).ConfigureAwait(false);
}
private static string GetFileAge(DateTime fileDateTime)
{
var now = DateTime.Now;
var difference = now - fileDateTime;
if (difference.TotalDays < 1)
return Main.Context.API.GetTranslation("Today");
if (difference.TotalDays < 30)
return string.Format(Main.Context.API.GetTranslation("DaysAgo"), (int)difference.TotalDays);
var monthsDiff = (now.Year - fileDateTime.Year) * 12 + now.Month - fileDateTime.Month;
if (monthsDiff == 1)
return Main.Context.API.GetTranslation("OneMonthAgo");
if (monthsDiff < 12)
return string.Format(Main.Context.API.GetTranslation("MonthsAgo"), monthsDiff);
var yearsDiff = now.Year - fileDateTime.Year;
if (now.Month < fileDateTime.Month || (now.Month == fileDateTime.Month && now.Day < fileDateTime.Day))
yearsDiff--;
return yearsDiff == 1 ? Main.Context.API.GetTranslation("OneYearAgo") :
string.Format(Main.Context.API.GetTranslation("YearsAgo"), yearsDiff);
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}