Skip to content
Merged
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
8 changes: 6 additions & 2 deletions samples/AvalonDraw/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
<MenuItem Header="Flip Horizontal" Click="FlipHMenuItem_Click"/>
<MenuItem Header="Flip Vertical" Click="FlipVMenuItem_Click"/>
<Separator/>
<MenuItem Header="Bring Forward" Click="BringForwardMenuItem_Click" InputGesture="Ctrl+]"/>
<MenuItem Header="Send Backward" Click="SendBackwardMenuItem_Click" InputGesture="Ctrl+["/>
<MenuItem Header="Bring Forward" Click="BringForwardMenuItem_Click" InputGesture="Ctrl+OemCloseBrackets"/>
<MenuItem Header="Send Backward" Click="SendBackwardMenuItem_Click" InputGesture="Ctrl+OemOpenBrackets"/>
</MenuItem>
<MenuItem Header="View">
<MenuItem Header="Wireframe" Click="WireframeMenuItem_Click"/>
Expand Down Expand Up @@ -210,6 +210,10 @@
<ListBox x:Name="BrushList" Margin="4" Height="80"
ItemsSource="{Binding BrushStyles}" SelectionChanged="BrushList_OnSelectionChanged"/>
</TabItem>
<TabItem Header="Symbols">
<ListBox x:Name="SymbolList" Margin="4" Height="80"
ItemsSource="{Binding Symbols}" SelectionChanged="SymbolList_OnSelectionChanged"/>
</TabItem>
<TabItem Header="Styles">
<ListBox x:Name="StyleList" Margin="4" Height="80"
ItemsSource="{Binding Styles}" SelectionChanged="StyleList_OnSelectionChanged"/>
Expand Down
43 changes: 41 additions & 2 deletions samples/AvalonDraw/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private struct DragInfo
private readonly LayerService _layerService = new();
private readonly PatternService _patternService = new();
private readonly BrushService _brushService = new();
private readonly SymbolService _symbolService = new();
private readonly AppearanceService _appearanceService = new();
public ObservableCollection<PropertyEntry> Properties => _propertiesService.Properties;
public ObservableCollection<PropertyEntry> FilteredProperties => _propertiesService.FilteredProperties;
Expand All @@ -65,16 +66,19 @@ private struct DragInfo
public ObservableCollection<LayerService.LayerEntry> Layers => _layerService.Layers;
public ObservableCollection<PatternService.PatternEntry> Patterns => _patternService.Patterns;
public ObservableCollection<BrushService.BrushEntry> BrushStyles => _brushService.Brushes;
public ObservableCollection<SymbolService.SymbolEntry> Symbols => _symbolService.Symbols;
public ObservableCollection<AppearanceService.StyleEntry> Styles => _appearanceService.Styles;
private ArtboardInfo? _selectedArtboard;
private LayerService.LayerEntry? _selectedLayer;
private PatternService.PatternEntry? _selectedPattern;
private BrushService.BrushEntry? _selectedBrush;
private SymbolService.SymbolEntry? _selectedSymbol;
private AppearanceService.StyleEntry? _selectedStyle;
private ListBox? _artboardList;
private TreeView? _layerTree;
private ListBox? _swatchList;
private ListBox? _brushList;
private ListBox? _symbolList;
private ListBox? _styleList;
private readonly HashSet<string> _expandedIds = new();
private HashSet<string> _filterBackup = new();
Expand Down Expand Up @@ -354,6 +358,7 @@ public MainWindow()
_layerTree = this.FindControl<TreeView>("LayerTree");
_swatchList = this.FindControl<ListBox>("SwatchList");
_brushList = this.FindControl<ListBox>("BrushList");
_symbolList = this.FindControl<ListBox>("SymbolList");
_styleList = this.FindControl<ListBox>("StyleList");
_strokeWidthBox = this.FindControl<TextBox>("StrokeWidthBox");
if (_strokeWidthBox is { })
Expand Down Expand Up @@ -1563,6 +1568,7 @@ private void BuildTree()
UpdateLayers();
UpdatePatterns();
UpdateBrushes();
UpdateSymbols();
UpdateStyles();
}

Expand Down Expand Up @@ -1635,6 +1641,17 @@ private void UpdateBrushes()
}
}

private void UpdateSymbols()
{
_symbolService.Load(_document);
if (Symbols.Count > 0)
{
_selectedSymbol = Symbols[0];
if (_symbolList is { })
_symbolList.SelectedIndex = 0;
}
}

private void UpdateStyles()
{
_appearanceService.Load(_document);
Expand Down Expand Up @@ -2237,7 +2254,7 @@ private async void CreateSymbolMenuItem_Click(object? sender, RoutedEventArgs e)
SaveUndoState();
var symbol = new SvgSymbol { ID = symId };
symbol.Children.Add((SvgElement)vis.DeepCopy());
_document.Children.Add(symbol);
_symbolService.AddSymbol(_document, symbol);
var use = new SvgUse { ReferencedElement = new Uri($"#{symId}", UriKind.Relative) };
if (vis.Parent is SvgElement parent)
{
Expand Down Expand Up @@ -2329,6 +2346,25 @@ private void BrushList_OnSelectionChanged(object? sender, SelectionChangedEventA
}
}

private void SymbolList_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && e.AddedItems[0] is SymbolService.SymbolEntry info)
{
_selectedSymbol = info;
if (_selectedSvgElement is SvgVisualElement ve)
{
SaveUndoState();
// TODO: info.Apply(ve);
SvgView.SkSvg!.FromSvgDocument(_document);
UpdateSelectedDrawable();
SaveExpandedNodes();
BuildTree();
SelectNodeFromElement(ve);
SvgView.InvalidateVisual();
}
}
}

private void StyleList_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && e.AddedItems[0] is AppearanceService.StyleEntry info)
Expand Down Expand Up @@ -2512,7 +2548,10 @@ private async void SymbolToolButton_Click(object? sender, RoutedEventArgs e)
_pathService.Stop();
if (_document is null)
return;
var ids = _document.Children.OfType<SvgSymbol>().Select(s => s.ID).Where(id => !string.IsNullOrEmpty(id)).ToList();
var ids = _symbolService.Symbols
.Select(s => s.Symbol.ID)
.Where(id => !string.IsNullOrEmpty(id))
.ToList();
if (ids.Count == 0)
return;
var win = new SymbolSelectWindow(ids!);
Expand Down
53 changes: 53 additions & 0 deletions samples/AvalonDraw/Services/SymbolService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.ObjectModel;
using System.Linq;
using Svg;

namespace AvalonDraw.Services;

public class SymbolService
{
public class SymbolEntry
{
public SvgSymbol Symbol { get; }
public string Name { get; }

public SymbolEntry(SvgSymbol symbol, string name)
{
Symbol = symbol;
Name = name;
}

public override string ToString() => Name;
}

public ObservableCollection<SymbolEntry> Symbols { get; } = new();

public void Load(SvgDocument? document)
{
Symbols.Clear();
if (document is null)
return;
var defs = document.Children.OfType<SvgDefinitionList>().FirstOrDefault();
if (defs is null)
return;
int index = 1;
foreach (var s in defs.Children.OfType<SvgSymbol>())
{
var name = string.IsNullOrEmpty(s.ID) ? $"Symbol {index++}" : s.ID!;
Symbols.Add(new SymbolEntry(s, name));
}
}

public void AddSymbol(SvgDocument document, SvgSymbol symbol)
{
var defs = document.Children.OfType<SvgDefinitionList>().FirstOrDefault();
if (defs is null)
{
defs = new SvgDefinitionList();
document.Children.Add(defs);
}
defs.Children.Add(symbol);
var name = string.IsNullOrEmpty(symbol.ID) ? $"Symbol {Symbols.Count + 1}" : symbol.ID!;
Symbols.Add(new SymbolEntry(symbol, name));
}
}
25 changes: 13 additions & 12 deletions samples/AvalonDraw/TextEditorWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
Expand Down Expand Up @@ -25,9 +26,9 @@ public TextEditorWindow(string text, string fontFamily, SvgFontWeight weight, fl
_wordBox = this.FindControl<TextBox>("WordBox");

_editor.Text = text;
_fontFamilyBox.Items = FontManager.Current?.InstalledFontFamilyNames;
_fontFamilyBox.ItemsSource = FontManager.Current?.SystemFonts;
_fontFamilyBox.SelectedItem = fontFamily;
_fontWeightBox.Items = Enum.GetValues(typeof(FontWeight)).Cast<FontWeight>();
_fontWeightBox.ItemsSource = Enum.GetValues(typeof(FontWeight)).Cast<FontWeight>();
_fontWeightBox.SelectedItem = ToFontWeight(weight);
_letterBox.Text = letter.ToString();
_wordBox.Text = word.ToString();
Expand All @@ -48,17 +49,17 @@ public TextEditorWindow(string text, string fontFamily, SvgFontWeight weight, fl
_ => FontWeight.Normal
};

private static SvgFontWeight FromFontWeight(FontWeight w) => w.Weight switch
private static SvgFontWeight FromFontWeight(FontWeight w) => w switch
{
100 => SvgFontWeight.W100,
200 => SvgFontWeight.W200,
300 => SvgFontWeight.W300,
400 => SvgFontWeight.W400,
500 => SvgFontWeight.W500,
600 => SvgFontWeight.W600,
700 => SvgFontWeight.W700,
800 => SvgFontWeight.W800,
900 => SvgFontWeight.W900,
FontWeight.Thin => SvgFontWeight.W100,
FontWeight.ExtraLight => SvgFontWeight.W200,
FontWeight.Light => SvgFontWeight.W300,
FontWeight.Normal => SvgFontWeight.W400,
FontWeight.Medium => SvgFontWeight.W500,
FontWeight.SemiBold => SvgFontWeight.W600,
FontWeight.Bold => SvgFontWeight.W700,
FontWeight.ExtraBold => SvgFontWeight.W800,
FontWeight.Black => SvgFontWeight.W900,
_ => SvgFontWeight.Normal
};

Expand Down
Loading