Browse Source
* Remove Console support in DevTools * Adjust breaking changes * And remove Microsoft.CodeAnalysis --------- Co-authored-by: Max Katz <maxkatz6@outlook.com>pull/13932/head
committed by
GitHub
10 changed files with 12 additions and 373 deletions
@ -0,0 +1,10 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids --> |
||||
|
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
||||
|
<Suppression> |
||||
|
<DiagnosticId>CP0001</DiagnosticId> |
||||
|
<Target>T:CompiledAvaloniaXaml.!AvaloniaResources.NamespaceInfo:/Diagnostics/Views/ConsoleView.xaml</Target> |
||||
|
<Left>baseline/netstandard2.0/Avalonia.Diagnostics.dll</Left> |
||||
|
<Right>target/netstandard2.0/Avalonia.Diagnostics.dll</Right> |
||||
|
</Suppression> |
||||
|
</Suppressions> |
||||
@ -1,36 +0,0 @@ |
|||||
#pragma warning disable IDE1006 // Naming Styles
|
|
||||
|
|
||||
using Avalonia.Diagnostics.ViewModels; |
|
||||
|
|
||||
namespace Avalonia.Diagnostics.Models |
|
||||
{ |
|
||||
internal class ConsoleContext |
|
||||
{ |
|
||||
private readonly ConsoleViewModel _owner; |
|
||||
|
|
||||
internal ConsoleContext(ConsoleViewModel owner) => _owner = owner; |
|
||||
|
|
||||
public readonly string help = @"Welcome to Avalonia DevTools. Here you can execute arbitrary C# code using Roslyn scripting.
|
|
||||
|
|
||||
The following variables are available: |
|
||||
|
|
||||
e: The control currently selected in the logical or visual tree view |
|
||||
root: The root of the visual tree |
|
||||
|
|
||||
The following commands are available: |
|
||||
|
|
||||
clear(): Clear the output history |
|
||||
";
|
|
||||
|
|
||||
public dynamic? e { get; internal set; } |
|
||||
public dynamic? root { get; internal set; } |
|
||||
|
|
||||
internal static object NoOutput { get; } = new object(); |
|
||||
|
|
||||
public object clear() |
|
||||
{ |
|
||||
_owner.History.Clear(); |
|
||||
return NoOutput; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,19 +0,0 @@ |
|||||
using System; |
|
||||
using Avalonia.Media; |
|
||||
|
|
||||
namespace Avalonia.Diagnostics.Models |
|
||||
{ |
|
||||
internal class ConsoleHistoryItem |
|
||||
{ |
|
||||
public ConsoleHistoryItem(string input, object output) |
|
||||
{ |
|
||||
Input = input; |
|
||||
Output = output; |
|
||||
Foreground = output is Exception ? Brushes.Red : Brushes.Green; |
|
||||
} |
|
||||
|
|
||||
public string Input { get; } |
|
||||
public object Output { get; } |
|
||||
public IBrush Foreground { get; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,113 +0,0 @@ |
|||||
using System; |
|
||||
using System.Reflection; |
|
||||
using System.Threading.Tasks; |
|
||||
using Avalonia.Collections; |
|
||||
using Avalonia.Diagnostics.Models; |
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting; |
|
||||
using Microsoft.CodeAnalysis.Scripting; |
|
||||
|
|
||||
namespace Avalonia.Diagnostics.ViewModels |
|
||||
{ |
|
||||
internal class ConsoleViewModel : ViewModelBase |
|
||||
{ |
|
||||
private readonly ConsoleContext _context; |
|
||||
private readonly Action<ConsoleContext> _updateContext; |
|
||||
private int _historyIndex = -1; |
|
||||
private string _input; |
|
||||
private bool _isVisible; |
|
||||
private ScriptState<object>? _state; |
|
||||
|
|
||||
public ConsoleViewModel(Action<ConsoleContext> updateContext) |
|
||||
{ |
|
||||
_context = new ConsoleContext(this); |
|
||||
_input = string.Empty; |
|
||||
_updateContext = updateContext; |
|
||||
} |
|
||||
|
|
||||
public string Input |
|
||||
{ |
|
||||
get => _input; |
|
||||
set => RaiseAndSetIfChanged(ref _input, value); |
|
||||
} |
|
||||
|
|
||||
public bool IsVisible |
|
||||
{ |
|
||||
get => _isVisible; |
|
||||
set => RaiseAndSetIfChanged(ref _isVisible, value); |
|
||||
} |
|
||||
|
|
||||
public AvaloniaList<ConsoleHistoryItem> History { get; } = new AvaloniaList<ConsoleHistoryItem>(); |
|
||||
|
|
||||
public async Task Execute() |
|
||||
{ |
|
||||
if (string.IsNullOrWhiteSpace(Input)) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
var options = ScriptOptions.Default |
|
||||
.AddReferences(Assembly.GetAssembly(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo))); |
|
||||
|
|
||||
_updateContext(_context); |
|
||||
|
|
||||
if (_state == null) |
|
||||
{ |
|
||||
_state = await CSharpScript.RunAsync(Input, options: options, globals: _context); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
_state = await _state.ContinueWithAsync(Input); |
|
||||
} |
|
||||
|
|
||||
if (_state.ReturnValue != ConsoleContext.NoOutput) |
|
||||
{ |
|
||||
History.Add(new ConsoleHistoryItem(Input, _state.ReturnValue ?? "(null)")); |
|
||||
} |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
History.Add(new ConsoleHistoryItem(Input, ex)); |
|
||||
} |
|
||||
|
|
||||
Input = string.Empty; |
|
||||
_historyIndex = -1; |
|
||||
} |
|
||||
|
|
||||
public void HistoryUp() |
|
||||
{ |
|
||||
if (History.Count > 0) |
|
||||
{ |
|
||||
if (_historyIndex == -1) |
|
||||
{ |
|
||||
_historyIndex = History.Count - 1; |
|
||||
} |
|
||||
else if (_historyIndex > 0) |
|
||||
{ |
|
||||
--_historyIndex; |
|
||||
} |
|
||||
|
|
||||
Input = History[_historyIndex].Input; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void HistoryDown() |
|
||||
{ |
|
||||
if (History.Count > 0 && _historyIndex >= 0) |
|
||||
{ |
|
||||
if (_historyIndex == History.Count - 1) |
|
||||
{ |
|
||||
_historyIndex = -1; |
|
||||
Input = string.Empty; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
Input = History[++_historyIndex].Input; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void ToggleVisibility() => IsVisible = !IsVisible; |
|
||||
} |
|
||||
} |
|
||||
@ -1,57 +0,0 @@ |
|||||
<UserControl xmlns="https://github.com/avaloniaui" |
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|
||||
xmlns:viewModels="using:Avalonia.Diagnostics.ViewModels" |
|
||||
x:Class="Avalonia.Diagnostics.Views.ConsoleView" |
|
||||
x:DataType="viewModels:ConsoleViewModel"> |
|
||||
<UserControl.Styles> |
|
||||
<Style Selector="TextBox.console"> |
|
||||
<Setter Property="FontFamily" Value="/Assets/Fonts/SourceSansPro-Regular.ttf"/> |
|
||||
<Setter Property="Template"> |
|
||||
<ControlTemplate> |
|
||||
<Border Name="border" |
|
||||
Background="{TemplateBinding Background}" |
|
||||
BorderBrush="{TemplateBinding BorderBrush}" |
|
||||
BorderThickness="{TemplateBinding BorderThickness}"> |
|
||||
<DockPanel Margin="{TemplateBinding Padding}"> |
|
||||
<TextBlock DockPanel.Dock="Left" Margin="0,0,4,0"></TextBlock> |
|
||||
<TextPresenter Name="PART_TextPresenter" |
|
||||
Text="{TemplateBinding Text, Mode=TwoWay}" |
|
||||
CaretIndex="{TemplateBinding CaretIndex}" |
|
||||
SelectionStart="{TemplateBinding SelectionStart}" |
|
||||
SelectionEnd="{TemplateBinding SelectionEnd}" |
|
||||
TextAlignment="{TemplateBinding TextAlignment}" |
|
||||
TextWrapping="{TemplateBinding TextWrapping}" |
|
||||
PasswordChar="{TemplateBinding PasswordChar}"/> |
|
||||
</DockPanel> |
|
||||
</Border> |
|
||||
</ControlTemplate> |
|
||||
</Setter> |
|
||||
</Style> |
|
||||
</UserControl.Styles> |
|
||||
|
|
||||
<DockPanel> |
|
||||
<TextBox Name="input" |
|
||||
Classes="console" |
|
||||
DockPanel.Dock="Bottom" |
|
||||
BorderThickness="0" |
|
||||
Text="{Binding Input}"/> |
|
||||
|
|
||||
<ListBox Name="historyList" |
|
||||
BorderBrush="{DynamicResource ThemeControlMidBrush}" |
|
||||
BorderThickness="0,0,0,1" |
|
||||
FontFamily="/Assets/Fonts/SourceSansPro-Regular.ttf" |
|
||||
ItemsSource="{Binding History}"> |
|
||||
<ListBox.ItemTemplate> |
|
||||
<DataTemplate> |
|
||||
<StackPanel Orientation="Vertical"> |
|
||||
<DockPanel> |
|
||||
<TextBlock DockPanel.Dock="Left" Margin="0,0,4,0"></TextBlock> |
|
||||
<TextBlock Text="{Binding Input}"/> |
|
||||
</DockPanel> |
|
||||
<TextBlock Foreground="{Binding Foreground}" Text="{Binding Output}"/> |
|
||||
</StackPanel> |
|
||||
</DataTemplate> |
|
||||
</ListBox.ItemTemplate> |
|
||||
</ListBox> |
|
||||
</DockPanel> |
|
||||
</UserControl> |
|
||||
@ -1,68 +0,0 @@ |
|||||
using System; |
|
||||
using System.Collections.Specialized; |
|
||||
using Avalonia.Controls; |
|
||||
using Avalonia.Diagnostics.ViewModels; |
|
||||
using Avalonia.Input; |
|
||||
using Avalonia.LogicalTree; |
|
||||
using Avalonia.Markup.Xaml; |
|
||||
using Avalonia.Threading; |
|
||||
|
|
||||
namespace Avalonia.Diagnostics.Views |
|
||||
{ |
|
||||
internal class ConsoleView : UserControl |
|
||||
{ |
|
||||
private readonly ListBox _historyList; |
|
||||
private readonly TextBox _input; |
|
||||
|
|
||||
public ConsoleView() |
|
||||
{ |
|
||||
this.InitializeComponent(); |
|
||||
_historyList = this.GetControl<ListBox>("historyList"); |
|
||||
((ILogical)_historyList).LogicalChildren.CollectionChanged += HistoryChanged; |
|
||||
_input = this.GetControl<TextBox>("input"); |
|
||||
_input.KeyDown += InputKeyDown; |
|
||||
} |
|
||||
|
|
||||
public void FocusInput() => _input.Focus(); |
|
||||
|
|
||||
private void InitializeComponent() |
|
||||
{ |
|
||||
AvaloniaXamlLoader.Load(this); |
|
||||
} |
|
||||
|
|
||||
private void HistoryChanged(object? sender, NotifyCollectionChangedEventArgs e) |
|
||||
{ |
|
||||
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems?[0] is Control control) |
|
||||
{ |
|
||||
DispatcherTimer.RunOnce(control.BringIntoView, TimeSpan.Zero); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private void InputKeyDown(object? sender, KeyEventArgs e) |
|
||||
{ |
|
||||
var vm = (ConsoleViewModel?)DataContext; |
|
||||
if (vm is null) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
switch (e.Key) |
|
||||
{ |
|
||||
case Key.Enter: |
|
||||
_ = vm.Execute(); |
|
||||
e.Handled = true; |
|
||||
break; |
|
||||
case Key.Up: |
|
||||
vm.HistoryUp(); |
|
||||
_input.CaretIndex = _input.Text?.Length ?? 0; |
|
||||
e.Handled = true; |
|
||||
break; |
|
||||
case Key.Down: |
|
||||
vm.HistoryDown(); |
|
||||
_input.CaretIndex = _input.Text?.Length ?? 0; |
|
||||
e.Handled = true; |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue