Skip to content

Commit

Permalink
Merge pull request #105 from whistyun/nightly
Browse files Browse the repository at this point in the history
update AvaloniaEdit version.
  • Loading branch information
whistyun authored Apr 8, 2023
2 parents 2bdc8bf + 5a081e0 commit 8aec868
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
versions: [ 11.0.0-preview5 ]
versions: [ 11.0.0-preview6 ]

steps:
- uses: actions/checkout@v2
Expand Down
34 changes: 28 additions & 6 deletions Markdown.Avalonia.SyntaxHigh/SyntaxSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using AvaloniaEdit.Highlighting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -50,13 +51,10 @@ private Border CodeBlocksEvaluator(Match match)
else
{
// check wheither style is set
if (!ThemeDetector.IsAvalonEditSetup && Application.Current is not null)
if (!ThemeDetector.IsAvalonEditSetup)
{
var aeStyle = new StyleInclude(new Uri("avares://Markdown.Avalonia/"))
{
Source = new Uri("avares://AvaloniaEdit/AvaloniaEdit.xaml")
};
Application.Current.Styles.Add(aeStyle);
SetupStyle();

}

var txtEdit = new TextEditor();
Expand All @@ -73,5 +71,29 @@ private Border CodeBlocksEvaluator(Match match)
return result;
}
}

private static void SetupStyle()
{
if (Application.Current is null)
return;

string resourceUriTxt;
if (ThemeDetector.IsFluentUsed)
resourceUriTxt = "avares://AvaloniaEdit/Themes/Fluent/AvaloniaEdit.xaml";
else if (ThemeDetector.IsSimpleUsed)
resourceUriTxt = "avares://AvaloniaEdit/Themes/Simple/AvaloniaEdit.xaml";
else
{
Debug.Print("Markdown.Avalonia.SyntaxHigh can't add style for AvaloniaEdit. See https://github.com/whistyun/Markdown.Avalonia/wiki/Setup-AvaloniaEdit-for-syntax-hightlighting");
return;
}

var aeStyle = new StyleInclude(new Uri("avares://Markdown.Avalonia/"))
{
Source = new Uri(resourceUriTxt)
};

Application.Current.Styles.Add(aeStyle);
}
}
}
60 changes: 60 additions & 0 deletions Markdown.Avalonia.SyntaxHigh/ThemeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ namespace Markdown.Avalonia.SyntaxHigh
{
static class ThemeDetector
{
private static readonly string s_SimpleThemeFQCN = "Avalonia.Themes.Simple.SimpleTheme";
private static readonly string s_FluentThemeFQCN = "Avalonia.Themes.Fluent.FluentTheme";

private static readonly string s_SimpleThemeHost = "Avalonia.Themes.Simple";
private static readonly string s_FluentThemeHost = "Avalonia.Themes.Fluent";

private static bool _isAvalonEditSetup;
private static bool? s_isSimpleUsed;
private static bool? s_isFluentUsed;

private static bool CheckStyleSourceHost(IStyle style, string hostName)
{
Expand All @@ -23,6 +31,34 @@ private static bool CheckStyleSourceHost(IStyle style, string hostName)
else return false;
}

private static bool? CheckApplicationCurrentStyle(string themeFQCN, string avaresHost)
{
if (Application.Current is null
|| Application.Current.Styles is null)
return null;

foreach (var style in Application.Current.Styles)
{
if (style.GetType().FullName == themeFQCN)
{
return true;
}
if (style is StyleInclude incld)
{
var uri = incld.Source;

if (uri is null) return false;
if (!uri.IsAbsoluteUri) return false;

try { return uri.Host == avaresHost; }
catch { return false; }
}
else return false;
}

return false;
}

public static bool IsAvalonEditSetup
{
get
Expand All @@ -45,5 +81,29 @@ public static bool IsAvalonEditSetup
return _isAvalonEditSetup = false;
}
}

public static bool IsFluentUsed
{
get
{
if (s_isFluentUsed.HasValue)
return s_isFluentUsed.Value;

return Nvl(s_isFluentUsed = CheckApplicationCurrentStyle(s_FluentThemeFQCN, s_FluentThemeHost));
}
}

public static bool IsSimpleUsed
{
get
{
if (s_isSimpleUsed.HasValue)
return s_isSimpleUsed.Value;

return Nvl(s_isSimpleUsed = CheckApplicationCurrentStyle(s_SimpleThemeFQCN, s_SimpleThemeHost));
}
}

private static bool Nvl(bool? val) => val.HasValue && val.Value;
}
}
2 changes: 1 addition & 1 deletion Markdown.Avalonia.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PropertyGroup>
<AvaloniaVersion>11.0.0-preview6</AvaloniaVersion>
<DemoAvaloniaVersion>11.0.0-preview6</DemoAvaloniaVersion>
<AvaloniaEditVersion>11.0.0-preview5</AvaloniaEditVersion>
<AvaloniaEditVersion>11.0.0-preview6</AvaloniaEditVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(AVA_VER)' != '' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<ItemGroup>
<PackageReference Include="Avalonia" Version="$(DemoAvaloniaVersion)" />
<PackageReference Include="Avalonia.Desktop" Version="$(DemoAvaloniaVersion)" />
<PackageReference Include="FluentAvaloniaUI" Version="2.0.0-preview5" />
<PackageReference Include="FluentAvaloniaUI" Version="2.0.0-preview6" />
<PackageReference Include="Avalonia.ReactiveUI" Version="$(DemoAvaloniaVersion)" />
<PackageReference Include="Avalonia.Markup.Xaml.Loader" Version="$(DemoAvaloniaVersion)" />
<None Remove="Assets\XamlTemplate.txt" />
Expand Down

0 comments on commit 8aec868

Please sign in to comment.