-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
405 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using FoxTunes.Interfaces; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace FoxTunes | ||
{ | ||
public class PlaylistSearchBehaviour : StandardBehaviour | ||
{ | ||
public IPlaylistManager PlaylistManager { get; private set; } | ||
|
||
public IPlaylistBrowser PlaylistBrowser { get; private set; } | ||
|
||
public override void InitializeComponent(ICore core) | ||
{ | ||
this.PlaylistManager = core.Managers.Playlist; | ||
this.PlaylistManager.FilterChanged += this.OnFilterChanged; | ||
this.PlaylistBrowser = core.Components.PlaylistBrowser; | ||
base.InitializeComponent(core); | ||
} | ||
|
||
protected virtual void OnFilterChanged(object sender, EventArgs e) | ||
{ | ||
if (this.PlaylistManager.SelectedPlaylist == null || string.IsNullOrEmpty(this.PlaylistManager.Filter)) | ||
{ | ||
return; | ||
} | ||
this.Dispatch(this.Search); | ||
} | ||
|
||
public void Search() | ||
{ | ||
var playlistItems = this.PlaylistBrowser.GetItems(this.PlaylistManager.SelectedPlaylist, this.PlaylistManager.Filter); | ||
if (playlistItems != null && playlistItems.Any()) | ||
{ | ||
this.PlaylistManager.SelectedItems = playlistItems; | ||
} | ||
} | ||
|
||
public bool IsDisposed { get; private set; } | ||
|
||
public void Dispose() | ||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (this.IsDisposed || !disposing) | ||
{ | ||
return; | ||
} | ||
this.OnDisposing(); | ||
this.IsDisposed = true; | ||
} | ||
|
||
protected virtual void OnDisposing() | ||
{ | ||
if (this.PlaylistManager != null) | ||
{ | ||
this.PlaylistManager.FilterChanged -= this.OnFilterChanged; | ||
} | ||
} | ||
|
||
~PlaylistSearchBehaviour() | ||
{ | ||
try | ||
{ | ||
this.Dispose(true); | ||
} | ||
catch | ||
{ | ||
//Nothing can be done, never throw on GC thread. | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
FoxTunes.UI.Windows/Extensions/ListView_EnsureSelectedItemVisible.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
|
||
namespace FoxTunes | ||
{ | ||
public static partial class ListViewExtensions | ||
{ | ||
private static readonly ConditionalWeakTable<ListView, EnsureSelectedItemVisibleBehaviour> EnsureSelectedItemVisibleBehaviours = new ConditionalWeakTable<ListView, EnsureSelectedItemVisibleBehaviour>(); | ||
|
||
public static readonly DependencyProperty EnsureSelectedItemVisibleProperty = DependencyProperty.RegisterAttached( | ||
"EnsureSelectedItemVisible", | ||
typeof(bool), | ||
typeof(ListViewExtensions), | ||
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEnsureSelectedItemVisiblePropertyChanged)) | ||
); | ||
|
||
public static bool GetEnsureSelectedItemVisible(ListView source) | ||
{ | ||
return (bool)source.GetValue(EnsureSelectedItemVisibleProperty); | ||
} | ||
|
||
public static void SetEnsureSelectedItemVisible(ListView source, bool value) | ||
{ | ||
source.SetValue(EnsureSelectedItemVisibleProperty, value); | ||
} | ||
|
||
private static void OnEnsureSelectedItemVisiblePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | ||
{ | ||
var listView = sender as ListView; | ||
if (listView == null) | ||
{ | ||
return; | ||
} | ||
if (GetEnsureSelectedItemVisible(listView)) | ||
{ | ||
var behaviour = default(EnsureSelectedItemVisibleBehaviour); | ||
if (!EnsureSelectedItemVisibleBehaviours.TryGetValue(listView, out behaviour)) | ||
{ | ||
EnsureSelectedItemVisibleBehaviours.Add(listView, new EnsureSelectedItemVisibleBehaviour(listView)); | ||
} | ||
} | ||
else | ||
{ | ||
var behaviour = default(EnsureSelectedItemVisibleBehaviour); | ||
if (EnsureSelectedItemVisibleBehaviours.TryGetValue(listView, out behaviour)) | ||
{ | ||
EnsureSelectedItemVisibleBehaviours.Remove(listView); | ||
behaviour.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
private class EnsureSelectedItemVisibleBehaviour : UIBehaviour | ||
{ | ||
public EnsureSelectedItemVisibleBehaviour(ListView listView) | ||
{ | ||
this.ListView = listView; | ||
this.ListView.SelectionChanged += this.OnSelectionChanged; | ||
} | ||
|
||
public ListView ListView { get; private set; } | ||
|
||
protected virtual void EnsureVisible(object value) | ||
{ | ||
if (value == null) | ||
{ | ||
return; | ||
} | ||
var index = this.ListView.Items.IndexOf(value); | ||
if (index < 0) | ||
{ | ||
return; | ||
} | ||
var scrollViewer = this.ListView.FindChild<ScrollViewer>(); | ||
if (scrollViewer != null) | ||
{ | ||
if (scrollViewer.ScrollToItemOffset<ListViewItem>(index, this.OnItemLoaded)) | ||
{ | ||
this.ListView.UpdateLayout(); | ||
} | ||
} | ||
var item = this.ListView.ItemContainerGenerator.ContainerFromItem(value) as ListViewItem; | ||
if (item != null) | ||
{ | ||
item.BringIntoView(); | ||
} | ||
} | ||
|
||
protected virtual void OnItemLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
this.EnsureVisible(this.ListView.SelectedItem); | ||
} | ||
|
||
protected virtual void OnSelectionChanged(object sender, SelectionChangedEventArgs e) | ||
{ | ||
this.EnsureVisible(this.ListView.SelectedItem); | ||
} | ||
|
||
protected override void OnDisposing() | ||
{ | ||
if (this.ListView != null) | ||
{ | ||
this.ListView.SelectionChanged -= this.OnSelectionChanged; | ||
} | ||
base.OnDisposing(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.