Skip to content

Commit 4940449

Browse files
committed
Use converter to display datetime for formats
1 parent 95e6c19 commit 4940449

File tree

4 files changed

+69
-96
lines changed

4 files changed

+69
-96
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace Flow.Launcher.Converters
6+
{
7+
public class DateTimeFormatToNowConverter : IValueConverter
8+
{
9+
10+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
11+
{
12+
return value is not string format ? null : DateTime.Now.ToString(format);
13+
}
14+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
15+
{
16+
throw new NotImplementedException();
17+
}
18+
}
19+
}

Flow.Launcher/SettingWindow.xaml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
4545
<converters:TextConverter x:Key="TextConverter" />
4646
<converters:TranlationConverter x:Key="TranlationConverter" />
47+
<converters:DateTimeFormatToNowConverter x:Key="DateTimeFormatToNowConverter"></converters:DateTimeFormatToNowConverter>
4748
<CollectionViewSource x:Key="SortedFonts" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
4849
<CollectionViewSource.SortDescriptions>
4950
<scm:SortDescription PropertyName="Source" />
@@ -2179,9 +2180,14 @@
21792180
Margin="0,0,18,0"
21802181
VerticalAlignment="Center"
21812182
FontSize="14"
2182-
ItemsSource="{Binding TimeFormatDisplayList}"
2183-
SelectedIndex="{Binding TimeFormatIndex, Mode=OneWayToSource}"
2184-
DropDownOpened="RefreshTimeList"/>
2183+
ItemsSource="{Binding TimeFormatList}"
2184+
SelectedIndex="{Binding TimeFormat}">
2185+
<ItemsControl.ItemTemplate>
2186+
<DataTemplate>
2187+
<TextBlock Text="{Binding Converter={StaticResource DateTimeFormatToNowConverter}}"/>
2188+
</DataTemplate>
2189+
</ItemsControl.ItemTemplate>
2190+
</ComboBox>
21852191
<ui:ToggleSwitch
21862192
IsOn="{Binding UseClock, Mode=TwoWay}"
21872193
OffContent="{DynamicResource disable}"
@@ -2216,9 +2222,14 @@
22162222
Margin="0,0,18,0"
22172223
VerticalAlignment="Center"
22182224
FontSize="14"
2219-
ItemsSource="{Binding DateFormatDisplayList}"
2220-
SelectedIndex="{Binding DateFormatIndex, Mode=OneWayToSource}"
2221-
DropDownOpened="RefreshDateList"/>
2225+
ItemsSource="{Binding DateFormatList}"
2226+
SelectedIndex="{Binding DateFormat}">
2227+
<ItemsControl.ItemTemplate>
2228+
<DataTemplate>
2229+
<TextBlock Text="{Binding Converter={StaticResource DateTimeFormatToNowConverter}}"/>
2230+
</DataTemplate>
2231+
</ItemsControl.ItemTemplate>
2232+
</ComboBox>
22222233
<ui:ToggleSwitch
22232234
IsOn="{Binding UseDate, Mode=TwoWay}"
22242235
OffContent="{DynamicResource disable}"

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public SettingWindow(IPublicAPI api, SettingWindowViewModel viewModel)
4242
InitializePosition();
4343
InitializeComponent();
4444

45-
RefreshDateListInternal(viewModel.DateFormatIndex);
46-
RefreshTimeListInternal(viewModel.TimeFormatIndex);
4745
}
4846

4947
#region General
@@ -563,28 +561,6 @@ private void StoreListItem_Click(object sender, RoutedEventArgs e)
563561

564562
}
565563

566-
private void RefreshDateList(object sender, EventArgs e)
567-
{
568-
RefreshDateListInternal(DateFormat.SelectedIndex);
569-
}
570-
571-
private void RefreshDateListInternal(int index)
572-
{
573-
viewModel.UpdateDateDisplayList();
574-
RefreshComboBox(DateFormat, index);
575-
}
576-
577-
private void RefreshTimeList(object sender, EventArgs e)
578-
{
579-
RefreshTimeListInternal(TimeFormat.SelectedIndex);
580-
}
581-
582-
private void RefreshTimeListInternal(int index)
583-
{
584-
viewModel.UpdateTimeDisplayList();
585-
RefreshComboBox(TimeFormat, index);
586-
}
587-
588564
private static void RefreshComboBox(System.Windows.Controls.ComboBox box, int index)
589565
{
590566
box.Items.Refresh();

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ public SettingWindowViewModel(Updater updater, IPortable portable)
4949
break;
5050
}
5151
};
52-
int tmp = 0;
53-
TimeFormatIndex = (tmp = TimeFormatList.FindIndex(x => x.Equals(Settings.TimeFormat))) >= 0 ? tmp : 0;
54-
DateFormatIndex = (tmp = DateFormatList.FindIndex(x => x.Equals(Settings.DateFormat))) >= 0 ? tmp : 0;
55-
// TODO: CurrentCulture may not equal to settings.language when this is constructed
56-
TimeFormatDisplayList = TimeFormatList.Select(x => DateTime.Now.ToString(x, CultureInfo.CurrentCulture)).ToList();
57-
DateFormatDisplayList = DateFormatList.Select(x => DateTime.Now.ToString(x, CultureInfo.CurrentCulture)).ToList();
5852
}
5953

6054
public Settings Settings { get; set; }
@@ -164,7 +158,10 @@ private List<LastQueryMode> InitLastQueryModes()
164158
{
165159
var key = $"LastQuery{e}";
166160
var display = _translater.GetTranslation(key);
167-
var m = new LastQueryMode { Display = display, Value = e, };
161+
var m = new LastQueryMode
162+
{
163+
Display = display, Value = e,
164+
};
168165
modes.Add(m);
169166
}
170167
return modes;
@@ -355,7 +352,6 @@ internal void DisplayPluginQuery(string queryToDisplay, PluginPair plugin, int a
355352
App.API.ShowMainWindow();
356353
}
357354

358-
359355
#endregion
360356

361357
#region theme
@@ -420,8 +416,7 @@ public List<ColorScheme> ColorSchemes
420416
var display = _translater.GetTranslation(key);
421417
var m = new ColorScheme
422418
{
423-
Display = display,
424-
Value = e,
419+
Display = display, Value = e,
425420
};
426421
modes.Add(m);
427422
}
@@ -445,14 +440,17 @@ public List<SearchWindowPosition> SearchWindowPositions
445440
{
446441
var key = $"SearchWindowPosition{e}";
447442
var display = _translater.GetTranslation(key);
448-
var m = new SearchWindowPosition { Display = display, Value = e, };
443+
var m = new SearchWindowPosition
444+
{
445+
Display = display, Value = e,
446+
};
449447
modes.Add(m);
450448
}
451449
return modes;
452450
}
453451
}
454452

455-
public List<string> TimeFormatList { get; set; } = new List<string>()
453+
public List<string> TimeFormatList { get; } = new()
456454
{
457455
"h:mm",
458456
"hh:mm",
@@ -464,7 +462,7 @@ public List<SearchWindowPosition> SearchWindowPositions
464462
"hh:mm tt"
465463
};
466464

467-
public List<string> DateFormatList { get; set; } = new List<string>()
465+
public List<string> DateFormatList { get; } = new()
468466
{
469467
"MM'/'dd dddd",
470468
"MM'/'dd ddd",
@@ -482,65 +480,30 @@ public List<SearchWindowPosition> SearchWindowPositions
482480
"dd', 'MMMM"
483481
};
484482

485-
private int timeFormatIndex = 0;
486-
public int TimeFormatIndex
483+
public string TimeFormat
487484
{
488-
get => timeFormatIndex;
485+
get { return Settings.TimeFormat; }
489486
set
490487
{
491-
if (value != -1)
492-
{
493-
timeFormatIndex = value;
494-
Settings.TimeFormat = TimeFormatList[value];
495-
ClockText = DateTime.Now.ToString(Settings.TimeFormat, CultureInfo.CurrentCulture);
496-
}
488+
Settings.TimeFormat = value;
489+
OnPropertyChanged();
497490
}
498491
}
499492

500-
private int dateFormatIndex = 0;
501-
public int DateFormatIndex
493+
public string DateFormat
502494
{
503-
get => dateFormatIndex;
504-
set
505-
{
506-
if (value != -1)
507-
{
508-
dateFormatIndex = value;
509-
Settings.DateFormat = DateFormatList[value];
510-
DateText = DateTime.Now.ToString(Settings.DateFormat, CultureInfo.CurrentCulture);
511-
}
495+
get { return Settings.DateFormat; }
496+
set
497+
{
498+
Settings.DateFormat = value;
499+
OnPropertyChanged();
512500
}
513501
}
514502

515503
public string ClockText { get; private set; }
516504

517505
public string DateText { get; private set; }
518506

519-
public List<string> TimeFormatDisplayList { get; set; } = null;
520-
521-
public List<string> DateFormatDisplayList { get; set; } = null;
522-
523-
public void UpdateDateDisplayList()
524-
{
525-
for (int i = 0; i < DateFormatList.Count; ++i)
526-
{
527-
DateFormatDisplayList[i] = DateTime.Now.ToString(DateFormatList[i], CultureInfo.CurrentCulture);
528-
}
529-
// TODO: CurrentCulture may not equal to settings.language
530-
// Cross thread issue?
531-
DateText = DateTime.Now.ToString(Settings.DateFormat, CultureInfo.CurrentCulture);
532-
}
533-
534-
public void UpdateTimeDisplayList()
535-
{
536-
for (int i = 0; i < TimeFormatList.Count; ++i)
537-
{
538-
TimeFormatDisplayList[i] = DateTime.Now.ToString(TimeFormatList[i], CultureInfo.CurrentCulture);
539-
}
540-
// TODO: CurrentCulture may not equal to settings.language
541-
// Cross thread issue?
542-
ClockText = DateTime.Now.ToString(Settings.TimeFormat, CultureInfo.CurrentCulture);
543-
}
544507

545508
public double WindowWidthSize
546509
{
@@ -783,7 +746,7 @@ public void DeleteSelectedCustomShortcut()
783746

784747
string deleteWarning = string.Format(
785748
InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
786-
item.Key, item.Value);
749+
item.Key, item.Value);
787750
if (MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
788751
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
789752
{
@@ -864,19 +827,22 @@ public string CheckLogFolder
864827
internal void ClearLogFolder()
865828
{
866829
var directory = new DirectoryInfo(
867-
Path.Combine(
868-
DataLocation.DataDirectory(),
869-
Constant.Logs,
870-
Constant.Version));
830+
Path.Combine(
831+
DataLocation.DataDirectory(),
832+
Constant.Logs,
833+
Constant.Version));
871834

872835
directory.EnumerateFiles()
873-
.ToList()
874-
.ForEach(x => x.Delete());
836+
.ToList()
837+
.ForEach(x => x.Delete());
875838
}
876839
internal string FormatBytes(long bytes)
877840
{
878841
const int scale = 1024;
879-
string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
842+
string[] orders = new string[]
843+
{
844+
"GB", "MB", "KB", "Bytes"
845+
};
880846
long max = (long)Math.Pow(scale, orders.Length - 1);
881847

882848
foreach (string order in orders)
@@ -888,6 +854,7 @@ internal string FormatBytes(long bytes)
888854
}
889855
return "0 Bytes";
890856
}
857+
891858
#endregion
892859
}
893860
}

0 commit comments

Comments
 (0)