-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenuUtils.cs
More file actions
170 lines (135 loc) · 5.25 KB
/
Copy pathMenuUtils.cs
File metadata and controls
170 lines (135 loc) · 5.25 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
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
using SylverInk.Notes;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static SylverInk.CommonUtils;
using static SylverInk.FileIO.FileUtils;
using static SylverInk.Notes.DatabaseUtils;
using static SylverInk.XAMLUtils.DataUtils;
namespace SylverInk;
/// <summary>
/// Extension methods serving needs tied to context menus (and the application toolbar) on the main application window.
/// </summary>
public static class MenuUtils
{
public static void MenuBackup(this MainWindow window, object? sender, RoutedEventArgs e) => CurrentDatabase.MakeBackup();
public static void MenuClose(this MainWindow window, object? sender, RoutedEventArgs e)
{
if (CurrentDatabase.Changed)
{
var res = MessageBox.Show("Do you want to save your changes?", "Sylver Ink: Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
if (res == MessageBoxResult.Cancel)
return;
if (res == MessageBoxResult.Yes)
CurrentDatabase.Save();
}
RemoveDatabase(CurrentDatabase);
DeferUpdateRecentNotes();
}
public static void MenuConnect(this MainWindow window, object? sender, RoutedEventArgs e)
{
window.ConnectAddress.IsOpen = true;
window.AddressBox.Text = string.Empty;
}
public static void MenuCopyCode(this MainWindow window, object? sender, RoutedEventArgs e) => Clipboard.SetText(CurrentDatabase.Server?.AddressCode);
public static void MenuCreate(this MainWindow window, object? sender, RoutedEventArgs e)
{
AddDatabase(new());
window.DatabasesPanel.SelectedIndex = window.DatabasesPanel.Items.Count - 1;
DeferUpdateRecentNotes();
}
public static void MenuDelete(this MainWindow window, object? sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure you want to permanently delete this database?", "Sylver Ink: Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
return;
Erase(CurrentDatabase.DBFile);
var BKPath = Path.GetDirectoryName(GetBackupPath(CurrentDatabase));
if (Directory.Exists(BKPath))
Directory.Delete(BKPath, true);
RemoveDatabase(CurrentDatabase);
DeferUpdateRecentNotes();
}
public static void MenuDisconnect(this MainWindow window, object? sender, RoutedEventArgs e)
{
CurrentDatabase.Client.Disconnect();
CurrentDatabase.Changed = true;
}
public static async void MenuOpen(this MainWindow window, object? sender, RoutedEventArgs e)
{
string dbFile = DialogFileSelect(filterIndex: 2);
if (string.IsNullOrWhiteSpace(dbFile))
return;
var path = Path.GetFullPath(dbFile);
if (DatabaseFiles.Contains(path))
{
var items = window.DatabasesPanel.Items.Cast<TabItem>().ToList();
var db = items?.FindIndex(new(item => {
var innerDB = (Database)item.Tag;
return Path.GetFullPath(innerDB.DBFile).Equals(path);
}));
window.DatabasesPanel.SelectedIndex = db ?? window.DatabasesPanel.SelectedIndex;
return;
}
await Database.Create(dbFile);
DeferUpdateRecentNotes();
}
public static void MenuProperties(this MainWindow window, object? sender, RoutedEventArgs e)
{
Properties properties = new() { DB = CurrentDatabase };
properties.Show();
}
public static void MenuRename(this MainWindow window, object? sender, RoutedEventArgs e)
{
window.RenameDatabase.IsOpen = true;
window.DatabaseNameBox.Text = CurrentDatabase.Name;
window.DatabaseNameBox.Focus();
window.DatabaseNameBox.CaretIndex = window.DatabaseNameBox.Text?.Length ?? 0;
}
public static void MenuSaveAs(this MainWindow window, object? sender, RoutedEventArgs e)
{
var newPath = DialogFileSelect(true, 2, CurrentDatabase.Name);
if (!string.IsNullOrWhiteSpace(newPath))
CurrentDatabase.DBFile = newPath;
CurrentDatabase.Format = HighestSIDBFormat;
}
public static void MenuSaveLocal(this MainWindow window, object? sender, RoutedEventArgs e)
{
CurrentDatabase.Changed = true;
CurrentDatabase.DBFile = Path.Join(Subfolders["Databases"], Path.GetFileNameWithoutExtension(CurrentDatabase.DBFile), Path.GetFileName(CurrentDatabase.DBFile));
CurrentDatabase.Format = HighestSIDBFormat;
CurrentDatabase.Save();
}
public static void MenuServe(this MainWindow window, object? sender, RoutedEventArgs e) => CurrentDatabase.Server.Serve(0);
public static void MenuShowAbout(this MainWindow window, object? sender, RoutedEventArgs e) => new About().Show();
public static void MenuSublistChanged(this MainWindow window, object? sender, RoutedEventArgs e)
{
if (Mouse.RightButton == MouseButtonState.Pressed)
return;
if (sender is not ListBoxItem box)
return;
if (box.DataContext is not NoteRecord record)
return;
RecentSelection = record;
OpenQuery(RecentSelection);
}
public static void MenuTabChanged(this MainWindow window, object? sender, SelectionChangedEventArgs e)
{
if (sender is not TabControl control)
return;
if (!control.Name.Equals("DatabasesPanel"))
return;
if (control.SelectedItem is not TabItem item)
return;
if (item.Tag is not Database newDB)
return;
if (newDB.Equals(CurrentDatabase))
return;
CurrentDatabase = newDB;
RecentNotesDirty = true;
CommonUtils.Settings.SearchResults.Clear();
DeferUpdateRecentNotes();
}
public static void MenuUnserve(this MainWindow window, object? sender, RoutedEventArgs e) => CurrentDatabase.Server.Close();
}