Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Check Nulls on "AutoCompletManager" #101

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 71 additions & 40 deletions NZazu/Fields/Libs/AutoCompleteManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -175,12 +175,16 @@ private void GetInnerElementReferences()
{
var border = (Border)_listBox.Template.FindName("Border", _listBox);
_scrollViewer = (ScrollViewer)border.Child;
if (_scrollViewer == null) return;

_resizeGrip = _scrollViewer.Template.FindName("ResizeGrip", _scrollViewer) as ResizeGrip;
_scrollBar = _scrollViewer.Template.FindName("PART_VerticalScrollBar", _scrollViewer) as ScrollBar;
}

private void UpdateGripVisual()
{
if (_resizeGrip == null) return;

var rectSize = SystemParameters.VerticalScrollBarWidth;
var triangle = (Path)_resizeGrip.Template.FindName("RG_TRIANGLE", _resizeGrip);
var pg = (PathGeometry)triangle.Data;
Expand All @@ -201,37 +205,46 @@ private void UpdateGripVisual()

private void SetupEventHandlers()
{
var ownerWindow = Window.GetWindow(_textBox);
// ReSharper disable InvocationIsSkipped
// ReSharper disable PossibleNullReferenceException
Debug.Assert(ownerWindow != null, "ownerWindow != null");
ownerWindow.PreviewMouseDown += OwnerWindowPreviewMouseDown;
ownerWindow.Deactivated += OwnerWindowDeactivated;
// ReSharper restore PossibleNullReferenceException
// ReSharper restore InvocationIsSkipped
if (_textBox != null)
{
var ownerWindow = Window.GetWindow(_textBox);
// ReSharper disable InvocationIsSkipped
// ReSharper disable PossibleNullReferenceException
Debug.Assert(ownerWindow != null, "ownerWindow != null");
ownerWindow.PreviewMouseDown += OwnerWindowPreviewMouseDown;
ownerWindow.Deactivated += OwnerWindowDeactivated;
// ReSharper restore PossibleNullReferenceException
// ReSharper restore InvocationIsSkipped

var wih = new WindowInteropHelper(ownerWindow);
var hwndSource = HwndSource.FromHwnd(wih.Handle);
// ReSharper disable InvocationIsSkipped
// ReSharper disable PossibleNullReferenceException
Debug.Assert(hwndSource != null, "hwndSource != null");
var hwndSourceHook = new HwndSourceHook(HookHandler);
hwndSource.AddHook(hwndSourceHook);
//hwndSource.RemoveHook();?
// ReSharper restore PossibleNullReferenceException
// ReSharper restore InvocationIsSkipped
var wih = new WindowInteropHelper(ownerWindow);
var hwndSource = HwndSource.FromHwnd(wih.Handle);
// ReSharper disable InvocationIsSkipped
// ReSharper disable PossibleNullReferenceException
Debug.Assert(hwndSource != null, "hwndSource != null");
var hwndSourceHook = new HwndSourceHook(HookHandler);
hwndSource.AddHook(hwndSourceHook);
//hwndSource.RemoveHook();?
// ReSharper restore PossibleNullReferenceException
// ReSharper restore InvocationIsSkipped

_textBox.TextChanged += TextBoxTextChanged;
_textBox.PreviewKeyDown += TextBoxPreviewKeyDown;
_textBox.LostFocus += TextBoxLostFocus;
_textBox.TextChanged += TextBoxTextChanged;
_textBox.PreviewKeyDown += TextBoxPreviewKeyDown;
_textBox.LostFocus += TextBoxLostFocus;
}

_listBox.PreviewMouseLeftButtonDown += ListBoxPreviewMouseLeftButtonDown;
_listBox.MouseLeftButtonUp += ListBoxMouseLeftButtonUp;
_listBox.PreviewMouseMove += ListBoxPreviewMouseMove;
if (_listBox != null)
{
_listBox.PreviewMouseLeftButtonDown += ListBoxPreviewMouseLeftButtonDown;
_listBox.MouseLeftButtonUp += ListBoxMouseLeftButtonUp;
_listBox.PreviewMouseMove += ListBoxPreviewMouseMove;
}

_resizeGrip.PreviewMouseLeftButtonDown += ResizeGripPreviewMouseLeftButtonDown;
_resizeGrip.PreviewMouseMove += ResizeGripPreviewMouseMove;
_resizeGrip.PreviewMouseUp += ResizeGripPreviewMouseUp;
if (_resizeGrip != null)
{
_resizeGrip.PreviewMouseLeftButtonDown += ResizeGripPreviewMouseLeftButtonDown;
_resizeGrip.PreviewMouseMove += ResizeGripPreviewMouseMove;
_resizeGrip.PreviewMouseUp += ResizeGripPreviewMouseUp;
}
}

#endregion
Expand All @@ -243,7 +256,7 @@ private void TextBoxTextChanged(object sender, TextChangedEventArgs e)
if (_textChangedByCode || Disabled || DataProvider == null)
return;

var text = _textBox.Text;
var text = _textBox?.Text ?? "";
if (string.IsNullOrEmpty(text))
{
_popup.IsOpen = false;
Expand All @@ -258,7 +271,7 @@ private void TextBoxTextChanged(object sender, TextChangedEventArgs e)
_asyncThread = new Thread(() =>
{
var dispatcher = _textBox.Dispatcher; // Application.Current.Dispatcher;
var currentText = (String)dispatcher.Invoke((Func<TextBox, string>)(txtBox => txtBox.Text), _textBox);
var currentText = (string)dispatcher?.Invoke((Func<TextBox, string>)(txtBox => txtBox.Text), _textBox);
if (text != currentText)
return;
var items = GetSuggestions(text, DataConnection);
Expand Down Expand Up @@ -295,7 +308,7 @@ private void TextBoxPreviewKeyDown(object sender, KeyEventArgs e)
if (e.Key == Key.Enter)
{
_popup.IsOpen = false;
_textBox.SelectAll();
_textBox?.SelectAll();
return;
}

Expand All @@ -319,6 +332,8 @@ private void TextBoxPreviewKeyDown(object sender, KeyEventArgs e)
index = -1;
break;
default:
if (_scrollBar == null) break;

if (index == (int)_scrollBar.Value)
{
index -= (int)_scrollBar.ViewportSize;
Expand All @@ -341,15 +356,15 @@ private void TextBoxPreviewKeyDown(object sender, KeyEventArgs e)
{
index = -1;
}
else if (index == (int)(_scrollBar.Value + _scrollBar.ViewportSize) - 1)
else if (index == (int)(_scrollBar.Value + _scrollBar.ViewportSize) - 1 && _scrollBar != null)
{
index += (int)_scrollBar.ViewportSize - 1;
if (index > _listBox.Items.Count - 1)
{
index = _listBox.Items.Count - 1;
}
}
else
else if (_scrollBar != null)
{
index = (int)(_scrollBar.Value + _scrollBar.ViewportSize - 1);
}
Expand Down Expand Up @@ -469,6 +484,8 @@ private void ListBoxMouseLeftButtonUp(object sender, MouseButtonEventArgs e)

private void ResizeGripPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_resizeGrip == null) return;

_downWidth = _chrome.ActualWidth + PopupShadowDepth;
_downHeight = _chrome.ActualHeight + PopupShadowDepth;

Expand All @@ -483,6 +500,8 @@ private void ResizeGripPreviewMouseLeftButtonDown(object sender, MouseButtonEven

private void ResizeGripPreviewMouseMove(object sender, MouseEventArgs e)
{
if (_resizeGrip == null) return;

if (e.LeftButton != MouseButtonState.Pressed)
{
return;
Expand Down Expand Up @@ -519,6 +538,8 @@ private void ResizeGripPreviewMouseMove(object sender, MouseEventArgs e)

private void ResizeGripPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (_resizeGrip == null) return;

_resizeGrip.ReleaseMouseCapture();
if (Math.Abs(_popup.Width - _downWidth) > Epsilon || Math.Abs(_popup.Height - _downHeight) > Epsilon)
{
Expand Down Expand Up @@ -563,7 +584,7 @@ private IntPtr HookHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, r

private void PopulatePopupList(IEnumerable<string> items)
{
var text = _textBox.Text;
var text = _textBox?.Text ?? "";

_listBox.ItemsSource = items;
if (_listBox.Items.Count == 0)
Expand All @@ -581,11 +602,11 @@ private void PopulatePopupList(IEnumerable<string> items)

_listBox.SelectedIndex = -1;
_textBeforeChangedByCode = text;
_scrollViewer.ScrollToHome();
_scrollViewer?.ScrollToHome();
ShowPopup();

//
if (AutoAppend && !_supressAutoAppend &&
if (AutoAppend && !_supressAutoAppend && _textBox != null &&
_textBox.SelectionLength == 0 &&
_textBox.SelectionStart == _textBox.Text.Length)
{
Expand All @@ -606,7 +627,7 @@ private void PopulatePopupList(IEnumerable<string> items)

private bool PopupOnTop
{
get { return _popupOnTop; }
get => _popupOnTop;
set
{
if (_popupOnTop == value)
Expand All @@ -616,15 +637,21 @@ private bool PopupOnTop
_popupOnTop = value;
if (_popupOnTop)
{
if (_scrollBar != null)
_scrollBar.Margin = new Thickness(0, SystemParameters.HorizontalScrollBarHeight, 0, 0);

if (_resizeGrip == null) return;
_resizeGrip.VerticalAlignment = VerticalAlignment.Top;
_scrollBar.Margin = new Thickness(0, SystemParameters.HorizontalScrollBarHeight, 0, 0);
_resizeGrip.LayoutTransform = new ScaleTransform(1, -1);
_resizeGrip.Cursor = Cursors.SizeNESW;
}
else
{
if (_scrollBar != null)
_scrollBar.Margin = new Thickness(0, 0, 0, SystemParameters.HorizontalScrollBarHeight);

if (_resizeGrip == null) return;
_resizeGrip.VerticalAlignment = VerticalAlignment.Bottom;
_scrollBar.Margin = new Thickness(0, 0, 0, SystemParameters.HorizontalScrollBarHeight);
_resizeGrip.LayoutTransform = Transform.Identity;
_resizeGrip.Cursor = Cursors.SizeNWSE;
}
Expand All @@ -635,6 +662,8 @@ private bool PopupOnTop

private void ShowPopup()
{
if (_textBox == null) return;

var popupOnTop = false;

var p = new Point(0, _textBox.ActualHeight);
Expand Down Expand Up @@ -687,6 +716,8 @@ private void ShowPopup()

private void UpdateText(string text, bool selectAll)
{
if (_textBox == null) return;

_textChangedByCode = true;
_textBox.Text = text;
if (selectAll)
Expand All @@ -702,4 +733,4 @@ private void UpdateText(string text, bool selectAll)

#endregion
}
}
}