Display current time in time/date format combobox

This commit is contained in:
Vic 2022-11-19 00:47:04 +08:00
parent 3426c94600
commit 7270c9f35d
3 changed files with 57 additions and 6 deletions

View file

@ -2169,8 +2169,8 @@
Margin="0,0,18,0"
VerticalAlignment="Center"
FontSize="14"
ItemsSource="{Binding TimeFormatList}"
SelectedValue="{Binding Settings.TimeFormat}"
ItemsSource="{Binding TimeFormatDisplayList}"
SelectedIndex="{Binding TimeFormatIndex, Mode=OneTime}"
SelectionChanged="PreviewClockAndDate" />
<ui:ToggleSwitch
IsOn="{Binding UseClock, Mode=TwoWay}"
@ -2207,8 +2207,8 @@
Margin="0,0,18,0"
VerticalAlignment="Center"
FontSize="14"
ItemsSource="{Binding DateFormatList}"
SelectedValue="{Binding Settings.DateFormat}"
ItemsSource="{Binding DateFormatDisplayList}"
SelectedIndex="{Binding DateFormatIndex, Mode=OneTime}"
SelectionChanged="PreviewClockAndDate" />
<ui:ToggleSwitch
IsOn="{Binding UseDate, Mode=TwoWay}"

View file

@ -7,6 +7,7 @@ using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.ViewModel;
using Microsoft.VisualBasic;
using ModernWpf;
using ModernWpf.Controls;
using System;
@ -516,6 +517,15 @@ namespace Flow.Launcher
private void PreviewClockAndDate(object sender, RoutedEventArgs e)
{
if (DateFormat != null && TimeFormat != null)
{
viewModel.UpdateDateTimeDisplayList(DateFormat.SelectedIndex, TimeFormat.SelectedIndex);
DateFormat.Items.Refresh();
TimeFormat.Items.Refresh(); // selected = -1
// todo avoid recursion
DateFormat.SelectedIndex = viewModel.DateFormatIndex;
TimeFormat.SelectedIndex = viewModel.TimeFormatIndex;
}
ClockDisplay();
}
@ -556,6 +566,7 @@ namespace Flow.Launcher
}
WindowState = settings.SettingWindowState;
}
public double WindowLeft()
{
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);

View file

@ -21,6 +21,7 @@ using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
using System.Globalization;
namespace Flow.Launcher.ViewModel
{
@ -48,6 +49,13 @@ 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;
TimeFormatDisplayList = TimeFormatList.Select(x => DateTime.Now.ToString(x, CultureInfo.CurrentCulture)).ToList();
DateFormatDisplayList = DateFormatList.Select(x => DateTime.Now.ToString(x, CultureInfo.CurrentCulture)).ToList();
UpdateSettingsDateTimeFormat(); // just in case something wrong
}
public Settings Settings { get; set; }
@ -422,8 +430,6 @@ namespace Flow.Launcher.ViewModel
}
}
public class SearchWindowPosition
{
public string Display { get; set; }
@ -470,6 +476,40 @@ namespace Flow.Launcher.ViewModel
"dd', 'MMMM"
};
public int TimeFormatIndex { get; set; } = 0;
public int DateFormatIndex { get; set; } = 0;
public List<string> TimeFormatDisplayList { get; set; } = null;
public List<string> DateFormatDisplayList { get; set; } = null;
public void UpdateSettingsDateTimeFormat()
{
Settings.DateFormat = DateFormatList[DateFormatIndex];
Settings.TimeFormat = TimeFormatList[TimeFormatIndex];
}
public void UpdateDateTimeDisplayList(int dateIndex, int timeIndex)
{
if (dateIndex == -1 || timeIndex == -1)
return;
DateFormatIndex = dateIndex;
TimeFormatIndex = timeIndex;
for (int i = 0; i < TimeFormatList.Count; ++i)
{
TimeFormatDisplayList[i] = DateTime.Now.ToString(TimeFormatList[i], CultureInfo.CurrentCulture);
}
for (int i = 0; i < DateFormatList.Count; ++i)
{
DateFormatDisplayList[i] = DateTime.Now.ToString(DateFormatList[i], CultureInfo.CurrentCulture);
}
UpdateSettingsDateTimeFormat();
}
public double WindowWidthSize
{
get => Settings.WindowSize;