Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewer for F3 based on AvaloniaEdit #81

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/SmartCommander/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</Application.DataTemplates>

<Application.Styles>
<FluentTheme />
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/>
</Application.Styles>
<FluentTheme />
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/>
<StyleInclude Source="avares://AvaloniaEdit/Themes/Fluent/AvaloniaEdit.xaml"/>
</Application.Styles>
</Application>
9 changes: 9 additions & 0 deletions src/SmartCommander/Assets/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/SmartCommander/Assets/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,7 @@
<data name="DirectoryExists" xml:space="preserve">
<value>Directory {0} already exists.</value>
</data>
<data name="TooLargeSize" xml:space="preserve">
<value>Too large file size for viewing.</value>
</data>
</root>
61 changes: 61 additions & 0 deletions src/SmartCommander/Behaviors/DocumentTextBindingBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Avalonia;
using Avalonia.Xaml.Interactivity;
using AvaloniaEdit;
using System;

namespace SmartCommander.Behaviors
{
public class DocumentTextBindingBehavior : Behavior<TextEditor>
{
private TextEditor? _textEditor;

public static readonly StyledProperty<string> TextProperty =
AvaloniaProperty.Register<DocumentTextBindingBehavior, string>(nameof(Text));

public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

protected override void OnAttached()
{
base.OnAttached();

if (AssociatedObject is TextEditor textEditor)
{
_textEditor = textEditor;
_textEditor.TextChanged += TextChanged;
this.GetObservable(TextProperty).Subscribe(TextPropertyChanged);
}
}

protected override void OnDetaching()
{
base.OnDetaching();

if (_textEditor != null)
{
_textEditor.TextChanged -= TextChanged;
}
}

private void TextChanged(object? sender, EventArgs? eventArgs)
{
if (_textEditor != null && _textEditor.Document != null)
{
Text = _textEditor.Document.Text;
}
}

private void TextPropertyChanged(string text)
{
if (_textEditor != null && _textEditor.Document != null && text != null)
{
var caretOffset = _textEditor.CaretOffset;
_textEditor.Document.Text = text;
_textEditor.CaretOffset = caretOffset;
}
}
}
}
1 change: 1 addition & 0 deletions src/SmartCommander/SmartCommander.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.1.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.10" />
Expand Down
31 changes: 15 additions & 16 deletions src/SmartCommander/ViewModels/FilesPaneViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using MsBox.Avalonia.Enums;
using ReactiveUI;
using SmartCommander.Assets;
Expand All @@ -12,10 +11,10 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Path = System.IO.Path;

namespace SmartCommander.ViewModels
Expand Down Expand Up @@ -54,11 +53,8 @@ public string CurrentDirectory

private MainWindowViewModel _mainVM;

public string CurrentDirectoryInfo
{
get { return string.Format(Resources.CurrentDirInfo, _totalFiles, _totalFolders); }
}

public string CurrentDirectoryInfo => string.Format(Resources.CurrentDirInfo, _totalFiles, _totalFolders);

public FileViewModel? CurrentItem { get; set; }

public List<FileViewModel> CurrentItems { get; set; } = new List<FileViewModel>();
Expand Down Expand Up @@ -112,6 +108,7 @@ public bool IsCurrentDirectoryDisplayed
public FilesPaneViewModel()
{
_mainVM = new MainWindowViewModel();
ShowViewerDialog = new Interaction<ViewerViewModel, ViewerViewModel?>();
}

public FilesPaneViewModel(MainWindowViewModel mainVM)
Expand All @@ -121,6 +118,7 @@ public FilesPaneViewModel(MainWindowViewModel mainVM)
EditCommand = ReactiveCommand.Create(Edit);
ZipCommand = ReactiveCommand.Create(Zip);
UnzipCommand = ReactiveCommand.Create(Unzip);
ShowViewerDialog = new Interaction<ViewerViewModel, ViewerViewModel?>();
_mainVM = mainVM;
}

Expand All @@ -130,6 +128,8 @@ public FilesPaneViewModel(MainWindowViewModel mainVM)
public ReactiveCommand<Unit, Unit>? ZipCommand { get; }
public ReactiveCommand<Unit, Unit>? UnzipCommand { get; }

public Interaction<ViewerViewModel, ViewerViewModel?> ShowViewerDialog { get; }

public void CellPointerPressed(object sender, object parameter)
{
_mainVM.SelectedPane = this;
Expand Down Expand Up @@ -268,24 +268,23 @@ public void Update()

public void View()
{
View(null);
_= View(null);
}

public void View(Action<ButtonResult, object?>? resultAction)
public async Task View(Action<ButtonResult, object?>? resultAction)
{
if (CurrentItem == null)
return;
if (!CurrentItem.IsFolder)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
if (Convert.ToUInt64(CurrentItem.Size) > 128*1024*1024)
{
LaunchProcess("less", CurrentItem.FullName);
MessageBox_Show(resultAction, Resources.TooLargeSize, Resources.Alert, ButtonEnum.Ok);
return;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start("LTFViewr5u.exe", CurrentItem.FullName);
}
resultAction?.Invoke(ButtonResult.Ok,null);
var copy = new ViewerViewModel(CurrentItem.FullName);
await ShowViewerDialog.Handle(copy);
resultAction?.Invoke(ButtonResult.Ok, null);
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions src/SmartCommander/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public string CommandText

public Interaction<OptionsViewModel, OptionsViewModel?> ShowOptionsDialog { get; }

public Interaction<ViewerViewModel, ViewerViewModel?> ShowViewerDialog => LeftFileViewModel.ShowViewerDialog;

public bool IsFunctionKeysDisplayed => OptionsModel.Instance.IsFunctionKeysDisplayed;
public bool IsCommandLineDisplayed => OptionsModel.Instance.IsCommandLineDisplayed;

Expand Down Expand Up @@ -206,7 +208,7 @@ public void View()
return;
}
_F3Busy = true;
SelectedPane.View(F3Finished);
_= SelectedPane.View(F3Finished);
}

public void Edit()
Expand All @@ -219,7 +221,7 @@ public void Edit()
SelectedPane.Edit(F4Finished);
}

public bool IsBackgroundOperation { get { return tokenSource != null && !tokenSource.IsDisposed; } }
public bool IsBackgroundOperation => tokenSource != null && !tokenSource.IsDisposed;

public void Cancel()
{
Expand Down
18 changes: 18 additions & 0 deletions src/SmartCommander/ViewModels/ViewerViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.IO;

namespace SmartCommander.ViewModels
{
public class ViewerViewModel : ViewModelBase
{
public ViewerViewModel(string filename)
{
try
{
Text = File.ReadAllText(filename);
}
catch { Text = ""; }
}

public string Text { get; set; }
}
}
12 changes: 1 addition & 11 deletions src/SmartCommander/Views/CopyMoveWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace SmartCommander.Views
{
Expand All @@ -9,14 +7,6 @@ public partial class CopyMoveWindow : Window
public CopyMoveWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
}
22 changes: 7 additions & 15 deletions src/SmartCommander/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public MainWindow()
Opened += OnOpened;
InitializeComponent();

this.WhenActivated(d => d(ViewModel!.ShowCopyDialog.RegisterHandler(DoShowCopyDialogAsync)));
this.WhenActivated(d => d(ViewModel!.ShowOptionsDialog.RegisterHandler(DoShowOptionsDialogAsync)));
this.WhenActivated(d => d(ViewModel!.ShowCopyDialog.RegisterHandler(DoShowDialogAsync<CopyMoveViewModel, CopyMoveWindow>)));
this.WhenActivated(d => d(ViewModel!.ShowOptionsDialog.RegisterHandler(DoShowDialogAsync<OptionsViewModel, OptionsWindow>)));
this.WhenActivated(d => d(ViewModel!.ShowViewerDialog.RegisterHandler(DoShowDialogAsync<ViewerViewModel, ViewerWindow>)));

progressWindow = new ProgressWindow();

Expand Down Expand Up @@ -53,25 +54,16 @@ public MainWindow()
progressWindow.Close();
}
};
}

private async Task DoShowCopyDialogAsync(InteractionContext<CopyMoveViewModel, CopyMoveViewModel?> interaction)
{
var dialog = new CopyMoveWindow();
dialog.DataContext = interaction.Input;

var result = await dialog.ShowDialog<CopyMoveViewModel>(this);
interaction.SetOutput(result);
}

private async Task DoShowOptionsDialogAsync(InteractionContext<OptionsViewModel, OptionsViewModel?> interaction)
private async Task DoShowDialogAsync<T1, T2>(InteractionContext<T1, T1?> interaction) where T2 : Window, new()
{
var dialog = new OptionsWindow();
var dialog = new T2();
dialog.DataContext = interaction.Input;

var result = await dialog.ShowDialog<OptionsViewModel>(this);
var result = await dialog.ShowDialog<T1>(this);
interaction.SetOutput(result);
}
}

private void OnOpened(object? sender, EventArgs e)
{
Expand Down
15 changes: 2 additions & 13 deletions src/SmartCommander/Views/OptionsWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace SmartCommander.Views
{
public partial class OptionsWindow : Window
{
public OptionsWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif

}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
InitializeComponent();
}
}
}
20 changes: 20 additions & 0 deletions src/SmartCommander/Views/ViewerWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Window 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"
ShowInTaskbar="True"
Icon="/Assets/main.ico"
WindowState="Maximized"
xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity"
xmlns:AvaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit"
xmlns:behaviors="clr-namespace:SmartCommander.Behaviors;assembly=SmartCommander"
x:Class="SmartCommander.ViewerWindow"
Title="SmartCommander Viewer">
<AvaloniaEdit:TextEditor IsReadOnly="True"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace">
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding Text, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</AvaloniaEdit:TextEditor>
</Window>
11 changes: 11 additions & 0 deletions src/SmartCommander/Views/ViewerWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Avalonia.Controls;

namespace SmartCommander;

public partial class ViewerWindow : Window
{
public ViewerWindow()
{
InitializeComponent();
}
}
Loading