Skip to content

Commit

Permalink
Fixed bug when opening a file by double clicking or through the open …
Browse files Browse the repository at this point in the history
…file dialog displayed the file with unsaved changes.
  • Loading branch information
Max DR committed Jul 21, 2020
1 parent e562181 commit 72df716
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
5 changes: 3 additions & 2 deletions ModernNotepad/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var mainWindow = MainViewModel.WindowService.CreateMainView(MainViewModel, typeof(MainViewModel));
MainViewModel.TextEditor.TextArea = mainWindow.TextArea;
MainViewModel.TextEditor.TextArea = mainWindow.TextArea;

if (!Directory.Exists(MainViewModel.SettingsManager.SettingsDirectoryPath))
{
Expand All @@ -33,10 +33,11 @@ protected override void OnStartup(StartupEventArgs e)

if (e.Args.Length > 0)
{
MainViewModel.TextEditor.SavedAsFile = true;
MainViewModel.Title = Path.GetFileName(e.Args[0]);
MainViewModel.FilePath = Path.GetFullPath(e.Args[0]);
MainViewModel.TextEditor.TextArea.Text = File.ReadAllText(e.Args[0], Encoding.Default);
MainViewModel.TextEditor.SavedAsFile = true;
//MainViewModel.TextEditor.SavedAsFile = true;
MainViewModel.SaveFileService.FileName = e.Args[0];
}
}
Expand Down
13 changes: 7 additions & 6 deletions ModernNotepad/Behaviors/TextChangedBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ protected override void OnDetaching()

private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var viewModel = Application.Current.MainWindow.DataContext as MainViewModel;
var textArea = viewModel.TextEditor.TextArea;
var viewModel = Application.Current.MainWindow.DataContext as MainViewModel;
var editor = viewModel.TextEditor;
var textArea = editor.TextArea;

if (e.Changes.FirstOrDefault().AddedLength > 0 ||
e.Changes.FirstOrDefault().AddedLength != textArea.Text.Length ||
e.Changes.FirstOrDefault().RemovedLength > 0) // BUG HERE
{
if ((!editor.SavedAsFile && e.Changes.FirstOrDefault().AddedLength > 0) ||
e.Changes.FirstOrDefault().AddedLength != textArea.Text.Length ||
e.Changes.FirstOrDefault().RemovedLength > 0)
{
viewModel.Title = $"*{viewModel.Title.Replace("*", "")}";
viewModel.TextEditor.UnsavedChanges = true;
}
Expand Down
8 changes: 4 additions & 4 deletions ModernNotepad/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@
</i:Interaction.Behaviors>
</Popup>

<cc:TextArea x:Name="textArea" Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
<cc:TextArea x:Name="textArea" x:FieldModifier="private" Grid.Row="1"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
IsInactiveSelectionHighlightEnabled="True" Padding="10,5,20,6" FontFamily="Consolas" FontSize="14"
Style="{StaticResource TextAreaStyle}"
SpellCheck.IsEnabled="{Binding SettingsViewModel.IsSpellCheckingEnabled}"
Style="{StaticResource TextAreaStyle}" SpellCheck.IsEnabled="{Binding SettingsViewModel.IsSpellCheckingEnabled}"
TextWrapping="{Binding SettingsViewModel.IsWordWrapEnabled, Converter={StaticResource BooleanToTextWrapping}}">
<i:Interaction.Behaviors>
<behaviors:PasteContentBehavior/>
Expand Down Expand Up @@ -192,7 +192,7 @@
<i:Interaction.Behaviors>
<behaviors:ZoomBehavior ZoomScale="{Binding Scale, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
<FlowDocumentPageViewer Name="pageViewer" Foreground="Black" ContextMenu="{x:Null}">
<FlowDocumentPageViewer Name="pageViewer" x:FieldModifier="private" Foreground="Black" ContextMenu="{x:Null}">
<FlowDocument ColumnGap="0" ColumnWidth="{x:Static util:PrintDialogWrapper.PrintableAreaWidth}"
PageHeight="{x:Static util:PrintDialogWrapper.PrintableAreaHeight}"
PageWidth="{x:Static util:PrintDialogWrapper.PrintableAreaWidth}">
Expand Down

0 comments on commit 72df716

Please sign in to comment.