This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MainWindow.xaml.cs
289 lines (237 loc) · 9.61 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using AvalonEditB;
using Functionalities;
using HandyControl.Themes;
using HandyControl.Tools;
using SkEditorPlus.Managers;
using SkEditorPlus.Windows;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using Window = HandyControl.Controls.Window;
namespace SkEditorPlus
{
public delegate void LoadFinishedEvent();
public partial class MainWindow : Window
{
public SkEditorAPI skEditor;
private FileManager fileManager;
private readonly string startupFile;
public Menu GetMenu()
{
return MenuBar;
}
[DllImport("user32.dll")]
static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
const uint GW_HWNDNEXT = 2;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
public event LoadFinishedEvent LoadFinished;
private static readonly string version = "1.4.1";
public static string Version { get => version; }
public MainWindow(SkEditorAPI skEditor)
{
try
{
NamedPipeManager pipeManager = new("SkEditor+");
pipeManager.StartServer();
pipeManager.ReceiveString += HandleNamedPipe_OpenRequest;
}
catch { }
startupFile = skEditor.GetStartupFile();
this.skEditor = skEditor;
Process process = Process.GetCurrentProcess();
InitializeComponent();
}
async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
string appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\SkEditor Plus";
if (!Directory.Exists(appPath))
{
Directory.CreateDirectory(appPath);
}
if (!File.Exists(appPath + @"\SkriptHighlighting.xshd") || !File.Exists(appPath + @"\YAMLHighlighting.xshd"))
{
string noFilesTitle = (string)FindResource("NoFilesTitle");
string noFilesDescription = (string)FindResource("NoFilesDescription");
string noFilesDownloaded = (string)FindResource("NoFilesDownloaded");
OptionsWindow optionsWindow = new(skEditor);
HandyControl.Controls.MessageBox.Show(noFilesDescription, noFilesTitle, MessageBoxButton.OK, MessageBoxImage.Information);
IsEnabled = false;
await optionsWindow.UpdateSyntaxFile().ContinueWith((t) =>
{
Dispatcher.Invoke(() =>
{
IsEnabled = true;
HandyControl.Controls.MessageBox.Show(noFilesDownloaded, noFilesTitle, MessageBoxButton.OK, MessageBoxImage.Information);
});
});
}
string countryName = RegionInfo.CurrentRegion.Name;
if (countryName.Equals("PL"))
{
ConfigHelper.Instance.SetLang("pl");
}
else
{
ConfigHelper.Instance.SetLang("en");
}
string lang = Properties.Settings.Default.Language;
string appDirectory = Path.GetDirectoryName(Application.ResourceAssembly.Location);
string langFile = appDirectory + @"\Languages\" + lang + ".xaml";
if (File.Exists(langFile))
{
ResourceDictionary dict = new()
{
Source = new Uri(langFile, UriKind.RelativeOrAbsolute)
};
Application.Current.Resources.MergedDictionaries.Add(dict);
}
else
{
Properties.Settings.Default.Language = "English";
Properties.Settings.Default.Save();
}
SetUpMica();
BackgroundFixManager.FixBackground(this);
fileManager = new FileManager(skEditor);
new FunctionalitiesManager().LoadAll(skEditor);
RPCManager.Initialize();
if (startupFile != null)
{
fileManager.NewFile();
fileManager.GetTextEditor().Load(startupFile);
TabItem currentTabItem = tabControl.SelectedItem as TabItem;
currentTabItem.ToolTip = startupFile;
currentTabItem.Header = Path.GetFileName(startupFile);
fileManager.OnTabChanged();
}
else
{
fileManager.NewFile();
}
OnFinishedLoad();
}
protected virtual void OnFinishedLoad()
{
LoadFinished?.Invoke();
}
public void SetUpMica(bool firstTime = true)
{
bool mica = false;
if (MicaHelper.IsSupported(BackdropType.Mica) && Properties.Settings.Default.Mica) mica = true;
var oldStyle = (Style)Application.Current.FindResource("TextEditorStyle");
Style newStyle = new(typeof(TextEditor));
foreach (Setter setter in oldStyle.Setters.Cast<Setter>())
{
newStyle.Setters.Add(setter);
}
byte transparency = (byte)Properties.Settings.Default.EditorTransparency;
newStyle.Setters.Add(new Setter(BackgroundProperty, mica ? new SolidColorBrush(Color.FromArgb(transparency, 30, 30, 30)) : new SolidColorBrush(Color.FromRgb(0x1e, 0x1e, 0x1e))));
Application.Current.Resources["TextEditorStyle"] = newStyle;
if (mica && firstTime)
{
tabControl.Style = (Style)Application.Current.FindResource("TabControlStyle");
tabControl.Background = Brushes.Transparent;
ThemeResources.Current.Add("SecondaryRegionBrush", Brushes.Transparent);
ThemeResources.Current.Add("SecondaryTextBrush", Brushes.Transparent);
}
}
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (tabControl.Items.Count > 0)
{
if (tabControl.Items.Cast<TabItem>().Any(tab => tab.Header.ToString().EndsWith("*")))
{
string title = (string)Application.Current.FindResource("UnsavedFiles");
string message = (string)Application.Current.FindResource("UnsavedFilesMessage");
MessageBoxResult result = HandyControl.Controls.MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result != MessageBoxResult.Yes)
{
e.Cancel = true;
}
}
}
}
private void MainWindow_Closed(object sender, EventArgs e)
{
RPCManager.Uninitialize();
Environment.Exit(0);
}
private void TabClosed(object sender, EventArgs e)
{
GC.Collect();
}
public FileManager GetFileManager()
{
return fileManager;
}
public void HandleNamedPipe_OpenRequest(string filesToOpen)
{
try
{
Dispatcher.Invoke(() =>
{
if (!string.IsNullOrEmpty(filesToOpen))
{
TabItem lastTab = null;
foreach (string file in filesToOpen.Split(Environment.NewLine))
{
if (string.IsNullOrEmpty(file))
{
continue;
}
fileManager.NewFile();
fileManager.GetTextEditor().Load(file);
TabItem currentTabItem = tabControl.SelectedItem as TabItem;
currentTabItem.ToolTip = file;
currentTabItem.Header = Path.GetFileName(file);
lastTab = currentTabItem;
fileManager.OnTabChanged();
}
if (lastTab != null)
Dispatcher.InvokeAsync(() => tabControl.SelectedItem = lastTab);
}
if (WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Topmost = true;
Activate();
Dispatcher.BeginInvoke(new Action(() => { Topmost = false; }));
});
}
catch { }
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
FixCut();
}
private void FixCut()
{
if (WindowState == WindowState.Maximized)
{
IntPtr hWnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;
IntPtr hNext = hWnd;
do
hNext = GetWindow(hNext, GW_HWNDNEXT);
while (!IsWindowVisible(hNext));
SetForegroundWindow(hNext);
Activate();
}
}
private void Window_StateChanged(object sender, EventArgs e)
{
FixCut();
}
}
}