266 lines
7.1 KiB
C#
266 lines
7.1 KiB
C#
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
|
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
|
// All Rights Reserved.
|
|
|
|
using Microsoft.Win32;
|
|
using WPFluent.Gallery.ControlsLookup;
|
|
using WPFluent.Gallery.Models;
|
|
using WPFluent.Gallery.Views.Pages;
|
|
|
|
namespace WPFluent.Gallery.ViewModels.Pages;
|
|
|
|
public partial class OpSystemViewModel : ViewModel
|
|
{
|
|
|
|
[ObservableProperty]
|
|
private string _clipboardContent = "Click the button!";
|
|
|
|
[ObservableProperty]
|
|
private string _fileToSaveContents = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _fileToSaveName = string.Empty;
|
|
[ObservableProperty]
|
|
private ICollection<NavigationCard> _navigationCards = new ObservableCollection<NavigationCard>(
|
|
ControlPages
|
|
.FromNamespace(typeof(OpSystemPage).Namespace!)
|
|
.Select(x => new NavigationCard()
|
|
{
|
|
Name = x.Name,
|
|
Icon = x.Icon,
|
|
Description = x.Description,
|
|
PageType = x.PageType,
|
|
})
|
|
);
|
|
|
|
[ObservableProperty]
|
|
private string _openedFilePath = string.Empty;
|
|
[ObservableProperty]
|
|
private Visibility _openedFilePathVisibility = Visibility.Collapsed;
|
|
|
|
[ObservableProperty]
|
|
private string _openedFolderPath = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private Visibility _openedFolderPathVisibility = Visibility.Collapsed;
|
|
|
|
[ObservableProperty]
|
|
private string _openedMultiplePath = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private Visibility _openedMultiplePathVisibility = Visibility.Collapsed;
|
|
|
|
[ObservableProperty]
|
|
private string _openedPicturePath = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private Visibility _openedPicturePathVisibility = Visibility.Collapsed;
|
|
|
|
[ObservableProperty]
|
|
private string _savedFileNotice = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private Visibility _savedFileNoticeVisibility = Visibility.Collapsed;
|
|
|
|
[ObservableProperty]
|
|
private Visibility _textCopiedVisibility = Visibility.Collapsed;
|
|
[ObservableProperty]
|
|
private string _textToCopy = "This text will be copied to the clipboard.";
|
|
|
|
[RelayCommand]
|
|
private async Task OnCopyTextToClipboard()
|
|
{
|
|
try
|
|
{
|
|
Clipboard.Clear();
|
|
Clipboard.SetText(TextToCopy);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.WriteLine(e);
|
|
}
|
|
|
|
if (TextCopiedVisibility == Visibility.Visible)
|
|
{
|
|
return;
|
|
}
|
|
|
|
TextCopiedVisibility = Visibility.Visible;
|
|
|
|
await Task.Delay(5000);
|
|
|
|
TextCopiedVisibility = Visibility.Collapsed;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void OnParseTextFromClipboard()
|
|
{
|
|
try
|
|
{
|
|
ClipboardContent = Clipboard.GetText();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.WriteLine(e);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenFile()
|
|
{
|
|
OpenedFilePathVisibility = Visibility.Collapsed;
|
|
|
|
OpenFileDialog openFileDialog = new()
|
|
{
|
|
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
|
Filter = "All files (*.*)|*.*",
|
|
};
|
|
|
|
if (openFileDialog.ShowDialog() != true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!File.Exists(openFileDialog.FileName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
OpenedFilePath = openFileDialog.FileName;
|
|
OpenedFilePathVisibility = Visibility.Visible;
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenFolder()
|
|
{
|
|
#if NET8_0_OR_GREATER
|
|
OpenedFolderPathVisibility = Visibility.Collapsed;
|
|
|
|
OpenFolderDialog openFolderDialog = new()
|
|
{
|
|
Multiselect = true,
|
|
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
|
};
|
|
|
|
if (openFolderDialog.ShowDialog() != true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (openFolderDialog.FolderNames.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
OpenedFolderPath = string.Join("\n", openFolderDialog.FolderNames);
|
|
OpenedFolderPathVisibility = Visibility.Visible;
|
|
#else
|
|
OpenedFolderPath = "OpenFolderDialog requires .NET 8 or newer";
|
|
OpenedFolderPathVisibility = Visibility.Visible;
|
|
#endif
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenMultiple()
|
|
{
|
|
OpenedMultiplePathVisibility = Visibility.Collapsed;
|
|
|
|
OpenFileDialog openFileDialog = new()
|
|
{
|
|
Multiselect = true,
|
|
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
|
Filter = "All files (*.*)|*.*",
|
|
};
|
|
|
|
if (openFileDialog.ShowDialog() != true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (openFileDialog.FileNames.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var fileNames = openFileDialog.FileNames;
|
|
|
|
OpenedMultiplePath = string.Join("\n", fileNames);
|
|
OpenedMultiplePathVisibility = Visibility.Visible;
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenPicture()
|
|
{
|
|
OpenedPicturePathVisibility = Visibility.Collapsed;
|
|
|
|
OpenFileDialog openFileDialog = new()
|
|
{
|
|
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
|
|
Filter = "Image files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png|All files (*.*)|*.*",
|
|
};
|
|
|
|
if (openFileDialog.ShowDialog() != true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!File.Exists(openFileDialog.FileName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
OpenedPicturePath = openFileDialog.FileName;
|
|
OpenedPicturePathVisibility = Visibility.Visible;
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnSaveFile(CancellationToken cancellation)
|
|
{
|
|
SavedFileNoticeVisibility = Visibility.Collapsed;
|
|
|
|
SaveFileDialog saveFileDialog = new()
|
|
{
|
|
Filter = "Text Files (*.txt)|*.txt",
|
|
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(FileToSaveName))
|
|
{
|
|
var invalidChars =
|
|
new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
|
|
|
|
saveFileDialog.FileName = string.Join(
|
|
"_",
|
|
FileToSaveName.Split(invalidChars.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
|
|
)
|
|
.Trim();
|
|
}
|
|
|
|
if (saveFileDialog.ShowDialog() != true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (File.Exists(saveFileDialog.FileName))
|
|
{
|
|
// Protect the user from accidental writes
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
File.WriteAllText(saveFileDialog.FileName, FileToSaveContents);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.WriteLine(e);
|
|
|
|
return;
|
|
}
|
|
|
|
SavedFileNoticeVisibility = Visibility.Visible;
|
|
SavedFileNotice = $"File {saveFileDialog.FileName} was saved.";
|
|
}
|
|
}
|