Skip to content

Commit

Permalink
Completes #23 (except for some lingering selection bugs)
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Oct 22, 2019
1 parent bb5f7ab commit 17d41ab
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions Shared/Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Models\ParseTreeNode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\Token.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\VisualizerData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\BindableSelectionTextBox.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\TokenViewModel.cs" />
<Compile Include="..\Shared\Util\DebugTraceListener.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\Extensions\HashSetT.cs" />
Expand Down
68 changes: 68 additions & 0 deletions Shared/Util/BindableSelectionTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static ParseTreeVisualizer.Util.Functions;

namespace ParseTreeVisualizer {
public class BindableSelectionTextBox : TextBox {
public static readonly DependencyProperty BindableSelectionStartProperty =
DPRegister<int, BindableSelectionTextBox>(0, OnBindableSelectionStartChanged);

public static readonly DependencyProperty BindableSelectionLengthProperty =
DPRegister<int, BindableSelectionTextBox>(0, OnBindableSelectionStartChanged);

private bool changeFromUI;

public BindableSelectionTextBox() : base() {
SelectionChanged += OnSelectionChanged;
}

public int BindableSelectionStart {
get => (int)GetValue(BindableSelectionStartProperty);
set => SetValue(BindableSelectionStartProperty, value);
}

public int BindableSelectionLength {
get => (int)GetValue(BindableSelectionLengthProperty);
set => SetValue(BindableSelectionLengthProperty, value);
}

private static void OnBindableSelectionStartChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) {
var textBox = dependencyObject as BindableSelectionTextBox;

if (!textBox.changeFromUI) {
int newValue = (int)args.NewValue;
textBox.SelectionStart = newValue;
} else {
textBox.changeFromUI = false;
}
}

private static void OnBindableSelectionLengthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) {
var textBox = dependencyObject as BindableSelectionTextBox;

if (!textBox.changeFromUI) {
int newValue = (int)args.NewValue;
textBox.SelectionLength = newValue;
} else {
textBox.changeFromUI = false;
}
}

private void OnSelectionChanged(object sender, RoutedEventArgs e) {
if (BindableSelectionStart != SelectionStart) {
changeFromUI = true;
BindableSelectionStart = SelectionStart;
}

if (BindableSelectionLength != SelectionLength) {
changeFromUI = true;
BindableSelectionLength = SelectionLength;
}
}
}
}
3 changes: 2 additions & 1 deletion Shared/Util/DebugTraceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace ParseTreeVisualizer.Util {
public class DebugTraceListener : TraceListener {
private readonly static List<string> ignoreMessages = new List<string> {
"Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')"
"Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')",
"Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')"
};
public override void Write(string message) { }
public override void WriteLine(string message) {
Expand Down
5 changes: 4 additions & 1 deletion Shared/ViewModels/VisualizerDataViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public int SourceSelectionLength {
set => NotifyChanged(ref sourceSelectionLength, value);
}

private int sourceSelectionEnd => sourceSelectionStart + sourceSelectionLength - 1;
private int sourceSelectionEnd =>
sourceSelectionLength == 0 ?
sourceSelectionStart :
sourceSelectionStart + sourceSelectionLength - 1;

public ParseTreeNodeViewModel Root { get; }
public ReadOnlyCollection<TokenViewModel> Tokens { get; }
Expand Down
1 change: 0 additions & 1 deletion Shared/VisualizerControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public VisualizerControl() {
tvi.BringIntoView();
}));


Loaded += (s, e) => {
// https://stackoverflow.com/a/21436273/111794
configPopup.CustomPopupPlacementCallback += (popupSize, targetSize, offset) =>
Expand Down

0 comments on commit 17d41ab

Please sign in to comment.