Made a storage provider interface and both classic and avalonia impl.
This commit is contained in:
36
Progrart.Core/Storage/ClassicStorageProvider.cs
Normal file
36
Progrart.Core/Storage/ClassicStorageProvider.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
namespace Progrart.Core.Storage
|
||||||
|
{
|
||||||
|
public class ClassicStorageProvider : IStorageProvider
|
||||||
|
{
|
||||||
|
public DirectoryInfo BaseDirectory;
|
||||||
|
|
||||||
|
public ClassicStorageProvider(DirectoryInfo baseDirectory)
|
||||||
|
{
|
||||||
|
BaseDirectory = baseDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Stream?> TryOpenRead(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return File.OpenRead(Path.Combine(BaseDirectory.FullName, path));
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Stream?> TryOpenWrite(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return File.OpenWrite(Path.Combine(BaseDirectory.FullName, path));
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Progrart.Core/Storage/IStorageProvider.cs
Normal file
12
Progrart.Core/Storage/IStorageProvider.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Progrart.Core.Storage
|
||||||
|
{
|
||||||
|
public interface IStorageProvider
|
||||||
|
{
|
||||||
|
Task<Stream?> TryOpenRead(string path);
|
||||||
|
Task<Stream?> TryOpenWrite(string path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -289,7 +289,7 @@
|
|||||||
<StyleInclude Source="avares://AvaloniaEdit/Themes/Fluent/AvaloniaEdit.xaml" />
|
<StyleInclude Source="avares://AvaloniaEdit/Themes/Fluent/AvaloniaEdit.xaml" />
|
||||||
<dialoghostavalonia:DialogHostStyles/>
|
<dialoghostavalonia:DialogHostStyles/>
|
||||||
<Style Selector="ContextMenu">
|
<Style Selector="ContextMenu">
|
||||||
<Setter Property="FontFamily" Value="{StaticResource SarasaMonoSlabCLFont}"/>
|
<!--<Setter Property="FontFamily" Value="{StaticResource SarasaMonoSlabCLFont}"/>-->
|
||||||
</Style>
|
</Style>
|
||||||
<Style Selector="MenuItem:selected">
|
<Style Selector="MenuItem:selected">
|
||||||
<Style Selector="^ /template/ Border#PART_LayoutRoot">
|
<Style Selector="^ /template/ Border#PART_LayoutRoot">
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ public partial class App : Application
|
|||||||
EditorProvider.Register("text", "Default Text Editor", typeof(EditorPage));
|
EditorProvider.Register("text", "Default Text Editor", typeof(EditorPage));
|
||||||
EditorProvider.Register("image", "Default Image Viewer", typeof(ImageViewPage));
|
EditorProvider.Register("image", "Default Image Viewer", typeof(ImageViewPage));
|
||||||
EditorProvider.Register("progrart", "Default Progrart Editor", typeof(ProgrartEditorPage));
|
EditorProvider.Register("progrart", "Default Progrart Editor", typeof(ProgrartEditorPage));
|
||||||
|
EditorProvider.Register("console", "Console", typeof(Console));
|
||||||
EditorProvider.BindFileType("cs", "text");
|
EditorProvider.BindFileType("cs", "text");
|
||||||
EditorProvider.BindFileType("c", "text");
|
EditorProvider.BindFileType("c", "text");
|
||||||
EditorProvider.BindFileType("cpp", "text");
|
EditorProvider.BindFileType("cpp", "text");
|
||||||
@@ -39,6 +40,7 @@ public partial class App : Application
|
|||||||
EditorProvider.BindFileType("png", "image");
|
EditorProvider.BindFileType("png", "image");
|
||||||
EditorProvider.BindFileType("bmp", "image");
|
EditorProvider.BindFileType("bmp", "image");
|
||||||
EditorProvider.BindFileType("jpg", "image");
|
EditorProvider.BindFileType("jpg", "image");
|
||||||
|
EditorProvider.BindFileType("wd", "console");
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
{
|
{
|
||||||
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
|
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
|
||||||
|
|||||||
42
Progrart/Core/Storage/AvaloniaStorageProvider.cs
Normal file
42
Progrart/Core/Storage/AvaloniaStorageProvider.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Progrart.Core.Storage
|
||||||
|
{
|
||||||
|
public class AvaloniaStorageProvider : IStorageProvider
|
||||||
|
{
|
||||||
|
IStorageFolder baseFolder;
|
||||||
|
|
||||||
|
public AvaloniaStorageProvider(IStorageFolder baseFolder)
|
||||||
|
{
|
||||||
|
this.baseFolder = baseFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Stream?> TryOpenRead(string path)
|
||||||
|
{
|
||||||
|
var file = await baseFolder.GetFileAsync(path);
|
||||||
|
if (file != null)
|
||||||
|
{
|
||||||
|
return await file.OpenReadAsync();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Stream?> TryOpenWrite(string path)
|
||||||
|
{
|
||||||
|
var file = await baseFolder.GetFileAsync(path);
|
||||||
|
if (file != null)
|
||||||
|
{
|
||||||
|
return await file.OpenWriteAsync();
|
||||||
|
}
|
||||||
|
file = await baseFolder.CreateFileAsync(path);
|
||||||
|
if (file is not null)
|
||||||
|
return await file.OpenWriteAsync();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,9 @@
|
|||||||
<ScrollViewer>
|
<ScrollViewer>
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
<StackPanel.Styles>
|
<StackPanel.Styles>
|
||||||
|
<Style Selector="TextBlock">
|
||||||
|
<Setter Property="TextWrapping" Value="Wrap"/>
|
||||||
|
</Style>
|
||||||
<Style Selector="Image">
|
<Style Selector="Image">
|
||||||
<Setter Property="Transitions">
|
<Setter Property="Transitions">
|
||||||
<Transitions>
|
<Transitions>
|
||||||
|
|||||||
24
Progrart/Pages/Console.axaml
Normal file
24
Progrart/Pages/Console.axaml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<UserControl xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
|
x:Class="Progrart.Pages.Console">
|
||||||
|
<Grid>
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition/>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
<ScrollViewer>
|
||||||
|
<TextBlock Name="Output"/>
|
||||||
|
</ScrollViewer>
|
||||||
|
<Grid Grid.Row="1">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition/>
|
||||||
|
<ColumnDefinition Width="Auto"/>
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
<TextBox Name="CommandBox"/>
|
||||||
|
<Button Name="ExecuteBtn" Grid.Column="1" Content="->"/>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</UserControl>
|
||||||
83
Progrart/Pages/Console.axaml.cs
Normal file
83
Progrart/Pages/Console.axaml.cs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Markup.Xaml;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
using Jint;
|
||||||
|
using Progrart.Controls.TabSystem;
|
||||||
|
using Progrart.Core;
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Progrart.Pages;
|
||||||
|
|
||||||
|
public partial class Console : UserControl, ITabPage, IEditorPage
|
||||||
|
{
|
||||||
|
Engine engine;
|
||||||
|
public Console()
|
||||||
|
{
|
||||||
|
engine = new Engine();
|
||||||
|
InitializeComponent();
|
||||||
|
engine.SetValue("log", new Action<object>((obj) =>
|
||||||
|
{
|
||||||
|
Output.Text += $"{obj}\n";
|
||||||
|
}));
|
||||||
|
engine.SetValue("getWDName", new Func<string>(() =>
|
||||||
|
{
|
||||||
|
return workdirectory?.Name ?? "<null>";
|
||||||
|
}));
|
||||||
|
engine.SetValue("setWD", new Action<string>((str) =>
|
||||||
|
{
|
||||||
|
if (workdirectory != null)
|
||||||
|
{
|
||||||
|
Task.Run(async () => workdirectory = await workdirectory.GetFolderAsync(str));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
engine.SetValue("mkdir", new Action<string>((str) =>
|
||||||
|
{
|
||||||
|
if (workdirectory != null)
|
||||||
|
{
|
||||||
|
Task.Run(async () => await workdirectory.CreateFolderAsync(str));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
ExecuteBtn.Click += (_, _) =>
|
||||||
|
{
|
||||||
|
engine.Execute(CommandBox.Text ?? "");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BindButton(TabButton button)
|
||||||
|
{
|
||||||
|
button.Title = "Console";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Execute(ExecuteArguments? args = null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsModified()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsSameFile(IStorageFile file)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
IStorageFolder? workdirectory;
|
||||||
|
public void LoadDocument(IStorageFile file)
|
||||||
|
{
|
||||||
|
Task.Run(async () =>
|
||||||
|
{
|
||||||
|
workdirectory = await file.GetParentAsync();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Save()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetHost(TabHost host)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user