Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Views/QuickAccessLinkSettings.xaml.cs

148 lines
4.4 KiB
C#
Raw Normal View History

using System;
2025-05-25 19:05:26 +00:00
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Forms;
2025-05-25 17:45:44 +00:00
using Flow.Launcher.Plugin.Explorer.Helper;
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
namespace Flow.Launcher.Plugin.Explorer.Views;
public partial class QuickAccessLinkSettings : INotifyPropertyChanged
{
private string _selectedPath;
public string SelectedPath
{
get => _selectedPath;
set
{
if (_selectedPath != value)
{
_selectedPath = value;
OnPropertyChanged();
2025-06-07 05:52:12 +00:00
if (string.IsNullOrEmpty(_selectedName))
{
SelectedName = _selectedPath.GetPathName();
}
}
}
}
private string _selectedName;
public string SelectedName
{
2025-05-25 06:04:18 +00:00
get
{
2025-06-07 05:52:12 +00:00
return string.IsNullOrEmpty(_selectedName) ? _selectedPath.GetPathName() : _selectedName;
2025-05-25 06:04:18 +00:00
}
set
{
if (_selectedName != value)
{
_selectedName = value;
OnPropertyChanged();
}
}
}
private bool IsEdit { get; }
2025-06-07 05:52:12 +00:00
private AccessLink SelectedAccessLink { get; }
2025-05-25 17:12:15 +00:00
2025-05-25 19:48:20 +00:00
public ObservableCollection<AccessLink> QuickAccessLinks { get; }
2025-05-25 19:05:26 +00:00
public QuickAccessLinkSettings(ObservableCollection<AccessLink> quickAccessLinks)
{
2025-06-07 06:07:07 +00:00
IsEdit = false;
2025-05-25 19:05:26 +00:00
QuickAccessLinks = quickAccessLinks;
InitializeComponent();
}
2025-05-25 07:31:08 +00:00
2025-06-07 05:52:12 +00:00
public QuickAccessLinkSettings(ObservableCollection<AccessLink> quickAccessLinks, AccessLink selectedAccessLink)
2025-05-25 07:31:08 +00:00
{
IsEdit = true;
_selectedName = selectedAccessLink.Name;
_selectedPath = selectedAccessLink.Path;
SelectedAccessLink = selectedAccessLink;
2025-05-25 19:05:26 +00:00
QuickAccessLinks = quickAccessLinks;
2025-05-25 07:31:08 +00:00
InitializeComponent();
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
{
// Validate the input before proceeding
2025-05-25 07:51:56 +00:00
if (string.IsNullOrEmpty(SelectedName) || string.IsNullOrEmpty(SelectedPath))
2025-05-25 06:04:18 +00:00
{
var warning = Main.Context.API.GetTranslation("plugin_explorer_quick_access_link_no_folder_selected");
Main.Context.API.ShowMsgBox(warning);
return;
}
// Check if the path already exists in the quick access links
2025-05-25 19:48:20 +00:00
if (QuickAccessLinks.Any(x =>
x.Path.Equals(SelectedPath, StringComparison.OrdinalIgnoreCase) &&
x.Name.Equals(SelectedName, StringComparison.OrdinalIgnoreCase)))
2025-05-25 17:26:47 +00:00
{
2025-05-25 19:48:20 +00:00
var warning = Main.Context.API.GetTranslation("plugin_explorer_quick_access_link_path_already_exists");
2025-05-25 17:26:47 +00:00
Main.Context.API.ShowMsgBox(warning);
return;
}
// If editing, update the existing link
2025-05-25 07:31:08 +00:00
if (IsEdit)
{
if (SelectedAccessLink == null) return;
var index = QuickAccessLinks.IndexOf(SelectedAccessLink);
if (index >= 0)
{
var updatedLink = new AccessLink
{
Name = SelectedName,
Type = SelectedAccessLink.Type,
Path = SelectedPath
};
QuickAccessLinks[index] = updatedLink;
}
DialogResult = true;
Close();
}
// Otherwise, add a new one
else
{
var newAccessLink = new AccessLink
{
Name = SelectedName,
Path = SelectedPath
};
QuickAccessLinks.Add(newAccessLink);
DialogResult = true;
Close();
2025-05-25 07:31:08 +00:00
}
}
private void SelectPath_OnClick(object commandParameter, RoutedEventArgs e)
{
var folderBrowserDialog = new FolderBrowserDialog();
2025-05-25 07:31:08 +00:00
if (folderBrowserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
SelectedPath = folderBrowserDialog.SelectedPath;
}
2025-05-25 07:31:08 +00:00
2025-06-07 05:52:12 +00:00
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}