aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI.Controls/ExtendedListBox.cs
blob: fb6738939e3415c0301ecaf5b09c350c0819a220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace MediaBrowser.UI.Controls
{
    /// <summary>
    /// Extends the ListBox to provide auto-focus behavior when items are moused over
    /// This also adds an ItemInvoked event that is fired when an item is clicked or invoked using the enter key
    /// </summary>
    public class ExtendedListBox : ListBox
    {
        /// <summary>
        /// Fired when an item is clicked or invoked using the enter key
        /// </summary>
        public event EventHandler<ItemEventArgs<object>> ItemInvoked;

        /// <summary>
        /// Called when [item invoked].
        /// </summary>
        /// <param name="boundObject">The bound object.</param>
        protected virtual void OnItemInvoked(object boundObject)
        {
            if (ItemInvoked != null)
            {
                ItemInvoked(this, new ItemEventArgs<object> { Argument = boundObject });
            }
        }

        /// <summary>
        /// The _auto focus
        /// </summary>
        private bool _autoFocus = true;
        /// <summary>
        /// Gets or sets a value indicating if the first list item should be auto-focused on load
        /// </summary>
        /// <value><c>true</c> if [auto focus]; otherwise, <c>false</c>.</value>
        public bool AutoFocus
        {
            get { return _autoFocus; }
            set
            {
                _autoFocus = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExtendedListBox" /> class.
        /// </summary>
        public ExtendedListBox()
            : base()
        {
            ItemContainerGenerator.StatusChanged += ItemContainerGeneratorStatusChanged;
        }

        /// <summary>
        /// The mouse down object
        /// </summary>
        private object mouseDownObject;

        /// <summary>
        /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.PreviewMouseDown" /> attached routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data. The event data reports that one or more mouse buttons were pressed.</param>
        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            // Get the item that the mouse down event occurred on
            mouseDownObject = GetBoundListItemObject((DependencyObject)e.OriginalSource);
        }

        /// <summary>
        /// Invoked when an unhandled <see cref="E:System.Windows.UIElement.MouseLeftButtonUp" /> routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs" /> that contains the event data. The event data reports that the left mouse button was released.</param>
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);

            // If the mouse up event occurred on the same item as the mousedown event, then fire ItemInvoked
            if (mouseDownObject != null)
            {
                var boundObject = GetBoundListItemObject((DependencyObject)e.OriginalSource);

                if (mouseDownObject == boundObject)
                {
                    mouseDownObject = null;
                    OnItemInvoked(boundObject);
                }
            }
        }

        /// <summary>
        /// The key down object
        /// </summary>
        private object keyDownObject;

        /// <summary>
        /// Responds to the <see cref="E:System.Windows.UIElement.KeyDown" /> event.
        /// </summary>
        /// <param name="e">Provides data for <see cref="T:System.Windows.Input.KeyEventArgs" />.</param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!e.IsRepeat)
                {
                    // Get the item that the keydown event occurred on
                    keyDownObject = GetBoundListItemObject((DependencyObject)e.OriginalSource);
                }

                e.Handled = true;
            }

            base.OnKeyDown(e);
        }

        /// <summary>
        /// Invoked when an unhandled <see cref="E:System.Windows.Input.Keyboard.KeyUp" /> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.Windows.Input.KeyEventArgs" /> that contains the event data.</param>
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);

            // Fire ItemInvoked when enter is pressed on an item
            if (e.Key == Key.Enter)
            {
                if (!e.IsRepeat)
                {
                    // If the keyup event occurred on the same item as the keydown event, then fire ItemInvoked
                    if (keyDownObject != null)
                    {
                        var boundObject = GetBoundListItemObject((DependencyObject)e.OriginalSource);

                        if (keyDownObject == boundObject)
                        {
                            keyDownObject = null;
                            OnItemInvoked(boundObject);
                        }
                    }
                }

                e.Handled = true;
            }
        }

        /// <summary>
        /// The _last mouse move point
        /// </summary>
        private Point? _lastMouseMovePoint;

        /// <summary>
        /// Handles OnMouseMove to auto-select the item that's being moused over
        /// </summary>
        /// <param name="e">Provides data for <see cref="T:System.Windows.Input.MouseEventArgs" />.</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            var window = this.GetWindow();

            // If the cursor is currently hidden, don't bother reacting to it
            if (Cursor == Cursors.None || window.Cursor == Cursors.None)
            {
                return;
            }

            // Store the last position for comparison purposes
            // Even if the mouse is not moving this event will fire as elements are showing and hiding
            var pos = e.GetPosition(window);

            if (!_lastMouseMovePoint.HasValue)
            {
                _lastMouseMovePoint = pos;
                return;
            }

            if (pos == _lastMouseMovePoint)
            {
                return;
            }

            _lastMouseMovePoint = pos;

            var dep = (DependencyObject)e.OriginalSource;

            while ((dep != null) && !(dep is ListBoxItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep != null)
            {
                var listBoxItem = dep as ListBoxItem;

                if (!listBoxItem.IsFocused)
                {
                    listBoxItem.Focus();
                }
            }
        }

        /// <summary>
        /// Gets the datacontext for a given ListBoxItem
        /// </summary>
        /// <param name="dep">The dep.</param>
        /// <returns>System.Object.</returns>
        private object GetBoundListItemObject(DependencyObject dep)
        {
            while ((dep != null) && !(dep is ListBoxItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
            {
                return null;
            }

            return ItemContainerGenerator.ItemFromContainer(dep);
        }

        /// <summary>
        /// Autofocuses the first list item when the list is loaded
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ItemContainerGeneratorStatusChanged(object sender, EventArgs e)
        {
            if (ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated && AutoFocus)
            {
                Dispatcher.InvokeAsync(OnContainersGenerated);
            }
        }

        /// <summary>
        /// Called when [containers generated].
        /// </summary>
        void OnContainersGenerated()
        {
            var index = 0;

            if (index >= 0)
            {
                var item = ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;

                if (item != null)
                {
                    item.Focus();
                    ItemContainerGenerator.StatusChanged -= ItemContainerGeneratorStatusChanged;
                }
            }
        }
    }
}