public override void Deserialize(JObject data)
            {
                base.Deserialize(data);

                if (data != null && PatternTexture != null)
                {
                    PatternEnabled = data.GetBoolValueOnly("patternEnabled") != false;
                    var current = data.GetStringValueOnly("patternSelected");
                    CurrentPattern = Patterns.FirstOrDefault(x => string.Equals(x.DisplayName, current, StringComparison.OrdinalIgnoreCase)) ?? CurrentPattern;
                    if (CurrentPattern != null)
                    {
                        DeserializeColors(CurrentPattern.Colors, data, "patternColors");
                    }
                }
            }
Exemple #2
0
        public PatternsViewModel(PatternUserControl patternUserControl)
        {
            patternsListBox      = patternUserControl.Find <ListBox>("PatternsListBox");
            patternTextBox       = patternUserControl.Find <TextBox>("PatternData");
            patternErrorsListBox = patternUserControl.Find <ListBox>("PatternErrors");
            logger = patternUserControl.Find <TextBox>("Logger");

            Dispatcher.UIThread.InvokeAsync(async() =>
            {
                bool error = false;
                if (!string.IsNullOrEmpty(Settings.PatternsFileName))
                {
                    try
                    {
                        PatternsFileName = Settings.PatternsFileName;
                        SelectedPattern  = Patterns.FirstOrDefault(p => p.Key == Settings.SelectedPatternKey) ?? Patterns.FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowDialog(ex.Message);
                        error = true;
                    }
                }

                if (string.IsNullOrEmpty(Settings.PatternsFileName) || error)
                {
                    Settings.PatternsFileName = Settings.DefaultPatternsFileName;
                    SelectedPattern           = Patterns.FirstOrDefault();
                    SavePatterns();
                    Settings.Save();
                }
            });

            patternsListBox.DoubleTapped += (object sender, Avalonia.Interactivity.RoutedEventArgs e) =>
            {
                OpenPatternAction();
            };

            patternErrorsListBox.DoubleTapped += (object sender, Avalonia.Interactivity.RoutedEventArgs e) =>
            {
                GuiHelpers.ProcessErrorOnDoubleClick(patternErrorsListBox, patternTextBox);
            };

            patternLogger = new GuiLogger(PatternErrors)
            {
                LogPatternErrors = true
            };
            patternLogger.LogEvent += PatternLogger_LogEvent;
            dslProcessor.Logger     = patternLogger;

            patternTextBox.GetObservable(TextBox.CaretIndexProperty)
            .Subscribe(UpdatePatternCaretIndex);

            patternTextBox.GetObservable(TextBox.TextProperty)
            .Throttle(TimeSpan.FromMilliseconds(250))
            .Subscribe(str =>
            {
                if (SelectedPattern != null)
                {
                    Value = str;
                }
            });

            OpenPatterns = ReactiveCommand.Create(async() =>
            {
                SavePatterns();
                var openFileDialog = new OpenFileDialog
                {
                    Title = "Select patterns database",
                };
                var fileNames = await openFileDialog.ShowAsync(ServiceLocator.MainWindow);
                if (fileNames != null)
                {
                    try
                    {
                        PatternsFileName = fileNames.First();
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowDialog(ex.Message);
                        Settings.PatternsFileName = Settings.DefaultPatternsFileName;
                        Settings.Save();
                        SelectedPattern = Patterns.FirstOrDefault();
                        ServiceLocator.MainWindowViewModel.ActivateWindow();
                    }
                }
            });

            CreatePattern = ReactiveCommand.Create(() =>
            {
                SavePatterns();
                PatternViewModel newPatternViewMode = CreateNewPattern();
                Patterns.Add(newPatternViewMode);
                SelectedPattern = newPatternViewMode;
                SavePatterns();
            });

            RemovePattern = ReactiveCommand.Create(async() =>
            {
                if (SelectedPattern != null && await MessageBox.ShowDialog($"Do you want to remove {SelectedPattern}?", messageBoxType: MessageBoxType.YesNo))
                {
                    Patterns.Remove(SelectedPattern);
                    SelectedPattern = Patterns.LastOrDefault();
                    SavePatterns();
                }
            });

            OpenPattern = ReactiveCommand.Create(OpenPatternAction);

            SavePattern = ReactiveCommand.Create(() => SavePatterns());

            void OpenPatternAction()
            {
                if (patternsListBox.SelectedItem != null)
                {
                    SelectedPattern = (PatternViewModel)patternsListBox.SelectedItem;
                }
            }
        }
        public PatternsViewModel(PatternUserControl patternUserControl)
        {
            patternsListBox      = patternUserControl.Find <ListBox>("PatternsListBox");
            patternTextBox       = patternUserControl.Find <TextEditor>("PatternData");
            patternErrorsListBox = patternUserControl.Find <ListBox>("PatternErrors");
            logger = patternUserControl.Find <TextBox>("Logger");

            Dispatcher.UIThread.InvokeAsync(async() =>
            {
                bool error = false;
                if (!string.IsNullOrEmpty(Settings.PatternsFileName))
                {
                    try
                    {
                        PatternsFileName = Settings.PatternsFileName;
                        SelectedPattern  = Patterns.FirstOrDefault(p => p.Key == Settings.SelectedPatternKey) ?? Patterns.FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowDialog(ex.Message);
                        error = true;
                    }
                }

                if (string.IsNullOrEmpty(Settings.PatternsFileName) || error)
                {
                    Settings.PatternsFileName = Settings.DefaultPatternsFileName;
                    SelectedPattern           = Patterns.FirstOrDefault();
                    SavePatterns();
                    Settings.Save();
                }
            });

            patternsListBox.DoubleTapped += (sender, e) =>
            {
                OpenPatternAction();
            };

            patternErrorsListBox.DoubleTapped += (sender, e) =>
            {
                GuiUtils.ProcessErrorOnDoubleClick(patternErrorsListBox, patternTextBox);
            };

            patternLogger           = GuiLogger.CreatePatternLogger(PatternErrors);
            patternLogger.LogEvent += PatternLogger_LogEvent;
            dslProcessor.Logger     = patternLogger;

            Observable.Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(200), RxApp.MainThreadScheduler)
            .Subscribe(_ => UpdatePatternCaretIndex());

            var patternTextBoxObserable = Observable.FromEventPattern <EventHandler, EventArgs>(
                h => patternTextBox.TextChanged += h,
                h => patternTextBox.TextChanged -= h);

            patternTextBoxObserable
            .Throttle(TimeSpan.FromMilliseconds(250))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(str =>
            {
                if (SelectedPattern != null)
                {
                    Value = patternTextBox.Text;
                }
            });

            OpenPatterns = ReactiveCommand.Create(async() =>
            {
                SavePatterns();
                var openFileDialog = new OpenFileDialog
                {
                    Title = "Select patterns database",
                };
                var fileNames = await openFileDialog.ShowAsync(ServiceLocator.MainWindow);
                if (fileNames != null)
                {
                    try
                    {
                        PatternsFileName = fileNames.First();
                    }
                    catch (Exception ex)
                    {
                        await MessageBox.ShowDialog(ex.Message);
                        Settings.PatternsFileName = Settings.DefaultPatternsFileName;
                        Settings.Save();
                        SelectedPattern = Patterns.FirstOrDefault();
                        ServiceLocator.MainWindowViewModel.ActivateWindow();
                    }
                }
            });

            CreatePattern = ReactiveCommand.Create(() =>
            {
                SavePatterns();
                PatternViewModel newPatternViewMode = CreateNewPattern();
                Patterns.Add(newPatternViewMode);
                SelectedPattern = newPatternViewMode;
                SavePatterns();
            });

            RemovePattern = ReactiveCommand.Create(async() =>
            {
                if (SelectedPattern != null && await MessageBox.ShowDialog($"Do you want to remove {SelectedPattern}?", messageBoxType: MessageBoxType.YesNo))
                {
                    Patterns.Remove(SelectedPattern);
                    SelectedPattern = Patterns.LastOrDefault();
                    SavePatterns();
                }
            });

            OpenPattern = ReactiveCommand.Create(OpenPatternAction);

            SavePattern = ReactiveCommand.Create(() => SavePatterns());

            void OpenPatternAction()
            {
                if (patternsListBox.SelectedItem != null)
                {
                    SelectedPattern = (PatternViewModel)patternsListBox.SelectedItem;
                }
            }
        }