Skip to content

Commit

Permalink
Fix crash when opening image
Browse files Browse the repository at this point in the history
  • Loading branch information
NotroDev committed Mar 26, 2024
1 parent c4db288 commit b1b988a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Installer/Package.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"
xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui">
<Package Name="SkEditor" Manufacturer="Notro" Version="2.3.0" UpgradeCode="14564974-da58-4917-8a0d-590043f589c2">
<Package Name="SkEditor" Manufacturer="Notro" Version="2.3.1" UpgradeCode="14564974-da58-4917-8a0d-590043f589c2">
<MajorUpgrade DowngradeErrorMessage="!(loc.DowngradeError)" />
<MediaTemplate EmbedCab="yes" />

Expand Down
6 changes: 3 additions & 3 deletions SkEditor/SkEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>SkEditor.ico</ApplicationIcon>
<PublishSingleFile>true</PublishSingleFile>
<AssemblyVersion>2.3.0</AssemblyVersion>
<FileVersion>2.3.0</FileVersion>
<Version>2.3.0</Version>
<AssemblyVersion>2.3.1</AssemblyVersion>
<FileVersion>2.3.1</FileVersion>
<Version>2.3.1</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
73 changes: 43 additions & 30 deletions SkEditor/Utilities/Files/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ public static async void NewFile()

public async static void OpenFile()
{
bool untitledFileOpen = ApiVault.Get().GetTabView().TabItems.Count() == 1 &&
ApiVault.Get().GetTextEditor() != null &&
ApiVault.Get().GetTextEditor().Text.Length == 0 &&
ApiVault.Get().GetTabView().SelectedItem is TabViewItem item &&
item.Header.ToString().Contains(Translation.Get("NewFileNameFormat").Replace("{0}", "")) &&
!item.Header.ToString().EndsWith('*');

var topLevel = TopLevel.GetTopLevel(ApiVault.Get().GetMainWindow());

var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
Expand All @@ -93,28 +86,22 @@ public async static void OpenFile()
});

files.ToList().ForEach(file => OpenFile(file.Path.AbsolutePath));
if (untitledFileOpen) await CloseFile((ApiVault.Get().GetTabView().TabItems as IList)[0] as TabViewItem);
}

public static async void OpenFile(string path)
{
path = Path.GetFullPath(path);
if ((ApiVault.Get().GetTabView().TabItems as IList)
.Cast<TabViewItem>()
.Where(tab => tab.Tag != null)
.Any(tab => tab.Tag.ToString() == path))
{
ApiVault.Get().GetTabView().SelectedItem = (ApiVault.Get().GetTabView().TabItems as IList)
.Cast<TabViewItem>()
.First(tab => tab.Tag.ToString() == path);
return;
}
bool untitledFileOpen = IsOnlyEmptyFileOpen();

string fileName = Uri.UnescapeDataString(Path.GetFileName(path));
path = Uri.UnescapeDataString(Path.GetFullPath(path));
if (CheckAlreadyOpen(path)) return;

string fileName = Path.GetFileName(path);
TabViewItem tabItem = await FileBuilder.Build(fileName, path);
if (tabItem == null) return;

(ApiVault.Get().GetTabView().TabItems as IList)?.Add(tabItem);
if (untitledFileOpen) await CloseFile((ApiVault.Get().GetTabView().TabItems as IList)[0] as TabViewItem);

OpenedFiles.Add(new OpenedFile()
{
Editor = tabItem.Content as TextEditor,
Expand All @@ -125,20 +112,46 @@ public static async void OpenFile(string path)
: null
});

if (ApiVault.Get().GetAppConfig().CheckForChanges)
AddChangeChecker(path, tabItem);

await SyntaxLoader.RefreshSyntaxAsync(Path.GetExtension(path));
}

private static void AddChangeChecker(string path, TabViewItem tabItem)
{
if (!ApiVault.Get().GetAppConfig().CheckForChanges || tabItem.Content is not TextEditor) return;

FileSystemWatcher watcher = new(Path.GetDirectoryName(path), Path.GetFileName(path));
watcher.Changed += (sender, e) => ChangeChecker.HasChangedDictionary[path] = true;
(tabItem.Content as TextEditor).Unloaded += (sender, e) =>
{
FileSystemWatcher watcher = new(Path.GetDirectoryName(path), Path.GetFileName(path));
watcher.Changed += (sender, e) => ChangeChecker.HasChangedDictionary[path] = true;
(tabItem.Content as TextEditor).Unloaded += (sender, e) =>
if (OpenedFiles.FirstOrDefault(openedFile => openedFile.TabViewItem == tabItem) is not OpenedFile openedFile)
{
if (OpenedFiles.FirstOrDefault(openedFile => openedFile.TabViewItem == tabItem) is not OpenedFile openedFile)
{
watcher.Dispose();
}
};
watcher.Dispose();
}
};
}

private static bool CheckAlreadyOpen(string path)
{
if ((ApiVault.Get().GetTabView().TabItems as IList).Cast<TabViewItem>().Where(tab => tab.Tag != null).Any(tab => tab.Tag.ToString() == path))
{
ApiVault.Get().GetTabView().SelectedItem = (ApiVault.Get().GetTabView().TabItems as IList)
.Cast<TabViewItem>()
.First(tab => tab.Tag.ToString() == path);
return true;
}
return false;
}

await SyntaxLoader.RefreshSyntaxAsync(Path.GetExtension(path));
private static bool IsOnlyEmptyFileOpen()
{
return ApiVault.Get().GetTabView().TabItems.Count() == 1 &&
ApiVault.Get().GetTextEditor() != null &&
ApiVault.Get().GetTextEditor().Text.Length == 0 &&
ApiVault.Get().GetTabView().SelectedItem is TabViewItem item &&
item.Header.ToString().Contains(Translation.Get("NewFileNameFormat").Replace("{0}", "")) &&
!item.Header.ToString().EndsWith('*');
}

public static async Task<(bool, Exception)> SaveFile()
Expand Down

0 comments on commit b1b988a

Please sign in to comment.