Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ColorManager/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ColorManager.App">
<Application.Resources>
<!-- Add any application resources here -->
</Application.Resources>
</Application>
23 changes: 23 additions & 0 deletions ColorManager/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

namespace ColorManager;

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
}

base.OnFrameworkInitializationCompleted();
}
}
29 changes: 18 additions & 11 deletions ColorManager/ColorManager.csproj
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>ColorManager</AssemblyName>
<RootNamespace>ColorManager</RootNamespace>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<!-- Support publishing for Windows and Linux runtimes -->
<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>
<!-- Enable Avalonia compiled XAML -->
<AvaloniaUseCompiledXaml>true</AvaloniaUseCompiledXaml>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GtkSharp" Version="3.24.24.117-develop" />
<PackageReference Include="Avalonia" Version="11.0.13" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.13" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.13" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.13" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.13" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="gtk-gui\gui.stetic">
<LogicalName>gui.stetic</LogicalName>
<EmbeddedResource Include="colors.xml">
<LogicalName>colors.xml</LogicalName>
</EmbeddedResource>
</ItemGroup>



<!-- Include XAML files as Avalonia resources for runtime/precompiled XAML -->
<ItemGroup>
<None Include="colors.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<AvaloniaResource Include="App.xaml" />
<AvaloniaResource Include="MainWindow.xaml" />
</ItemGroup>

</Project>
26 changes: 13 additions & 13 deletions ColorManager/Main.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using Gtk;
using Avalonia;

namespace ColorManager
namespace ColorManager;

public static class Program
{
class MainClass
[STAThread]
public static void Main(string[] args)
{
public static void Main (string[] args)
{
Application.Init ();
//MainWindow win = new MainWindow ();
MyForm win = new MyForm ();
win.Show ();
Application.Run ();
}
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
}

public static AppBuilder BuildAvaloniaApp() =>
AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace();
}
186 changes: 186 additions & 0 deletions ColorManager/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Xml.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;


namespace ColorManager
{
public partial class MainViewModel : ObservableObject
{
private PaletteManager paletteManager;

[ObservableProperty]
private ObservableCollection<string> palettes = new();

[ObservableProperty]
private string selectedPalette = string.Empty;

[ObservableProperty]
private ObservableCollection<MyColor> colors = new();

[ObservableProperty]
private string newPaletteName = string.Empty;

public MainViewModel()
{
try
{
paletteManager = new PaletteManager();
LoadPalettesCommand.Execute(null);
}
catch (Exception ex)
{
Debug.WriteLine($"Error initializing MainViewModel: {ex.Message}");
}
}

[RelayCommand]
private void LoadPalettes()
{
try
{
Palettes.Clear();
var paletteList = paletteManager.GetList();
foreach (var palette in paletteList)
{
Palettes.Add(palette);
}

if (Palettes.Count > 0 && string.IsNullOrEmpty(SelectedPalette))
{
SelectedPalette = Palettes[0];
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error loading palettes: {ex.Message}");
}
}

partial void OnSelectedPaletteChanged(string value)
{
if (value != null)
{
LoadColors(value);
}
}

private void LoadColors(string paletteName)
{
try
{
Colors.Clear();
var colorElements = paletteManager.GetPalette(paletteName);
foreach (var element in colorElements)
{
var colorList = element.Elements("Color");
foreach (var colorElement in colorList)
{
var hexAttr = colorElement.Attribute("hex");
if (hexAttr != null)
{
var myColor = new MyColor { Hex = hexAttr.Value };
Colors.Add(myColor);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error loading colors: {ex.Message}");
}
}

[RelayCommand]
private void CreatePalette()
{
if (string.IsNullOrWhiteSpace(NewPaletteName))
{
Debug.WriteLine("Palette name cannot be empty");
return;
}

try
{
paletteManager.CreatePalette(NewPaletteName);
NewPaletteName = string.Empty;
LoadPalettesCommand.Execute(null);
}
catch (Exception ex)
{
Debug.WriteLine($"Error creating palette: {ex.Message}");
}
}

[RelayCommand]
private void DeletePalette()
{
if (string.IsNullOrWhiteSpace(SelectedPalette))
{
Debug.WriteLine("No palette selected");
return;
}

try
{
paletteManager.DeletePalette(SelectedPalette);
LoadPalettesCommand.Execute(null);
}
catch (Exception ex)
{
Debug.WriteLine($"Error deleting palette: {ex.Message}");
}
}

[RelayCommand]
private void UpdateColor(MyColor color)
{
if (color == null)
return;

int index = Colors.IndexOf(color);
if (index >= 0)
{
// Color updated via binding
SaveColorsInternal();
}
}

public void SaveColorsInternal()
{
try
{
if (SelectedPalette == null)
return;

var paletteElement = paletteManager.GetPalette(SelectedPalette).FirstOrDefault();
if (paletteElement == null)
return;

// Remove existing colors
paletteElement.Elements("Color").Remove();

// Add updated colors
foreach (var color in Colors)
{
paletteElement.Add(new XElement("Color", new XAttribute("hex", color.Hex)));
}

paletteManager.save();
}
catch (Exception ex)
{
Debug.WriteLine($"Error saving colors: {ex.Message}");
}
}

[RelayCommand]
private void SaveColors()
{
SaveColorsInternal();
}
}
}
Loading