private void RefreshFile() { lock (_fileLock) { if (File.Exists(_path)) { var fileHash = GetHashFromFile(_path); if (_currentFile == null || !fileHash.SequenceEqual(_currentFile)) { _currentFile = fileHash; ReloadFile?.Invoke(this, EventArgs.Empty); } } else if (_currentFile != null) { _currentFile = null; ReloadFile?.Invoke(this, EventArgs.Empty); } } }
public MainWindowViewModel(Window w) { window = w; window.WindowState = Settings.WindowState; if (Settings.Width > 0) { window.Width = Settings.Width; } if (Settings.Height > 0) { window.Height = Settings.Height; } if (Settings.Left != -1 && Settings.Top != -1) { window.Position = new Point(Settings.Left, Settings.Top); } patternsPanelColumn = window.Find <Grid>("MainGrid").ColumnDefinitions[0]; sourceCodeTextBox = window.Find <TextBox>("SourceCode"); sourceCodeErrorsListBox = window.Find <ListBox>("SourceCodeErrors"); matchingResultListBox = window.Find <ListBox>("MatchingResult"); logger = window.Find <TextBox>("Logger"); patternsPanelColumn.Width = GridLength.Parse(Settings.PatternsPanelWidth.ToString(), CultureInfo.InvariantCulture); sourceCodeErrorsListBox.DoubleTapped += (object sender, Avalonia.Interactivity.RoutedEventArgs e) => { GuiHelpers.ProcessErrorOnDoubleClick(sourceCodeErrorsListBox, sourceCodeTextBox); }; matchingResultListBox.DoubleTapped += MatchingResultListBox_DoubleTapped; sourceCodeLogger = new GuiLogger(SourceCodeErrors) { LogPatternErrors = false }; languageDetector.Logger = sourceCodeLogger; OpenSourceCodeFile.Subscribe(async _ => { var dialog = new OpenFileDialog(); string[] fileNames = await dialog.ShowAsync(window); if (fileNames != null) { string fileName = fileNames.Single(); OpenedFileName = fileName; fileOpened = true; sourceCodeTextBox.Text = File.ReadAllText(sourceCodeFileName); } }); SaveSourceCodeFile.Subscribe(_ => { if (!string.IsNullOrEmpty(sourceCodeFileName)) { File.WriteAllText(sourceCodeFileName, sourceCodeTextBox.Text); } }); ReloadFile.Subscribe(_ => { if (!string.IsNullOrEmpty(sourceCodeFileName)) { sourceCodeTextBox.Text = File.ReadAllText(sourceCodeFileName); } }); Reset.Subscribe(_ => { OpenedFileName = ""; sourceCodeTextBox.Text = ""; }); if (string.IsNullOrEmpty(Settings.SourceCodeFile) || !File.Exists(Settings.SourceCodeFile)) { fileOpened = false; sourceCodeFileName = ""; sourceCodeTextBox.Text = Settings.SourceCode; } else { fileOpened = true; sourceCodeFileName = Settings.SourceCodeFile; sourceCodeTextBox.Text = File.ReadAllText(Settings.SourceCodeFile); } CheckSourceCode(); this.RaisePropertyChanged(nameof(SelectedLanguageInfo)); this.RaisePropertyChanged(nameof(OpenedFileName)); sourceCodeTextBox.GetObservable(TextBox.CaretIndexProperty) .Subscribe(UpdateSourceCodeCaretIndex); sourceCodeTextBox.GetObservable(TextBox.SelectionStartProperty) .Subscribe(selectionStart => { if (sourceCodeTextBox.IsFocused) { sourceCodeSelectionStart = selectionStart; } }); sourceCodeTextBox.GetObservable(TextBox.SelectionEndProperty) .Subscribe(selectionEnd => { if (sourceCodeTextBox.IsFocused) { sourceCodeSelectionEnd = selectionEnd; } }); sourceCodeTextBox.GetObservable(TextBox.TextProperty) .Throttle(TimeSpan.FromMilliseconds(500)) .Subscribe(str => CheckSourceCode()); SetupWindowSubscriptions(); this.RaisePropertyChanged(nameof(IsErrorsExpanded)); this.RaisePropertyChanged(nameof(IsTokensExpanded)); this.RaisePropertyChanged(nameof(IsParseTreeExpanded)); this.RaisePropertyChanged(nameof(IsUstExpanded)); this.RaisePropertyChanged(nameof(IsMatchingsExpanded)); }