Use converter to display datetime for formats

This commit is contained in:
Hongtao Zhang 2022-11-19 16:16:11 -06:00
parent 95e6c19cef
commit 4940449b41
No known key found for this signature in database
GPG key ID: 75F655B91C7AC9BB
4 changed files with 69 additions and 96 deletions

View file

@ -0,0 +1,19 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace Flow.Launcher.Converters
{
public class DateTimeFormatToNowConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is not string format ? null : DateTime.Now.ToString(format);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View file

@ -44,6 +44,7 @@
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<converters:TextConverter x:Key="TextConverter" />
<converters:TranlationConverter x:Key="TranlationConverter" />
<converters:DateTimeFormatToNowConverter x:Key="DateTimeFormatToNowConverter"></converters:DateTimeFormatToNowConverter>
<CollectionViewSource x:Key="SortedFonts" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Source" />
@ -2179,9 +2180,14 @@
Margin="0,0,18,0"
VerticalAlignment="Center"
FontSize="14"
ItemsSource="{Binding TimeFormatDisplayList}"
SelectedIndex="{Binding TimeFormatIndex, Mode=OneWayToSource}"
DropDownOpened="RefreshTimeList"/>
ItemsSource="{Binding TimeFormatList}"
SelectedIndex="{Binding TimeFormat}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource DateTimeFormatToNowConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ComboBox>
<ui:ToggleSwitch
IsOn="{Binding UseClock, Mode=TwoWay}"
OffContent="{DynamicResource disable}"
@ -2216,9 +2222,14 @@
Margin="0,0,18,0"
VerticalAlignment="Center"
FontSize="14"
ItemsSource="{Binding DateFormatDisplayList}"
SelectedIndex="{Binding DateFormatIndex, Mode=OneWayToSource}"
DropDownOpened="RefreshDateList"/>
ItemsSource="{Binding DateFormatList}"
SelectedIndex="{Binding DateFormat}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource DateTimeFormatToNowConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ComboBox>
<ui:ToggleSwitch
IsOn="{Binding UseDate, Mode=TwoWay}"
OffContent="{DynamicResource disable}"

View file

@ -42,8 +42,6 @@ namespace Flow.Launcher
InitializePosition();
InitializeComponent();
RefreshDateListInternal(viewModel.DateFormatIndex);
RefreshTimeListInternal(viewModel.TimeFormatIndex);
}
#region General
@ -563,28 +561,6 @@ namespace Flow.Launcher
}
private void RefreshDateList(object sender, EventArgs e)
{
RefreshDateListInternal(DateFormat.SelectedIndex);
}
private void RefreshDateListInternal(int index)
{
viewModel.UpdateDateDisplayList();
RefreshComboBox(DateFormat, index);
}
private void RefreshTimeList(object sender, EventArgs e)
{
RefreshTimeListInternal(TimeFormat.SelectedIndex);
}
private void RefreshTimeListInternal(int index)
{
viewModel.UpdateTimeDisplayList();
RefreshComboBox(TimeFormat, index);
}
private static void RefreshComboBox(System.Windows.Controls.ComboBox box, int index)
{
box.Items.Refresh();

View file

@ -49,12 +49,6 @@ namespace Flow.Launcher.ViewModel
break;
}
};
int tmp = 0;
TimeFormatIndex = (tmp = TimeFormatList.FindIndex(x => x.Equals(Settings.TimeFormat))) >= 0 ? tmp : 0;
DateFormatIndex = (tmp = DateFormatList.FindIndex(x => x.Equals(Settings.DateFormat))) >= 0 ? tmp : 0;
// TODO: CurrentCulture may not equal to settings.language when this is constructed
TimeFormatDisplayList = TimeFormatList.Select(x => DateTime.Now.ToString(x, CultureInfo.CurrentCulture)).ToList();
DateFormatDisplayList = DateFormatList.Select(x => DateTime.Now.ToString(x, CultureInfo.CurrentCulture)).ToList();
}
public Settings Settings { get; set; }
@ -164,7 +158,10 @@ namespace Flow.Launcher.ViewModel
{
var key = $"LastQuery{e}";
var display = _translater.GetTranslation(key);
var m = new LastQueryMode { Display = display, Value = e, };
var m = new LastQueryMode
{
Display = display, Value = e,
};
modes.Add(m);
}
return modes;
@ -355,7 +352,6 @@ namespace Flow.Launcher.ViewModel
App.API.ShowMainWindow();
}
#endregion
#region theme
@ -420,8 +416,7 @@ namespace Flow.Launcher.ViewModel
var display = _translater.GetTranslation(key);
var m = new ColorScheme
{
Display = display,
Value = e,
Display = display, Value = e,
};
modes.Add(m);
}
@ -445,14 +440,17 @@ namespace Flow.Launcher.ViewModel
{
var key = $"SearchWindowPosition{e}";
var display = _translater.GetTranslation(key);
var m = new SearchWindowPosition { Display = display, Value = e, };
var m = new SearchWindowPosition
{
Display = display, Value = e,
};
modes.Add(m);
}
return modes;
}
}
public List<string> TimeFormatList { get; set; } = new List<string>()
public List<string> TimeFormatList { get; } = new()
{
"h:mm",
"hh:mm",
@ -464,7 +462,7 @@ namespace Flow.Launcher.ViewModel
"hh:mm tt"
};
public List<string> DateFormatList { get; set; } = new List<string>()
public List<string> DateFormatList { get; } = new()
{
"MM'/'dd dddd",
"MM'/'dd ddd",
@ -482,33 +480,23 @@ namespace Flow.Launcher.ViewModel
"dd', 'MMMM"
};
private int timeFormatIndex = 0;
public int TimeFormatIndex
public string TimeFormat
{
get => timeFormatIndex;
get { return Settings.TimeFormat; }
set
{
if (value != -1)
{
timeFormatIndex = value;
Settings.TimeFormat = TimeFormatList[value];
ClockText = DateTime.Now.ToString(Settings.TimeFormat, CultureInfo.CurrentCulture);
}
Settings.TimeFormat = value;
OnPropertyChanged();
}
}
private int dateFormatIndex = 0;
public int DateFormatIndex
public string DateFormat
{
get => dateFormatIndex;
set
{
if (value != -1)
{
dateFormatIndex = value;
Settings.DateFormat = DateFormatList[value];
DateText = DateTime.Now.ToString(Settings.DateFormat, CultureInfo.CurrentCulture);
}
get { return Settings.DateFormat; }
set
{
Settings.DateFormat = value;
OnPropertyChanged();
}
}
@ -516,31 +504,6 @@ namespace Flow.Launcher.ViewModel
public string DateText { get; private set; }
public List<string> TimeFormatDisplayList { get; set; } = null;
public List<string> DateFormatDisplayList { get; set; } = null;
public void UpdateDateDisplayList()
{
for (int i = 0; i < DateFormatList.Count; ++i)
{
DateFormatDisplayList[i] = DateTime.Now.ToString(DateFormatList[i], CultureInfo.CurrentCulture);
}
// TODO: CurrentCulture may not equal to settings.language
// Cross thread issue?
DateText = DateTime.Now.ToString(Settings.DateFormat, CultureInfo.CurrentCulture);
}
public void UpdateTimeDisplayList()
{
for (int i = 0; i < TimeFormatList.Count; ++i)
{
TimeFormatDisplayList[i] = DateTime.Now.ToString(TimeFormatList[i], CultureInfo.CurrentCulture);
}
// TODO: CurrentCulture may not equal to settings.language
// Cross thread issue?
ClockText = DateTime.Now.ToString(Settings.TimeFormat, CultureInfo.CurrentCulture);
}
public double WindowWidthSize
{
@ -783,7 +746,7 @@ namespace Flow.Launcher.ViewModel
string deleteWarning = string.Format(
InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
item.Key, item.Value);
item.Key, item.Value);
if (MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
@ -864,19 +827,22 @@ namespace Flow.Launcher.ViewModel
internal void ClearLogFolder()
{
var directory = new DirectoryInfo(
Path.Combine(
DataLocation.DataDirectory(),
Constant.Logs,
Constant.Version));
Path.Combine(
DataLocation.DataDirectory(),
Constant.Logs,
Constant.Version));
directory.EnumerateFiles()
.ToList()
.ForEach(x => x.Delete());
.ToList()
.ForEach(x => x.Delete());
}
internal string FormatBytes(long bytes)
{
const int scale = 1024;
string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
string[] orders = new string[]
{
"GB", "MB", "KB", "Bytes"
};
long max = (long)Math.Pow(scale, orders.Length - 1);
foreach (string order in orders)
@ -888,6 +854,7 @@ namespace Flow.Launcher.ViewModel
}
return "0 Bytes";
}
#endregion
}
}