Add selected item import export
This commit is contained in:
@@ -1260,6 +1260,114 @@ public partial class MainWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
private void ExportSelectedItem_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (selectedItem is null)
|
||||
{
|
||||
MessageBox.Show("Select an item or menu to export first.", "Taskbar Launcher");
|
||||
return;
|
||||
}
|
||||
|
||||
ApplyCurrentItem();
|
||||
string safeTitle = MakeSafeFileName(string.IsNullOrWhiteSpace(selectedItem.Title) ? "launcher-item" : selectedItem.Title);
|
||||
using SaveFileDialog dialog = new()
|
||||
{
|
||||
Title = "Export selected launcher item",
|
||||
Filter = "JSON item files|*.json|All files|*.*",
|
||||
FileName = $"{safeTitle}-{DateTime.Now:yyyyMMdd-HHmmss}.json",
|
||||
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string exportPath = ConfigService.ExportItemTo(dialog.FileName, selectedItem);
|
||||
StatusText.Text = $"Exported {selectedItem.Title} to {exportPath}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Could not export selected item.\n\n{ex.Message}", "Taskbar Launcher");
|
||||
}
|
||||
}
|
||||
|
||||
private void ImportItem_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (controller is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using OpenFileDialog dialog = new()
|
||||
{
|
||||
Title = "Import launcher item",
|
||||
Filter = "JSON item files|*.json|All files|*.*",
|
||||
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ApplyCurrentItem();
|
||||
LauncherItem importedItem = ConfigService.ImportItemFrom(dialog.FileName);
|
||||
AddImportedItem(importedItem);
|
||||
selectedPath = FindItemPath(importedItem);
|
||||
RefreshView(importedItem);
|
||||
StatusText.Text = $"Imported {importedItem.Title}. Click Save to keep it.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Could not import item.\n\n{ex.Message}", "Taskbar Launcher");
|
||||
}
|
||||
}
|
||||
|
||||
private void AddImportedItem(LauncherItem importedItem)
|
||||
{
|
||||
if (controller is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedItem is null)
|
||||
{
|
||||
controller.Config.Items.Add(importedItem);
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedItem.Type == LauncherItemType.Menu)
|
||||
{
|
||||
selectedItem.Children.Add(importedItem);
|
||||
return;
|
||||
}
|
||||
|
||||
List<LauncherItem>? siblings = FindSiblings(controller.Config.Items, selectedItem);
|
||||
if (siblings is null)
|
||||
{
|
||||
controller.Config.Items.Add(importedItem);
|
||||
return;
|
||||
}
|
||||
|
||||
int selectedIndex = siblings.IndexOf(selectedItem);
|
||||
siblings.Insert(selectedIndex + 1, importedItem);
|
||||
}
|
||||
|
||||
private static string MakeSafeFileName(string fileName)
|
||||
{
|
||||
foreach (char invalidChar in Path.GetInvalidFileNameChars())
|
||||
{
|
||||
fileName = fileName.Replace(invalidChar, '-');
|
||||
}
|
||||
|
||||
return fileName.Trim();
|
||||
}
|
||||
|
||||
private void ResetConfig_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
MessageBoxResult result = MessageBox.Show(
|
||||
|
||||
Reference in New Issue
Block a user