diff --git a/OneWare.sln.DotSettings b/OneWare.sln.DotSettings index dfbd1405..c831a3cb 100644 --- a/OneWare.sln.DotSettings +++ b/OneWare.sln.DotSettings @@ -3,4 +3,5 @@ True True + True True \ No newline at end of file diff --git a/src/OneWare.ImageViewer/Views/ImageView.axaml b/src/OneWare.ImageViewer/Views/ImageView.axaml index 5e5b6437..c619af96 100644 --- a/src/OneWare.ImageViewer/Views/ImageView.axaml +++ b/src/OneWare.ImageViewer/Views/ImageView.axaml @@ -5,7 +5,7 @@ xmlns:viewModels="clr-namespace:OneWare.ImageViewer.ViewModels" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="OneWare.ImageViewer.Views.ImageView" x:DataType="viewModels:ImageViewModel"> - + diff --git a/src/OneWare.NetListSvgIntegration/Assets/theme_dark.svg b/src/OneWare.NetListSvgIntegration/Assets/theme_dark.svg new file mode 100644 index 00000000..6e828833 --- /dev/null +++ b/src/OneWare.NetListSvgIntegration/Assets/theme_dark.svg @@ -0,0 +1,738 @@ + + + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + input + + + + + + + constant + + + + + + + + + output + + + + + + + + + + + + + hi:lo + + + hi:lo + + + + + + + + + hi:lo + + + hi:lo + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + generic + + + + out0 + + + out1 + + + in0 + + + in1 + + + + diff --git a/src/OneWare.NetListSvgIntegration/Assets/theme_light.svg b/src/OneWare.NetListSvgIntegration/Assets/theme_light.svg new file mode 100644 index 00000000..d34df210 --- /dev/null +++ b/src/OneWare.NetListSvgIntegration/Assets/theme_light.svg @@ -0,0 +1,737 @@ + + + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + input + + + + + + + constant + + + + + + + + + output + + + + + + + + + + + + + hi:lo + + + hi:lo + + + + + + + + + hi:lo + + + hi:lo + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + generic + + + + out0 + + + out1 + + + in0 + + + in1 + + + + diff --git a/src/OneWare.NetListSvgIntegration/Assets/theme_teros.svg b/src/OneWare.NetListSvgIntegration/Assets/theme_teros.svg new file mode 100644 index 00000000..a4dc5975 --- /dev/null +++ b/src/OneWare.NetListSvgIntegration/Assets/theme_teros.svg @@ -0,0 +1,401 @@ + + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + input + + + + + + + constant + + + + + + + + + output + + + + + + + + + + + + + hi:lo + + + hi:lo + + + + + + + + + hi:lo + + + hi:lo + + + + + + generic + + + + out0 + + + out1 + + + in0 + + + in1 + + + + + sub_odd + + + + out0 + + + out1 + + + in0 + + + in1 + + + + + sub_even + + + + out0 + + + out1 + + + in0 + + + in1 + + + + diff --git a/src/OneWare.NetListSvgIntegration/NetListSvgIntegrationModule.cs b/src/OneWare.NetListSvgIntegration/NetListSvgIntegrationModule.cs index a2860a1a..05a30146 100644 --- a/src/OneWare.NetListSvgIntegration/NetListSvgIntegrationModule.cs +++ b/src/OneWare.NetListSvgIntegration/NetListSvgIntegrationModule.cs @@ -28,7 +28,7 @@ public void OnInitialized(IContainerProvider containerProvider) new MenuItemModel("NetListSvg") { Header = "NetListSvg", - Command = new AsyncRelayCommand(() => netListSvgService.CreateFromJsonAsync(json)) + Command = new AsyncRelayCommand(() => netListSvgService.ShowSchemeAsync(json)) } }; } diff --git a/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.csproj b/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.csproj index 0ec938ee..68d47b9d 100644 --- a/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.csproj +++ b/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.csproj @@ -9,7 +9,6 @@ - - + diff --git a/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.sln b/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.sln new file mode 100644 index 00000000..347cc481 --- /dev/null +++ b/src/OneWare.NetListSvgIntegration/OneWare.NetListSvgIntegration.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OneWare.NetListSvgIntegration", "OneWare.NetListSvgIntegration.csproj", "{DA9B79A2-C0E6-442E-A785-001E509B9199}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DA9B79A2-C0E6-442E-A785-001E509B9199}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA9B79A2-C0E6-442E-A785-001E509B9199}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA9B79A2-C0E6-442E-A785-001E509B9199}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA9B79A2-C0E6-442E-A785-001E509B9199}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2F01758F-FFEA-408A-B6CF-7135800E934C} + EndGlobalSection +EndGlobal diff --git a/src/OneWare.NetListSvgIntegration/Services/NetListSvgService.cs b/src/OneWare.NetListSvgIntegration/Services/NetListSvgService.cs index 098519ce..ad8c7a0f 100644 --- a/src/OneWare.NetListSvgIntegration/Services/NetListSvgService.cs +++ b/src/OneWare.NetListSvgIntegration/Services/NetListSvgService.cs @@ -1,33 +1,61 @@ -using Avalonia.Platform; +using System.Drawing; +using System.Net.Mime; +using System.Text.Json; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Platform; +using Avalonia.Styling; +using ImTools; using Jint; +using OneWare.Shared.Enums; using OneWare.Shared.Models; using OneWare.Shared.Services; +using Prism.Ioc; +using Color = Avalonia.Media.Color; namespace OneWare.NetListSvgIntegration.Services; public class NetListSvgService { - public NetListSvgService(ILogger logger) + private readonly ILogger _logger; + private readonly IActive _active; + private readonly IDockService _dockService; + private bool _isRecording; + + private string _output = string.Empty; + + private Engine? _engine; + + public NetListSvgService(ILogger logger, IActive active, IDockService dockService) + { + _logger = logger; + _active = active; + _dockService = dockService; + } + + private void PrepareEngine() { try { - var engine = new Engine(); + _engine = new Engine(); var console = new { - log = new Action(x => Console.WriteLine(x)), - warn = new Action(x => Console.WriteLine("WARN: " + x)), - error = new Action(x => Console.WriteLine("ERROR: " + x)) + log = new Action(x => + { + if(_isRecording) _output += x; + }), + warn = new Action(x => _logger.Warning(x.ToString() ?? "")), + error = new Action(x => _logger.Error(x.ToString() ?? "")) }; - engine.SetValue("console", console); - engine.Execute("const window = {};"); - engine.Execute("window.Math = Math"); - engine.Execute("window.Array = Array"); - engine.Execute("console.log(window.Math.sqrt(9));"); - engine.SetValue("window.Array", engine.GetValue("Array")); - engine.SetValue("window.Date", new Func((input) => DateTime.Parse(input))); - engine.Execute("const exports = {}"); - engine.SetValue("setTimeout", new Action((action, delay) => + _engine.SetValue("console", console); + _engine.Execute("const window = {};"); + _engine.Execute("window.Math = Math"); + _engine.Execute("window.Array = Array"); + _engine.SetValue("window.Array", _engine.GetValue("Array")); + _engine.SetValue("window.Date", new Func((input) => DateTime.Parse(input))); + _engine.Execute("const exports = {}"); + _engine.SetValue("setTimeout", new Action((action, delay) => { if (delay <= 0) { @@ -42,30 +70,97 @@ public NetListSvgService(ILogger logger) }; timer.Start(); })); + + var script1 = GetAvaloniaAsset("avares://OneWare.NetListSvgIntegration/Assets/elk.bundled.js"); + _engine.Execute(script1); + var script2 = GetAvaloniaAsset("avares://OneWare.NetListSvgIntegration/Assets/netlistsvg.bundle.js"); + _engine.Execute(script2); - ExecuteScript(engine, "avares://OneWare.NetListSvgIntegration/Assets/elk.bundled.js"); - ExecuteScript(engine, "avares://OneWare.NetListSvgIntegration/Assets/netlistsvg.bundle.js"); - - engine.Execute("var netlistsvg = window.netlistsvg"); - - engine.Execute("netlistsvg.render(netlistsvg.digitalSkin, netlistsvg.exampleDigital, (err, result) => console.log(result));"); + _engine.Execute("var netlistsvg = window.netlistsvg"); } catch (Exception e) { - logger.Error(e.Message, e); + _logger.Error(e.Message, e); } } - private void ExecuteScript(Engine engine, string resource) + public async Task ShowSchemeAsync(IProjectFile jsonFile) { - using var stream = AssetLoader.Open(new Uri(resource)); - using var reader = new StreamReader(stream); - var script = reader.ReadToEnd(); - engine.Execute(script); + var svgStr = await CreateFromJsonAsync(jsonFile); + + if (!string.IsNullOrEmpty(svgStr)) + { + + var newFileName = Path.GetFileNameWithoutExtension(jsonFile.FullPath) + ".svg"; + var newFile = jsonFile.TopFolder!.AddFile(newFileName, true); + await File.WriteAllTextAsync(newFile.FullPath, svgStr); + await _dockService.OpenFileAsync(newFile); + } } - public async Task CreateFromJsonAsync(IProjectFile jsonFile) + public async Task CreateFromJsonAsync(IProjectFile jsonFile) { + _isRecording = true; + _output = string.Empty; + + var state = _active.AddState("Rendering Scheme...", AppState.Loading); + + var theme = Application.Current!.ActualThemeVariant; + var skin = LoadSkin(theme); + var backgroundHex = Application.Current!.FindResource(theme, "ThemeControlLowColor") is Color background ? + $"#{background.R:X2}{background.G:X2}{background.B:X2}" : "#FFFFFF"; + + try + { + await Task.Run(() => + { + if (_engine is null) PrepareEngine(); + if (_engine is null) throw new NullReferenceException(nameof(_engine)); + + var json = File.ReadAllText(jsonFile.FullPath); + + _engine.SetValue("OneWareJsonData", json); + _engine.Execute("var OneWareNetList = JSON.parse(OneWareJsonData)"); + + _engine.SetValue("OneWareSkin", skin); + _engine.Execute( + "netlistsvg.render(OneWareSkin, OneWareNetList, (err, result) => console.log(result));"); + + if (theme != ThemeVariant.Light) + { + _output = _output.Replace("fill: white; stroke: none", $"fill: {backgroundHex}; stroke: none"); + _output = _output.Replace("fill:#000", $"fill:#FFF"); + } + }); + } + catch (Exception e) + { + _logger.Error(e.Message, e); + } + + _active.RemoveState(state); + _isRecording = false; + return _output; + } + + private string GetAvaloniaAsset(string resource) + { + using var stream = AssetLoader.Open(new Uri(resource)); + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); + } + + private string LoadSkin(ThemeVariant themeVariant) + { + if (themeVariant == ThemeVariant.Light) + { + return GetAvaloniaAsset("avares://OneWare.NetListSvgIntegration/Assets/theme_teros.svg"); + //return GetAvaloniaAsset("avares://OneWare.NetListSvgIntegration/Assets/theme_light.svg"); + } + else + { + return GetAvaloniaAsset("avares://OneWare.NetListSvgIntegration/Assets/theme_dark.svg"); + } } } \ No newline at end of file