"General" pane in settings fully functional except for auto-updater and portable mode switches

This commit is contained in:
Yusyuriv 2024-04-29 12:25:10 +06:00
parent a084af683d
commit 2285040857
No known key found for this signature in database
GPG key ID: A91C52E6F73148E0
5 changed files with 264 additions and 297 deletions

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.SettingPages.ViewModels;
public class DropdownDataGeneric<TValue> : BaseModel where TValue : Enum
{
public string Display { get; set; }
public TValue Value { get; set; }
public static List<TR> GetValues<TR>(string keyPrefix) where TR : DropdownDataGeneric<TValue>, new()
{
var data = new List<TR>();
var enumValues = (TValue[])Enum.GetValues(typeof(TValue));
foreach (var value in enumValues)
{
var key = keyPrefix + value;
var display = InternationalizationManager.Instance.GetTranslation(key);
data.Add(new TR { Display = display, Value = value });
}
return data;
}
}

View file

@ -1,255 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Helper;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
using static Flow.Launcher.SettingPages.ViewModels.GeneralViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Helper;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
using Flow.Launcher.Infrastructure;
using static Flow.Launcher.ViewModel.SettingWindowViewModel;
namespace Flow.Launcher.SettingPages.ViewModels
{
public partial class GeneralViewModel
{
public Settings Settings { get; set; }
private readonly Updater _updater;
private readonly IPortable _portable;
public GeneralViewModel(Settings settings)
{
Settings = settings;
}
public class SearchWindowScreen
{
public string Display { get; set; }
public SearchWindowScreens Value { get; set; }
}
public bool StartFlowLauncherOnSystemStartup
{
get => Settings.StartFlowLauncherOnSystemStartup;
set
{
Settings.StartFlowLauncherOnSystemStartup = value;
try
{
if (value)
AutoStartup.Enable();
else
AutoStartup.Disable();
}
catch (Exception e)
{
Notification.Show(InternationalizationManager.Instance.GetTranslation("setAutoStartFailed"),
e.Message);
}
}
}
private Internationalization _translater => InternationalizationManager.Instance;
public List<SearchWindowScreen> SearchWindowScreens
{
get
{
List<SearchWindowScreen> modes = new List<SearchWindowScreen>();
var enums = (SearchWindowScreens[])Enum.GetValues(typeof(SearchWindowScreens));
foreach (var e in enums)
{
var key = $"SearchWindowScreen{e}";
var display = _translater.GetTranslation(key);
var m = new SearchWindowScreen { Display = display, Value = e, };
modes.Add(m);
}
return modes;
}
}
public List<SearchWindowAlign> SearchWindowAligns
{
get
{
List<SearchWindowAlign> modes = new List<SearchWindowAlign>();
var enums = (SearchWindowAligns[])Enum.GetValues(typeof(SearchWindowAligns));
foreach (var e in enums)
{
var key = $"SearchWindowAlign{e}";
var display = _translater.GetTranslation(key);
var m = new SearchWindowAlign { Display = display, Value = e, };
modes.Add(m);
}
return modes;
}
}
public List<int> ScreenNumbers
{
get
{
var screens = System.Windows.Forms.Screen.AllScreens;
var screenNumbers = new List<int>();
for (int i = 1; i <= screens.Length; i++)
{
screenNumbers.Add(i);
}
return screenNumbers;
}
}
// This is only required to set at startup. When portable mode enabled/disabled a restart is always required
private bool _portableMode = DataLocation.PortableDataLocationInUse();
public bool PortableMode
{
get => _portableMode;
set
{
if (!_portable.CanUpdatePortability())
return;
if (DataLocation.PortableDataLocationInUse())
{
_portable.DisablePortableMode();
}
else
{
_portable.EnablePortableMode();
}
}
}
// todo a better name?
public class LastQueryMode : BaseModel
{
public string Display { get; set; }
public Infrastructure.UserSettings.LastQueryMode Value { get; set; }
}
private List<LastQueryMode> _lastQueryModes = new List<LastQueryMode>();
public List<LastQueryMode> LastQueryModes
{
get
{
if (_lastQueryModes.Count == 0)
{
_lastQueryModes = InitLastQueryModes();
}
return _lastQueryModes;
}
}
private List<LastQueryMode> InitLastQueryModes()
{
var modes = new List<LastQueryMode>();
var enums = (Infrastructure.UserSettings.LastQueryMode[])Enum.GetValues(
typeof(Infrastructure.UserSettings.LastQueryMode));
foreach (var e in enums)
{
var key = $"LastQuery{e}";
var display = _translater.GetTranslation(key);
var m = new LastQueryMode { Display = display, Value = e, };
modes.Add(m);
}
return modes;
}
private void UpdateLastQueryModeDisplay()
{
foreach (var item in LastQueryModes)
{
item.Display = _translater.GetTranslation($"LastQuery{item.Value}");
}
}
public string Language
{
get
{
return Settings.Language;
}
set
{
InternationalizationManager.Instance.ChangeLanguage(value);
if (InternationalizationManager.Instance.PromptShouldUsePinyin(value))
ShouldUsePinyin = true;
UpdateLastQueryModeDisplay();
}
}
public bool ShouldUsePinyin
{
get
{
return Settings.ShouldUsePinyin;
}
set
{
Settings.ShouldUsePinyin = value;
}
}
public List<string> QuerySearchPrecisionStrings
{
get
{
var precisionStrings = new List<string>();
var enumList = Enum.GetValues(typeof(SearchPrecisionScore)).Cast<SearchPrecisionScore>().ToList();
enumList.ForEach(x => precisionStrings.Add(x.ToString()));
return precisionStrings;
}
}
public List<string> OpenResultModifiersList => new List<string>
{
KeyConstant.Alt, KeyConstant.Ctrl, $"{KeyConstant.Ctrl}+{KeyConstant.Alt}"
};
public List<Language> Languages => _translater.LoadAvailableLanguages();
public IEnumerable<int> MaxResultsRange => Enumerable.Range(2, 16);
public string AlwaysPreviewToolTip =>
string.Format(_translater.GetTranslation("AlwaysPreviewToolTip"), Settings.PreviewHotkey);
}
}

View file

@ -0,0 +1,218 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core;
using Flow.Launcher.Core.Configuration;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.SharedModels;
namespace Flow.Launcher.SettingPages.ViewModels;
// TODO: _updater, _portable
public partial class SettingsPaneGeneralViewModel
{
private static Internationalization Translator => InternationalizationManager.Instance;
public Settings Settings { get; set; }
private readonly Updater _updater;
private readonly IPortable _portable;
public SettingsPaneGeneralViewModel(Settings settings)
{
Settings = settings;
}
public class SearchWindowScreen : DropdownDataGeneric<SearchWindowScreens> { }
public class SearchWindowAlign : DropdownDataGeneric<SearchWindowAligns> { }
// todo a better name?
public class LastQueryMode : DropdownDataGeneric<Infrastructure.UserSettings.LastQueryMode> { }
public bool StartFlowLauncherOnSystemStartup
{
get => Settings.StartFlowLauncherOnSystemStartup;
set
{
Settings.StartFlowLauncherOnSystemStartup = value;
try
{
if (value)
AutoStartup.Enable();
else
AutoStartup.Disable();
}
catch (Exception e)
{
Notification.Show(InternationalizationManager.Instance.GetTranslation("setAutoStartFailed"),
e.Message);
}
}
}
public List<SearchWindowScreen> SearchWindowScreens =>
DropdownDataGeneric<SearchWindowScreens>.GetValues<SearchWindowScreen>("SearchWindowScreen");
public List<SearchWindowAlign> SearchWindowAligns =>
DropdownDataGeneric<SearchWindowAligns>.GetValues<SearchWindowAlign>("SearchWindowAlign");
public List<int> ScreenNumbers
{
get
{
var screens = Screen.AllScreens;
var screenNumbers = new List<int>();
for (int i = 1; i <= screens.Length; i++)
{
screenNumbers.Add(i);
}
return screenNumbers;
}
}
// This is only required to set at startup. When portable mode enabled/disabled a restart is always required
private bool _portableMode = DataLocation.PortableDataLocationInUse();
public bool PortableMode
{
get => _portableMode;
set
{
if (!_portable.CanUpdatePortability())
return;
if (DataLocation.PortableDataLocationInUse())
{
_portable.DisablePortableMode();
}
else
{
_portable.EnablePortableMode();
}
}
}
private List<LastQueryMode> _lastQueryModes = new();
public List<LastQueryMode> LastQueryModes
{
get
{
if (_lastQueryModes.Count == 0)
{
_lastQueryModes =
DropdownDataGeneric<Infrastructure.UserSettings.LastQueryMode>
.GetValues<LastQueryMode>("LastQuery");
}
return _lastQueryModes;
}
}
private void UpdateLastQueryModeDisplay()
{
foreach (var item in LastQueryModes)
{
item.Display = Translator.GetTranslation($"LastQuery{item.Value}");
}
}
public string Language
{
get => Settings.Language;
set
{
InternationalizationManager.Instance.ChangeLanguage(value);
if (InternationalizationManager.Instance.PromptShouldUsePinyin(value))
ShouldUsePinyin = true;
UpdateLastQueryModeDisplay();
}
}
public bool ShouldUsePinyin
{
get => Settings.ShouldUsePinyin;
set => Settings.ShouldUsePinyin = value;
}
public List<string> QuerySearchPrecisionStrings => Enum
.GetValues(typeof(SearchPrecisionScore))
.Cast<SearchPrecisionScore>()
.Select(v => v.ToString())
.ToList();
public List<string> OpenResultModifiersList => new List<string>
{
KeyConstant.Alt,
KeyConstant.Ctrl,
$"{KeyConstant.Ctrl}+{KeyConstant.Alt}"
};
public List<Language> Languages => Translator.LoadAvailableLanguages();
public IEnumerable<int> MaxResultsRange => Enumerable.Range(2, 16);
public string AlwaysPreviewToolTip =>
string.Format(Translator.GetTranslation("AlwaysPreviewToolTip"), Settings.PreviewHotkey);
private string GetFileFromDialog(string title, string filter = "")
{
var dlg = new OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true,
Title = title,
Filter = filter
};
return dlg.ShowDialog() switch
{
DialogResult.OK => dlg.FileName,
_ => string.Empty
};
}
[RelayCommand]
private void SelectPython()
{
var selectedFile = GetFileFromDialog(
Translator.GetTranslation("selectPythonExecutable"),
"Python|pythonw.exe"
);
if (!string.IsNullOrEmpty(selectedFile))
Settings.PluginSettings.PythonExecutablePath = selectedFile;
}
[RelayCommand]
private void SelectNode()
{
var selectedFile = GetFileFromDialog(Translator.GetTranslation("selectNodeExecutable"), "*.exe");
if (!string.IsNullOrEmpty(selectedFile))
Settings.PluginSettings.NodeExecutablePath = selectedFile;
}
[RelayCommand]
private void SelectFileManager()
{
var fileManagerChangeWindow = new SelectFileManagerWindow(Settings);
fileManagerChangeWindow.ShowDialog();
}
[RelayCommand]
private void SelectBrowser()
{
var browserWindow = new SelectBrowserWindow(Settings);
browserWindow.ShowDialog();
}
}

View file

@ -7,7 +7,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:userSettings="clr-namespace:Flow.Launcher.Infrastructure.UserSettings;assembly=Flow.Launcher.Infrastructure"
xmlns:settingsViewModels="clr-namespace:Flow.Launcher.SettingPages.ViewModels"
Title="General"
d:DataContext="{d:DesignInstance settingsViewModels:SettingsPaneGeneralViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
@ -292,6 +294,7 @@
Width="160"
MaxWidth="250"
Margin="10,0,0,0"
Command="{Binding SelectFileManagerCommand}"
Content="{Binding Settings.CustomExplorer.Name}" />
</cc:Card>
@ -303,6 +306,7 @@
Width="160"
MaxWidth="250"
Margin="10,0,0,0"
Command="{Binding SelectBrowserCommand}"
Content="{Binding Settings.CustomBrowser.Name}" />
</cc:Card>
@ -315,6 +319,7 @@
<Button
Height="34"
Margin="10,0,0,0"
Command="{Binding SelectPythonCommand}"
Content="{DynamicResource select}" />
</StackPanel>
</cc:Card>
@ -328,6 +333,7 @@
<Button
Height="34"
Margin="10,0,0,0"
Command="{Binding SelectNodeCommand}"
Content="{DynamicResource select}" />
</StackPanel>
</cc:Card>

View file

@ -1,53 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Navigation;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.Infrastructure.UserSettings;
using Path = System.IO.Path;
using MessageBox = System.Windows.Forms.MessageBox;
using Flow.Launcher.SettingPages.ViewModels;
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Navigation;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.SettingPages.ViewModels;
using Flow.Launcher.ViewModel;
namespace Flow.Launcher.SettingPages.Views
namespace Flow.Launcher.SettingPages.Views;
public partial class SettingsPaneGeneral
{
public partial class SettingsPaneGeneral
private SettingsPaneGeneralViewModel _viewModel = null!;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
protected override void OnNavigatedTo(NavigationEventArgs e)
if (!IsInitialized)
{
if (!IsInitialized)
{
if (e.ExtraData is not Settings settings)
throw new ArgumentException("Settings is not passed to General page");
DataContext = new GeneralViewModel(settings);
InitializeComponent();
}
base.OnNavigatedTo(e);
if (e.ExtraData is not Settings settings)
throw new ArgumentException("Settings is not passed to General page");
_viewModel = new SettingsPaneGeneralViewModel(settings);
DataContext = _viewModel;
InitializeComponent();
}
base.OnNavigatedTo(e);
}
}