1
+ using System . Reactive ;
2
+
3
+ using ReactiveUI ;
4
+
5
+ using TwitchStreamingTools . Services ;
6
+ using TwitchStreamingTools . ViewModels ;
7
+
8
+ namespace TwitchStreamingTools . Controls . ViewModels ;
9
+
10
+ /// <summary>
11
+ /// Handles storing information required to visualize a keybind.
12
+ /// </summary>
13
+ public class KeybindViewModel : ViewModelBase {
14
+ /// <summary>
15
+ /// The listener for keystrokes on the keyboard.
16
+ /// </summary>
17
+ private readonly IGlobalKeyPressService _service ;
18
+
19
+ /// <summary>
20
+ /// The keybind, if set.
21
+ /// </summary>
22
+ private Keybind ? _keybind ;
23
+
24
+ /// <summary>
25
+ /// Initializes a new instance of the <see cref="KeybindViewModel" /> class.
26
+ /// </summary>
27
+ /// <param name="service">The listener for keystrokes on the keyboard.</param>
28
+ public KeybindViewModel ( IGlobalKeyPressService service ) {
29
+ _service = service ;
30
+ ListenForKeystroke = ReactiveCommand . Create ( StartListenKeystroke ) ;
31
+ }
32
+
33
+ /// <summary>
34
+ /// The keybind.
35
+ /// </summary>
36
+ public Keybind ? Keybind {
37
+ get => _keybind ;
38
+ set => this . RaiseAndSetIfChanged ( ref _keybind , value ) ;
39
+ }
40
+
41
+ /// <summary>
42
+ /// Listens for keystrokes.
43
+ /// </summary>
44
+ public ReactiveCommand < Unit , Unit > ListenForKeystroke { get ; }
45
+
46
+ /// <summary>
47
+ /// Starts listening for keystrokes.
48
+ /// </summary>
49
+ private void StartListenKeystroke ( ) {
50
+ _service . OnKeystroke -= OnKeystroke ;
51
+ _service . OnKeystroke += OnKeystroke ;
52
+ }
53
+
54
+ /// <summary>
55
+ /// Called whenever a keystroke is pressed.
56
+ /// </summary>
57
+ /// <param name="keybind">The key that was press.</param>
58
+ private void OnKeystroke ( Keybind keybind ) {
59
+ if ( _service . IsModifier ( keybind . Key ) ) {
60
+ return ;
61
+ }
62
+
63
+ Keybind = keybind ;
64
+ _service . OnKeystroke -= OnKeystroke ;
65
+ }
66
+ }
0 commit comments