From 73f7fd1d3eed0a9cb0bb7757a1ee1b728f68af6a Mon Sep 17 00:00:00 2001 From: Tim <47110241+timunie@users.noreply.github.com> Date: Wed, 13 Dec 2023 01:32:17 +0100 Subject: [PATCH] Remove Console support in DevTools (#13920) * Remove Console support in DevTools * Adjust breaking changes * And remove Microsoft.CodeAnalysis --------- Co-authored-by: Max Katz --- api/Avalonia.Diagnostics.nupkg.xml | 10 ++ .../Avalonia.Diagnostics.csproj | 4 - .../Diagnostics/Models/ConsoleContext.cs | 36 ------ .../Diagnostics/Models/ConsoleHistoryItem.cs | 19 --- .../ViewModels/ConsoleViewModel.cs | 113 ------------------ .../Diagnostics/ViewModels/MainViewModel.cs | 13 -- .../Diagnostics/Views/ConsoleView.xaml | 57 --------- .../Diagnostics/Views/ConsoleView.xaml.cs | 68 ----------- .../Diagnostics/Views/MainView.xaml | 17 +-- .../Diagnostics/Views/MainView.xaml.cs | 48 -------- 10 files changed, 12 insertions(+), 373 deletions(-) create mode 100644 api/Avalonia.Diagnostics.nupkg.xml delete mode 100644 src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleContext.cs delete mode 100644 src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleHistoryItem.cs delete mode 100644 src/Avalonia.Diagnostics/Diagnostics/ViewModels/ConsoleViewModel.cs delete mode 100644 src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml delete mode 100644 src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml.cs diff --git a/api/Avalonia.Diagnostics.nupkg.xml b/api/Avalonia.Diagnostics.nupkg.xml new file mode 100644 index 0000000000..d364618e59 --- /dev/null +++ b/api/Avalonia.Diagnostics.nupkg.xml @@ -0,0 +1,10 @@ + + + + + CP0001 + T:CompiledAvaloniaXaml.!AvaloniaResources.NamespaceInfo:/Diagnostics/Views/ConsoleView.xaml + baseline/netstandard2.0/Avalonia.Diagnostics.dll + target/netstandard2.0/Avalonia.Diagnostics.dll + + \ No newline at end of file diff --git a/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj b/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj index 289f239ec1..dfe2691f96 100644 --- a/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj +++ b/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj @@ -17,10 +17,6 @@ - - - - diff --git a/src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleContext.cs b/src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleContext.cs deleted file mode 100644 index 4fd3de25dd..0000000000 --- a/src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleContext.cs +++ /dev/null @@ -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; - } - } -} diff --git a/src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleHistoryItem.cs b/src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleHistoryItem.cs deleted file mode 100644 index 86da18145a..0000000000 --- a/src/Avalonia.Diagnostics/Diagnostics/Models/ConsoleHistoryItem.cs +++ /dev/null @@ -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; } - } -} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ConsoleViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ConsoleViewModel.cs deleted file mode 100644 index 717b49d074..0000000000 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ConsoleViewModel.cs +++ /dev/null @@ -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 _updateContext; - private int _historyIndex = -1; - private string _input; - private bool _isVisible; - private ScriptState? _state; - - public ConsoleViewModel(Action 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 History { get; } = new AvaloniaList(); - - 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; - } -} diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs index 06e4e06f8a..4cb4162af5 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs @@ -67,7 +67,6 @@ namespace Avalonia.Diagnostics.ViewModels } }); } - Console = new ConsoleViewModel(UpdateConsoleContext); } public bool FreezePopups @@ -152,8 +151,6 @@ namespace Avalonia.Diagnostics.ViewModels public void ToggleRenderTimeGraphOverlay() => ShowRenderTimeGraphOverlay = !ShowRenderTimeGraphOverlay; - public ConsoleViewModel Console { get; } - public ViewModelBase? Content { get { return _content; } @@ -236,16 +233,6 @@ namespace Avalonia.Diagnostics.ViewModels private set => RaiseAndSetIfChanged(ref _pointerOverElementName, value); } - private void UpdateConsoleContext(ConsoleContext context) - { - context.root = _root; - - if (Content is TreePageViewModel tree) - { - context.e = tree.SelectedNode?.Visual; - } - } - public void SelectControl(Control control) { var tree = Content as TreePageViewModel; diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml b/src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml deleted file mode 100644 index dad87f72fd..0000000000 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml.cs b/src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml.cs deleted file mode 100644 index 01e8503495..0000000000 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/ConsoleView.xaml.cs +++ /dev/null @@ -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("historyList"); - ((ILogical)_historyList).LogicalChildren.CollectionChanged += HistoryChanged; - _input = this.GetControl("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; - } - } - } -} diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml b/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml index f810d8450c..83f17e67e9 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml @@ -20,7 +20,7 @@ - + @@ -56,13 +56,6 @@ - - - - - @@ -273,13 +266,7 @@ Background="{DynamicResource ThemeControlMidBrush}" IsVisible="False" /> - - - - diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml.cs b/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml.cs index df5188b29b..ac53d3d913 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/MainView.xaml.cs @@ -8,62 +8,14 @@ namespace Avalonia.Diagnostics.Views { internal class MainView : UserControl { - private readonly ConsoleView _console; - private readonly GridSplitter _consoleSplitter; - private readonly Grid _rootGrid; - private readonly int _consoleRow; - private double _consoleHeight = -1; - public MainView() { InitializeComponent(); - AddHandler(KeyUpEvent, PreviewKeyUp); - _console = this.GetControl("console"); - _consoleSplitter = this.GetControl("consoleSplitter"); - _rootGrid = this.GetControl("rootGrid"); - _consoleRow = Grid.GetRow(_console); - } - - public void ToggleConsole() - { - var vm = (MainViewModel?)DataContext; - if (vm is null) - { - return; - } - - if (_consoleHeight == -1) - { - _consoleHeight = Bounds.Height / 3; - } - - vm.Console.ToggleVisibility(); - _consoleSplitter.IsVisible = vm.Console.IsVisible; - - if (vm.Console.IsVisible) - { - _rootGrid.RowDefinitions[_consoleRow].Height = new GridLength(_consoleHeight, GridUnitType.Pixel); - Dispatcher.UIThread.Post(() => _console.FocusInput(), DispatcherPriority.Background); - } - else - { - _consoleHeight = _rootGrid.RowDefinitions[_consoleRow].Height.Value; - _rootGrid.RowDefinitions[_consoleRow].Height = new GridLength(0, GridUnitType.Pixel); - } } private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } - - private void PreviewKeyUp(object? sender, KeyEventArgs e) - { - if (e.Key == Key.Escape) - { - ToggleConsole(); - e.Handled = true; - } - } } }