From 541135e063117ffbe694a017ad7ed720d2e8552f Mon Sep 17 00:00:00 2001 From: mathias Date: Sat, 14 Feb 2026 15:07:20 +0100 Subject: [PATCH] add export --- MaxSlurper/MainWindow.xaml | 8 ++++ MaxSlurper/ViewModels/MainViewModel.cs | 54 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/MaxSlurper/MainWindow.xaml b/MaxSlurper/MainWindow.xaml index a2d0f5a..937032f 100644 --- a/MaxSlurper/MainWindow.xaml +++ b/MaxSlurper/MainWindow.xaml @@ -120,6 +120,14 @@ Padding="16,8" Command="{Binding ClearHistoryCommand}" Padding="8,4" VerticalAlignment="Center" /> + diff --git a/MaxSlurper/ViewModels/MainViewModel.cs b/MaxSlurper/ViewModels/MainViewModel.cs index d49f33b..c98826d 100644 --- a/MaxSlurper/ViewModels/MainViewModel.cs +++ b/MaxSlurper/ViewModels/MainViewModel.cs @@ -1,6 +1,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; +using System.Text; using System.Windows.Input; using System.Windows.Media; using MaxSlurper.Models; @@ -42,6 +43,7 @@ namespace MaxSlurper.ViewModels public ICommand CopyHexCommand { get; } public ICommand OpenColorPickerCommand { get; } public ICommand OpenSettingsCommand { get; } + public ICommand ExportHistoryCommand { get; } public MainViewModel(IColorPickerService colorPicker, IAudioPlayer audioPlayer, ISettingsService settingsService) { @@ -57,6 +59,7 @@ namespace MaxSlurper.ViewModels CopyHexCommand = new RelayCommand(CopyHex, _ => Selected != null || _ is ColorItem); OpenColorPickerCommand = new RelayCommand(_ => OpenColorPicker()); OpenSettingsCommand = new RelayCommand(_ => OpenSettings()); + ExportHistoryCommand = new RelayCommand(_ => ExportHistory(), _ => History.Count > 0); // Restore persisted history foreach (var item in _settingsService.LoadHistory()) @@ -90,6 +93,7 @@ namespace MaxSlurper.ViewModels System.Windows.Clipboard.SetText(item.Hex); _settingsService.SaveHistory(History); ((RelayCommand)ClearHistoryCommand).RaiseCanExecuteChanged(); + ((RelayCommand)ExportHistoryCommand).RaiseCanExecuteChanged(); } private void OpenColorPicker() @@ -117,6 +121,7 @@ namespace MaxSlurper.ViewModels System.Windows.Clipboard.SetText(item.Hex); _settingsService.SaveHistory(History); ((RelayCommand)ClearHistoryCommand).RaiseCanExecuteChanged(); + ((RelayCommand)ExportHistoryCommand).RaiseCanExecuteChanged(); } } @@ -135,6 +140,7 @@ namespace MaxSlurper.ViewModels Selected = null; _settingsService.SaveHistory(History); ((RelayCommand)ClearHistoryCommand).RaiseCanExecuteChanged(); + ((RelayCommand)ExportHistoryCommand).RaiseCanExecuteChanged(); } private void RemoveColor(object? parameter) @@ -149,6 +155,7 @@ namespace MaxSlurper.ViewModels _settingsService.SaveHistory(History); ((RelayCommand)ClearHistoryCommand).RaiseCanExecuteChanged(); + ((RelayCommand)ExportHistoryCommand).RaiseCanExecuteChanged(); } private void CopyHex(object? parameter) @@ -164,6 +171,53 @@ namespace MaxSlurper.ViewModels // Show a notification (optional - can add UI feedback) } + private void ExportHistory() + { + var dialog = new Microsoft.Win32.SaveFileDialog + { + Title = "Farbpalette exportieren", + Filter = "CSS-Datei (*.css)|*.css|JSON-Datei (*.json)|*.json|Textdatei (*.txt)|*.txt", + DefaultExt = ".css", + FileName = "palette" + }; + + if (dialog.ShowDialog() != true) return; + + var sb = new StringBuilder(); + var ext = System.IO.Path.GetExtension(dialog.FileName).ToLowerInvariant(); + + switch (ext) + { + case ".css": + sb.AppendLine(":root {"); + for (int i = 0; i < History.Count; i++) + { + var c = History[i]; + sb.AppendLine($" --color-{i + 1}: {c.Hex};"); + } + sb.AppendLine("}"); + break; + + case ".json": + sb.AppendLine("["); + for (int i = 0; i < History.Count; i++) + { + var c = History[i]; + var comma = i < History.Count - 1 ? "," : ""; + sb.AppendLine($" {{ \"hex\": \"{c.Hex}\", \"rgb\": \"{c.Rgb}\" }}{comma}"); + } + sb.AppendLine("]"); + break; + + default: // .txt + foreach (var c in History) + sb.AppendLine(c.Hex); + break; + } + + System.IO.File.WriteAllText(dialog.FileName, sb.ToString()); + } + private void UpdateSelected() { if (Selected == null)