Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
111 lines (101 loc) · 3.06 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
111 lines (101 loc) · 3.06 KB
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
using System;
using System.IO;
using System.Text;
#if WPF
using System.Windows;
using System.Windows.Controls;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
#endif
using Newtonsoft.Json;
using PilotAIAssistantControl;
using TestWPFApp;
#if WPF
namespace TestWPFApp {
#else
namespace TestWinUIApp {
#endif
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public static MainWindow? Instance;
private bool _isLoaded;
public MainWindow() {
Instance = this;
InitializeComponent();
#if WPF
Loaded += MainWindow_Loaded;
Closing += MainWindow_Closing;
#else
Activated += Window_Activated;
Closed += MainWindow_Closed;
#endif
}
#if WPF
private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e) {
SaveConfig();
}
#else
private void MainWindow_Closed(object sender, WindowEventArgs args) {
SaveConfig();
}
public TextBox txtTestPublic => txtTest;
public TextBox txtRegexPublic => txtRegex;
//public
#endif
private void SaveConfig() {
var data = ucAi.AiControl.ExportData();
File.WriteAllText(json_config, JsonConvert.SerializeObject(data));
}
#if WPF
private static string json_config = @"test_exported_data.json";
#else
private static string json_config = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"test_exported_data.json");
#endif
private OurOptions GeneratorAgentOpts;
private SimpleExplainerOptions QueryAgentOpts;
#if WPF
private void MainWindow_Loaded(object sender, RoutedEventArgs e) {
#else
private void Window_Activated(object sender, WindowActivatedEventArgs e) {
#endif
if (_isLoaded)
return;
_isLoaded = true;
this.GeneratorAgentOpts = new OurOptions();
this.QueryAgentOpts = new SimpleExplainerOptions();
ucAi.AiControl.Configure(GeneratorAgentOpts);
if (File.Exists(json_config)) {
var data = JsonConvert.DeserializeObject<AIUserConfig>(File.ReadAllText(json_config));
ucAi.AiControl.ImportData(data);
}
var args = Environment.GetCommandLineArgs();
var pos = Array.IndexOf(args,"--load");
if (pos > -1)
txtTest.Text = File.ReadAllText(args[pos+1]);
pos = Array.IndexOf(args,"--add_ai_msg_from_file");
if (pos > -1)
ucAi.AddAIResponseToChat(File.ReadAllText(args[pos+1]));
ucAi.ExpanderControl.IsExpanded = true;
}
private void agentCombo_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if (!_isLoaded)
return;
AIOptions newAgent = agentCombo.SelectedItem?.ToString()?.Contains("Explain") == true ? QueryAgentOpts : GeneratorAgentOpts;
ucAi.Configure(newAgent);
}
private async void ExplainRegex_Click(object sender, RoutedEventArgs e) {
agentCombo.SelectedIndex = 1; // Switch to Explain agent
await ucAi.SendMessage($"`{txtRegex.Text}`");
}
#if !WPF
private void ToggleTheme_Click(object sender, RoutedEventArgs e) {
if (Content is FrameworkElement root) {
root.RequestedTheme = root.ActualTheme == ElementTheme.Dark ? ElementTheme.Light : ElementTheme.Dark;
}
}
#endif
}
}