diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.SharedUI/Controls | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d9.tar.gz Tango-00a491d9.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.SharedUI/Controls')
8 files changed, 713 insertions, 1052 deletions
diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/AllSelectedCheckboxList.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/AllSelectedCheckboxList.cs deleted file mode 100644 index 6e3cb862f..000000000 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/AllSelectedCheckboxList.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System; -using System.Windows; -using System.Windows.Controls; -using Tango.Core.Commands; -//using System.Collections; -using System.Linq; -using System.Collections; - -namespace Tango.SharedUI.Controls -{ - public class AllSelectedCheckboxList : ListBox - { - #region Properties - - private ItemsControl _itemsControl; - - public ItemsControl ItemsControl - { - get { return _itemsControl; } - set { _itemsControl = value; } - } - - public bool? AllSelected - { - get { return (bool?)GetValue(AllSelectedProperty); } - set - { - SetValue(AllSelectedProperty, value); - } - } - - public static readonly DependencyProperty AllSelectedProperty = - DependencyProperty.Register("AllSelected", typeof(bool?), typeof(AllSelectedCheckboxList), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnAllSelectedChanged))); - - - private static void OnAllSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - AllSelectedCheckboxList l = o as AllSelectedCheckboxList; - var nv = e.NewValue; - l.AllSelectedChanged((bool?)nv); - } - - public static DependencyProperty ClickCheckBoxCommandProperty = DependencyProperty.Register("ClickCheckBoxCommand", typeof(RelayCommand<object>), typeof(AllSelectedCheckboxList)); - public RelayCommand<object> ClickCheckBoxCommand - { - get { return (RelayCommand<object>)GetValue(ClickCheckBoxCommandProperty); } - private set { SetValue(ClickCheckBoxCommandProperty, value); } - } - - #endregion - - #region "Constructors" - - static AllSelectedCheckboxList() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(AllSelectedCheckboxList), new FrameworkPropertyMetadata(typeof(AllSelectedCheckboxList))); - - } - public AllSelectedCheckboxList() : base() - { - ClickCheckBoxCommand = new RelayCommand<object>(ClickCheckBox); - } - - /// <summary> - /// Clicks the CheckBox.Update Selection ListBoxitem. - /// </summary> - /// <param name="obj">The object.</param> - private void ClickCheckBox(object obj) - { - if (obj is ListBoxItem) - { - ListBoxItem lbItem = obj as ListBoxItem; - CheckBox checkBox = lbItem.FindVisualChildren<CheckBox>().FirstOrDefault(); - if (checkBox != null) - { - bool? check = checkBox.IsChecked; - if (check != null) - { - lbItem.IsSelected = (bool)check; - } - } - } - - if (SelectedItems.Count == 0) - { - AllSelected = false; - } - else if (Items.Count > 0 && SelectedItems.Count == Items.Count) - { - AllSelected = true; - } - else - { - AllSelected = null; - } - } - - #endregion - - #region Override - - /// <summary> - /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />. - /// </summary> - public override void OnApplyTemplate() - { - base.OnApplyTemplate(); - AllSelectedChanged(AllSelected); - - - - } - - protected override void OnSelectionChanged(SelectionChangedEventArgs e) - { - base.OnSelectionChanged(e); - foreach(var addItem in e.AddedItems.OfType<Components.SelectedObject>()) - { - addItem.IsSelected = true; - } - foreach (var removeItem in e.RemovedItems.OfType<Components.SelectedObject>()) - { - removeItem.IsSelected = false; - } - } - - #endregion - - private void AllSelectedChanged(bool? value) - { - if (value == null) - return; - SelectionMode = SelectionMode.Multiple; - if (value == true) - { - SelectAll(); - } - else - { - UnselectAll(); - } - } - } -} diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs deleted file mode 100644 index 153b36804..000000000 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs +++ /dev/null @@ -1,344 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using Tango.Core.Commands; -using System.ComponentModel; -using System.Windows.Controls.Primitives; -using System.Windows.Threading; -using System.Diagnostics; - -namespace Tango.SharedUI.Controls -{ - internal sealed class MultiSelectComboBoxTemplateSelector : DataTemplateSelector - { - public override DataTemplate SelectTemplate(object item, DependencyObject container) - { - FrameworkElement element = container as FrameworkElement; - if (element != null && item != null && item is MultiSelectComboBoxItem) - return element.FindResource("CheckItem") as DataTemplate; - - return element.FindResource("SearchItem") as DataTemplate; ; - - } - } - - [TemplatePart(Name = MultiSelectComboBox.PartEditor, Type = typeof(TextBox))] - [TemplatePart(Name = MultiSelectComboBox.PartPopup, Type = typeof(Popup))] - public class MultiSelectComboBox : Control - { - #region Fields - public const string PartEditor = "Edit_PART"; - public const string PartPopup = "MultiSel_Popup"; - private ItemsControl _items_control; - private bool _isLoaded; - #endregion - #region "Constructors" - - static MultiSelectComboBox() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(typeof(MultiSelectComboBox))); - - } - public MultiSelectComboBox() : base() - { - SelectedItemsList = new ObservableCollection<object>(); - RemoveAllCommand = new RelayCommand(RemoveAllSelectedItems, CanRemoveAllSelectedItems); - RemoveItemCommand = new RelayCommand<MultiSelectComboBoxItem>(RemoveSelectedItem); - AddItemCommand = new RelayCommand<string>(AddSelectedItem); - SearchText = new MultiSelectComboBoxSearchItem(); - Loaded += MultiSelectComboBox_Loaded; - _isLoaded = false; - - - } - #endregion - - #region Properties - - private TextBox _editor; - - public TextBox Editor - { - get { return _editor; } - set { _editor = value; } - } - - private Popup _popup; - - public Popup Popup - { - get { return _popup; } - set { _popup = value; } - } - - private DispatcherTimer _fetchTimer; - - public DispatcherTimer FetchTimer - { - get { return _fetchTimer; } - set { _fetchTimer = value; } - } - - public ObservableCollection<object> SelectedItemsList - { - get { return (ObservableCollection<object>)GetValue(SelectedItemsListProperty); } - set { SetValue(SelectedItemsListProperty, value); } - } - - public static readonly DependencyProperty SelectedItemsListProperty = - DependencyProperty.Register("SelectedItemsList", typeof(ObservableCollection<object>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<object>))); - - - public ListCollectionView SearchItemsList - { - get { return (ListCollectionView)GetValue(SearchItemsListProperty); } - set { SetValue(SearchItemsListProperty, value); } - } - /// <summary> - /// The search items list property for popup items list - /// </summary> - public static readonly DependencyProperty SearchItemsListProperty = - DependencyProperty.Register("SearchItemsList", typeof(ListCollectionView), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>))); - - public ObservableCollection<string> Items - { - get { return (ObservableCollection<string>)GetValue(ItemsProperty); } - set { SetValue(ItemsProperty, value); } - } - - public static readonly DependencyProperty ItemsProperty = - DependencyProperty.Register("Items", typeof(ObservableCollection<string>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>))); - - - public bool IsToggleChecked - { - get { return (bool)GetValue(IsToggleCheckedProperty); } - set { SetValue(IsToggleCheckedProperty, value); } - } - - public static readonly DependencyProperty IsToggleCheckedProperty = - DependencyProperty.Register("IsToggleChecked", typeof(bool), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(false)); - - private MultiSelectComboBoxSearchItem _searchText; - /// <summary> - /// Gets or sets the search text of TextBox. - /// </summary> - public MultiSelectComboBoxSearchItem SearchText - { - get { return _searchText; } - set - { - _searchText = value; - OnTextChanged(); - } - } - #endregion - - private void MultiSelectComboBox_Loaded(object sender, RoutedEventArgs e) - { - if (!_isLoaded) - { - ToggleButton b2 = GetTemplateChild("MultiSelToggleButton") as ToggleButton; - if (b2 != null) - { - // ct.LoadContent(); - _items_control = b2.Template.FindName("SelectedItemsControl", b2) as ItemsControl; - if (_items_control != null) - { - var wrapPanel = _items_control.FindChild<WrapPanel>(); - if (wrapPanel != null) - { - _items_control.Bind(ItemsControl.HeightProperty, wrapPanel, WrapPanel.ActualHeightProperty, BindingMode.OneWay); - _isLoaded = true; - - var searchItem =(_items_control.ItemContainerGenerator.ContainerFromItem(SearchText)); - Editor = searchItem.FindVisualChildren< TextBox>().FirstOrDefault(x => x.Name == PartEditor); - if (Editor != null) - { - Editor.TextChanged += OnEditorTextChanged; - Editor.PreviewKeyDown += OnEditorKeyDown; - Editor.LostFocus += OnEditorLostFocus; - Editor.GotFocus += OnEditorGotFocus; - } - } - } - this.GotFocus += SelTextBox_GotFocus; - } - } - } - - void OnTextChanged() - { - if(SearchItemsList != null) - { - if (String.IsNullOrEmpty(SearchText.Text)) - SearchItemsList.Filter = null; - else - SearchItemsList.Filter = new Predicate<object>(o => (((string)o).ToLower().Contains(SearchText.Text.ToLower()))); - } - } - /// <summary> - /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />. - /// </summary> - public override void OnApplyTemplate() - { - base.OnApplyTemplate(); - if (SelectedItemsList == null) - SelectedItemsList = new ObservableCollection<object>(); - SelectedItemsList.Add(SearchText); - - SearchItemsList = new ListCollectionView(this.Items); - Popup = Template.FindName(PartPopup, this) as Popup; - - - if (Popup != null) - { - Popup.StaysOpen = false; - Popup.Opened += OnPopupOpened; - Popup.Closed += OnPopupClosed; - } - } - - private void OnEditorTextChanged(object sender, TextChangedEventArgs e) - { - if (FetchTimer == null) - { - FetchTimer = new DispatcherTimer(); - FetchTimer.Interval = TimeSpan.FromMilliseconds(500); - FetchTimer.Tick += OnFetchTimerTick; - } - FetchTimer.IsEnabled = false; - FetchTimer.Stop(); - IsToggleChecked = true; - - FetchTimer.IsEnabled = true; - FetchTimer.Start(); - - } - - private void OnFetchTimerTick(object sender, EventArgs e) - { - FetchTimer.IsEnabled = false; - FetchTimer.Stop(); - SearchText.Text = Editor.Text; - OnTextChanged(); - } - - private void OnEditorKeyDown(object sender, KeyEventArgs e) - { - IsToggleChecked = true; - } - - private void OnEditorLostFocus(object sender, RoutedEventArgs e) - { - if (!IsKeyboardFocusWithin) - { - IsToggleChecked = false; - } - } - - private void SelTextBox_GotFocus(object sender, RoutedEventArgs e) - { - Editor?.Focus(); - } - - private void OnEditorGotFocus(object sender, RoutedEventArgs e) - { - IsToggleChecked = true; - } - - private void OnPopupOpened(object sender, EventArgs e) - { - - } - private void OnPopupClosed(object sender, EventArgs e) - { - IsToggleChecked = false; - - } - #region Commands - /// <summary> - /// The remove all command property for clear SelectedItemsList - /// </summary> - public static DependencyProperty RemoveAllCommandProperty = DependencyProperty.Register("RemoveAllCommand", typeof(RelayCommand), typeof(MultiSelectComboBox)); - public RelayCommand RemoveAllCommand - { - get { return (RelayCommand)GetValue(RemoveAllCommandProperty); } - private set { SetValue(RemoveAllCommandProperty, value); } - } - - /// <summary> - /// The remove item from SelectedItemsList command property - /// </summary> - public static DependencyProperty RemoveItemCommandProperty = DependencyProperty.Register("RemoveItemCommand", typeof(RelayCommand<MultiSelectComboBoxItem>), typeof(MultiSelectComboBox)); - public RelayCommand<MultiSelectComboBoxItem> RemoveItemCommand - { - get { return (RelayCommand<MultiSelectComboBoxItem>)GetValue(RemoveItemCommandProperty); } - private set { SetValue(RemoveItemCommandProperty, value); } - } - - /// <summary> - /// The add item to SelectedItemsList command property - /// </summary> - public static DependencyProperty AddItemCommandProperty = DependencyProperty.Register("AddItemCommand", typeof(RelayCommand<string>), typeof(MultiSelectComboBox)); - public RelayCommand<string> AddItemCommand - { - get { return (RelayCommand<string>)GetValue(AddItemCommandProperty); } - private set { SetValue(AddItemCommandProperty, value); } - } - - /// <summary> - /// Removes the selected item. - /// </summary> - /// <param name="item">The item.</param> - private void RemoveSelectedItem(IMultiSelectComboBoxItem item) - { - SelectedItemsList.Remove(item); - RemoveAllCommand.RaiseCanExecuteChanged(); - } - - /// <summary> - /// Adds the selected item. - /// </summary> - /// <param name="item">The item.</param> - private void AddSelectedItem(string item) - { - if (SelectedItemsList.Any(x => (x as IMultiSelectComboBoxItem).Text == item)) - return; - SelectedItemsList.Insert(SelectedItemsList.Count - 1, new MultiSelectComboBoxItem(item)); - RemoveAllCommand.RaiseCanExecuteChanged(); - } - /// <summary> - /// Removes all selected items. - /// </summary> - private void RemoveAllSelectedItems() - { - int index = SelectedItemsList.Count - 2; - while (index >= 0) - { - SelectedItemsList.RemoveAt(index--); - } - RemoveAllCommand.RaiseCanExecuteChanged(); - } - private bool CanRemoveAllSelectedItems() - { - return (SelectedItemsList != null && SelectedItemsList.Count > 1); - } - #endregion - - - } -} diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs deleted file mode 100644 index 2dcaa977a..000000000 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.SharedUI.Controls -{ - interface IMultiSelectComboBoxItem - { - string Text { get; set; } - } - - public class MultiSelectComboBoxItem : IMultiSelectComboBoxItem - { - public string Text { get; set; } - public MultiSelectComboBoxItem(string text) - { - Text = text; - } - public override string ToString() - { - return Text; - } - } - - public class MultiSelectComboBoxSearchItem : IMultiSelectComboBoxItem - { - public string Text { get; set; } - public MultiSelectComboBoxSearchItem(string text = "") - { - Text = text; - } - public override string ToString() - { - return Text; - } - } -} diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectDataGrid.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectDataGrid.cs index 12c4645e3..8f54a2ce4 100644 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectDataGrid.cs +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectDataGrid.cs @@ -9,7 +9,7 @@ using System.Windows.Controls; namespace Tango.SharedUI.Controls { - public class MultiSelectDataGrid : DoubleClickDataGrid + public class MultiSelectDataGrid : DataGrid { public IList SelectedItemsList { diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs index cefdbbfd6..b3869daff 100644 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Diagnostics; using System.Linq; using System.Text; -using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -13,7 +11,6 @@ using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Threading; -using Tango.Core; using Tango.SharedUI.Helpers; namespace Tango.SharedUI.Controls @@ -21,17 +18,7 @@ namespace Tango.SharedUI.Controls [ContentProperty(nameof(Elements))] public class NavigationControl : UserControl { - private event Action NavigationCompleted; - public event EventHandler<FrameworkElement> SelectedElementChanged; - private Thread _navigationThread; - private bool _preventSelectedObjectNavigation; - private ProducerConsumerQueue<NavigationQueueItem> _navigationQueue; - - private class NavigationQueueItem - { - public NavigationElement FromElement { get; set; } - public NavigationElement ToElement { get; set; } - } + private Action _onCompleted; #region Transition Types @@ -186,24 +173,6 @@ namespace Tango.SharedUI.Controls #region Properties - - - public object SelectedObject - { - get { return (object)GetValue(SelectedObjectProperty); } - set { SetValue(SelectedObjectProperty, value); } - } - public static readonly DependencyProperty SelectedObjectProperty = - DependencyProperty.Register("SelectedObject", typeof(object), typeof(NavigationControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (d, e) => (d as NavigationControl).OnSelectedObjectChanged())); - - private void OnSelectedObjectChanged() - { - if (SelectedObject != null && !_preventSelectedObjectNavigation) - { - NavigateTo(SelectedObject); - } - } - public ObservableCollection<FrameworkElement> Elements { get { return (ObservableCollection<FrameworkElement>)GetValue(ElementsProperty); } @@ -218,7 +187,7 @@ namespace Tango.SharedUI.Controls set { SetValue(SelectedElementProperty, value); } } public static readonly DependencyProperty SelectedElementProperty = - DependencyProperty.Register("SelectedElement", typeof(FrameworkElement), typeof(NavigationControl), new FrameworkPropertyMetadata(null, (d, e) => (d as NavigationControl).OnSelectedElementChanged(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement))); + DependencyProperty.Register("SelectedElement", typeof(FrameworkElement), typeof(NavigationControl), new PropertyMetadata(null, (d, e) => (d as NavigationControl).OnSelectedElementChanged(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement))); public TransitionTypes TransitionType { @@ -252,13 +221,7 @@ namespace Tango.SharedUI.Controls public static readonly DependencyProperty KeepElementsAttachedProperty = DependencyProperty.Register("KeepElementsAttached", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false)); - public bool GalleryMode - { - get { return (bool)GetValue(GalleryModeProperty); } - set { SetValue(GalleryModeProperty, value); } - } - public static readonly DependencyProperty GalleryModeProperty = - DependencyProperty.Register("GalleryMode", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false)); + public int SelectedIndex { @@ -316,9 +279,7 @@ namespace Tango.SharedUI.Controls /// <returns></returns> public static String GetNavigationName(FrameworkElement element) { - var name = element.GetValue(NavigationName).ToStringSafe(); - if (String.IsNullOrEmpty(name)) name = element.GetType().Name; - return name; + return element.GetValue(NavigationName).ToStringSafe(); } #endregion @@ -334,11 +295,6 @@ namespace Tango.SharedUI.Controls Content = _grid; Elements = new ObservableCollection<FrameworkElement>(); Loaded += NavigationControl_Loaded; - - _navigationQueue = new ProducerConsumerQueue<NavigationQueueItem>(); - _navigationThread = new Thread(NavigationThread); - _navigationThread.IsBackground = true; - _navigationThread.Start(); } #endregion @@ -391,8 +347,6 @@ namespace Tango.SharedUI.Controls } else { - Elements.ToList().ForEach(x => x.FocusVisualStyle = null); - var toRemove = _grid.Children.OfType<NavigationElement>().ToList().Where(x => !Elements.Contains(x.Element)).ToList(); var toAdd = Elements.Where(x => !_grid.Children.OfType<NavigationElement>().Select(y => y.Element).ToList().Contains(x)).ToList(); @@ -454,181 +408,135 @@ namespace Tango.SharedUI.Controls private void Navigate(NavigationElement fromElement, NavigationElement toElement) { - _navigationQueue.BlockEnqueue(new NavigationQueueItem() + if (toElement == null || toElement == fromElement) { - FromElement = fromElement, - ToElement = toElement, - }); - } + _onCompleted?.Invoke(); + } - private void NavigationThread() - { - try + if (fromElement != null) { - while (true) - { - var item = _navigationQueue.BlockDequeue(); - - var navigationCompleted = false; - - NavigationElement fromElement = item.FromElement; - NavigationElement toElement = item.ToElement; - - Dispatcher.BeginInvoke(new Action(() => - { - if (toElement == null || toElement == fromElement) - { - //navigationCompleted = true; - Debug.WriteLine("NavigationControl: THIS MIGHT CAUSE PROBLEMS !!"); - } - - if (fromElement != null) - { - DoubleAnimation toAnimation = new DoubleAnimation(); - toAnimation.Duration = TransitionDuration; - - DoubleAnimation fromAnimation = new DoubleAnimation(); - fromAnimation.Duration = TransitionDuration; - - int fromIndex = Elements.IndexOf(fromElement.Element); - int toIndex = Elements.IndexOf(toElement.Element); - - fromElement.Reset(); - toElement.Reset(); + DoubleAnimation toAnimation = new DoubleAnimation(); + toAnimation.Duration = TransitionDuration; - fromAnimation.Completed += (_, __) => - { - fromElement.Deactivate(!KeepElementsAttached); - }; + DoubleAnimation fromAnimation = new DoubleAnimation(); + fromAnimation.Duration = TransitionDuration; - bool completed = false; - - toAnimation.Completed += (_, __) => - { - if (!completed) - { - completed = true; - - INavigationView fromNavigationView = fromElement.Element as INavigationView; - INavigationView toNavigationView = toElement.Element as INavigationView; - - if (fromNavigationView != null) - { - fromNavigationView.OnNavigatedFrom(); - } - if (toNavigationView != null) - { - toNavigationView.OnNavigatedTo(); - } + int fromIndex = Elements.IndexOf(fromElement.Element); + int toIndex = Elements.IndexOf(toElement.Element); - INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel; - INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; + fromElement.Reset(); + toElement.Reset(); - if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom(); - if (toVM != null) toVM.OnNavigatedTo(); + fromAnimation.Completed += (_, __) => + { + fromElement.Deactivate(!KeepElementsAttached); + }; - navigationCompleted = true; - } - }; + bool completed = false; - switch (TransitionType) - { - case TransitionTypes.Fade: - fromAnimation.From = 1; - fromAnimation.To = 0; - - toAnimation.From = 0; - toAnimation.To = 1; + toAnimation.Completed += (_, __) => + { + if (!completed) + { + completed = true; - fromElement.AnimateOpacity(fromAnimation); - toElement.AnimateOpacity(toAnimation); + INavigationView fromNavigationView = fromElement.Element as INavigationView; + INavigationView toNavigationView = toElement.Element as INavigationView; - break; - case TransitionTypes.Zoom: - fromAnimation.From = 1; - fromAnimation.To = 0; + if (fromNavigationView != null) + { + fromNavigationView.OnNavigatedFrom(); + } + if (toNavigationView != null) + { + toNavigationView.OnNavigatedTo(); + } - toAnimation.From = 0; - toAnimation.To = 1; + INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel; + INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; - fromElement.AnimateScale(fromAnimation); - toElement.AnimateScale(toAnimation); + if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom(); + if (toVM != null) toVM.OnNavigatedTo(); - break; - case TransitionTypes.Slide: + _onCompleted?.Invoke(); + } + }; - if (toIndex > fromIndex || GalleryMode) - { - fromAnimation.From = 0; - fromAnimation.To = -ActualWidth; + switch (TransitionType) + { + case TransitionTypes.Fade: + fromAnimation.From = 1; + fromAnimation.To = 0; - toAnimation.From = ActualWidth; - toAnimation.To = 0; - } - else - { - fromAnimation.From = 0; - fromAnimation.To = ActualWidth; + toAnimation.From = 0; + toAnimation.To = 1; - toAnimation.From = -ActualWidth; - toAnimation.To = 0; - } + fromElement.AnimateOpacity(fromAnimation); + toElement.AnimateOpacity(toAnimation); - fromElement.AnimateTranslateX(fromAnimation); - toElement.AnimateTranslateX(toAnimation); + break; + case TransitionTypes.Zoom: + fromAnimation.From = 1; + fromAnimation.To = 0; - break; - } + toAnimation.From = 0; + toAnimation.To = 1; - if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade) - { - DoubleAnimation fromFadeAnimation = new DoubleAnimation(); - fromFadeAnimation.From = 1; - fromFadeAnimation.To = 0; - fromFadeAnimation.Duration = TransitionDuration; + fromElement.AnimateScale(fromAnimation); + toElement.AnimateScale(toAnimation); - DoubleAnimation toFadeAnimation = new DoubleAnimation(); - toFadeAnimation.From = 0; - toFadeAnimation.To = 1; - toFadeAnimation.Duration = TransitionDuration; + break; + case TransitionTypes.Slide: - fromElement.AnimateOpacity(fromFadeAnimation); - toElement.AnimateOpacity(toFadeAnimation); - } + if (toIndex > fromIndex) + { + fromAnimation.From = 0; + fromAnimation.To = -ActualWidth; - fromElement.Activate(); - toElement.Activate(); + toAnimation.From = ActualWidth; + toAnimation.To = 0; } else { - toElement.Activate(); - toElement.Reset(); - - INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; - if (toVM != null) toVM.OnNavigatedTo(); + fromAnimation.From = 0; + fromAnimation.To = ActualWidth; - navigationCompleted = true; + toAnimation.From = -ActualWidth; + toAnimation.To = 0; } - })); - for (int i = 0; i < 30; i++) - { - if (navigationCompleted) - { - break; - } - Thread.Sleep(100); - } + fromElement.AnimateTranslateX(fromAnimation); + toElement.AnimateTranslateX(toAnimation); - Dispatcher.BeginInvoke(new Action(() => - { - NavigationCompleted?.Invoke(); - })); + break; } + + if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade) + { + DoubleAnimation fromFadeAnimation = new DoubleAnimation(); + fromFadeAnimation.From = 1; + fromFadeAnimation.To = 0; + fromFadeAnimation.Duration = TransitionDuration; + + DoubleAnimation toFadeAnimation = new DoubleAnimation(); + toFadeAnimation.From = 0; + toFadeAnimation.To = 1; + toFadeAnimation.Duration = TransitionDuration; + + fromElement.AnimateOpacity(fromFadeAnimation); + toElement.AnimateOpacity(toFadeAnimation); + } + + fromElement.Activate(); + toElement.Activate(); } - catch + else { - + toElement.Activate(); + toElement.Reset(); + + INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; + if (toVM != null) toVM.OnNavigatedTo(); } } @@ -639,14 +547,6 @@ namespace Tango.SharedUI.Controls protected virtual void OnSelectedElementChanged(FrameworkElement fromElement, FrameworkElement toElement) { Navigate(GetElementContainer(fromElement), GetElementContainer(toElement)); - SelectedElementChanged?.Invoke(this, toElement); - - if (toElement != null) - { - _preventSelectedObjectNavigation = true; - SelectedObject = GetNavigationName(toElement); - _preventSelectedObjectNavigation = false; - } } #endregion @@ -668,67 +568,27 @@ namespace Tango.SharedUI.Controls return NavigateTo(navigationName, null); } - public FrameworkElement NavigateTo(Object obj) - { - return NavigateTo(obj.ToString(), null); - } - public FrameworkElement NavigateTo(String navigationName, Action onCompleted = null) { - Action completed = null; - - completed = () => - { - try - { - onCompleted?.Invoke(); - } - catch { } - NavigationCompleted -= completed; - }; - - NavigationCompleted += completed; + _onCompleted = onCompleted; var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName); - Task.Factory.StartNew(() => + if (element != null) { - Thread.Sleep(10); - - Dispatcher.BeginInvoke(new Action(async () => + if (SelectedElement == element) { - if (element != null) - { - if (SelectedElement == element) - { - await Task.Delay((int)TransitionDuration.TimeSpan.TotalMilliseconds * 2); - NavigationCompleted?.Invoke(); - } - else - { - SelectedElement = element; - } - } - })); - }); + _onCompleted?.Invoke(); + } + else + { + SelectedElement = element; + } + } return element; } - public Task<FrameworkElement> NavigateToAsync(String navigationName) - { - TaskCompletionSource<FrameworkElement> source = new TaskCompletionSource<FrameworkElement>(); - - FrameworkElement view = null; - - view = NavigateTo(navigationName, () => - { - source.SetResult(view); - }); - - return source.Task; - } - public FrameworkElement GetElement(String navigationName) { return Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName); @@ -741,11 +601,6 @@ namespace Tango.SharedUI.Controls return element; } - public String GetSelectedElementNavigationName() - { - return GetNavigationName(SelectedElement); - } - /// <remarks> /// This method needs to be called in order for // the element to print visibly at the correct size. @@ -768,6 +623,7 @@ namespace Tango.SharedUI.Controls catch { } } } + #endregion } } diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/ScriptEditorControl.xaml b/Software/Visual_Studio/Tango.SharedUI/Controls/ScriptEditorControl.xaml new file mode 100644 index 000000000..817297191 --- /dev/null +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/ScriptEditorControl.xaml @@ -0,0 +1,113 @@ +<UserControl x:Class="Tango.SharedUI.Controls.ScriptEditorControl" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:fa="http://schemas.fontawesome.io/icons/" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + mc:Ignorable="d" + d:DesignHeight="400" d:DesignWidth="600" Background="#151515"> + + <UserControl.InputBindings> + <KeyBinding Key="F5" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=RunCommand}" /> + </UserControl.InputBindings> + + <Grid> + <Grid> + <Grid.RowDefinitions> + <RowDefinition Height="40"/> + <RowDefinition Height="1*"/> + </Grid.RowDefinitions> + <Border BorderThickness="0 0 0 1" BorderBrush="#545454"> + <ToolBar Background="#202020"> + <StackPanel Margin="20 0 0 0" Orientation="Horizontal"> + <Button Cursor="Hand" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=SaveCommand}" ToolTip="Save" Style="{DynamicResource MetroCircleButtonStyle}" Width="16" Height="16"> + <fa:ImageAwesome Icon="Save" Foreground="LightGray"></fa:ImageAwesome> + </Button> + <Rectangle Margin="20 7 10 5" HorizontalAlignment="Center" VerticalAlignment="Stretch" Stroke="#3E3E3E" StrokeThickness="2"></Rectangle> + <Button Cursor="Hand" Command="Undo" Margin="10 0 0 0" ToolTip="Undo" Style="{DynamicResource MetroCircleButtonStyle}" Width="16" Height="16"> + <fa:ImageAwesome Icon="Undo" Foreground="LightGray"></fa:ImageAwesome> + </Button> + <Button Cursor="Hand" Command="Redo" Margin="10 0 0 0" ToolTip="Redo" Style="{DynamicResource MetroCircleButtonStyle}" Width="16" Height="16"> + <fa:ImageAwesome Icon="Repeat" Foreground="LightGray"></fa:ImageAwesome> + </Button> + <Rectangle Margin="20 7 10 5" HorizontalAlignment="Center" VerticalAlignment="Stretch" Stroke="#3E3E3E" StrokeThickness="2"></Rectangle> + <Button Cursor="Hand" Command="Cut" Margin="10 0 0 0" ToolTip="Cut" Style="{DynamicResource MetroCircleButtonStyle}" Width="16" Height="16"> + <fa:ImageAwesome Icon="Cut" Foreground="LightGray"></fa:ImageAwesome> + </Button> + <Button Cursor="Hand" Command="Copy" Margin="10 0 0 0" ToolTip="Copy" Style="{DynamicResource MetroCircleButtonStyle}" Width="20" Height="16"> + <fa:ImageAwesome Icon="Copy" Foreground="LightGray"></fa:ImageAwesome> + </Button> + <Button Cursor="Hand" Command="Paste" Margin="10 0 0 0" ToolTip="Paste" Style="{DynamicResource MetroCircleButtonStyle}" Width="16" Height="20"> + <fa:ImageAwesome Icon="Paste" Foreground="LightGray"></fa:ImageAwesome> + </Button> + <Rectangle Margin="20 7 10 5" HorizontalAlignment="Center" VerticalAlignment="Stretch" Stroke="#3E3E3E" StrokeThickness="2"></Rectangle> + <Button Cursor="Hand" Margin="10 0 0 0" ToolTip="Run (F5)" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=RunCommand}" Style="{DynamicResource MetroCircleButtonStyle}" Width="16" Height="16"> + <fa:ImageAwesome Icon="Play"> + <fa:ImageAwesome.Style> + <Style TargetType="fa:ImageAwesome"> + <Setter Property="Foreground" Value="#8DD28A"></Setter> + <Style.Triggers> + <Trigger Property="IsEnabled" Value="False"> + <Setter Property="Foreground" Value="Gray"></Setter> + </Trigger> + </Style.Triggers> + </Style> + </fa:ImageAwesome.Style> + </fa:ImageAwesome> + </Button> + <Button Cursor="Hand" Margin="15 0 0 0" ToolTip="Stop" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=StopCommand}" Style="{DynamicResource MetroCircleButtonStyle}" Width="14" Height="14"> + <fa:ImageAwesome Icon="Stop"> + <fa:ImageAwesome.Style> + <Style TargetType="fa:ImageAwesome"> + <Setter Property="Foreground" Value="#F38B76"></Setter> + <Style.Triggers> + <Trigger Property="IsEnabled" Value="False"> + <Setter Property="Foreground" Value="Gray"></Setter> + </Trigger> + </Style.Triggers> + </Style> + </fa:ImageAwesome.Style> + </fa:ImageAwesome> + </Button> + </StackPanel> + </ToolBar> + </Border> + + <Border Grid.Row="1" CornerRadius="5" BorderThickness="0" BorderBrush="#404040"> + <avalonEdit:TextEditor Padding="5" TextChanged="textEditor_TextChanged" Background="#151515" Foreground="Gainsboro" Margin="5 5 0 0" ScrollViewer.HorizontalScrollBarVisibility="Auto" + Name="textEditor" + FontFamily="Consolas" + FontSize="10pt" + SyntaxHighlighting="C#" + ShowLineNumbers="True"> + <avalonEdit:TextEditor.ContextMenu> + <ContextMenu> + <ContextMenu.Resources> + <Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}"> + <Setter Property="Foreground" Value="Gainsboro"></Setter> + </Style> + </ContextMenu.Resources> + <MenuItem Header="Cut" MinWidth="150" Command="Cut"> + <MenuItem.Icon> + <fa:ImageAwesome Icon="Cut" Width="12" Foreground="Gainsboro" Margin="2" /> + </MenuItem.Icon> + </MenuItem> + <Separator/> + <MenuItem Header="Copy" Command="Copy"> + <MenuItem.Icon> + <fa:ImageAwesome Icon="Copy" Width="12" Foreground="Gainsboro" Margin="2" /> + </MenuItem.Icon> + </MenuItem> + <MenuItem Header="Paste" Command="Paste"> + <MenuItem.Icon> + <fa:ImageAwesome Icon="Paste" Width="12" Foreground="Gainsboro" Margin="2" /> + </MenuItem.Icon> + </MenuItem> + </ContextMenu> + </avalonEdit:TextEditor.ContextMenu> + </avalonEdit:TextEditor> + </Border> + </Grid> + </Grid> +</UserControl> diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/ScriptEditorControl.xaml.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/ScriptEditorControl.xaml.cs new file mode 100644 index 000000000..3fe87c7e2 --- /dev/null +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/ScriptEditorControl.xaml.cs @@ -0,0 +1,488 @@ +using ICSharpCode.AvalonEdit.CodeCompletion; +using ICSharpCode.AvalonEdit.Document; +using ICSharpCode.AvalonEdit.Editing; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Windows.Threading; +using System.Xml; +using Tango.Core.Commands; +using Tango.Scripting; +using Tango.SharedUI; +using Tango.SharedUI.Helpers; + +namespace Tango.SharedUI.Controls +{ + /// <summary> + /// Represents a C# script editor control. + /// </summary> + /// <seealso cref="System.Windows.Controls.UserControl" /> + /// <seealso cref="System.Windows.Markup.IComponentConnector" /> + public partial class ScriptEditorControl : UserControl + { + #region Completion + + /// <summary> + /// Represents an auto complete item. + /// </summary> + /// <seealso cref="ICSharpCode.AvalonEdit.CodeCompletion.ICompletionData" /> + internal class CompletionData : ICompletionData + { + private String _description; + + /// <summary> + /// Gets or sets the icon source. + /// </summary> + public BitmapSource Source { get; set; } + + /// <summary> + /// Initializes a new instance of the <see cref="CompletionData"/> class. + /// </summary> + /// <param name="text">The text.</param> + /// <param name="description">The description.</param> + public CompletionData(string text, String description) + { + this.Text = text; + _description = description; + } + + /// <summary> + /// Gets the image. + /// </summary> + public System.Windows.Media.ImageSource Image + { + get { return Source; } + } + + /// <summary> + /// Gets the text. This property is used to filter the list of visible elements. + /// </summary> + public string Text { get; private set; } + + // Use this property if you want to show a fancy UIElement in the drop down list. + public object Content + { + get { return this.Text; } + } + + /// <summary> + /// Gets the description. + /// </summary> + public object Description + { + get { return _description; } + } + + /// <summary> + /// Gets the priority. This property is used in the selection logic. You can use it to prefer selecting those items + /// which the user is accessing most frequently. + /// </summary> + public double Priority { get { return 0; } } + + /// <summary> + /// Perform the completion. + /// </summary> + /// <param name="textArea">The text area on which completion is performed.</param> + /// <param name="completionSegment">The text segment that was used by the completion window if + /// the user types (segment between CompletionWindow.StartOffset and CompletionWindow.EndOffset).</param> + /// <param name="insertionRequestEventArgs">The EventArgs used for the insertion request. + /// These can be TextCompositionEventArgs, KeyEventArgs, MouseEventArgs, depending on how + /// the insertion was triggered.</param> + public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) + { + textArea.Document.Replace(completionSegment, this.Text); + } + + /// <summary> + /// Returns a <see cref="System.String" /> that represents this instance. + /// </summary> + /// <returns> + /// A <see cref="System.String" /> that represents this instance. + /// </returns> + public override string ToString() + { + return Text; + } + } + + #endregion + + private CompletionWindow completionWindow; //Holds the auto-complete window instance. + + #region Constructors + + /// <summary> + /// Initializes a new instance of the <see cref="ScriptEditorControl"/> class. + /// </summary> + public ScriptEditorControl() + { + InitializeComponent(); + + textEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.CSharp.CSharpIndentationStrategy(); + textEditor.TextArea.TextEntering += textEditor_TextArea_TextEntering; + textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered; + + this.Loaded += ScriptEditorControl_Loaded; + } + + #endregion + + #region Event Handlers + + /// <summary> + /// Handles the TextEntered event of the textEditor_TextArea control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param> + private void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e) + { + if (e.Text == ".") + { + String keyword = textEditor.TextArea.GetJustCurrentWord(); + + if (keyword != null) + { + completionWindow = new CompletionWindow(textEditor.TextArea); + completionWindow.WindowStyle = WindowStyle.None; + completionWindow.AllowsTransparency = true; + completionWindow.ResizeMode = ResizeMode.NoResize; + + IList<ICompletionData> data = completionWindow.CompletionList.CompletionData; + + bool ok = false; + + List<KeyValuePair<String, Type>> types = new List<KeyValuePair<String, Type>>(); + + types.AddRange(IntellisenseTypes); + + + if (IntellisenseTypes != null) + { + ScriptParser parser = new ScriptParser(); + + try + { + var variables = parser.ParseScript(textEditor.Text); + + foreach (var v in variables) + { + var hT = IntellisenseTypes.SingleOrDefault(x => x.Key == v.Type); + + if (hT.Value != null) + { + types.Add(new KeyValuePair<string, Type>(v.Name, hT.Value)); + } + } + + } + catch { } + } + + KeyValuePair<String, Type> type = types.LastOrDefault(x => keyword == x.Key); + + if (type.Key != null) + { + ok = true; + FillType(type.Value, data); + } + + if (ok) + { + completionWindow.Show(); + completionWindow.Closed += delegate + { + completionWindow = null; + }; + } + } + } + } + + /// <summary> + /// Handles the TextEntering event of the textEditor_TextArea control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param> + private void textEditor_TextArea_TextEntering(object sender, TextCompositionEventArgs e) + { + if (e.Text.Length > 0 && completionWindow != null) + { + if (!char.IsLetterOrDigit(e.Text[0])) + { + // Whenever a non-letter is typed while the completion window is open, + // insert the currently selected element. + completionWindow.CompletionList.RequestInsertion(e); + } + } + } + + /// <summary> + /// Handles the TextChanged event of the textEditor control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> + private void textEditor_TextChanged(object sender, EventArgs e) + { + Text = textEditor.Text; + } + + private void ScriptEditorControl_Loaded(object sender, RoutedEventArgs e) + { + if (HighlightTypes != null) + { + Stream xshd_stream = typeof(ScriptEditorControl).Assembly.GetManifestResourceStream("Tango.SharedUI.CSharp-Mode.xshd"); + + String text = String.Empty; + + using (StreamReader reader = new StreamReader(xshd_stream)) + { + text = reader.ReadToEnd(); + } + + String code = String.Empty; + + + + foreach (var name in HighlightTypes.Select(x => x.Key)) + { + code += String.Format("<Word>{0}</Word>", name) + Environment.NewLine; + } + + text = text.Replace("@CUSTOM_TYPES@", code); + + using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text))) + { + XmlTextReader xshd_reader = new XmlTextReader(ms); + textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance); + xshd_reader.Close(); + } + } + } + + #endregion + + #region Private Methods + + /// <summary> + /// Fills the type. + /// </summary> + /// <param name="type">The type.</param> + /// <param name="data">The data.</param> + private void FillType(Type type, IList<ICompletionData> data) + { + List<CompletionData> items = new List<CompletionData>(); + + foreach (var method in type.GetMethods().Where(x => x.IsPublic && !x.IsSpecialName)) + { + String desc = method.ReturnType.Name + " " + method.Name + "(" + String.Join(", ", method.GetParameters().Select(x => x.ParameterType.Name + " " + x.Name).ToArray()) + ")"; + items.Add(new CompletionData(method.Name, desc) { Source = ResourceHelper.GetImageFromResources("Images/pubmethod.gif") }); + } + foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) + { + String desc = property.PropertyType.Name + " " + property.Name; + items.Add(new CompletionData(property.Name, desc) { Source = ResourceHelper.GetImageFromResources("Images/pubproperty.gif") }); + } + foreach (var ev in type.GetEvents(BindingFlags.Instance | BindingFlags.Public)) + { + try + { + String desc = ev.Name + " " + "(" + String.Join(", ", ev.EventHandlerType.GetMethod("Invoke").GetParameters().Select(x => x.ParameterType.Name + " " + x.Name).ToArray()) + ")"; + items.Add(new CompletionData(ev.Name, desc) { Source = ResourceHelper.GetImageFromResources("Images/pubevent.gif") }); + } + catch { } + } + + foreach (var item in items.OrderBy(x => x.Text)) + { + data.Add(item); + } + } + + /// <summary> + /// Fills the assembly. + /// </summary> + /// <param name="asm">The asm.</param> + /// <param name="data">The data.</param> + private void FillAssembly(Assembly asm, IList<ICompletionData> data) + { + var q = from t in asm.GetTypes() + where t.IsClass + select t; + + foreach (var type in q) + { + data.Add(new CompletionData(type.Name, "Class") { Source = ResourceHelper.GetImageFromResources("Images/pubclass.gif") }); + } + } + + #endregion + + #region Properties + + /// <summary> + /// Gets or sets the text. + /// </summary> + public String Text + { + get { return (String)GetValue(TextProperty); } + set { SetValue(TextProperty, value); } + } + public static readonly DependencyProperty TextProperty = + DependencyProperty.Register("Text", typeof(String), typeof(ScriptEditorControl), new PropertyMetadata(null, (d, e) => (d as ScriptEditorControl).OnTextChanged())); + + /// <summary> + /// Gets or sets the highlight types. + /// </summary> + public ObservableCollection<KeyValuePair<string, Type>> HighlightTypes + { + get { return (ObservableCollection<KeyValuePair<string, Type>>)GetValue(HighlightTypesProperty); } + set { SetValue(HighlightTypesProperty, value); } + } + public static readonly DependencyProperty HighlightTypesProperty = + DependencyProperty.Register("HighlightTypes", typeof(ObservableCollection<KeyValuePair<string, Type>>), typeof(ScriptEditorControl), new PropertyMetadata(null)); + + /// <summary> + /// Gets or sets the intellisense types. + /// </summary> + public ObservableCollection<KeyValuePair<String,Type>> IntellisenseTypes + { + get { return (ObservableCollection<KeyValuePair<String,Type>>)GetValue(IntellisenseTypesProperty); } + set { SetValue(IntellisenseTypesProperty, value); } + } + public static readonly DependencyProperty IntellisenseTypesProperty = + DependencyProperty.Register("IntellisenseTypes", typeof(ObservableCollection<KeyValuePair<String,Type>>), typeof(ScriptEditorControl), new PropertyMetadata(null)); + + + #endregion + + #region Virtual Methods + + /// <summary> + /// Called when the text has changed. + /// </summary> + protected virtual void OnTextChanged() + { + if (textEditor.Text != Text) + { + textEditor.Text = Text; + } + } + + /// <summary> + /// Called when the insert script command has changed. + /// </summary> + protected virtual void OnInsertScriptCommandChanged() + { + if (InsertSnippetCommand != null) + { + InsertSnippetCommand.Executed += (x, snippet) => + { + textEditor.Document.Insert(textEditor.TextArea.Caret.Offset, snippet); + }; + } + } + + #endregion + + #region Commands + + /// <summary> + /// Gets or sets the run command. + /// </summary> + public RelayCommand RunCommand + { + get { return (RelayCommand)GetValue(RunCommandProperty); } + set { SetValue(RunCommandProperty, value); } + } + public static readonly DependencyProperty RunCommandProperty = + DependencyProperty.Register("RunCommand", typeof(RelayCommand), typeof(ScriptEditorControl), new PropertyMetadata(null)); + + /// <summary> + /// Gets or sets the stop command. + /// </summary> + public RelayCommand StopCommand + { + get { return (RelayCommand)GetValue(StopCommandProperty); } + set { SetValue(StopCommandProperty, value); } + } + public static readonly DependencyProperty StopCommandProperty = + DependencyProperty.Register("StopCommand", typeof(RelayCommand), typeof(ScriptEditorControl), new PropertyMetadata(null)); + + /// <summary> + /// Gets or sets the save command. + /// </summary> + public RelayCommand SaveCommand + { + get { return (RelayCommand)GetValue(SaveCommandProperty); } + set { SetValue(SaveCommandProperty, value); } + } + public static readonly DependencyProperty SaveCommandProperty = + DependencyProperty.Register("SaveCommand", typeof(RelayCommand), typeof(ScriptEditorControl), new PropertyMetadata(null)); + + /// <summary> + /// Gets or sets the insert snippet command. + /// </summary> + public RelayCommand<String> InsertSnippetCommand + { + get { return (RelayCommand<String>)GetValue(InsertSnippetCommandProperty); } + set { SetValue(InsertSnippetCommandProperty, value); } + } + public static readonly DependencyProperty InsertSnippetCommandProperty = + DependencyProperty.Register("InsertSnippetCommand", typeof(RelayCommand<String>), typeof(ScriptEditorControl), new PropertyMetadata(null, (d, e) => (d as ScriptEditorControl).OnInsertScriptCommandChanged())); + + #endregion + } + + internal static class DocumentUtils + { + private static Regex _wordRegex = new Regex(@"[^\W\d][\w]*(?<=\w)", RegexOptions.Compiled); + + public static string GetJustCurrentWord(this TextArea textArea) + { + try + { + DocumentLine line = textArea.Document.GetLineByNumber(textArea.Caret.Line); + if (line.Length == 0) + return null; + + int lineCaretPosition = textArea.Caret.Offset - line.Offset; + String l = textArea.Document.GetText(line); + + String trimmed = l.Remove(lineCaretPosition, l.Length - lineCaretPosition); + + return SplitToWords(trimmed).LastOrDefault(x => !String.IsNullOrWhiteSpace(x)); + } + catch + { + return null; + } + } + + public static List<String> SplitToWords(String text) + { + text = text.Replace(".", " "); + text = text.Replace("(", " "); + text = text.Replace(")", " "); + text = text.Replace(",", " "); + var punctuation = text.Where(Char.IsPunctuation).Distinct().ToArray(); + var words = text.Split().Select(x => x.Trim(punctuation)); + return words.ToList(); + } + } +} diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs deleted file mode 100644 index 1f2856a32..000000000 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs +++ /dev/null @@ -1,269 +0,0 @@ -using System; -using System.Collections; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Diagnostics; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Controls.Primitives; -using System.Windows.Data; -using System.Windows.Input; -using System.Linq; -using System.Windows.Media; -using System.Windows.Threading; -using Tango.Core.ExtensionMethods; - -namespace Tango.SharedUI.Controls -{ - public class SearchComboBox : ComboBox - { - private TextBox _textBox; - private ListBox _listBox; - private ICollectionView _view; - - public bool IsOpened - { - get { return (bool)GetValue(IsOpenedProperty); } - set { SetValue(IsOpenedProperty, value); } - } - public static readonly DependencyProperty IsOpenedProperty = - DependencyProperty.Register("IsOpened", typeof(bool), typeof(SearchComboBox), new PropertyMetadata(false, (d, e) => (d as SearchComboBox).OnIsOpenedChanged())); - - public String SearchProperty - { - get { return (String)GetValue(SearchPropertyProperty); } - set { SetValue(SearchPropertyProperty, value); } - } - public static readonly DependencyProperty SearchPropertyProperty = - DependencyProperty.Register("SearchProperty", typeof(String), typeof(SearchComboBox), new PropertyMetadata(null)); - - public String SearchFilter - { - get { return (String)GetValue(SearchFilterProperty); } - set { SetValue(SearchFilterProperty, value); } - } - public static readonly DependencyProperty SearchFilterProperty = - DependencyProperty.Register("SearchFilter", typeof(String), typeof(SearchComboBox), new PropertyMetadata(null, (d, e) => (d as SearchComboBox).OnFilterChanged())); - - public IEnumerable ListItemsSource - { - get { return (IEnumerable)GetValue(ListItemsSourceProperty); } - set { SetValue(ListItemsSourceProperty, value); } - } - public static readonly DependencyProperty ListItemsSourceProperty = - DependencyProperty.Register("ListItemsSource", typeof(IEnumerable), typeof(SearchComboBox), new PropertyMetadata(null)); - - - public override void OnApplyTemplate() - { - base.OnApplyTemplate(); - _textBox = GetTemplateChild("txt") as TextBox; - _listBox = GetTemplateChild("list") as ListBox; - _textBox.PreviewKeyDown += _textBox_KeyDown; - _listBox.PreviewKeyDown += _listBox_PreviewKeyDown; - - _listBox.PreviewMouseLeftButtonUp += _listBox_PreviewMouseLeftButtonUp; - - KeyboardNavigation.SetDirectionalNavigation(this, KeyboardNavigationMode.Once); - } - - private void _listBox_PreviewKeyDown(object sender, KeyEventArgs e) - { - if (e.Key == Key.Return) - { - IsOpened = false; - SelectedItem = _listBox.SelectedItem; - - if (SelectedValuePath.IsNotNullOrEmpty() && SelectedItem != null) - { - SelectedValue = SelectedItem.GetPropertyValueByPath(SelectedValuePath); - } - } - else if (e.Key == Key.Up && _listBox.SelectedIndex == 0) - { - _textBox.Focus(); - Keyboard.Focus(_textBox); - } - } - - private void _textBox_KeyDown(object sender, KeyEventArgs e) - { - if (e.Key == Key.Down) - { - e.Handled = true; - FocusList(); - } - } - - private void FocusList() - { - if (!String.IsNullOrWhiteSpace(SearchFilter)) - { - _listBox.SelectedIndex = 0; - } - - Keyboard.Focus(_listBox); - - if (_listBox.SelectedIndex != -1) - { - var container = _listBox.ItemContainerGenerator.ContainerFromIndex(_listBox.SelectedIndex) as UIElement; - if (container != null) - { - container.Focus(); - Keyboard.Focus(container); - } - } - - _listBox.ScrollIntoView(_listBox.SelectedItem); - } - - private void _listBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) - { - if (_listBox.SelectedItem != null) - { - if (SelectedItem != _listBox.SelectedItem) - { - IsOpened = false; - SelectedItem = _listBox.SelectedItem; - - if (SelectedValuePath.IsNotNullOrEmpty() && SelectedItem != null) - { - SelectedValue = SelectedItem.GetPropertyValueByPath(SelectedValuePath); - } - } - } - } - - private async void OnIsOpenedChanged() - { - if (IsOpened) - { - SearchFilter = String.Empty; - _listBox.SelectedItem = SelectedItem; - _listBox.ScrollIntoView(_listBox.SelectedItem); - await Task.Delay(100); - _textBox.Focus(); - Keyboard.Focus(_textBox); - } - } - - protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) - { - base.OnItemsSourceChanged(oldValue, newValue); - - if (ItemsSource != null) - { - ListItemsSource = ItemsSource.Cast<Object>().ToList(); - - _view = CollectionViewSource.GetDefaultView(ListItemsSource); - _view.Filter = (x) => - { - if (String.IsNullOrWhiteSpace(SearchFilter) || SearchFilter == null) return true; - - if (x != null) - { - if (!String.IsNullOrWhiteSpace(SearchProperty)) - { - var prop = x.GetType().GetProperty(SearchProperty, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - if (prop != null) - { - String propValue = prop.GetValue(x).ToString(); - return propValue.ToLower().Contains(SearchFilter.ToLower()); - } - } - - return x.ToStringSafe().ToLower().Contains(SearchFilter.ToLower()); - } - - return false; - }; - } - } - - private void OnFilterChanged() - { - _view?.Refresh(); - - if (_listBox != null && _listBox.Items.Count > 0) - { - _listBox.ScrollIntoView(_listBox.Items[0]); - } - } - - protected override void OnPreviewKeyDown(KeyEventArgs e) - { - //base.OnPreviewKeyDown(e); - e.Handled = false; - } - - protected override void OnPreviewKeyUp(KeyEventArgs e) - { - //base.OnPreviewKeyUp(e); - e.Handled = false; - } - - protected override void OnKeyDown(KeyEventArgs e) - { - //base.OnKeyDown(e); - e.Handled = false; - } - - protected override void OnKeyUp(KeyEventArgs e) - { - //base.OnKeyUp(e); - e.Handled = false; - } - - protected override void OnPreviewMouseWheel(MouseWheelEventArgs e) - { - e.Handled = true; - - if (!_listBox.IsFocused) - { - FocusList(); - } - - if (e.Delta > 0) - { - _listBox.RaiseEvent( - new KeyEventArgs( - System.Windows.Input.Keyboard.PrimaryDevice, - PresentationSource.FromVisual(_listBox), 0, Key.Down) - { RoutedEvent = System.Windows.Input.Keyboard.KeyDownEvent }); - - _listBox.RaiseEvent( - new KeyEventArgs( - System.Windows.Input.Keyboard.PrimaryDevice, - PresentationSource.FromVisual(_listBox), 0, Key.Down) - { RoutedEvent = System.Windows.Input.Keyboard.KeyUpEvent }); - } - else - { - _listBox.RaiseEvent( - new KeyEventArgs( - System.Windows.Input.Keyboard.PrimaryDevice, - PresentationSource.FromVisual(_listBox), 0, Key.Up) - { RoutedEvent = System.Windows.Input.Keyboard.KeyDownEvent }); - - _listBox.RaiseEvent( - new KeyEventArgs( - System.Windows.Input.Keyboard.PrimaryDevice, - PresentationSource.FromVisual(_listBox), 0, Key.Up) - { RoutedEvent = System.Windows.Input.Keyboard.KeyUpEvent }); - } - //base.OnPreviewMouseWheel(e); - } - - protected override void OnMouseWheel(MouseWheelEventArgs e) - { - e.Handled = true; - //base.OnMouseWheel(e); - } - - static SearchComboBox() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchComboBox), new FrameworkPropertyMetadata(typeof(SearchComboBox))); - } - } -} |
