Skip to content

Commit

Permalink
Merge pull request #100 from Diaskhan/dev/Lister_plugins
Browse files Browse the repository at this point in the history
Dev/lister plugins  (Core functionalyti)
  • Loading branch information
anovik authored Sep 30, 2024
2 parents 6629249 + b1b2176 commit 1410af8
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 32 deletions.
70 changes: 70 additions & 0 deletions src/SmartCommander/Plugins/ListerPluginWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Runtime.InteropServices;

namespace SmartCommander.Plugins;

public class ListerPluginWrapper : IDisposable
{
private IntPtr _pluginHandle;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr ListLoadDelegate(IntPtr parentWin, string fileToLoad, int showFlags);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void ListCloseWindowDelegate(IntPtr listWin);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void ListSendCommandDelegate(IntPtr listWin, int command, int parameter);

[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate void ListGetDetectStringDelegate(IntPtr detectString, int maxlen);

private ListLoadDelegate ListLoad;
private ListCloseWindowDelegate ListCloseWindow;
private ListSendCommandDelegate ListSendCommand;
private ListGetDetectStringDelegate ListGetDetectString;

public ListerPluginWrapper(string pluginPath)
{
_pluginHandle = NativeLibrary.Load(pluginPath);

ListLoad = Marshal.GetDelegateForFunctionPointer<ListLoadDelegate>(NativeLibrary.GetExport(_pluginHandle, nameof(ListLoad)));
ListCloseWindow = Marshal.GetDelegateForFunctionPointer<ListCloseWindowDelegate>(NativeLibrary.GetExport(_pluginHandle, nameof(ListCloseWindow)));
ListSendCommand = Marshal.GetDelegateForFunctionPointer<ListSendCommandDelegate>(NativeLibrary.GetExport(_pluginHandle, nameof(ListSendCommand)));
ListGetDetectString = Marshal.GetDelegateForFunctionPointer<ListGetDetectStringDelegate>(NativeLibrary.GetExport(_pluginHandle, nameof(ListGetDetectString)));
}

public IntPtr LoadFile(IntPtr parentWindowHandle, string filePath, int showFlags)
{
return ListLoad!(parentWindowHandle, filePath, showFlags);
}

public void CloseWindow(IntPtr listerWindowHandle)
{
ListCloseWindow!(listerWindowHandle);
}

public void SendCommand(IntPtr listerWindowHandle, int command, int parameter)
{
ListSendCommand!(listerWindowHandle, command, parameter);
}

public string? DetectString(int maxlen = 2000)
{
IntPtr detectStringPtr = Marshal.AllocHGlobal(maxlen);
ListGetDetectString(detectStringPtr, maxlen);
string? detectString = Marshal.PtrToStringAnsi(detectStringPtr);
Marshal.FreeHGlobal(detectStringPtr);
return detectString;
}

public void Dispose()
{
if (_pluginHandle != IntPtr.Zero)
{
NativeLibrary.Free(_pluginHandle);
_pluginHandle = IntPtr.Zero;
}
}
}

21 changes: 21 additions & 0 deletions src/SmartCommander/Plugins/PluginManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace SmartCommander.Plugins;

public static class PluginManager
{
public static ListerPluginWrapper CreateListerWrapper()
{
//string pluginPath = "C:\\totalcmd\\plugins\\CodeViewer\\CodeViewer.wlx64";
string pluginPath = "C:\\totalcmd\\plugins\\wlx\\CodeViewer\\CodeViewer.wlx64";
return new ListerPluginWrapper(pluginPath);
}

public static IntPtr CreateListerWindow(this ListerPluginWrapper listerWrapper, IntPtr parentWindowHandle, string fileToLoad)
{
int showFlags = 1;
return listerWrapper.LoadFile(parentWindowHandle, fileToLoad, showFlags);
}

}

Binary file added src/SmartCommander/Plugins/TestBin/CodeViewer.zip
Binary file not shown.
10 changes: 10 additions & 0 deletions src/SmartCommander/SmartCommander.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
<Version>0.1.0</Version>
<Authors>Anna Novikova</Authors>
<ApplicationIcon>Assets\main.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
<AvaloniaXaml Remove="TcPlugins\ListerAppNetCore\**" />
<Compile Remove="TcPlugins\ListerAppNetCore\**" />
<EmbeddedResource Remove="TcPlugins\ListerAppNetCore\**" />
<None Remove="TcPlugins\ListerAppNetCore\**" />
<AvaloniaResource Remove="Assets\Resources.resx" />
<AvaloniaResource Remove="Assets\Resources.ru-RU.resx" />
<None Remove=".gitignore" />
Expand Down Expand Up @@ -61,4 +66,9 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="app.manifest">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
52 changes: 28 additions & 24 deletions src/SmartCommander/ViewModels/FilesPaneViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ public void Tapped(object sender, object parameter)
}

public void DoubleTapped(object sender, object parameter)
{
{
var args = parameter as TappedEventArgs;
if (args != null)
{
var source = args.Source as Control;
if (source != null &&
var source = args.Source as Control;
if (source != null &&
(source.TemplatedParent is DataGridCell || source.Parent is DataGridCell))
{
{
ProcessCurrentItem();
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public void Update()

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

public async Task View(Action<ButtonResult, object?>? resultAction)
Expand All @@ -297,7 +297,7 @@ public async Task View(Action<ButtonResult, object?>? resultAction)
return;
if (!CurrentItem.IsFolder)
{
if (Convert.ToUInt64(CurrentItem.Size) > 128*1024*1024)
if (Convert.ToUInt64(CurrentItem.Size) > 128 * 1024 * 1024)
{
MessageBox_Show(resultAction, Resources.TooLargeSize, Resources.Alert, ButtonEnum.Ok);
return;
Expand Down Expand Up @@ -355,7 +355,7 @@ private void LaunchProcess(string program, string argument)
process.StartInfo.Arguments = $"-e {program} \"{argument}\""; // Specify the command to run in the new terminal window
process.StartInfo.UseShellExecute = false; // Required to use the terminal emulator
process.Start();
}
}

public void Delete(FileViewModel? item)
{
Expand All @@ -367,7 +367,7 @@ public void Delete(FileViewModel? item)
}
if (item.IsFolder)
{
Utils.DeleteDirectoryWithHiddenFiles(item.FullName);
Utils.DeleteDirectoryWithHiddenFiles(item.FullName);
}
else
{
Expand All @@ -377,7 +377,7 @@ public void Delete(FileViewModel? item)
catch
{
}
}
}

public void CreateNewFolder(string name)
{
Expand All @@ -391,7 +391,7 @@ public void CreateNewFolder(string name)
}

public void ProcessCurrentItem(bool goToParent = false)
{
{
if (CurrentItem == null)
{
return;
Expand All @@ -406,7 +406,9 @@ public void ProcessCurrentItem(bool goToParent = false)

var prevFolder = Path.GetFileName(CurrentDirectory);
CurrentDirectory = Directory.GetParent(CurrentDirectory) != null ? Directory.GetParent(CurrentDirectory)!.FullName : CurrentDirectory;
CurrentItem = FoldersFilesList.First(f => f.Name == prevFolder);
CurrentItem = FoldersFilesList.FirstOrDefault(f => f.Name == prevFolder);
if (CurrentItem == null)
CurrentItem = FoldersFilesList[0];
return;
}

Expand All @@ -416,7 +418,9 @@ public void ProcessCurrentItem(bool goToParent = false)
{
var prevFolder = Path.GetFileName(CurrentDirectory);
CurrentDirectory = Directory.GetParent(CurrentDirectory) != null ? Directory.GetParent(CurrentDirectory)!.FullName : CurrentDirectory;
CurrentItem= FoldersFilesList.First(f => f.Name == prevFolder);
CurrentItem = FoldersFilesList.FirstOrDefault(f => f.Name == prevFolder);
if (CurrentItem == null)
CurrentItem = FoldersFilesList[0];
}
else
{
Expand Down Expand Up @@ -510,7 +514,7 @@ private void GetFilesFolders(string dir, IList<FileViewModel> filesFoldersList)
{
foldersList = foldersList.OrderByDescending(entry => entry.Extension).ToList();
filesList = filesList.OrderByDescending(entry => entry.Extension).ToList();
}
}
}
else if (Sorting == SortingBy.SortingBySize)
{
Expand All @@ -523,7 +527,7 @@ private void GetFilesFolders(string dir, IList<FileViewModel> filesFoldersList)
{
foldersList = foldersList.OrderByDescending(entry => entry.Size).ToList();
filesList = filesList.OrderByDescending(entry => Convert.ToUInt64(entry.Size)).ToList();
}
}
}
else if (Sorting == SortingBy.SortingByDate)
{
Expand All @@ -536,7 +540,7 @@ private void GetFilesFolders(string dir, IList<FileViewModel> filesFoldersList)
{
foldersList = foldersList.OrderByDescending(entry => entry.DateCreated).ToList();
filesList = filesList.OrderByDescending(entry => entry.DateCreated).ToList();
}
}
}

foreach (var folder in foldersList)
Expand All @@ -558,33 +562,33 @@ private void GetFilesFolders(string dir, IList<FileViewModel> filesFoldersList)

public void NavigateToFileItem(string resultFilename)
{
var parent=Directory.GetParent(resultFilename);
CurrentDirectory = parent!.FullName;
CurrentItem= FoldersFilesList.First(f => f.FullName == resultFilename);
RequestScroll(CurrentItem, null);
var parent = Directory.GetParent(resultFilename);
CurrentDirectory = parent!.FullName;
CurrentItem = FoldersFilesList.First(f => f.FullName == resultFilename);
RequestScroll(CurrentItem, null);
}

string? _selectedDrive;
string? SelectedDrive
{
get { return _selectedDrive; }
set
{
set
{
if (!Directory.Exists(value))
{
MessageBox_Show(null, Resources.DriveNotAvailable, Resources.Alert, ButtonEnum.Ok);
MessageBox_Show(null, Resources.DriveNotAvailable, Resources.Alert, ButtonEnum.Ok);
return;
}

FileInfo f = new FileInfo(CurrentDirectory);
var driveFromDirectory = Path.GetPathRoot(f.FullName);

_selectedDrive = value;
_selectedDrive = value;
if (_selectedDrive != driveFromDirectory)
{
CurrentDirectory = _selectedDrive;
}
this.RaisePropertyChanged(nameof(SelectedDrive));
this.RaisePropertyChanged(nameof(SelectedDrive));
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/SmartCommander/ViewModels/ViewerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System.IO;
using Avalonia.Controls;
using System;
using System.IO;

namespace SmartCommander.ViewModels
{
public class ViewerViewModel : ViewModelBase
{

public ViewerViewModel(string filename)
{
try
{
Text = File.ReadAllText(filename);
}
catch { Text = ""; }
Filename= filename;
}

public string Text { get; set; }
public string Filename { get; set; }
}
}
12 changes: 7 additions & 5 deletions src/SmartCommander/Views/ViewerWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
xmlns:behaviors="clr-namespace:SmartCommander.Behaviors;assembly=SmartCommander"
x:Class="SmartCommander.ViewerWindow"
Title="SmartCommander Viewer">
<AvaloniaEdit:TextEditor IsReadOnly="True"
<Grid x:Name="GridPanel">
<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>
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding Text, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</AvaloniaEdit:TextEditor>
</Grid>
</Window>
Loading

0 comments on commit 1410af8

Please sign in to comment.