Skip to content

Commit

Permalink
Merge pull request #103 from VPKSoft/fix_trackDeFocus
Browse files Browse the repository at this point in the history
Fix user idle focus loss on the track list
  • Loading branch information
Petteri Kautonen authored Aug 21, 2022
2 parents 3d9c8af + b98e3bf commit 2f78af4
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 96 deletions.
8 changes: 4 additions & 4 deletions EtoForms.Controls.Custom/UserIdle/UserIdleChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public bool IsUserIdle
if (!idleEventInvoked)
{
previousActiveTime = DateTime.Now;
UserActivated?.Invoke(this, new UserIdleEventArgs());
UserActivated?.Invoke(this, new UserIdleEventArgs(false));
}
else
{
UserIdle?.Invoke(this, new UserIdleEventArgs());
UserIdle?.Invoke(this, new UserIdleEventArgs(true));
}
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ private DateTime PreviousActiveTime
if (idleEventInvoked)
{
idleEventInvoked = false;
UserActivated?.Invoke(this, new UserIdleEventArgs());
UserActivated?.Invoke(this, new UserIdleEventArgs(false));
}
}
}
Expand All @@ -188,7 +188,7 @@ private void ThreadFunc()
if (spanSeconds > UserInactiveInterval)
{
idleEventInvoked = true;
UserIdle?.Invoke(this, new UserIdleEventArgs());
UserIdle?.Invoke(this, new UserIdleEventArgs(true));
}
else
{
Expand Down
14 changes: 14 additions & 0 deletions EtoForms.Controls.Custom/UserIdle/UserIdleEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,18 @@ namespace EtoForms.Controls.Custom.UserIdle;
/// <seealso cref="EventArgs" />
public class UserIdleEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="UserIdleEventArgs"/> class.
/// </summary>
/// <param name="isUserIdle">if set to <c>true</c> the user is currently idle.</param>
public UserIdleEventArgs(bool isUserIdle)
{
IsUserIdle = isUserIdle;
}

/// <summary>
/// Gets a value indicating whether the user is currently idle.
/// </summary>
/// <value><c>true</c> if the user is currently idle; otherwise, <c>false</c>.</value>
private bool IsUserIdle { get; }
}
3 changes: 1 addition & 2 deletions amp.EtoForms/Dialogs/DialogAddFilesProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.ComponentModel;
using amp.Database;
using amp.Database.DataModel;
using amp.EtoForms.Utilities;
using amp.Shared.Classes;
using amp.Shared.Constants;
using amp.Shared.Localization;
Expand Down Expand Up @@ -172,7 +171,7 @@ private DialogAddFilesProgress(AmpContext context, long albumId)

private void DialogAddFilesProgress_Closed(object? sender, EventArgs e)
{
defaultCancelButtonHandler.Dispose();
defaultCancelButtonHandler?.Dispose();
}

#region InternalEvents
Expand Down
1 change: 0 additions & 1 deletion amp.EtoForms/Dialogs/DialogCheckNewVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System.Globalization;
using System.Text;
using amp.EtoForms.Utilities;
using amp.Shared.Classes;
using amp.Shared.Localization;
using amp.Shared.UpdateCheck;
Expand Down
1 change: 0 additions & 1 deletion amp.EtoForms/Dialogs/DialogDatabaseConvertProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System.ComponentModel;
using amp.Database.LegacyConvert;
using amp.EtoForms.Utilities;
using amp.Shared.Localization;
using Eto.Drawing;
using Eto.Forms;
Expand Down
1 change: 0 additions & 1 deletion amp.EtoForms/Dialogs/DialogUpdateTagData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using amp.Database;
using amp.EtoForms.DtoClasses;
using amp.EtoForms.Utilities;
using amp.Shared.Classes;
using amp.Shared.Localization;
using Eto.Drawing;
Expand Down
31 changes: 19 additions & 12 deletions amp.EtoForms/FormMain.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ await playbackOrder.MoveToQueueTopOrBottom(tracks, shift, e.Key == Keys.PageUp,

if (e.Key == Keys.Delete)
{
await gvAudioTracks.DeleteSongs(context, tracks, FilterTracks);
await gvAudioTracks.DeleteSongs(context, tracks, () => FilterTracks(false));

e.Handled = true;
return;
Expand Down Expand Up @@ -175,12 +175,12 @@ private void FormMain_LocationChanged(object? sender, EventArgs e)

private void TbSearch_TextChanged(object? sender, EventArgs e)
{
FilterTracks();
FilterTracks(false);
}

private void BtnShowQueue_CheckedChange(object? sender, CheckedChangeEventArguments e)
{
FilterTracks();
FilterTracks(false);
}

private void FormMain_Closing(object? sender, CancelEventArgs e)
Expand Down Expand Up @@ -272,14 +272,7 @@ private void PlaybackManagerTrackChanged(object? sender, TrackChangedArgs e)
lbTracksTitle.Text = track?.GetAudioTrackName() ?? string.Empty;
currentTrackId = track != null ? e.AudioTrackId : 0;
var dataSource = gvAudioTracks.DataStore.Cast<AlbumTrack>().ToList();
var displayTrack = dataSource.FindIndex(f => f.AudioTrackId == e.AudioTrackId);
if (displayTrack != -1)
{
gvAudioTracks.SelectedRow = displayTrack;
gvAudioTracks.ScrollToRow(displayTrack);
gvAudioTracks.Focus();
}
FocusPlayingTrack(e.AudioTrackId);
if (track != null && Globals.Settings.ShowAlbumImage)
{
Expand Down Expand Up @@ -419,7 +412,7 @@ private void AddFilesToDatabase_Executed(object? sender, EventArgs e)

private void IdleChecker_UserIdleChanged(object? sender, UserIdleEventArgs e)
{
FilterTracks();
FilterTracks(true);
}

private void FormMain_Shown(object? sender, EventArgs e)
Expand Down Expand Up @@ -821,4 +814,18 @@ private void OpenHelp_Executed(object? sender, EventArgs e)
{
Help.LaunchHelpFromSettings(this);
}

private void CmbAlbumSelect_SelectedIndexChanged(object? sender, EventArgs e)
{
if (suspendAlbumChange || cmbAlbumSelect.SelectedIndex < 0)
{
return;
}

Globals.LoggerSafeInvoke(() =>
{
CurrentAlbumId = albums[cmbAlbumSelect.SelectedIndex].Id;
RefreshCurrentAlbum();
});
}
}
4 changes: 4 additions & 0 deletions amp.EtoForms/FormMain.Fields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ partial class FormMain

// Update check
private readonly UITimer timerCheckUpdates = new() { Interval = 15, };

// User idle
private List<int> userIdleSelectedRows = new();
private int userIdleSelectedRow = -1;
}
14 changes: 0 additions & 14 deletions amp.EtoForms/FormMain.Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,6 @@ private Control CreateAlbumSelector()
return result;
}

private void CmbAlbumSelect_SelectedIndexChanged(object? sender, EventArgs e)
{
if (suspendAlbumChange || cmbAlbumSelect.SelectedIndex < 0)
{
return;
}

Globals.LoggerSafeInvoke(() =>
{
CurrentAlbumId = albums[cmbAlbumSelect.SelectedIndex].Id;
RefreshCurrentAlbum();
});
}

private StackLayout CreateToolbar()
{
var result = new StackLayout
Expand Down
47 changes: 45 additions & 2 deletions amp.EtoForms/FormMain.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private async Task UpdateQueueFunc(Dictionary<long, int> updateQueueData, bool a

if (btnShowQueue.Checked && count > 0)
{
FilterTracks();
FilterTracks(false);
}

UpdateCounters();
Expand Down Expand Up @@ -263,14 +263,26 @@ private void SetupInitialSettings()
TrackDisplayNameGenerate.TrackNamingFallbackToFileNameWhenNoLetters = Globals.Settings.TrackNamingFallbackToFileNameWhenNoLetters;
}

private void FilterTracks()
private void FilterTracks(bool fromUserIdleEvent)
{
Application.Instance.Invoke(() =>
{
if (!fromUserIdleEvent)
{
userIdleSelectedRows.Clear();
userIdleSelectedRow = -1;
}
var text = tbSearch.Text;
var queueOnly = btnShowQueue.Checked;
var userIdle = idleChecker.IsUserIdle;
// The user went idle, save the current selection.
if (userIdle && fromUserIdleEvent)
{
userIdleSelectedRows = gvAudioTracks.SelectedRows.ToList();
userIdleSelectedRow = gvAudioTracks.SelectedRow;
}
filteredTracks = tracks;
Expand All @@ -289,12 +301,25 @@ private void FilterTracks()
}
gvAudioTracks.DataStore = filteredTracks;
if (!userIdle && userIdleSelectedRow != -1 && fromUserIdleEvent)
{
gvAudioTracks.SelectedRows = userIdleSelectedRows;
gvAudioTracks.SelectedRow = userIdleSelectedRow;
gvAudioTracks.ScrollToRow(userIdleSelectedRow);
}
UpdateCounters();
if (userIdle && !gvAudioTracks.HasFocus)
{
gvAudioTracks.Focus();
}
if (userIdle)
{
FocusPlayingTrack(currentTrackId, userIdleSelectedRow);
}
});
}

Expand Down Expand Up @@ -474,4 +499,22 @@ private async Task UpdateCheck(bool autoCheck)
Globals.SaveSettings();
}
}

private void FocusPlayingTrack(long trackId, int compareToRow = -1)
{
var dataSource = gvAudioTracks.DataStore.Cast<AlbumTrack>().ToList();
var displayTrack = dataSource.FindIndex(f => f.AudioTrackId == trackId);

if (compareToRow != -1 && displayTrack == compareToRow)
{
return;
}

if (displayTrack != -1)
{
gvAudioTracks.SelectedRow = displayTrack;
gvAudioTracks.ScrollToRow(displayTrack);
gvAudioTracks.Focus();
}
}
}
2 changes: 2 additions & 0 deletions amp.EtoForms/FormMain.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ private long CurrentAlbumId
Globals.Settings.SelectedAlbum = value;
Globals.SaveSettings();
SetTitle();
userIdleSelectedRow = -1;
userIdleSelectedRows.Clear();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion amp.EtoForms/Forms/FormAlbums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using EtoForms.Controls.Custom.Utilities;
using FluentIcons.Resources.Filled;
using Microsoft.EntityFrameworkCore;
using amp.EtoForms.Utilities;

namespace amp.EtoForms.Forms;

Expand Down
1 change: 0 additions & 1 deletion amp.EtoForms/Forms/FormColorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Globalization;
using System.Reflection;
using amp.EtoForms.Settings;
using amp.EtoForms.Utilities;
using amp.Shared.Extensions;
using amp.Shared.Localization;
using Eto.Drawing;
Expand Down
1 change: 0 additions & 1 deletion amp.EtoForms/Forms/FormDialogTrackInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using amp.EtoForms.DtoClasses;
using amp.EtoForms.Forms.EventArguments;
using amp.EtoForms.Utilities;
using amp.Shared.Classes;
using amp.Shared.Localization;
using ATL;
Expand Down
2 changes: 1 addition & 1 deletion amp.EtoForms/amp.EtoForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<DocumentationFile>bin\$(Configuration)\amp.EtoForms.xml</DocumentationFile>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
<Version>1.0.0.1</Version>
<Version>1.0.0.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(IsOSX)'=='true'">
Expand Down
Loading

0 comments on commit 2f78af4

Please sign in to comment.