Add basic asynchronous loading pattern to SettingsPanel

This commit is contained in:
Dean Herbert 2021-08-16 19:18:39 +09:00
parent b541550ea9
commit c6bd8520a7

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using osuTK; using osuTK;
using osuTK.Graphics; using osuTK.Graphics;
@ -13,6 +14,7 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Threading;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
@ -58,6 +60,8 @@ namespace osu.Game.Overlays
private readonly bool showSidebar; private readonly bool showSidebar;
private LoadingLayer loading;
protected SettingsPanel(bool showSidebar) protected SettingsPanel(bool showSidebar)
{ {
this.showSidebar = showSidebar; this.showSidebar = showSidebar;
@ -86,6 +90,13 @@ namespace osu.Game.Overlays
Colour = OsuColour.Gray(0.05f), Colour = OsuColour.Gray(0.05f),
Alpha = 1, Alpha = 1,
}, },
loading = new LoadingLayer
{
State = { Value = Visibility.Visible }
}
}
};
SectionsContainer = new SettingsSectionsContainer SectionsContainer = new SettingsSectionsContainer
{ {
Masking = true, Masking = true,
@ -104,14 +115,31 @@ namespace osu.Game.Overlays
}, },
}, },
Footer = CreateFooter() Footer = CreateFooter()
},
}
}; };
if (showSidebar) if (showSidebar)
{ {
AddInternal(Sidebar = new Sidebar { Width = sidebar_width }); AddInternal(Sidebar = new Sidebar { Width = sidebar_width });
}
CreateSections()?.ForEach(AddSection);
}
private void ensureContentLoaded()
{
if (SectionsContainer.LoadState > LoadState.NotLoaded)
return;
Debug.Assert(SectionsContainer != null);
LoadComponentAsync(SectionsContainer, d =>
{
ContentContainer.Add(d);
d.FadeInFromZero(500);
loading.Hide();
if (Sidebar != null)
{
SectionsContainer.SelectedSection.ValueChanged += section => SectionsContainer.SelectedSection.ValueChanged += section =>
{ {
selectedSidebarButton.Selected = false; selectedSidebarButton.Selected = false;
@ -120,9 +148,9 @@ namespace osu.Game.Overlays
}; };
} }
searchTextBox.Current.ValueChanged += term => SectionsContainer.SearchContainer.SearchTerm = term.NewValue; searchTextBox.Current.BindValueChanged(term => SectionsContainer.SearchContainer.SearchTerm = term.NewValue, true);
searchTextBox.TakeFocus();
CreateSections()?.ForEach(AddSection); });
} }
protected void AddSection(SettingsSection section) protected void AddSection(SettingsSection section)
@ -136,6 +164,10 @@ namespace osu.Game.Overlays
Section = section, Section = section,
Action = () => Action = () =>
{ {
// may not be loaded yet.
if (SectionsContainer == null)
return;
SectionsContainer.ScrollTo(section); SectionsContainer.ScrollTo(section);
Sidebar.State = ExpandedState.Contracted; Sidebar.State = ExpandedState.Contracted;
}, },
@ -159,7 +191,8 @@ namespace osu.Game.Overlays
{ {
base.PopIn(); base.PopIn();
ContentContainer.MoveToX(ExpandedPosition, TRANSITION_LENGTH, Easing.OutQuint); ContentContainer.MoveToX(ExpandedPosition, TRANSITION_LENGTH, Easing.OutQuint)
.OnComplete(_ => ensureContentLoaded());
Sidebar?.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint); Sidebar?.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint);
this.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint); this.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);
@ -187,7 +220,7 @@ namespace osu.Game.Overlays
protected override void OnFocus(FocusEvent e) protected override void OnFocus(FocusEvent e)
{ {
searchTextBox.TakeFocus(); searchTextBox?.TakeFocus();
base.OnFocus(e); base.OnFocus(e);
} }
@ -209,6 +242,8 @@ namespace osu.Game.Overlays
{ {
public SearchContainer<SettingsSection> SearchContainer; public SearchContainer<SettingsSection> SearchContainer;
public new ScheduledDelegate Schedule(Action action) => Scheduler.AddDelayed(action, TransformDelay);
protected override FlowContainer<SettingsSection> CreateScrollContentContainer() protected override FlowContainer<SettingsSection> CreateScrollContentContainer()
=> SearchContainer = new SearchContainer<SettingsSection> => SearchContainer = new SearchContainer<SettingsSection>
{ {