Skip to content

Commit 923a41f

Browse files
committed
take last modified date from project folder, add custom last modified date format setting, add human friendly last modified date option
1 parent b9492e7 commit 923a41f

File tree

10 files changed

+254
-11
lines changed

10 files changed

+254
-11
lines changed

UnityLauncherPro/App.config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@
8181
<setting name="runAutomatically" serializeAs="String">
8282
<value>False</value>
8383
</setting>
84+
<setting name="useCustomLastModified" serializeAs="String">
85+
<value>False</value>
86+
</setting>
87+
<setting name="customDateFormat" serializeAs="String">
88+
<value>dd/MM/yyyy HH:mm:ss</value>
89+
</setting>
90+
<setting name="useHumandFriendlyLastModified" serializeAs="String">
91+
<value>False</value>
92+
</setting>
8493
</UnityLauncherPro.Properties.Settings>
8594
</userSettings>
8695
</configuration>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace UnityLauncherPro.Converters
6+
{
7+
// https://stackoverflow.com/a/14283973/5452781
8+
[ValueConversion(typeof(DateTime), typeof(String))]
9+
public class LastModifiedConverter : IValueConverter
10+
{
11+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12+
{
13+
// TODO: without this, editor mode fails with null references.. but would be nice to get rid of if's..
14+
if (value == null) return null;
15+
16+
DateTime date = (DateTime)value;
17+
18+
return MainWindow.useHumanFriendlyDateFormat ? Tools.GetElapsedTime(date) : date.ToString(MainWindow.currentDateFormat);
19+
}
20+
21+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
22+
{
23+
return DateTime.ParseExact((string)value, MainWindow.currentDateFormat, culture);
24+
}
25+
26+
}
27+
}

UnityLauncherPro/Data/Project.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Globalization;
34
using System.Windows.Data;
45

56
namespace UnityLauncherPro
@@ -27,17 +28,16 @@ public override string ToString()
2728
}
2829

2930
// https://stackoverflow.com/a/5551986/5452781
30-
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
31+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
3132
{
3233
bool b = (bool)value;
3334
return b;
3435
//return string.IsNullOrEmpty(str);
3536
}
3637

37-
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
38+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
3839
{
3940
throw new NotSupportedException();
4041
}
41-
4242
}
4343
}

UnityLauncherPro/GetProjects.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public static List<Project> Scan(bool getGitBranch = false, bool getArguments =
9494
csprojFile = Path.Combine(projectPath, projectName + ".sln");
9595
}
9696

97-
// get last modified date
98-
DateTime? lastUpdated = folderExists ? Tools.GetLastModifiedTime(csprojFile) : null;
99-
97+
// get last modified date from folder
98+
DateTime? lastUpdated = folderExists ? Tools.GetLastModifiedTime(projectPath) : null;
99+
100100
// get project version
101101
string projectVersion = folderExists ? Tools.GetProjectVersion(projectPath) : null;
102102

UnityLauncherPro/MainWindow.xaml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:UnityLauncherPro"
7-
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="UnityLauncherPro.MainWindow"
7+
xmlns:System="clr-namespace:System;assembly=mscorlib"
8+
xmlns:converters="clr-namespace:UnityLauncherPro.Converters" x:Class="UnityLauncherPro.MainWindow"
89
mc:Ignorable="d"
910
Title="UnityLauncherPro" Height="650" Width="880" WindowStartupLocation="CenterScreen" Background="{DynamicResource ThemeDarkestBackground}" MinWidth="600" MinHeight="650" AllowsTransparency="True" WindowStyle="None" Margin="0" KeyDown="OnWindowKeyDown" Closing="Window_Closing" SizeChanged="Window_SizeChanged" Icon="Images/icon.ico" Activated="Window_Activated">
1011
<Window.Resources>
1112

13+
<converters:LastModifiedConverter x:Key="lastModifiedConverter"/>
14+
1215
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
1316
<Grid>
1417
<Grid.ColumnDefinitions>
@@ -485,7 +488,8 @@
485488
</Style>
486489
</DataGridTextColumn.CellStyle>
487490
</DataGridTextColumn>
488-
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Modified, StringFormat=\{0:dd/MM/yyyy HH:mm:ss\}}" ClipboardContentBinding="{x:Null}" Header="Modified" IsReadOnly="True" Width="120"/>
491+
<!--<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Modified, StringFormat=\{0:dd/MM/yyyy HH:mm:ss\}}" ClipboardContentBinding="{x:Null}" Header="Modified" IsReadOnly="True" Width="120"/>-->
492+
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Modified, Converter={StaticResource lastModifiedConverter}}" ClipboardContentBinding="{x:Null}" Header="Modified" IsReadOnly="True" Width="120"/>
489493
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Arguments}" ClipboardContentBinding="{x:Null}" Header="Arguments" IsReadOnly="False" Width="100"/>
490494
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding GITBranch}" ClipboardContentBinding="{x:Null}" Header="Branch" IsReadOnly="True" Width="60"/>
491495
<!--TODO platform as dropdown?-->
@@ -891,8 +895,13 @@
891895
</StackPanel>
892896

893897
<StackPanel Grid.Row="3" Orientation="Vertical" Margin="5,10,3,3" >
894-
<CheckBox x:Name="chkEnablePlatformSelection" Content="Enable Platform Selection (Experimental!)" Foreground="{DynamicResource ThemeButtonForeground}" Checked="ChkEnablePlatformSelection_Checked" Unchecked="ChkEnablePlatformSelection_Checked" Margin="0,0,0,4" ToolTip="" HorizontalAlignment="Left"/>
895-
<CheckBox x:Name="chkRunAutomatically" Content="Run this app automatically on startup" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,0,0,4" ToolTip="" HorizontalAlignment="Left" Checked="ChkRunAutomatically_Checked" Unchecked="ChkRunAutomatically_Checked"/>
898+
<CheckBox x:Name="chkEnablePlatformSelection" Content="Enable Platform Selection (Experimental!)" Foreground="{DynamicResource ThemeButtonForeground}" Checked="ChkEnablePlatformSelection_Checked" Unchecked="ChkEnablePlatformSelection_Checked" Margin="0,0,0,4" ToolTip="Select target platform" HorizontalAlignment="Left"/>
899+
<CheckBox x:Name="chkRunAutomatically" Content="Run this app automatically on startup" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,0,0,4" ToolTip="Run automatically using startup registry key" HorizontalAlignment="Left" Checked="ChkRunAutomatically_Checked" Unchecked="ChkRunAutomatically_Checked"/>
900+
<StackPanel Grid.Row="3" Orientation="Horizontal">
901+
<CheckBox x:Name="chkUseCustomLastModified" Content="Date format" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,0,0,4" ToolTip="Last modified date format" HorizontalAlignment="Left" Checked="ChkUseCustomLastModified_Checked" Unchecked="ChkUseCustomLastModified_Checked"/>
902+
<TextBox x:Name="txtCustomDateTimeFormat" MinWidth="150" ToolTip="Default is dd/MM/yyyy HH:mm:ss" Padding="0,3,0,0" Margin="5,0,0,0" Text="dd/MM/yyyy HH:mm:ss" LostFocus="TxtCustomDateTimeFormat_LostFocus" TextChanged="TxtCustomDateTimeFormat_TextChanged" />
903+
</StackPanel>
904+
<CheckBox x:Name="chkHumanFriendlyDateTime" Content="Use human friendly last modified" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,0,0,4" ToolTip="Last modified date format" HorizontalAlignment="Left" Checked="ChkHumanFriendlyDateTime_Checked" Unchecked="ChkHumanFriendlyDateTime_Checked"/>
896905
</StackPanel>
897906
</StackPanel>
898907

UnityLauncherPro/MainWindow.xaml.cs

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public partial class MainWindow : Window
4949
public static string preferredVersion = "none";
5050
Mutex myMutex;
5151

52+
string defaultDateFormat = "dd/MM/yyyy HH:mm:ss";
53+
public static string currentDateFormat = null;
54+
public static bool useHumanFriendlyDateFormat = false;
55+
5256
Dictionary<string, SolidColorBrush> origResourceColors = new Dictionary<string, SolidColorBrush>();
5357
string themefile = "theme.ini";
5458

@@ -299,6 +303,29 @@ void LoadSettings()
299303

300304
// other setting vars
301305
preferredVersion = Properties.Settings.Default.preferredVersion;
306+
307+
// get last modified date format
308+
chkUseCustomLastModified.IsChecked = Properties.Settings.Default.useCustomLastModified;
309+
txtCustomDateTimeFormat.Text = Properties.Settings.Default.customDateFormat;
310+
311+
312+
if (Properties.Settings.Default.useCustomLastModified)
313+
{
314+
currentDateFormat = Properties.Settings.Default.customDateFormat;
315+
}
316+
else // use default
317+
{
318+
currentDateFormat = defaultDateFormat;
319+
}
320+
321+
chkHumanFriendlyDateTime.IsChecked = Properties.Settings.Default.useHumandFriendlyLastModified;
322+
// if both enabled, then disable custom
323+
if (chkHumanFriendlyDateTime.IsChecked == true && chkUseCustomLastModified.IsChecked == true)
324+
{
325+
chkUseCustomLastModified.IsChecked = false;
326+
}
327+
328+
useHumanFriendlyDateFormat = Properties.Settings.Default.useHumandFriendlyLastModified;
302329
} // LoadSettings()
303330

304331
private void SaveSettingsOnExit()
@@ -720,7 +747,7 @@ private void MenuItemCopyUpdateDownloadURL_Click(object sender, RoutedEventArgs
720747
{
721748
string copy = null;
722749
var unity = GetSelectedUpdate();
723-
copy = unity?.Version;https://unity3d.com/get-unity/download?thank-you=update&download_nid=65083&os=Win
750+
copy = unity?.Version; https://unity3d.com/get-unity/download?thank-you=update&download_nid=65083&os=Win
724751
string exeURL = Tools.ParseDownloadURLFromWebpage(copy);
725752
if (exeURL != null) Clipboard.SetText(exeURL);
726753
}
@@ -1897,6 +1924,76 @@ private void ChkRunAutomatically_Checked(object sender, RoutedEventArgs e)
18971924
Tools.SetStartupRegistry(isChecked);
18981925
}
18991926

1927+
private void ChkUseCustomLastModified_Checked(object sender, RoutedEventArgs e)
1928+
{
1929+
if (this.IsActive == false) return; // dont run code on window init
1930+
1931+
var isChecked = (bool)((CheckBox)sender).IsChecked;
1932+
chkUseCustomLastModified.IsChecked = isChecked;
1933+
Properties.Settings.Default.useCustomLastModified = isChecked;
1934+
Properties.Settings.Default.Save();
1935+
1936+
if (isChecked)
1937+
{
1938+
ValidateCustomDateFormat(txtCustomDateTimeFormat.Text);
1939+
}
1940+
else
1941+
{
1942+
currentDateFormat = defaultDateFormat;
1943+
}
1944+
}
1945+
1946+
private void TxtCustomDateTimeFormat_LostFocus(object sender, RoutedEventArgs e)
1947+
{
1948+
if (this.IsActive == false) return; // dont run code on window init
1949+
TextBox textBox = sender as TextBox;
1950+
ValidateCustomDateFormat(textBox.Text);
1951+
}
1952+
1953+
private void TxtCustomDateTimeFormat_TextChanged(object sender, TextChangedEventArgs e)
1954+
{
1955+
if (this.IsActive == false) return; // dont run code on window init
1956+
TextBox textBox = sender as TextBox;
1957+
ValidateCustomDateFormat(textBox.Text);
1958+
}
1959+
1960+
void ValidateCustomDateFormat(string format)
1961+
{
1962+
if (Tools.ValidateDateFormat(format))
1963+
{
1964+
currentDateFormat = format;
1965+
Properties.Settings.Default.customDateFormat = currentDateFormat;
1966+
Properties.Settings.Default.Save();
1967+
txtCustomDateTimeFormat.Foreground = System.Windows.Media.Brushes.Black;
1968+
}
1969+
else // invalid format
1970+
{
1971+
txtCustomDateTimeFormat.Foreground = System.Windows.Media.Brushes.Red;
1972+
currentDateFormat = defaultDateFormat;
1973+
}
1974+
}
1975+
1976+
private void ChkHumanFriendlyDateTime_Checked(object sender, RoutedEventArgs e)
1977+
{
1978+
if (this.IsActive == false) return; // dont run code on window init
1979+
var isChecked = (bool)((CheckBox)sender).IsChecked;
1980+
1981+
// cannot have both date formats
1982+
if (isChecked == true)
1983+
{
1984+
if (chkUseCustomLastModified.IsChecked == true) chkUseCustomLastModified.IsChecked = false;
1985+
}
1986+
else
1987+
{
1988+
currentDateFormat = defaultDateFormat;
1989+
}
1990+
1991+
useHumanFriendlyDateFormat = isChecked;
1992+
1993+
Properties.Settings.Default.useHumandFriendlyLastModified = isChecked;
1994+
Properties.Settings.Default.Save();
1995+
}
1996+
19001997
//private void CmbPlatformSelection_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
19011998
//{
19021999
// var comb = (ComboBox)sender;

UnityLauncherPro/Properties/Settings.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityLauncherPro/Properties/Settings.settings

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,14 @@
7474
<Setting Name="runAutomatically" Type="System.Boolean" Scope="User">
7575
<Value Profile="(Default)">False</Value>
7676
</Setting>
77+
<Setting Name="useCustomLastModified" Type="System.Boolean" Scope="User">
78+
<Value Profile="(Default)">False</Value>
79+
</Setting>
80+
<Setting Name="customDateFormat" Type="System.String" Scope="User">
81+
<Value Profile="(Default)">dd/MM/yyyy HH:mm:ss</Value>
82+
</Setting>
83+
<Setting Name="useHumandFriendlyLastModified" Type="System.Boolean" Scope="User">
84+
<Value Profile="(Default)">False</Value>
85+
</Setting>
7786
</Settings>
7887
</SettingsFile>

UnityLauncherPro/Tools.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,5 +1042,60 @@ public static Dictionary<string, string> ScanTemplates(string unityInstallPath)
10421042
return items;
10431043
}
10441044

1045+
// https://codereview.stackexchange.com/a/93247
1046+
public static string GetElapsedTime(DateTime datetime)
1047+
{
1048+
TimeSpan ts = DateTime.Now.Subtract(datetime);
1049+
1050+
// The trick: make variable contain date and time representing the desired timespan,
1051+
// having +1 in each date component.
1052+
DateTime date = DateTime.MinValue + ts;
1053+
1054+
return ProcessPeriod(date.Year - 1, date.Month - 1, "year")
1055+
?? ProcessPeriod(date.Month - 1, date.Day - 1, "month")
1056+
?? ProcessPeriod(date.Day - 1, date.Hour, "day", "Yesterday")
1057+
?? ProcessPeriod(date.Hour, date.Minute, "hour")
1058+
?? ProcessPeriod(date.Minute, date.Second, "minute")
1059+
?? ProcessPeriod(date.Second, 0, "second")
1060+
?? "Right now";
1061+
}
1062+
1063+
private static string ProcessPeriod(int value, int subValue, string name, string singularName = null)
1064+
{
1065+
if (value == 0)
1066+
{
1067+
return null;
1068+
}
1069+
if (value == 1)
1070+
{
1071+
if (!String.IsNullOrEmpty(singularName))
1072+
{
1073+
return singularName;
1074+
}
1075+
string articleSuffix = name[0] == 'h' ? "n" : String.Empty;
1076+
return subValue == 0
1077+
? String.Format("A{0} {1} ago", articleSuffix, name)
1078+
: String.Format("a{0} {1} ago", articleSuffix, name);
1079+
}
1080+
return subValue == 0
1081+
? String.Format("{0} {1}s ago", value, name)
1082+
: String.Format("{0} {1}s ago", value, name);
1083+
}
1084+
1085+
public static bool ValidateDateFormat(string format)
1086+
{
1087+
try
1088+
{
1089+
String formattedDate = DateTime.Now.ToString(format);
1090+
DateTime.Parse(formattedDate);
1091+
return true;
1092+
}
1093+
catch (Exception)
1094+
{
1095+
//Console.WriteLine("Invalid custom datetime format: " + format);
1096+
return false;
1097+
}
1098+
}
1099+
10451100
} // class
10461101
} // namespace

UnityLauncherPro/UnityLauncherPro.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Generator>MSBuild:Compile</Generator>
7676
<SubType>Designer</SubType>
7777
</ApplicationDefinition>
78+
<Compile Include="Converters\LastModifiedConverter.cs" />
7879
<Compile Include="Data\BuildReport.cs" />
7980
<Compile Include="Data\Platform.cs" />
8081
<Compile Include="Data\Tabs.cs" />

0 commit comments

Comments
 (0)