Skip to content

Commit

Permalink
Initial version of file viewer.
Browse files Browse the repository at this point in the history
  • Loading branch information
anovik committed Aug 30, 2024
1 parent 34dbdfb commit a67c025
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
27 changes: 10 additions & 17 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,7 +11,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -54,11 +52,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 +107,7 @@ public bool IsCurrentDirectoryDisplayed
public FilesPaneViewModel()
{
_mainVM = new MainWindowViewModel();
ShowViewerDialog = new Interaction<ViewerViewModel, ViewerViewModel?>();
}

public FilesPaneViewModel(MainWindowViewModel mainVM)
Expand All @@ -121,6 +117,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 +127,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 @@ -271,21 +270,15 @@ public void View()
View(null);
}

public void View(Action<ButtonResult, object?>? resultAction)
public async void View(Action<ButtonResult, object?>? resultAction)
{
if (CurrentItem == null)
return;
if (!CurrentItem.IsFolder)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
LaunchProcess("less", CurrentItem.FullName);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start("LTFViewr5u.exe", CurrentItem.FullName);
}
resultAction?.Invoke(ButtonResult.Ok,null);
// TODO: check file size
var copy = new ViewerViewModel(CurrentItem.FullName);
await ShowViewerDialog.Handle(copy);
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion 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 @@ -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)

Check warning on line 7 in src/SmartCommander/ViewModels/ViewerViewModel.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in src/SmartCommander/ViewModels/ViewerViewModel.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in src/SmartCommander/ViewModels/ViewerViewModel.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in src/SmartCommander/ViewModels/ViewerViewModel.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
try
{
Text = File.ReadAllText(filename);
}
catch { }
}

public string Text { get; set; }
}
}
12 changes: 11 additions & 1 deletion src/SmartCommander/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public MainWindow()

this.WhenActivated(d => d(ViewModel!.ShowCopyDialog.RegisterHandler(DoShowCopyDialogAsync)));
this.WhenActivated(d => d(ViewModel!.ShowOptionsDialog.RegisterHandler(DoShowOptionsDialogAsync)));
this.WhenActivated(d => d(ViewModel!.ShowViewerDialog.RegisterHandler(DoShowViewerDialogAsync)));

progressWindow = new ProgressWindow();

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

private async Task DoShowViewerDialogAsync(InteractionContext<ViewerViewModel, ViewerViewModel?> interaction)
{
var dialog = new ViewerWindow();
dialog.DataContext = interaction.Input;

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

private async Task DoShowCopyDialogAsync(InteractionContext<CopyMoveViewModel, CopyMoveViewModel?> interaction)
{
Expand Down
12 changes: 12 additions & 0 deletions src/SmartCommander/Views/ViewerWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<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"
WindowState="Maximized"
x:Class="SmartCommander.ViewerWindow"
Title="SmartCommander Viewer">
<TextBlock Text="{Binding Text}" ScrollViewer.VerticalScrollBarVisibility="Visible" ></TextBlock>

</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();
}
}

0 comments on commit a67c025

Please sign in to comment.