Skip to content

Commit

Permalink
allowing twice the same monitor, but brakes old configs saved. Sorry …
Browse files Browse the repository at this point in the history
…for that. fix #47
  • Loading branch information
mgth committed Jan 17, 2018
1 parent a481d2b commit fa77a4e
Show file tree
Hide file tree
Showing 65 changed files with 880 additions and 244 deletions.
1 change: 1 addition & 0 deletions HLab/MonitorVcp/HLab.Windows.MonitorVcp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="lbm.licenseheader" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
11 changes: 11 additions & 0 deletions HLab/MonitorVcp/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.2.0" newVersion="1.2.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
18 changes: 18 additions & 0 deletions HLab/Mvvm/HLab.Mvvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,20 @@
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="Swordfish.NET, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Swordfish.NET.CollectionsV3.3.0.0.1\lib\net45\Swordfish.NET.dll</HintPath>
</Reference>
<Reference Include="Swordfish.NET.CollectionsV3, Version=3.0.0.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Swordfish.NET.CollectionsV3.3.0.0.1\lib\net45\Swordfish.NET.CollectionsV3.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -139,15 +151,21 @@
</Page>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="HLab.Mvvm.licenseheader" />
<None Include="Icons\svg2xaml.xsl.bak" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Icons\colors.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Icons\html2xaml.xslt" />
<Content Include="Icons\svg2xaml.old.xsl" />
<EmbeddedResource Include="Icons\svg2xaml.xsl" />
</ItemGroup>
<ItemGroup>
<Folder Include="Html\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
86 changes: 77 additions & 9 deletions HLab/Mvvm/Icons/IconService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
Expand Down Expand Up @@ -54,6 +55,9 @@ public UIElement GetIcon(string assemblyName, string name)
}
public UIElement GetIcon(Assembly assembly, string name)
{

return GetIconXaml(assembly, name) ?? GetFromSvg(assembly, name);

var cache = _cache.GetValue(assembly, a => new IconCache(a));

return cache.Get(name,(a,n)=> GetIconXaml(a, n) ?? GetFromSvg(a, n));
Expand All @@ -74,13 +78,14 @@ public UIElement GetIconXaml(Assembly assembly, string name)
}
}

private XslCompiledTransform _transform;
private XslCompiledTransform _transformSvg;
private XslCompiledTransform _transformHtml;

private XslCompiledTransform Transform
private XslCompiledTransform TransformSvg
{
get
{
if (_transform == null)
if (_transformSvg == null)
{
using (var xslStream = Assembly.GetAssembly(this.GetType())
.GetManifestResourceStream("HLab.Mvvm.Icons.svg2xaml.xsl"))
Expand All @@ -89,14 +94,74 @@ private XslCompiledTransform Transform
using (var stylesheet = XmlReader.Create(xslStream))
{
var settings = new XsltSettings { EnableDocumentFunction = true };
_transform = new XslCompiledTransform();
_transform.Load(stylesheet, settings, new XmlUrlResolver());
_transformSvg = new XslCompiledTransform();
_transformSvg.Load(stylesheet, settings, new XmlUrlResolver());
}
}
}
return _transform;
return _transformSvg;
}
}
private XslCompiledTransform TransformHtml
{
get
{
if (_transformHtml == null)
{
using (var xslStream = Assembly.GetAssembly(this.GetType())
.GetManifestResourceStream("HLab.Mvvm.Icons.html2xaml.xslt"))
{
if (xslStream == null) throw new IOException("xsl file not found");
using (var stylesheet = XmlReader.Create(xslStream))
{
var settings = new XsltSettings { EnableDocumentFunction = true };
_transformHtml = new XslCompiledTransform();
_transformHtml.Load(stylesheet, settings, new XmlUrlResolver());
}
}
}
return _transformHtml;
}
}

public TextBlock GetFromHtml(string html)
{
TextBlock textBlock = null;
Application.Current.Dispatcher.Invoke(
() =>
{
using (var s = new MemoryStream())
{
using (var stringReader = new StringReader(html))
{
using (var htmlReader = XmlReader.Create(stringReader))
{
using (var w = XmlWriter.Create(s))
{
TransformHtml.Transform(htmlReader, w);
}
try
{
s.Seek(0, SeekOrigin.Begin);
var sz = Encoding.UTF8.GetString(s.ToArray());
using (var reader = XmlReader.Create(s))
{
textBlock = (TextBlock) System.Windows.Markup.XamlReader.Load(reader);
// Code to run on the GUI thread.
}
}
catch (IOException)
{
}
}
}
}
});
return textBlock;
}

public UIElement GetFromSvg(Assembly assembly, string name)
{
Expand All @@ -112,15 +177,18 @@ public UIElement GetFromSvg(Assembly assembly, string name)
{
using (var w = XmlWriter.Create(s))
{
Transform.Transform(svgReader, w);
TransformSvg.Transform(svgReader, w);
}
try
{
s.Seek(0, SeekOrigin.Begin);
var sz = Encoding.UTF8.GetString(s.ToArray());
//var sz = Encoding.UTF8.GetString(s.ToArray());

using (var reader = XmlReader.Create(s))
return (UIElement) System.Windows.Markup.XamlReader.Load(reader);
{
var icon = (UIElement) System.Windows.Markup.XamlReader.Load(reader);
return icon;
}
}
catch (IOException)
{
Expand Down
127 changes: 127 additions & 0 deletions HLab/Mvvm/Icons/html2xaml.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>


<!-- The html root element must be div, it translates to a xaml richtextblock.-->
<xsl:template match="/div" priority="9">
<TextBlock TextWrapping="WrapWithOverflow" >
<TextBlock.Resources>
<Style x:Key="Bullet" TargetType="Ellipse">
<Setter Property="Fill" Value="Black" />
<Setter Property="Width" Value="6" />
<Setter Property="Height" Value="6" />
<Setter Property="Margin" Value="-30,0,0,1" />
</Style>
<Style x:Key="Link" TargetType="Hyperlink">
<!--<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontSize" Value="11" />
<Setter Property="Margin" Value="-15,-11" />-->
</Style>
</TextBlock.Resources>
<xsl:if test="normalize-space(text()) != ''">
<Run><xsl:value-of select="normalize-space(text())" /></Run>
</xsl:if>
<xsl:apply-templates select="/div/*" />
</TextBlock>
</xsl:template>
<xsl:template match="div" priority="0">
<Span><xsl:apply-templates /></Span>
</xsl:template>


<!-- XAML Paragraphs cannot contain paragraphs, so we convert top-level html paragraphs to xaml paragraphs and convert nested html paragraphs to xaml spans with linebreaks -->
<xsl:template match="/div/P | /div/p" priority="9">
<Span>
<xsl:if test="@font-color">
<xsl:attribute name="Foreground">
<xsl:value-of select="@font-color"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</Span>
</xsl:template>
<xsl:template match="P | p" priority="0">
<Span>
<xsl:if test="@font-color">
<xsl:attribute name="Foreground">
<xsl:value-of select="@font-color"/>
</xsl:attribute>
</xsl:if>
<LineBreak /><xsl:apply-templates />
</Span>
</xsl:template>

<xsl:template match="SPAN | span">
<Span>
<xsl:if test="@font-color">
<xsl:attribute name="Foreground">
<xsl:value-of select="@font-color"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</Span>
</xsl:template>

<!-- The RichTextBlock XAML element can contain only paragraph child elements, so any unknown html child elements of the root element will become XAML paragraphs -->
<xsl:template match="/div/*">
<Span><xsl:apply-templates /></Span>
</xsl:template>


<!-- The RichTextBlock XAML element can contain only paragraph child elements, so any unknown html child elements of the root element will become XAML paragraphs -->
<xsl:template match="/div/*">
<Span><xsl:apply-templates /></Span>
</xsl:template>

<!-- Lists can only occur outside paragraphs, at the top level -->
<xsl:template match="/div/UL | /div/ul"><xsl:apply-templates /></xsl:template>

<xsl:template match="/div/UL/LI | /div/ul/LI | /div/UL/li | /div/ul/li" priority="9" >
<Span Margin="20,0,0,0"><Span><InlineUIContainer><Ellipse Style="{{StaticResource Bullet}}"/></InlineUIContainer><xsl:apply-templates /><LineBreak /></Span></Span>
</xsl:template>
<!-- An UL can only contain LI, so ignore all other elements within an UL -->
<xsl:template match="/div/UL/* | /div/ul/*" priority="8" />

<xsl:template match="B | b | STRONG | strong">
<Bold><xsl:apply-templates /></Bold>
</xsl:template>

<xsl:template match="I | i | EM | em">
<Italic><xsl:apply-templates /></Italic>
</xsl:template>

<xsl:template match="U | u">
<Underline><xsl:apply-templates /></Underline>
</xsl:template>
<xsl:template match="SUP | sup">
<Run Typography.Variants="Superscript">
<xsl:value-of select="."/>
</Run>
</xsl:template>
<xsl:template match="CODE | code">
<Run FontFamily="Courier New" >
<xsl:value-of select="."/>
</Run>
</xsl:template>

<xsl:template match="BR | br" priority="0" >
<LineBreak />
</xsl:template>

<xsl:template match="A | a">
<Span><InlineUIContainer><Hyperlink Style="{{StaticResource Link}}"><xsl:attribute name="NavigateUri"><xsl:value-of select="@href"/></xsl:attribute><xsl:apply-templates /></Hyperlink></InlineUIContainer></Span>
</xsl:template>

<xsl:template match="IMG | img">
<Span><InlineUIContainer><Image Stretch="None" ><xsl:attribute name="Source"><xsl:value-of select="@src"/></xsl:attribute><xsl:apply-templates /></Image></InlineUIContainer></Span>
</xsl:template>

<!-- Note that by default, the text content of any unmatched HTML elements will be copied in the XAML. -->
</xsl:stylesheet>
21 changes: 18 additions & 3 deletions HLab/Mvvm/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace HLab.Mvvm
{
Expand Down Expand Up @@ -152,16 +153,30 @@ private static void OnViewModeContextChanged(DependencyObject dependencyObject,
public ViewLocator()
{
this.DataContextChanged += ViewLocator_DataContextChanged;

var b = new Binding
{
Source = this,
Path = new PropertyPath("DataContext"),
Mode = BindingMode.OneWay
};
BindingOperations.SetBinding(this, ModelProperty, b);
}

private void ViewLocator_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
//TODO : problème dans lbm mais insipensable dans erp
//if (sender is ViewLocator vl && ReferenceEquals(vl.Model, e.OldValue))
//{
// var oldModel = vl.Model;
// vl.Model = e.NewValue;
// vl.Update(oldModel, vl.ViewMode, vl.ViewClass);
// if(vl.Model != null)
// { }

// if (GetBindingExpression(ModelProperty).Status == BindingStatus.Unattached)
// {
// var oldModel = vl.Model;
// vl.Model = e.NewValue;
// vl.Update(oldModel, vl.ViewMode, vl.ViewClass);
// }
//}
}

Expand Down
8 changes: 4 additions & 4 deletions HLab/Mvvm/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ You should have received a copy of the GNU General Public License

namespace HLab.Mvvm
{
public class ViewModel : INotifyPropertyChanged
public class ViewModel<T> : NotifierObject, IViewModel<T>
{
public event PropertyChangedEventHandler PropertyChanged
protected ViewModel(bool init = true) : base(init)
{
add => this.Add(value);
remove => this.Remove(value);
}

public T Model => this.Get<T>();
}
}
Loading

0 comments on commit fa77a4e

Please sign in to comment.