Skip to content

Commit

Permalink
Remove flickering from code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 27, 2023
1 parent 501ed10 commit 750e371
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/WireMockInspector/ViewModels/CodeGenerator.cs
Expand Up @@ -37,8 +37,8 @@ public MappingCodeGeneratorConfigViewModel Config
private set;
} = new MappingCodeGeneratorConfigViewModel();

private readonly ObservableAsPropertyHelper<Markdown> _outputCode;
public Markdown OutputCode => _outputCode.Value;
private readonly ObservableAsPropertyHelper<MarkdownCode> _outputCode;
public MarkdownCode OutputCode => _outputCode.Value;


public MappingCodeGeneratorViewModel()
Expand All @@ -49,7 +49,7 @@ public MappingCodeGeneratorViewModel()
.Select(x =>
{
var code = CodeGenerator.GenerateCSharpCode(Request, Response, x);
return new Markdown("cs", code);
return new MarkdownCode("cs", code);
}).ToProperty(this, x => x.OutputCode, out _outputCode);
}
}
Expand Down
48 changes: 24 additions & 24 deletions src/WireMockInspector/ViewModels/MainWindowViewModel.cs
Expand Up @@ -782,9 +782,9 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
{
"String" or "FormUrlEncoded" => WrapBodyInMarkdown(req.Raw.Request.Body?? string.Empty),
"Json" => AsMarkdownCode("json", req.Raw.Request.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => new Markdown("plaintext", req.Raw.Request.BodyAsBytes?.ToString()?? string.Empty),
"File" => new Markdown("plaintext","[FileContent]"),
_ => new Markdown("plaintext", "")
"Bytes" => new MarkdownCode("plaintext", req.Raw.Request.BodyAsBytes?.ToString()?? string.Empty),
"File" => new MarkdownCode("plaintext","[FileContent]"),
_ => new MarkdownCode("plaintext", "")
}
},
Expectations = ExpectationsAsJson(expectations.Request?.Body),
Expand Down Expand Up @@ -824,44 +824,44 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
Expectations = expectations.Response switch
{
{Body: {} bodyResponse} => WrapBodyInMarkdown(bodyResponse),
{BodyAsJson: {} bodyAsJson} => new Markdown("json", bodyAsJson.ToString()!),
{BodyAsBytes: {} bodyAsBytes} => new Markdown("plaintext", bodyAsBytes.ToString()?? string.Empty),
{BodyAsFile: {} bodyAsFile} => new Markdown("plaintext",bodyAsFile),
_ => new Markdown("plaintext",string.Empty)
{BodyAsJson: {} bodyAsJson} => new MarkdownCode("json", bodyAsJson.ToString()!),
{BodyAsBytes: {} bodyAsBytes} => new MarkdownCode("plaintext", bodyAsBytes.ToString()?? string.Empty),
{BodyAsFile: {} bodyAsFile} => new MarkdownCode("plaintext",bodyAsFile),
_ => new MarkdownCode("plaintext",string.Empty)
}
}
}
};
}

private static Markdown GetActualForRequestBody(RequestViewModel req)
private static MarkdownCode GetActualForRequestBody(RequestViewModel req)
{
return req.Raw.Response?.DetectedBodyType.ToString() switch
{
"Json" => new Markdown("json",req.Raw.Response.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => new Markdown("plaintext", req.Raw.Response.BodyAsBytes?.ToString()?? string.Empty),
"File" => new Markdown("plaintext",req.Raw.Response.BodyAsFile?.ToString() ?? string.Empty),
"Json" => new MarkdownCode("json",req.Raw.Response.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => new MarkdownCode("plaintext", req.Raw.Response.BodyAsBytes?.ToString()?? string.Empty),
"File" => new MarkdownCode("plaintext",req.Raw.Response.BodyAsFile?.ToString() ?? string.Empty),
_ => WrapBodyInMarkdown( req.Raw.Response?.Body?? string.Empty),
};
}

private static Markdown WrapBodyInMarkdown(string bodyResponse)
private static MarkdownCode WrapBodyInMarkdown(string bodyResponse)
{
var cleanBody = bodyResponse.Trim();
if (cleanBody.StartsWith("[") || cleanBody.StartsWith("{"))
{
return new Markdown("json", bodyResponse);
return new MarkdownCode("json", bodyResponse);

}
if (cleanBody.StartsWith("<"))
{
return new Markdown("xml", bodyResponse);
return new MarkdownCode("xml", bodyResponse);

}
return new Markdown("plaintext", bodyResponse);
return new MarkdownCode("plaintext", bodyResponse);
}

public static Markdown AsMarkdownCode(string lang, string code) => new Markdown(lang, code);
public static MarkdownCode AsMarkdownCode(string lang, string code) => new MarkdownCode(lang, code);

public string RequestSearchTerm
{
Expand All @@ -873,11 +873,11 @@ public string RequestSearchTerm



private static Markdown ExpectationsAsJson(object? data)
private static MarkdownCode ExpectationsAsJson(object? data)
{
if (data == null)
{
return new Markdown("plaintext", string.Empty);
return new MarkdownCode("plaintext", string.Empty);
}

return AsMarkdownCode("json", JsonConvert.SerializeObject(data, Formatting.Indented));
Expand Down Expand Up @@ -906,8 +906,8 @@ public class RequestLogEntry
public DateTime Timestamp { get; set; }
public string Method { get; set; }
public string Path { get; set; }
public Markdown RequestDefinition { get; set; }
public Markdown ResponseDefinition { get; set; }
public MarkdownCode RequestDefinition { get; set; }
public MarkdownCode ResponseDefinition { get; set; }
public string StatusCode { get; set; }
}

Expand All @@ -917,7 +917,7 @@ public record ScenarioEdgeNode(string Id, bool Current, bool Hit):ScenarioNode(I
public record ScenarioTransition(string Id, ScenarioNode From, ScenarioNode To, bool Hit)
{
public string Description { get; set; }
public Markdown MappingDefinition { get; set; }
public MarkdownCode MappingDefinition { get; set; }
public RequestLogViewModel TriggeredBy { get; set; }
public DateTime? LastHit { get; set; }
};
Expand Down Expand Up @@ -991,19 +991,19 @@ public class MappingViewModel: ViewModelBase
public DateTime? UpdatedOn { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public Markdown Content { get; set; }
public MarkdownCode Content { get; set; }

public int PerfectHitCount { get; set; }
public int PartialHitCount { get; set; }
public MappingHitType HitType { get; set; }

public Markdown Code
public MarkdownCode Code
{
get => _code;
set => this.RaiseAndSetIfChanged(ref _code, value);
}

private Markdown _code;
private MarkdownCode _code;

public Scenario Scenario { get; set; }
public RequestLogViewModel PerfectMatches { get; set; }
Expand Down
20 changes: 10 additions & 10 deletions src/WireMockInspector/ViewModels/MatchDetailsViewModel.cs
Expand Up @@ -14,7 +14,7 @@ namespace WireMockInspector.ViewModels;
public class MatchDetailsViewModel:ViewModelBase
{
private ActualValue _actualValue;
private Markdown _expectations;
private MarkdownCode _expectations;
public string RuleName { get; set; }
public bool? Matched { get; set; }
public bool NoExpectations { get; set; }
Expand All @@ -27,7 +27,7 @@ public ActualValue ActualValue



public Markdown Expectations
public MarkdownCode Expectations
{
get => _expectations;
set => this.RaiseAndSetIfChanged(ref _expectations, value);
Expand Down Expand Up @@ -64,7 +64,7 @@ public MatchDetailsViewModel()

CopyExpectations = ReactiveCommand.Create(async () =>
{
if (Expectations is Markdown{rawValue: var value})
if (Expectations is MarkdownCode{rawValue: var value})
{
if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Expand Down Expand Up @@ -96,15 +96,15 @@ public MatchDetailsViewModel()
}, this.WhenAnyValue(x=>x.Expectations).Select(IsJsonMarkdown));
}

private static Markdown TryToReformat(Markdown markdown)
private static MarkdownCode TryToReformat(MarkdownCode markdownCode)
{
if (IsJsonMarkdown(markdown))
if (IsJsonMarkdown(markdownCode))
{

try
{

var formatted = JToken.Parse(markdown.rawValue).ToString(Formatting.Indented);
var formatted = JToken.Parse(markdownCode.rawValue).ToString(Formatting.Indented);
return MainWindowViewModel.AsMarkdownCode("json", formatted);
}
catch (Exception e)
Expand All @@ -113,10 +113,10 @@ private static Markdown TryToReformat(Markdown markdown)
}
}

return markdown;
return markdownCode;
}

private static bool IsJsonMarkdown(Markdown rawValue)
private static bool IsJsonMarkdown(MarkdownCode rawValue)
{
return rawValue?.lang == "json";
}
Expand All @@ -135,11 +135,11 @@ public class SimpleActualValue:ActualValue

public class MarkdownActualValue:ActualValue
{
public Markdown Value { get; set; }
public MarkdownCode Value { get; set; }
public string MarkdownValue { get; set; }
}

public record Markdown(string lang, string rawValue)
public record MarkdownCode(string lang, string rawValue)
{
public string AsMarkdownSyntax()
{
Expand Down
51 changes: 32 additions & 19 deletions src/WireMockInspector/Views/CodeBlockViewer.cs
Expand Up @@ -6,6 +6,7 @@
using AvaloniaEdit.Document;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;
using WireMockInspector.ViewModels;

namespace WireMockInspector.Views;

Expand All @@ -14,12 +15,6 @@ public class CodeBlockViewer:TextEditor, IStyleable
Type IStyleable.StyleKey => typeof(TextEditor);
public CodeBlockViewer()
{
this.Loaded += (sender, args) =>
{
};


this.Initialized += (sender, args) =>
{
//First of all you need to have a reference for your TextEditor for it to be used inside AvaloniaEdit.TextMate project.
Expand All @@ -30,27 +25,43 @@ public CodeBlockViewer()
_textEditor.IsReadOnly = true;
_textEditor.FontFamily = "Cascadia Code,Consolas,Menlo,Monospace";
};
this.PropertyChanged+= (sender, args) =>
{
if (args.Property.Name == nameof(Code))
{
SetMarkdown(args.NewValue as ViewModels.Markdown);
}
};
this._registryOptions = new RegistryOptions(ThemeName.DarkPlus);
this._textMateInstallation = this.InstallTextMate(_registryOptions);
}

static CodeBlockViewer()
{
CodeProperty.Changed.Subscribe(OnCodeChanged);
}

private void SetMarkdown(ViewModels.Markdown md)
private static void OnCodeChanged(AvaloniaPropertyChangedEventArgs e)
{
(e.Sender as CodeBlockViewer)?.OnCodeChanged((ViewModels.MarkdownCode)e.OldValue, (ViewModels.MarkdownCode)e.NewValue);
}

private void OnCodeChanged(MarkdownCode newValue, MarkdownCode NewValue)
{
SetMarkdown(NewValue);
}


private void SetMarkdown(ViewModels.MarkdownCode md)
{
if (md is not null)
{
this.Document = new TextDocument(md.rawValue);
if (this.Document is not null)
{
this.Document.Text = md.rawValue;
}
else
{
this.Document = new TextDocument(md.rawValue);
}
if (_currentLang != md.lang)
{
_currentLang = md.lang;
CodeBlockViewer _textEditor = this;

var _registryOptions = new RegistryOptions(ThemeName.DarkPlus);
var _textMateInstallation = _textEditor.InstallTextMate(_registryOptions);
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(_registryOptions.GetLanguageByExtension("."+md.lang).Id));
}
}
Expand All @@ -62,7 +73,7 @@ private void SetMarkdown(ViewModels.Markdown md)
}

private string? _currentLang;
public ViewModels.Markdown Code
public ViewModels.MarkdownCode Code
{
get { return GetValue(CodeProperty); }
set
Expand All @@ -72,5 +83,7 @@ public ViewModels.Markdown Code
}
}

public static readonly StyledProperty<ViewModels.Markdown> CodeProperty = AvaloniaProperty.Register<CodeBlockViewer, ViewModels.Markdown>(nameof(Code));
public static readonly StyledProperty<ViewModels.MarkdownCode> CodeProperty = AvaloniaProperty.Register<CodeBlockViewer, ViewModels.MarkdownCode>(nameof(Code));
private readonly TextMate.Installation _textMateInstallation;
private readonly RegistryOptions _registryOptions;
}

0 comments on commit 750e371

Please sign in to comment.