-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
269 lines (220 loc) · 9.49 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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
namespace Google_Bookmarks_Manager_for_GPOs
{
public class Bookmark
{
#region Public Properties
public string Name { get; set; }
public string Url { get; set; }
#endregion Public Properties
}
public partial class MainWindow : Window
{
#region Private Fields
private List<Bookmark> bookmarks;
private string topLevelName;
public event PropertyChangedEventHandler PropertyChanged;
#endregion Private Fields
#region Public Constructors
private static bool _isDarkModeEnabled;
public MainWindow()
{
InitializeComponent();
// Set up the data context
bookmarks = new List<Bookmark>();
bookmarks.Add(new Bookmark());
bookmarksDataGrid.ItemsSource = bookmarks;
bookmarksDataGrid.CanUserAddRows = true;
DataContext = this;
// Clear the text boxes
bookmarkUrlTextBox.Clear();
bookmarkFolderNameTextBox.Clear();
// Clear the bookmarks list and refresh the data grid
bookmarks.Clear();
bookmarksDataGrid.Items.Refresh();
// Add an event handler for the PropertyChanged event
PropertyChanged += MainWindow_PropertyChanged;
}
//private void SwitchTheme(bool isDark)
//{
// var themeSource = isDark
// ? "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml"
// : "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml";
// var themeResource = Resources.MergedDictionaries
// .Where(d => d.Source != null && (d.Source.OriginalString.Contains("MaterialDesignTheme.Light.xaml") || d.Source.OriginalString.Contains("MaterialDesignTheme.Dark.xaml")))
// .FirstOrDefault();
// if (themeResource != null)
// {
// Resources.MergedDictionaries.Remove(themeResource);
// }
public static bool IsDarkModeEnabled
{
get => _isDarkModeEnabled;
set
{
if (_isDarkModeEnabled != value)
{
_isDarkModeEnabled = value;
UpdateTheme();
}
}
}
private static void UpdateTheme()
{
if (IsDarkModeEnabled)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
new ResourceDictionary { Source = new Uri("DarkTheme.xaml", UriKind.Relative) });
}
else
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
new ResourceDictionary { Source = new Uri("LightTheme.xaml", UriKind.Relative) });
}
}
private void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IsDarkModeEnabled))
{
UpdateTheme();
}
}
// Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(themeSource) });
//}
private void SwitchTheme(bool isDark)
{
var themeSource = isDark
? "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml"
: "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml";
var themeResource = Application.Current.Resources.MergedDictionaries
.Where(d => d.Source != null && (d.Source.OriginalString.Contains("MaterialDesignTheme.Light.xaml") || d.Source.OriginalString.Contains("MaterialDesignTheme.Dark.xaml")))
.FirstOrDefault();
if (themeResource != null)
{
Application.Current.Resources.MergedDictionaries.Remove(themeResource);
}
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(themeSource) });
}
#endregion Public Constructors
#region Private Methods
private void addBookmarkButton_Click(object sender, RoutedEventArgs e)
{
}
private void bookmarksDataGrid_Loaded(object sender, RoutedEventArgs e)
{
if (bookmarks.Any())
{
bookmarks.RemoveAt(0);
bookmarksDataGrid.Items.Refresh();
}
}
private void clearFormButton_Click(object sender, RoutedEventArgs e)
{
// Clear the text boxes
bookmarkUrlTextBox.Clear();
bookmarkFolderNameTextBox.Clear();
// Clear the bookmarks list and refresh the data grid
bookmarks.Clear();
bookmarksDataGrid.Items.Refresh();
}
private void exportBookmarksButton_Click_1(object sender, RoutedEventArgs e)
{
// Create a new JSON object with an array of bookmarks
JArray bookmarksArray = new JArray();
string topLevelName = bookmarkFolderNameTextBox.Text;
bookmarksArray.Add(new JObject(new JProperty("toplevel_name", topLevelName)));
foreach (Bookmark bookmark in bookmarks)
{
if (bookmark.Name != null || bookmark.Url != null) // check if the bookmark is not null
{
JObject bookmarkObject = new JObject(new JProperty("name", bookmark.Name ?? ""), new JProperty("url", bookmark.Url ?? ""));
bookmarksArray.Add(bookmarkObject);
}
}
// Convert the JSON object to a single line of text
var json = bookmarksArray.ToString(Formatting.None);
// Save the JSON string to a file
var saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "Bookmark Files (*.json)|*.json";
if (saveFileDialog.ShowDialog() == true)
{
File.WriteAllText(saveFileDialog.FileName, json);
}
}
private void importBookmarksButton_Click(object sender, RoutedEventArgs e)
{
var importWindow = new ImportWindow();
if (importWindow.ShowDialog() == true)
{
ParseBookmarks(importWindow.Json);
}
}
private void ParseBookmarks(string json)
{
try
{
var parsedJson = JArray.Parse(json);
topLevelName = parsedJson.First["toplevel_name"].ToString();
bookmarkFolderNameTextBox.Text = topLevelName;
bookmarks = JsonConvert.DeserializeObject<List<Bookmark>>(parsedJson.ToString());
// Remove the first element from the bookmarks list
bookmarks.RemoveAt(0);
bookmarksDataGrid.ItemsSource = bookmarks;
}
catch (Exception ex)
{
MessageBox.Show("Error parsing bookmarks: " + ex.Message);
}
}
#endregion Private Methods
#region Public Methods
public static MessageBoxResult ShowCustomMessageBox(string message, string caption, MessageBoxButton buttons)
{
var customMessageBox = new CustomMessageBox(message, caption, buttons);
customMessageBox.Owner = Application.Current.MainWindow;
customMessageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
customMessageBox.ShowDialog();
return customMessageBox.Result;
}
#endregion Public Methods
private void Button_Click(object sender, RoutedEventArgs e)
{
JArray bookmarksArray = new JArray();
string topLevelName = bookmarkFolderNameTextBox.Text;
bookmarksArray.Add(new JObject(new JProperty("toplevel_name", topLevelName)));
foreach (Bookmark bookmark in bookmarks)
{
if (bookmark.Name != null || bookmark.Url != null) // check if the bookmark is not null
{
JObject bookmarkObject = new JObject(new JProperty("name", bookmark.Name ?? ""), new JProperty("url", bookmark.Url ?? ""));
bookmarksArray.Add(bookmarkObject);
}
}
// Convert the JSON object to a single line of text
var json = bookmarksArray.ToString(Formatting.None);
// Copy the JSON string to the clipboard
Clipboard.SetText(json);
// Show a confirmation message
ShowCustomMessageBox("Bookmarks copied to clipboard!", "Confirmation", MessageBoxButton.OK);
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
var isDark = darkModeCheckBox.IsChecked.HasValue && darkModeCheckBox.IsChecked.Value;
SwitchTheme(isDark);
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
var isDark = darkModeCheckBox.IsChecked.HasValue && darkModeCheckBox.IsChecked.Value;
SwitchTheme(isDark);
}
}
}