FileItem will no longer open more than 1 tab for a file.

Added Ctrl+S shortcut in BaseEditor.
This commit is contained in:
Creeper Lv
2025-12-28 23:23:33 +11:00
parent ac5053a5df
commit 25efa1127f
8 changed files with 136 additions and 39 deletions

View File

@@ -7,5 +7,13 @@
x:Class="Progrart.Controls.BaseEditor">
<avaloniaedit:TextEditor Name="CodeEditBox" Text="" SyntaxHighlighting="JavaScript"
ShowLineNumbers="True"
FontFamily="Sarasa Mono Slab CL,Cascadia Code,Consolas,Menlo,Monospace"/>
FontFamily="Sarasa Mono Slab CL,Cascadia Code,Consolas,Menlo,Monospace">
<avaloniaedit:TextEditor.ContextMenu>
<ContextMenu>
<MenuItem Name="Copy" Click="Copy_Click"/>
<MenuItem Name="Cut" Click="Cut_Click"/>
<MenuItem Name="Paste" Click="Paste_Click"/>
</ContextMenu>
</avaloniaedit:TextEditor.ContextMenu>
</avaloniaedit:TextEditor>
</UserControl>

View File

@@ -1,7 +1,11 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using System;
using System.Windows.Input;
using TextMateSharp.Grammars;
namespace Progrart.Controls;
@@ -15,6 +19,7 @@ public partial class BaseEditor : UserControl
get => CodeEditBox.Text;
set => CodeEditBox.Text = value;
}
public Action? onSaveCmd;
public BaseEditor()
{
InitializeComponent();
@@ -24,6 +29,31 @@ public partial class BaseEditor : UserControl
_textMateInstallation = _textEditor.InstallTextMate(_registryOptions);
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(_registryOptions.GetLanguageByExtension(".js").Id));
}
var saveBinding = new KeyBinding
{
Gesture = new KeyGesture(Key.S, KeyModifiers.Control),
Command = new saveCmd() { onSave = Save }
};
CodeEditBox.KeyBindings.Add(saveBinding);
}
void Save()
{
onSaveCmd?.Invoke();
}
class saveCmd : ICommand
{
public event EventHandler? CanExecuteChanged;
public Action? onSave;
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
{
onSave?.Invoke();
}
}
public void SetGrammerByExtension(string extension_name)
{
@@ -34,4 +64,19 @@ public partial class BaseEditor : UserControl
);
}
private void Copy_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
CodeEditBox.Copy();
}
private void Paste_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
CodeEditBox.Paste();
}
private void Cut_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
CodeEditBox.Cut();
}
}

View File

@@ -149,7 +149,12 @@ public partial class FileItem : UserControl
else
if (currentItem is IStorageFile file)
{
var btn = EditorProvider.IsOpen(file);
if (btn is not null)
{
EditorProvider.SelectTabPage(btn);
}
else
if (EditorProvider.TryGetEditor(extension, out var page))
{
if (page is ITabPage editor)

View File

@@ -1,6 +1,8 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using Progrart.Pages;
namespace Progrart.Controls.TabSystem;
@@ -30,6 +32,17 @@ public partial class TabHost : UserControl
page.BindButton(button);
SelectButton(button);
}
public TabButton? IsOpen(IStorageFile file)
{
foreach (var item in TabContainer.Children)
{
if (item is TabButton itemBtn)
if (itemBtn.page is IEditorPage page)
if (page.IsSameFile(file)) return itemBtn;
}
return null;
}
public void SelectButton(TabButton button)
{
foreach (var item in TabContainer.Children)

View File

@@ -24,6 +24,10 @@ public partial class EditorPage : UserControl, ITabPage, IEditorPage
}
public bool IsSameFile(IStorageFile file)
{
return (this.file?.Path == file.Path);
}
public void BindButton(TabButton button)
{
btn = button;

View File

@@ -29,6 +29,14 @@ namespace Progrart.Pages
{
ExtensionMapping[type] = id;
}
public static TabButton? IsOpen(IStorageFile file)
{
return currentHost?.IsOpen(file);
}
public static void SelectTabPage(TabButton button)
{
currentHost?.SelectButton(button);
}
public static void OpenEditor(ITabPage page)
{
currentHost?.AddPage(page);
@@ -56,6 +64,7 @@ namespace Progrart.Pages
{
void LoadDocument(IStorageFile file);
void Save();
bool IsSameFile(IStorageFile file);
void Execute(ExecuteArguments? args = null);
}
}

View File

@@ -12,6 +12,7 @@ namespace Progrart.Pages;
public partial class ImageViewPage : UserControl, ITabPage, IEditorPage
{
IStorageFile? file;
public ImageViewPage()
{
InitializeComponent();
@@ -31,15 +32,16 @@ public partial class ImageViewPage : UserControl, ITabPage, IEditorPage
{
return false;
}
public void LoadDocument(IStorageFile file)
{
this.file = file;
Task.Run(async () =>
{
using var fs = await file.OpenReadAsync();
Bitmap bitmap = new Bitmap(fs);
Dispatcher.UIThread.Invoke(() => {
Dispatcher.UIThread.Invoke(() =>
{
this.ImageViewer.SetImage(bitmap);
});
});
@@ -52,4 +54,9 @@ public partial class ImageViewPage : UserControl, ITabPage, IEditorPage
public void SetHost(TabHost host)
{
}
public bool IsSameFile(IStorageFile file)
{
return (this.file?.Path == file.Path);
}
}

View File

@@ -21,6 +21,7 @@ public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
public ProgrartEditorPage()
{
InitializeComponent();
CodeEditor.onSaveCmd = Save;
}
public void BindButton(TabButton button)
@@ -72,6 +73,11 @@ public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
return false;
}
public bool IsSameFile(IStorageFile file)
{
return (this.file?.Path==file.Path);
}
public void LoadDocument(IStorageFile file)
{
this.file = file;