Skip to content

Commit

Permalink
Merge pull request #701 from b-editor/use-frame-in-mainview
Browse files Browse the repository at this point in the history
MainViewでページの表示にFrameを使うようにした
  • Loading branch information
yuto-trd authored Sep 9, 2023
2 parents 14641ec + 251ae99 commit 4b5d1f9
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 105 deletions.
26 changes: 13 additions & 13 deletions src/Beutl/Pages/ExtensionsPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public ExtensionsPage()
nav.BackRequested += Nav_BackRequested;

nav.SelectedItem = selected;
}

this.GetObservable(IsVisibleProperty).Subscribe(b =>
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (nav.SelectedItem is NavigationViewItem selected)
{
if (b)
{
if (nav.SelectedItem is NavigationViewItem selected)
{
OnItemInvoked(selected);
}
}
else
{
frame.SetNavigationState("|\n0\n0");
}
});
OnItemInvoked(selected);
}
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
frame.SetNavigationState("|\n0\n0");
}

private void Search_Click(object? sender, RoutedEventArgs e)
Expand Down
19 changes: 8 additions & 11 deletions src/Beutl/Pages/OutputPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ public OutputPage()
{
InitializeComponent();
contentControl.ContentTemplate = s_sharedDataTemplate;
}

this.GetObservable(IsVisibleProperty)
.Where(b => b)
.SkipUntil(this.GetObservable(DataContextProperty).Where(x => x is OutputPageViewModel))
.Take(1)
.Subscribe(_ =>
{
if (DataContext is OutputPageViewModel viewModel)
{
viewModel.Restore();
}
});
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (DataContext is OutputPageViewModel viewModel)
{
viewModel.Restore();
}
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
Expand Down
26 changes: 13 additions & 13 deletions src/Beutl/Pages/SettingsPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public SettingsPage()
nav.BackRequested += Nav_BackRequested;

nav.SelectedItem = selected;
}

this.GetObservable(IsVisibleProperty).Subscribe(b =>
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (nav.SelectedItem is NavigationViewItem selected)
{
if (b)
{
if (nav.SelectedItem is NavigationViewItem selected)
{
OnItemInvoked(selected);
}
}
else
{
frame.SetNavigationState("|\n0\n0");
}
});
OnItemInvoked(selected);
}
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
frame.SetNavigationState("|\n0\n0");
}

protected override void OnDataContextChanged(EventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions src/Beutl/ViewModels/LibraryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ public LibraryViewModel(EditViewModel editViewModel)

public ReactiveCollection<KeyValuePair<int, LibraryItemViewModel>> SearchResult { get; } = new();

public int SelectedTab { get; set; } = 2;

private void AddAllItems(List<LibraryItemViewModel> items)
{
foreach (LibraryItemViewModel innerItem in items)
Expand Down
4 changes: 2 additions & 2 deletions src/Beutl/Views/Library.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Grid RowDefinitions="Auto,*">
<TabStrip x:Name="tabStrip"
Margin="0,2"
SelectedIndex="1"
SelectedIndex="2"
Theme="{StaticResource LiteNavTabStrip}">
<TabStrip.Styles>
<Style Selector="TabStripItem">
Expand Down Expand Up @@ -48,7 +48,7 @@
<TextBlock Text="{x:Static lang:Strings.Easings}" />
</StackPanel>
</TabStripItem>
<TabStripItem IsSelected="True">
<TabStripItem>
<StackPanel>
<icons:SymbolIcon Symbol="Library" />
<TextBlock Text="{x:Static lang:Strings.Library}" />
Expand Down
19 changes: 18 additions & 1 deletion src/Beutl/Views/Library.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Generators;
using Avalonia.Input;
using Avalonia.Interactivity;

Expand Down Expand Up @@ -30,6 +29,24 @@ public Library()
splineEasing.AddHandler(PointerPressedEvent, OnSplineEasingPointerPressed, RoutingStrategies.Tunnel);
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (DataContext is LibraryViewModel viewModel)
{
tabStrip.SelectedIndex = viewModel.SelectedTab;
}
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
if (DataContext is LibraryViewModel viewModel)
{
viewModel.SelectedTab = tabStrip.SelectedIndex;
}
}

private void OnItemContainerPrepared(object? sender, ContainerPreparedEventArgs e)
{
if (e.Container is TreeViewItem treeItem)
Expand Down
3 changes: 1 addition & 2 deletions src/Beutl/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@
</ui:NavigationViewItem>
</ui:NavigationView.FooterMenuItems>

<!--<TransitioningContentControl x:Name="NaviContent" />-->
<Panel x:Name="NaviContent" />
<ui:Frame x:Name="frame" />
</ui:NavigationView>

<StackPanel x:Name="NotificationPanel"
Expand Down
95 changes: 32 additions & 63 deletions src/Beutl/Views/MainView.axaml.InitializePages.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,51 @@
using System.Collections;
using System.Collections.Specialized;

using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.Xaml.Interactivity;

using Beutl.Controls;
using Beutl.ViewModels;

using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Media.Animation;

using Reactive.Bindings.Extensions;

namespace Beutl.Views;

public partial class MainView
{
private static readonly Binding s_headerBinding = new("Context.Header");
private readonly AvaloniaList<NavigationViewItem> _navigationItems = new();
private Control? _settingsView;
private readonly Avalonia.Animation.Animation _animation = new()
private sealed class NavigationPageFactory : INavigationPageFactory
{
Easing = new SplineEasing(0.1, 0.9, 0.2, 1.0),
Children =
public Control GetPage(Type srcType)
{
new KeyFrame
{
Setters =
{
new Setter(OpacityProperty, 0.0),
new Setter(TranslateTransform.YProperty, 28.0)
},
Cue = new Cue(0d)
},
new KeyFrame
return null!;
}

public Control GetPageFromObject(object target)
{
if (target is MainViewModel.NavItemViewModel item)
{
Setters =
{
new Setter(OpacityProperty, 1.0),
new Setter(TranslateTransform.YProperty, 0.0)
},
Cue = new Cue(1d)
return CreateView(item);
}
},
Duration = TimeSpan.FromSeconds(0.67),
FillMode = FillMode.Forward
};

return null!;
}
}

private static readonly Binding s_headerBinding = new("Context.Header");
private readonly AvaloniaList<NavigationViewItem> _navigationItems = new();
private NavigationTransitionInfo? _navigationTransition;

private void NavigationView_ItemInvoked(object? sender, NavigationViewItemInvokedEventArgs e)
{
if (e.InvokedItemContainer.DataContext is MainViewModel.NavItemViewModel itemViewModel
&& DataContext is MainViewModel viewModel)
{
_navigationTransition = e.RecommendedNavigationTransitionInfo;
viewModel.SelectedPage.Value = itemViewModel;
}
}
Expand Down Expand Up @@ -91,7 +80,6 @@ private static Control CreateView(MainViewModel.NavItemViewModel item)
"
};

view.IsVisible = false;
view.DataContext = item.Context;

return view;
Expand All @@ -106,9 +94,7 @@ void Add(int index, IList items)
if (item != null)
{
int idx = index++;
Control view = CreateView(item);

NaviContent.Children.Insert(idx, view);
_navigationItems.Insert(idx, new NavigationViewItem()
{
Classes = { "SideNavigationViewItem" },
Expand Down Expand Up @@ -137,7 +123,6 @@ void Remove(int index, IList items)
int idx = index + i;

(item.Context as IDisposable)?.Dispose();
NaviContent.Children.RemoveAt(idx);
_navigationItems.RemoveAt(idx);
}
}
Expand All @@ -162,21 +147,17 @@ void Remove(int index, IList items)
case NotifyCollectionChangedAction.Reset:
throw new Exception("'MainViewModel.Pages' does not support the 'Clear' method.");
}

if (sender is IList list)
frame.CacheSize = list.Count + 1;
}

private void InitializePages(MainViewModel viewModel)
{
_settingsView = new Pages.SettingsPage
{
IsVisible = false,
DataContext = viewModel.SettingsPage.Context
};
NaviContent.Children.Clear();
NaviContent.Children.Add(_settingsView);
_navigationItems.Clear();
frame.CacheSize = viewModel.Pages.Count + 1;
frame.NavigationPageFactory = new NavigationPageFactory();

Control[] pageViews = viewModel.Pages.Select(CreateView).ToArray();
NaviContent.Children.InsertRange(0, pageViews);
_navigationItems.Clear();

NavigationViewItem[] navItems = viewModel.Pages.Select(item =>
{
Expand All @@ -199,31 +180,19 @@ private void InitializePages(MainViewModel viewModel)

viewModel.Pages.CollectionChanged += OnPagesCollectionChanged;
_disposables.Add(Disposable.Create(viewModel.Pages, obj => obj.CollectionChanged -= OnPagesCollectionChanged));
viewModel.SelectedPage.Subscribe(async obj =>
viewModel.SelectedPage.Subscribe(obj =>
{
if (DataContext is MainViewModel viewModel)
{
int idx = obj == null ? -1 : viewModel.Pages.IndexOf(obj);
Control? oldControl = null;
for (int i = 0; i < NaviContent.Children.Count; i++)
{
if (NaviContent.Children[i] is Control { IsVisible: true } control)
{
control.IsVisible = false;
oldControl = control;
}
}
Navi.SelectedItem = idx >= 0 ? _navigationItems[idx] : Navi.FooterMenuItems.Cast<object>().First();
Control newControl = idx >= 0 ? NaviContent.Children[idx] : _settingsView;
newControl.IsVisible = true;
newControl.Opacity = 0;
await _animation.RunAsync(newControl);
newControl.Opacity = 1;
Navi.SelectedItem = (NavigationViewItem)(idx >= 0 ? _navigationItems[idx] : Navi.FooterMenuItems.Cast<object>().First());
newControl.Focus();
frame.NavigateFromObject(
obj,
_navigationTransition != null
? new() { TransitionInfoOverride = _navigationTransition }
: null);
}
}).AddTo(_disposables);
}
Expand Down

0 comments on commit 4b5d1f9

Please sign in to comment.