Skip to content

Commit

Permalink
Removed FocusChanged and get back to old logic for SelectedPane becau…
Browse files Browse the repository at this point in the history
…se new focus logic worked badly with Avalonia message boxes.
  • Loading branch information
anovik committed Oct 9, 2024
1 parent a2c2dcd commit 953327e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
22 changes: 7 additions & 15 deletions src/SmartCommander/ViewModels/FilesPaneViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Input;
using Path = System.IO.Path;

namespace SmartCommander.ViewModels
Expand All @@ -37,8 +36,7 @@ public class FilesPaneViewModel : ViewModelBase

private bool _isSelected;
private SortingBy _sorting = SortingBy.SortingByName;
private bool _ascending = true;
public event EventHandler? FocusChanged;
private bool _ascending = true;

public string CurrentDirectory
{
Expand Down Expand Up @@ -96,12 +94,7 @@ public bool IsSelected
set
{
_isSelected = value;
this.RaisePropertyChanged(nameof(GridBorderBrush));

if (value)
{
FocusChanged?.Invoke(this, EventArgs.Empty);
}
this.RaisePropertyChanged(nameof(GridBorderBrush));
}
}

Expand All @@ -124,16 +117,15 @@ public FilesPaneViewModel()
ShowViewerDialog = new Interaction<ViewerViewModel, ViewerViewModel?>();
}

public FilesPaneViewModel(MainWindowViewModel mainVM, EventHandler focusHandler)
public FilesPaneViewModel(MainWindowViewModel mainVM)
{
CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
ViewCommand = ReactiveCommand.Create(View);
EditCommand = ReactiveCommand.Create(Edit);
ZipCommand = ReactiveCommand.Create(Zip);
UnzipCommand = ReactiveCommand.Create(Unzip);
ShowViewerDialog = new Interaction<ViewerViewModel, ViewerViewModel?>();
_mainVM = mainVM;
FocusChanged += focusHandler;
_mainVM = mainVM;
}

public event Action<object, object>? ScrollToItemRequested;
Expand All @@ -151,12 +143,12 @@ public void RequestScroll(object item, object? column)

public void CellPointerPressed(object sender, object parameter)
{
_mainVM.SelectedPane = this;
IsSelected = true;
}

public void SortingStarted(object sender, object parameter)
{
_mainVM.SelectedPane = this;
IsSelected = true;

DataGridColumnEventArgs? args = parameter as DataGridColumnEventArgs;
if (args != null)
Expand Down Expand Up @@ -248,7 +240,7 @@ public void SelectionChanged(object sender, object parameter)

public void Tapped(object sender, object parameter)
{
_mainVM.SelectedPane = this;
IsSelected = true;
}

public void DoubleTapped(object sender, object parameter)
Expand Down
38 changes: 25 additions & 13 deletions src/SmartCommander/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Styling;
using MsBox.Avalonia.Enums;
using ReactiveUI;
using Serilog;
using SmartCommander.Assets;
using SmartCommander.Models;
using System;
Expand Down Expand Up @@ -43,9 +44,9 @@ public MainWindowViewModel()

OptionsCommand = ReactiveCommand.CreateFromTask(ShowOptions);

LeftFileViewModel = new FilesPaneViewModel(this, OnFocusChanged);
RightFileViewModel = new FilesPaneViewModel(this, OnFocusChanged);
SelectedPane = RightFileViewModel;
LeftFileViewModel = new FilesPaneViewModel(this);
RightFileViewModel = new FilesPaneViewModel(this);
LeftFileViewModel.IsSelected = true;

if (!string.IsNullOrEmpty(OptionsModel.Instance.LeftPanePath))
{
Expand All @@ -66,15 +67,7 @@ private void SetLanguage()
var culture = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}

private void OnFocusChanged(object? sender, EventArgs e)
{
if (sender is FilesPaneViewModel)
{
SelectedPane = (FilesPaneViewModel)sender;
}
}
}

public ReactiveCommand<Unit, Unit> ExitCommand { get; }

Expand Down Expand Up @@ -186,7 +179,26 @@ public FilesPaneViewModel SecondPane
}
}

public FilesPaneViewModel SelectedPane { get; set; }
public FilesPaneViewModel SelectedPane
{
get
{
if (LeftFileViewModel.IsSelected)
{
return LeftFileViewModel;
}
else if (RightFileViewModel.IsSelected)
{
return RightFileViewModel;
}
else
{
Log.Error("No pane is selected");
}
return LeftFileViewModel;
}
}


public void Execute()
{
Expand Down
3 changes: 1 addition & 2 deletions src/SmartCommander/Views/FilesPane.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
</Grid>
<DataGrid Name="PaneDataGrid" Loaded="OnLoaded"
IsFocused="{Binding IsSelected, Mode=OneWayToSource}"

Margin="5" Grid.Row="1" IsTabStop="True"
Margin="5" Grid.Row="1" IsTabStop="True"
ItemsSource="{Binding FoldersFilesList}"
AutoGenerateColumns="False"
CanUserResizeColumns="True"
Expand Down

0 comments on commit 953327e

Please sign in to comment.