Printing with Dark Theme

SyntaxEditor for Windows Forms Forum

Posted 7 months ago by AF
Version: 22.1.0
Avatar

I'm sure this has been asked before, but I cannot find a good way to swap the color highlighting when printing in dark theme.

I would like to be able to Clone the initial state of AmbientHighlightingStyleRegistry before the dark theme is applied, and swap the state before and after printing.

My workaround is to just set...

mySyntaxEditor.PrintSettings.HighlightingStyleRegistry = new HighlightingStyleRegistry();

...but obviously this then removes the syntax colors for the specified lanugauge.

Comments (5)

Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Actually we allow you to set a custom highlighting style registry to only be used for printing.  That way you can have your editor in a dark theme but printouts will be in a light theme.  Please see this documentation topic for details.


Actipro Software Support

Posted 7 months ago by AF
Avatar

Yes, but I was hoping there would be a simple way to Clone the initial state of AmbientHighlightingStyleRegistry before the dark theme is applied...

// create static languages
CSharpSyntaxLanguage = RegisterLanguageDef("CSharp.langdef");


// CAPTURE STATE HERE FOR PRINTING
this.PrinterStyleRegistry = AmbientHighlightingStyleRegistry.Instance.Clone();


// apply Dark Theme
AmbientHighlightingStyleRegistry.Instance = ApplyTheme("Dark.vssettings");

Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

While we don't have a Clone method, the registry itself is an IEnumerable<KeyValuePair<IClassificationType, IHighlightingStyle>>, similar to a dictionary.  You can enumerate that and on a new registry instance, call the newRegistry.Register method for each enumerated entry.  That will clone it.


Actipro Software Support

Answer - Posted 7 months ago by AF
Avatar

OK but that isn't great as I then need to Clone the HighlightingStyles myself, which may miss important information e.g. internal data or data from a derived class...

private static IHighlightingStyleRegistry CreatePrinterStyleRegistry()
{
    var registry = new HighlightingStyleRegistry();

    foreach (var item in (IEnumerable<KeyValuePair<IClassificationType, IHighlightingStyle>>)AmbientHighlightingStyleRegistry.Instance)
    {
        var style = item.Value;
        registry.Register(item.Key, new HighlightingStyle(style.Foreground,
                                                          style.Background,
                                                          style.Bold,
                                                          style.Italic,
                                                          style.UnderlineKind,
                                                          style.BackgroundSpansVirtualSpace));
    }

    return registry;
}

Note: if you just Register the reference, the new Registry value will change when the original changes...

registry.Register(item.Key, item.Value);

So it would be better if either a Clone or ExportHighlightingStyles method was available.

[Modified 7 months ago]

Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Thank you for talking through the scenario. Your points are valid about cloning and export/import. We currently have research we are doing to make highlighting styles more friendly to different themes (i.e., light and dark). I believe the ideal solution for printing is to be able to instruct it to use a light-themed registry that is maintained separately from a dark-themed registry in use by the control. That's the goal we want to work towards.

For now, the code you suggested for cloning the registry should work just fine for the purpose of building the default styles for printing. When a light/dark-based approach is available, you could move to that and not worry about long-term implications of missing important information in the clone process.


Actipro Software Support

The latest build of this product (v22.1.3) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.