Skip to content

Commit

Permalink
fix(performance): improved ui responsibility while switching between …
Browse files Browse the repository at this point in the history
…pages
  • Loading branch information
AlexanderDotH committed Jun 7, 2023
1 parent 1f7b9d1 commit 76d7b82
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Avalonia.Controls;

namespace OpenLyricsClient.Frontend.Events.EventArgs;

public class PageSelectionChangedEventArgs : System.EventArgs
{
private UserControl _fromPage;
private UserControl _toPage;

public PageSelectionChangedEventArgs(UserControl fromPage, UserControl toPage)
{
this._fromPage = fromPage;
this._toPage = toPage;
}

public UserControl FromPage
{
get => _fromPage;
}

public UserControl ToPage
{
get => _toPage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;
using OpenLyricsClient.Frontend.Events.EventArgs;

namespace OpenLyricsClient.Frontend.Events.EventHandler;

public delegate void PageSelectionChangedEventHandler(Object sender, PageSelectionChangedEventArgs pageSelectionChanged);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;
using OpenLyricsClient.Frontend.Events.EventArgs;

namespace OpenLyricsClient.Frontend.Events.EventHandler;

public delegate void PageSelectionChangedFinishedEventHandler(Object sender, PageSelectionChangedEventArgs pageSelectionChanged);
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
using OpenLyricsClient.Backend.Events.EventArgs;
using OpenLyricsClient.Backend.Handler.Services.Services;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Frontend.Events.EventArgs;
using OpenLyricsClient.Frontend.Extensions;
using OpenLyricsClient.Frontend.Models.Pages.Settings;
using OpenLyricsClient.Frontend.Structure.Enum;
using OpenLyricsClient.Frontend.Utils;
using OpenLyricsClient.Frontend.View.Pages;
using OpenLyricsClient.Frontend.View.Windows;
using OpenLyricsClient.Shared.Structure.Lyrics;
using OpenLyricsClient.Shared.Utils;
using Squalr.Engine.Utils.Extensions;
Expand Down Expand Up @@ -88,13 +91,32 @@ public NoteOverlay()
AvaloniaXamlLoader.Load(this);

ApplyAnimationToClasses(this._idleTimeSpan, this._noteTimeSpan);

MainWindow.Instance.PageSelectionChanged += InstanceOnPageSelectionChanged;
MainWindow.Instance.PageSelectionChangedFinished += InstanceOnPageSelectionChangedFinished;
}

private void InstanceOnPageSelectionChanged(object sender, PageSelectionChangedEventArgs pageselectionchanged)
{
if (pageselectionchanged.ToPage.GetType() == typeof(SettingsPage))
{
this.Headless = true;
}
}

private void InstanceOnPageSelectionChangedFinished(object sender, PageSelectionChangedEventArgs pageselectionchanged)
{
if (pageselectionchanged.ToPage.GetType() == typeof(LyricsPage))
{
this.Headless = false;
}
}

#region Events

private void LyricHandlerOnLyricsFound(object sender, LyricsFoundEventArgs lyricsfoundeventargs)
{
if (this._headlessMode)
if (this.Headless)
return;

Dispatcher.UIThread.InvokeAsync(() =>
Expand All @@ -105,7 +127,7 @@ private void LyricHandlerOnLyricsFound(object sender, LyricsFoundEventArgs lyric

private void SettingsHandlerOnSettingsChanged(object sender, SettingsChangedEventArgs args)
{
if (this._headlessMode)
if (this.Headless)
return;

if (!args.Section.Equals(typeof(SettingsLyricsViewModel)))
Expand All @@ -117,7 +139,7 @@ private void SettingsHandlerOnSettingsChanged(object sender, SettingsChangedEven

private void LyricHandlerOnLyricsPercentageUpdated(object sender, LyricsPercentageUpdatedEventArgs args)
{
if (this._headlessMode)
if (this.Headless)
return;

if (this._lyricPart.Equals(args.LyricPart))
Expand All @@ -134,6 +156,8 @@ private void LyricHandlerOnLyricsPercentageUpdated(object sender, LyricsPercenta

private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
MainWindow.Instance.WindowDragable = false;

IService service = Core.INSTANCE.ServiceHandler.GetActiveService();

if (!DataValidator.ValidateData(service))
Expand All @@ -145,6 +169,8 @@ private void InputElement_OnPointerPressed(object? sender, PointerPressedEventAr

private void InputElement_OnPointerEnter(object? sender, PointerEventArgs e)
{
MainWindow.Instance.WindowDragable = true;

IService service = Core.INSTANCE.ServiceHandler.GetActiveService();

if (!DataValidator.ValidateData(service))
Expand Down Expand Up @@ -177,7 +203,7 @@ private void InputElement_OnPointerLeave(object? sender, PointerEventArgs e)

private void ApplyAnimationToClasses(TimeSpan idleTimeSpan, TimeSpan noteTimeSpan)
{
if (this._headlessMode)
if (this.Headless)
return;

Styles styles = new Styles();
Expand Down Expand Up @@ -282,7 +308,7 @@ private Style ActiveAnimationStyle(string className, TimeSpan duration, TimeSpan
private void ApplyDelay(string classes, TimeSpan span)
{
double h = span.TotalMilliseconds / 3;
double factor = (h / (3 * 4)) * 0.01d;
double factor = (h / (3 * 6)) * 0.01d;

int position = 0;
for (int i = 0; i < this._animatale.Length; i++)
Expand All @@ -292,7 +318,6 @@ private void ApplyDelay(string classes, TimeSpan span)
if (element.Item1.SequenceEqual($"{classes}{position + 1}"))
{
element.Item2.Delay = TimeSpan.FromMilliseconds(position * factor);
Debug.WriteLine($"{element.Item2.Delay} : {factor} : {i}");
position++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
using OpenLyricsClient.Backend.Handler.Services.Services;
using OpenLyricsClient.Backend.Romanization;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Frontend.Events.EventArgs;
using OpenLyricsClient.Frontend.Extensions;
using OpenLyricsClient.Frontend.Structure;
using OpenLyricsClient.Frontend.Utils;
using OpenLyricsClient.Frontend.View.Pages;
using OpenLyricsClient.Frontend.View.Windows;
using OpenLyricsClient.Shared.Structure.Lyrics;
using OpenLyricsClient.Shared.Structure.Visual;
using OpenLyricsClient.Shared.Utils;
Expand Down Expand Up @@ -66,13 +69,19 @@ public partial class TextOverlay : UserControl, INotifyPropertyChanged
private bool _isPointerOver;

private bool _headlessMode;
private bool _suppressActivity;

private readonly int LEFT_SPACE;

public TextOverlay()
{
AvaloniaXamlLoader.Load(this);

LEFT_SPACE = 50;

this._initialized = false;
this.Headless = false;
this.SuppressActivity = false;

this._isPointerOver = false;

Expand All @@ -90,11 +99,35 @@ public TextOverlay()
Core.INSTANCE.LyricHandler.LyricsPercentageUpdated += LyricHandlerOnLyricsPercentageUpdated;

this._lyricPart = new LyricPart(-9999, "Hello there ;)");

MainWindow.Instance.PageSelectionChanged += InstanceOnPageSelectionChanged;
MainWindow.Instance.PageSelectionChangedFinished += InstanceOnPageSelectionChangedFinished;
}

private void InstanceOnPageSelectionChanged(object sender, PageSelectionChangedEventArgs pageselectionchanged)
{
if (pageselectionchanged.ToPage.GetType() == typeof(SettingsPage))
{
this.Headless = true;
this.SuppressActivity = true;
}
}

private void InstanceOnPageSelectionChangedFinished(object sender, PageSelectionChangedEventArgs pageselectionchanged)
{
if (pageselectionchanged.ToPage.GetType() == typeof(LyricsPage))
{
this.Headless = false;
this.SuppressActivity = false;
}
}

private void UpdateView(double width, double height)
{
UpdateTextWrappingLines(this._lyricPart.Part, width, height);
if (this.SuppressActivity)
return;

UpdateTextWrappingLines(this._lyricPart.Part, NewLyricsScroller.Instance.Bounds.Width - LEFT_SPACE, height);
}

private void UpdateTextWrappingLines(string text, double width, double height)
Expand Down Expand Up @@ -176,9 +209,6 @@ private void LyricHandlerOnLyricsPercentageUpdated(object sender, LyricsPercenta

private void InstanceOnEffectiveViewportChanged(object? sender, EffectiveViewportChangedEventArgs e)
{
if (this._headlessMode)
return;

UpdateView(e.EffectiveViewport.Width, e.EffectiveViewport.Height);
}

Expand All @@ -195,6 +225,8 @@ private void InputElement_OnPointerPressed(object? sender, PointerPressedEventAr

private void InputElement_OnPointerEnter(object? sender, PointerEventArgs e)
{
MainWindow.Instance.WindowDragable = false;

IService service = Core.INSTANCE.ServiceHandler.GetActiveService();

if (!DataValidator.ValidateData(service))
Expand All @@ -209,6 +241,8 @@ private void InputElement_OnPointerEnter(object? sender, PointerEventArgs e)

private void InputElement_OnPointerLeave(object? sender, PointerEventArgs e)
{
MainWindow.Instance.WindowDragable = true;

IService service = Core.INSTANCE.ServiceHandler.GetActiveService();

if (!DataValidator.ValidateData(service))
Expand Down Expand Up @@ -255,7 +289,10 @@ public LyricPart LyricPart

SetAndRaise(LyricPartProperty, ref _lyricPart, value);

UpdateTextWrappingLines(this._lyricPart.Part, NewLyricsScroller.Instance.Bounds.Width,
if (this.SuppressActivity)
return;

UpdateTextWrappingLines(this._lyricPart.Part, NewLyricsScroller.Instance.Bounds.Width - LEFT_SPACE,
double.PositiveInfinity);

this._initialized = true;
Expand Down Expand Up @@ -332,7 +369,13 @@ public bool Headless
get => this._headlessMode;
set => this._headlessMode = value;
}


public bool SuppressActivity
{
get => _suppressActivity;
set => _suppressActivity = value;
}

public Size Size
{
get
Expand Down
18 changes: 17 additions & 1 deletion OpenLyricsClient/Frontend/View/Pages/LyricsPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Material.Styles;
using OpenLyricsClient.Backend;
using OpenLyricsClient.Backend.Settings.Sections.Lyrics;
using OpenLyricsClient.Frontend.Events.EventArgs;
using OpenLyricsClient.Shared.Utils;
using OpenLyricsClient.Frontend.Models.Pages;
using OpenLyricsClient.Frontend.View.Custom;
Expand Down Expand Up @@ -89,9 +90,24 @@ public LyricsPage()
if (!DataValidator.ValidateData(this._lyricsPageViewModel))
return;

MainWindow.Instance.PageSelectionChanged += InstanceOnPageSelectionChanged;

this._lyricsPageViewModel.PropertyChanged += DataContextOnPropertyChanged;
}

private void InstanceOnPageSelectionChanged(object sender, PageSelectionChangedEventArgs pageselectionchanged)
{
/*if (pageselectionchanged.FromPage.GetType() == typeof(LyricsPage))
{
this._cstmLyricsDisplay.IsVisible = false;
}
else
{
this._cstmLyricsDisplay.IsVisible = true;
}*/
}

private void DataContextOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (!DataValidator.ValidateData(e))
Expand Down Expand Up @@ -150,7 +166,7 @@ private void InputElement_OnPointerLeave(object? sender, PointerEventArgs e)

private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
MainWindow.Instance.BeginMoveDrag(e);
MainWindow.Instance.DragWindow(e);
}

private void Button_OnClick(object? sender, RoutedEventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions OpenLyricsClient/Frontend/View/Windows/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@
</Style>
</Carousel.Styles>

<pages:LyricsPage/>
<pages:LyricsPage Name="LyricsPage"/>

<pages:FullLyricsPage/>
<pages:FullLyricsPage Name="FullLyricsPage"/>

<pages:SettingsPage/>
<pages:SettingsPage Name="SettingsPage"/>

</Carousel>
</DockPanel>
Expand Down
Loading

0 comments on commit 76d7b82

Please sign in to comment.