Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example instructions not working with theme colors #437

Open
renderhjs opened this issue Jul 14, 2024 · 1 comment
Open

Example instructions not working with theme colors #437

renderhjs opened this issue Jul 14, 2024 · 1 comment

Comments

@renderhjs
Copy link

Followed getting started instructions from here multiple times from scratch but I can't seem to apply theme styling (colors) to the AvaloniaEdit.

image
New Avalonia project with additional steps from https://github.com/AvaloniaUI/AvaloniaEdit to add AvaloniaEdit + apply a theme.

The AvaloniaEdit.Demo project on GitHub seems to have different properties in the TextMate classes for example

public bool TryGetThemeColor(string colorKey, out string colorString)
and behaves differently to what's in NuGet. When I am trying to replicate the demo implementation I can't make it work with the NuGet libraries which offer different methods in TextMate and other classes.

Sample

MainView.axaml

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:AvaloniaEditTheme2.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="AvaloniaEditTheme2.Views.MainView"
             x:DataType="vm:MainViewModel"
						 xmlns:AvaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit"
						 >
  <Design.DataContext>
    <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
    <vm:MainViewModel />
  </Design.DataContext>
	<Grid>
		<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
		<AvaloniaEdit:TextEditor Name="Editor" Text="Hello AvaloniaEdit!"
                           ShowLineNumbers="True"
                           FontFamily="Cascadia Code,Consolas,Menlo,Monospace"/>
	</Grid>
</UserControl>

MainView.axaml.cs
...

using Avalonia.Controls;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;

namespace AvaloniaEditTheme2.Views;

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();

		//First of all you need to have a reference for your TextEditor for it to be used inside AvaloniaEdit.TextMate project.
		var _textEditor = this.FindControl<TextEditor>("Editor");

		//Here we initialize RegistryOptions with the theme we want to use.
		var  _registryOptions = new RegistryOptions(ThemeName.LightPlus);

		//Initial setup of TextMate.
		var _textMateInstallation = _textEditor.InstallTextMate(_registryOptions);

		//Here we are getting the language by the extension and right after that we are initializing grammar with this language.
		//And that's all , you are ready to use AvaloniaEdit with syntax highlighting!
		_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(_registryOptions.GetLanguageByExtension(".cs").Id));


		// Python Sample
		_textEditor.Text = "import subprocess\r\n\r\ndef open_visual_studio(path):\r\n    try:\r\n        # Use subprocess to run the command\r\n        subprocess.run(f'start devenv \"{path}\"', shell=True, check=True)";
	}
}

Expected Result

Theme color of ThemeName.LightPlus or any other should render in their colors

Actual result

There are no syntax theme colors applied

@mgarstenauer
Copy link
Contributor

What NuGet packages are you using? When I run your code with the following NuGet packages, it seems to be working fine.

    <PackageReference Include="Avalonia" Version="11.0.11" />
    <PackageReference Include="Avalonia.Desktop" Version="11.0.11" />
    <PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.11" />
    <PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.11" />
    <PackageReference Include="Avalonia.AvaloniaEdit" Version="11.0.6" />
    <PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.6" />

Note that I had to change:

_registryOptions.GetLanguageByExtension(".cs")

to

_registryOptions.GetLanguageByExtension(".py")

Are you perhaps including an incompatible version of TextMateSharp? For example, this one works with AvaloniaEdit 11.0.6:

<PackageReference Include="TextMateSharp.Grammars" Version="1.0.56" />

But this one is incompatible:

<PackageReference Include="TextMateSharp.Grammars" Version="1.0.57" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants