Skip to content

add a right-click open folder option for treeview items #180

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Definitions/Database/ObjectIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class ObjectIndex
[JsonIgnore]
public const string DefaultIndexDbFileName = "objectIndex.db";

public LocoDb Database { get; set; }

public ObjectIndex()
{ }

Expand Down
17 changes: 17 additions & 0 deletions Gui/Models/Converters/IsOfTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Avalonia.Data.Converters;
using System;
using System.Globalization;

namespace OpenLoco.Gui.Models.Converters
{
public class IsOfTypeConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value == null || parameter is not Type targetTypeParameter
? false
: (object)targetTypeParameter.IsAssignableFrom(value.GetType());

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
}
13 changes: 13 additions & 0 deletions Gui/ViewModels/FolderTreeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class FolderTreeViewModel : ReactiveObject

public ReactiveCommand<Unit, Unit>? OpenCurrentFolder { get; }

public ReactiveCommand<FileSystemItemBase, Unit>? OpenFolderFor { get; }

public ObservableCollection<ObjectDisplayMode> DisplayModeItems { get; } = [.. Enum.GetValues<ObjectDisplayMode>()];

[Reactive]
Expand All @@ -85,6 +87,17 @@ public FolderTreeViewModel(ObjectEditorModel model)

RefreshDirectoryItems = ReactiveCommand.Create(() => ReloadDirectoryAsync(false));
OpenCurrentFolder = ReactiveCommand.Create(() => PlatformSpecific.FolderOpenInDesktop(IsLocal ? CurrentLocalDirectory : Model.Settings.DownloadFolder, Model.Logger));
OpenFolderFor = ReactiveCommand.Create((FileSystemItemBase clickedOn) =>
{
if (IsLocal && clickedOn is FileSystemItemObject clickedOnObject && File.Exists(clickedOnObject.Filename))
{
var dir = Directory.GetParent(clickedOnObject.Filename)?.FullName;
if (Directory.Exists(dir))
{
PlatformSpecific.FolderOpenInDesktop(dir, Model.Logger);
}
}
});

_ = this.WhenAnyValue(o => o.CurrentLocalDirectory)
.Skip(1)
Expand Down
13 changes: 12 additions & 1 deletion Gui/Views/FolderTreeView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:OpenLoco.Gui.ViewModels"
xmlns:moc="using:OpenLoco.Gui.Models.Converters"
xmlns:mo="using:OpenLoco.Gui.Models"
xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
mc:Ignorable="d" d:DesignWidth="384" d:DesignHeight="768"
Width="384"
Expand All @@ -19,6 +20,7 @@

<UserControl.Resources>
<moc:EnumToBooleanConverter x:Key="EnumToBooleanConverter"/>
<moc:IsOfTypeConverter x:Key="IsOfTypeConverter" />
</UserControl.Resources>

<DockPanel>
Expand Down Expand Up @@ -86,7 +88,16 @@
<TreeView ItemsSource="{Binding DirectoryItems}" SelectedItem="{Binding CurrentlySelectedObject}" >
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding SubNodes}">
<ContentControl Content="{Binding}" />
<ContentControl Content="{Binding}">
<ContentControl.ContextMenu>
<ContextMenu>
<ContextMenu.IsVisible>
<Binding Converter="{StaticResource IsOfTypeConverter}" ConverterParameter="{x:Type mo:FileSystemItemObject}"/>
</ContextMenu.IsVisible>
<MenuItem Header="Open folder" Command="{Binding $parent[TreeView].((vm:FolderTreeViewModel)DataContext).OpenFolderFor}" CommandParameter="{Binding}" />
</ContextMenu>
</ContentControl.ContextMenu>
</ContentControl>
</TreeDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Expand Down