forked from ac87/GoogleAssistantWindows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
147 lines (122 loc) · 4.74 KB
/
MainWindow.xaml.cs
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
using System;
using System.Windows;
using System.Windows.Forms;
namespace GoogleAssistantWindows
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const int NormalHeight = 100;
private const int DebugHeight = 350;
private readonly UserManager _userManager;
private readonly Assistant _assistant;
private readonly KeyboardHook _hook;
private readonly NotifyIcon _notifyIcon;
private readonly AudioOut _audioOut;
private AssistantState _assistantState = AssistantState.Inactive;
public MainWindow()
{
InitializeComponent();
_audioOut = new AudioOut();
_hook = new KeyboardHook();
_hook.KeyDown += OnHookKeyDown;
void OnHookKeyDown(object sender, HookEventArgs e)
{
// Global keyboard hook for Ctrl+Alt+G to start listening.
if (e.Control && e.Alt && e.Key == Keys.G)
StartListening();
}
// When minimized it will hide in the tray. but the global keyboard hook should still work
_notifyIcon = new NotifyIcon();
_notifyIcon.Icon = new System.Drawing.Icon("Mic.ico");
_notifyIcon.Text = Title;
_notifyIcon.DoubleClick +=
delegate
{
_notifyIcon.Visible = false;
Show();
WindowState = WindowState.Normal;
};
_assistant = new Assistant();
_assistant.OnDebug += Output;
_assistant.OnAssistantStateChanged += OnAssistantStateChanged;
_userManager = UserManager.Instance;
_userManager.OnUserUpdate += OnUserUpdate;
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
_notifyIcon.Visible = true;
Hide();
}
base.OnStateChanged(e);
}
private void OnAssistantStateChanged(AssistantState state)
{
_assistantState = state;
UpdateButtonText(state);
}
private void UpdateButtonText(AssistantState state)
{
if (ButtonRecord.Dispatcher.CheckAccess())
ButtonRecord.Content = state == AssistantState.Inactive ? "Press" : state.ToString();
else
ButtonRecord.Dispatcher.BeginInvoke(new Action(()=>UpdateButtonText(state)));
}
private void OnUserUpdate(UserManager.GoogleUserData userData)
{
ButtonRecord.IsEnabled = false;
_assistant.Shutdown();
if (userData != null)
{
_assistant.InitAssistantForUser(_userManager.GetChannelCredential());
ButtonRecord.IsEnabled = true;
}
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
if (Utils.HasTokenFile())
_userManager.GetOrRefreshCredential(); // we don't need to wait for this UserManager will throw an event on loaded.
}
private void ButtonRecord_OnClick(object sender, RoutedEventArgs e)
{
StartListening();
}
private void StartListening()
{
if (_assistant.IsInitialised() && _assistantState == AssistantState.Inactive)
{
_assistant.NewConversation();
_audioOut.PlayNotification();
}
}
public void Output(string output, bool consoleOnly = false)
{
if (consoleOnly)
{
System.Diagnostics.Debug.WriteLine(output);
return;
}
if (ListBoxOutput.Dispatcher.CheckAccess())
{
System.Diagnostics.Debug.WriteLine(output);
// stop using memory for old debug lines.
if (ListBoxOutput.Items.Count > 500)
ListBoxOutput.Items.RemoveAt(0);
ListBoxOutput.Items.Add(output);
ListBoxOutput.ScrollIntoView(ListBoxOutput.Items[ListBoxOutput.Items.Count-1]);
if (output.StartsWith("Error") && Height == NormalHeight)
Height = DebugHeight;
}
else
ListBoxOutput.Dispatcher.BeginInvoke(new Action(() => Output(output)));
}
private void DebugButton_OnClick(object sender, RoutedEventArgs e)
{
Height = (Height == NormalHeight ? DebugHeight : NormalHeight);
}
}
}