Skip to content

Commit d7240e1

Browse files
committed
整理代码
1 parent 3b57356 commit d7240e1

8 files changed

Lines changed: 161 additions & 108 deletions

File tree

ShellCommand/Env.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public static class Env
1414
public const string GlobalSettingFileName= "global.shellcommand.yaml";
1515
public const string GlobalTemplateSettingFileName = "global.template.shellcommand.yaml";
1616

17+
public const string SrmExeName = "ServerRegistrationManager.exe";
18+
public const string LogFileName = "log.txt";
19+
1720
public const string CreateFolderSpecificFileText = "Create .shellcommand.yaml";
1821
public const string OpenGlobalSettingFileText = "Edit Global Setting";
1922
public const string OpenAppText = "Open ShellCommand";
@@ -22,22 +25,5 @@ public static class Env
2225

2326
public const string VAR_DIR = "%DIR%";
2427
public const string VAR_SEPNAME = "---";
25-
26-
public static string GetAppFolder()
27-
{
28-
var path = Reg.GetExePath();
29-
return Path.GetDirectoryName(path);
30-
}
31-
32-
public static string GetSrmPath()
33-
{
34-
return Path.Combine(GetAppFolder(), "ServerRegistrationManager.exe");
35-
}
36-
37-
public static string GetLogPath()
38-
{
39-
var appfolder = GetAppFolder();
40-
return Path.Combine(appfolder, "log.txt");
41-
}
4228
}
4329
}

ShellCommand/MainWindow.xaml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,23 @@
4242
<Grid Grid.Row="1" Margin="20">
4343
<StackPanel>
4444
<StackPanel Orientation="Horizontal" Visibility="{Binding DEBUG}">
45-
<Button Click="TestGlobalMenu">Test GlobalMenu</Button>
45+
<Button Click="TestGlobalTemplateMenu">Show Global Template Menu</Button>
4646
</StackPanel>
47-
<wpf:Link Content="Open App Setting Folder" Click="OpenAppSettingFolder"></wpf:Link>
48-
<wpf:Link Content="Edit Global Setting File" Click="EditGlobalSettingFile"></wpf:Link>
47+
<wpf:Link Content="Open App Folder" Click="OpenAppFolder"></wpf:Link>
48+
<DockPanel>
49+
<wpf:Link Content="Reset Global Setting" DockPanel.Dock="Right" Click="ResetGlobalSettingFile">
50+
<wpf:Link.HyperLinkStyle>
51+
<Style TargetType="Hyperlink">
52+
<Style.Triggers>
53+
<Trigger Property="IsMouseOver" Value="False">
54+
<Setter Property="Foreground" Value="Gray"></Setter>
55+
</Trigger>
56+
</Style.Triggers>
57+
</Style>
58+
</wpf:Link.HyperLinkStyle>
59+
</wpf:Link>
60+
<wpf:Link Content="Edit Global Setting File" Click="EditGlobalSettingFile"></wpf:Link>
61+
</DockPanel>
4962
<Label></Label>
5063
<wpf:AutoLinkTextBlock xml:space="preserve">
5164
Install: Easy Press Install Button.

ShellCommand/MainWindow.xaml.cs

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Reflection;
78
using System.Text;
89
using System.Threading.Tasks;
910
using System.Windows;
@@ -14,7 +15,7 @@
1415
using System.Windows.Media;
1516
using System.Windows.Media.Imaging;
1617
using System.Windows.Navigation;
17-
using System.Windows.Shapes;
18+
using XJK;
1819
using XJK.SysX;
1920

2021
namespace ShellCommand
@@ -43,7 +44,7 @@ public MainWindow()
4344
App.Current.Shutdown();
4445
}
4546

46-
if (XJK.ENV.IsAdministrator()) Title += " (Admin)";
47+
if (ENV.IsAdministrator()) Title += " (Admin)";
4748
if (Environment.GetCommandLineArgs().Length == 2)
4849
{
4950
switch (Environment.GetCommandLineArgs()[1])
@@ -62,66 +63,98 @@ public MainWindow()
6263

6364
private void Install(object sender, RoutedEventArgs e)
6465
{
65-
if (XJK.ENV.IsAdministrator())
66+
if (ENV.IsAdministrator())
6667
{
6768
AdminInstall();
6869
}
6970
else
7071
{
71-
Cmd.RunAsAdmin(XJK.ENV.EntryLocation, ARG_INSTALL);
72+
Cmd.RunAsAdmin(ENV.EntryLocation, ARG_INSTALL);
7273
}
7374

74-
var globalfile = System.IO.Path.Combine(XJK.ENV.BaseDirectory, Env.GlobalSettingFileName);
75+
var globalfile = Path.Combine(ENV.BaseDirectory, Env.GlobalSettingFileName);
7576
if (!File.Exists(globalfile))
7677
{
77-
var templatefile = System.IO.Path.Combine(XJK.ENV.BaseDirectory, Env.GlobalTemplateSettingFileName);
78-
if (File.Exists(templatefile))
79-
{
80-
var templateObj = Util.Yaml.LoadYaml<GlobalConfig>(templatefile);
81-
Util.Yaml.SaveYaml(globalfile, templateObj);
82-
}
78+
ResetGlobalSettingFile(false);
8379
}
8480
}
8581

8682
private void Uninstall(object sender, RoutedEventArgs e)
8783
{
88-
if (XJK.ENV.IsAdministrator())
84+
if (ENV.IsAdministrator())
8985
{
9086
AdminUninstall();
9187
}
9288
else
9389
{
94-
Cmd.RunAsAdmin(XJK.ENV.EntryLocation, ARG_UNINSTALL);
90+
Cmd.RunAsAdmin(ENV.EntryLocation, ARG_UNINSTALL);
9591
}
9692
}
9793

9894
private void AdminInstall()
9995
{
100-
Util.Reg.SetExePath(XJK.ENV.EntryLocation);
96+
Util.Reg.SetExePath(ENV.EntryLocation);
10197
Util.Reg.SetLogPath();
102-
Cmd.RunAsInvoker(Env.GetSrmPath(), "install ShellCommand.exe -codebase");
98+
var srm = Path.Combine(ENV.BaseDirectory, Env.SrmExeName);
99+
Cmd.RunAsInvoker(srm, "install ShellCommand.exe -codebase");
103100
}
104101

105102
private void AdminUninstall()
106103
{
107-
Cmd.RunAsInvoker(Env.GetSrmPath(), "uninstall ShellCommand.exe");
104+
var srm = Path.Combine(ENV.BaseDirectory, Env.SrmExeName);
105+
Cmd.RunAsInvoker(srm, "uninstall ShellCommand.exe");
108106
Util.Reg.DeleteSubKey();
109107
}
110108

111-
private void OpenAppSettingFolder(object sender, RoutedEventArgs e)
109+
private void OpenAppFolder(object sender, RoutedEventArgs e)
112110
{
113-
Cmd.RunAsInvoker("explorer", Env.GetAppFolder());
111+
Cmd.RunAsInvoker("explorer", ENV.BaseDirectory);
114112
}
115113

116114
private void EditGlobalSettingFile(object sender, RoutedEventArgs e)
117115
{
118-
var configpath = System.IO.Path.Combine(XJK.ENV.BaseDirectory, Env.GlobalSettingFileName);
116+
var configpath = Path.Combine(ENV.BaseDirectory, Env.GlobalSettingFileName);
119117
Cmd.RunAsInvoker(configpath, "");
120118
}
121119

122-
private void TestGlobalMenu(object sender, RoutedEventArgs e)
120+
private void ResetGlobalSettingFile(object sender, RoutedEventArgs e)
121+
{
122+
ResetGlobalSettingFile(true);
123+
}
124+
125+
private void ResetGlobalSettingFile(bool showMessage)
126+
{
127+
var templatefile = Path.Combine(ENV.BaseDirectory, Env.GlobalTemplateSettingFileName);
128+
var targetfile = Path.Combine(ENV.BaseDirectory, Env.GlobalSettingFileName);
129+
if (!File.Exists(templatefile))
130+
{
131+
if (showMessage)
132+
{
133+
MessageBox.Show("Template global setting file NOT exist!", Env.AppName);
134+
}
135+
return;
136+
}
137+
if (File.Exists(targetfile))
138+
{
139+
if (showMessage)
140+
{
141+
var result = MessageBox.Show("Global setting file will be overwrite, sure?", Env.AppName, MessageBoxButton.OKCancel);
142+
if (result == MessageBoxResult.Cancel)
143+
{
144+
return;
145+
}
146+
}
147+
}
148+
File.Copy(templatefile, targetfile, true);
149+
}
150+
151+
private void TestGlobalTemplateMenu(object sender, RoutedEventArgs e)
123152
{
124-
DirectoryBackgroundContextMenu.CreateMenu(Env.GetAppFolder(), "").Show(XJK.SysX.Device.Mouse.GetPosition());
153+
var position = XJK.SysX.Device.Mouse.GetPosition();
154+
var globalPath = Path.Combine(ENV.BaseDirectory, Env.GlobalTemplateSettingFileName);
155+
var menu = MenuCreator.Create(ENV.BaseDirectory, "", globalPath);
156+
menu.Show(position);
125157
}
158+
126159
}
127160
}
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using SharpShell.Attributes;
22
using SharpShell.SharpContextMenu;
33
using ShellCommand.DataModel;
4+
using ShellCommand.Util;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
@@ -9,8 +10,6 @@
910
using System.Text;
1011
using System.Threading.Tasks;
1112
using System.Windows.Forms;
12-
using XJK.SysX;
13-
using XJK.SysX.CommandHelper;
1413

1514
namespace ShellCommand.MenuDefinition
1615
{
@@ -20,44 +19,16 @@ public class DirectoryBackgroundContextMenu : SharpContextMenu
2019
{
2120
public DirectoryBackgroundContextMenu() { }
2221

23-
string CommandFilePath => Path.Combine(FolderPath, Env.CommandFileName);
24-
2522
protected override bool CanShowMenu()
2623
{
2724
return true;
2825
}
2926

3027
protected override ContextMenuStrip CreateMenu()
3128
{
32-
return CreateMenu(FolderPath, CommandFilePath);
33-
}
34-
35-
internal static ContextMenuStrip CreateMenu(string workingDir, string CommandFilePath)
36-
{
37-
var menu = new ContextMenuStrip();
38-
var container = new ToolStripMenuItem(Env.MenuDisplay);
39-
40-
var list = new List<ToolStripItem>();
41-
42-
MenuItemsBuilder.ParseDirectoryCommandInto(list, CommandFilePath, workingDir);
43-
MenuItemsBuilder.AddSeparator(list);
44-
45-
var globalSettingPath = Path.Combine(Env.GetAppFolder(), Env.GlobalSettingFileName);
46-
if (File.Exists(globalSettingPath))
47-
{
48-
MenuItemsBuilder.ParseGlobalCommandInto(list, globalSettingPath, workingDir);
49-
}
50-
51-
list.Add(BuildinMenuItems.OpenApp());
52-
if (!File.Exists(CommandFilePath))
53-
{
54-
MenuItemsBuilder.AddSeparator(list);
55-
list.Add(BuildinMenuItems.InitDirectoryFile(CommandFilePath));
56-
}
57-
58-
container.DropDownItems.AddRange(list.ToArray());
59-
menu.Items.Add(container);
60-
return menu;
29+
var directoryFilePath = Path.Combine(FolderPath, Env.CommandFileName);
30+
var globalSettingPath = Path.Combine(Reg.GetAppFolder(), Env.GlobalSettingFileName);
31+
return MenuCreator.Create(FolderPath, directoryFilePath, globalSettingPath);
6132
}
6233
}
6334
}

ShellCommand/MenuDefinition/BuildinMenuItems.cs renamed to ShellCommand/MenuDefinition/MenuCreator.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ShellCommand.DataModel;
2+
using ShellCommand.Util;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -10,8 +11,39 @@
1011

1112
namespace ShellCommand.MenuDefinition
1213
{
13-
static class BuildinMenuItems
14+
static class MenuCreator
1415
{
16+
public static ContextMenuStrip Create(string workingDir, string directoryFilePath, string globalFilePath)
17+
{
18+
var menu = new ContextMenuStrip();
19+
var container = new ToolStripMenuItem(Env.MenuDisplay);
20+
21+
var list = new List<ToolStripItem>();
22+
23+
if (File.Exists(directoryFilePath))
24+
{
25+
list.AddParseDirectoryCommandInto(directoryFilePath, workingDir);
26+
list.AddSeparator();
27+
}
28+
29+
if (File.Exists(globalFilePath))
30+
{
31+
list.AddParseGlobalCommandInto(globalFilePath, workingDir);
32+
}
33+
34+
list.Add(GetOpenAppMenuItem());
35+
if (!File.Exists(directoryFilePath))
36+
{
37+
list.AddSeparator();
38+
list.Add(GetInitDirectoryFileMenuItem(directoryFilePath));
39+
}
40+
41+
container.DropDownItems.AddRange(list.ToArray());
42+
menu.Items.Add(container);
43+
return menu;
44+
}
45+
46+
1547
public static ToolStripMenuItem GetCopyPathMenuItem(string workingDir)
1648
{
1749
var item = new ToolStripMenuItem("Copy Folder Path");
@@ -24,7 +56,7 @@ public static ToolStripMenuItem GetCopyPathMenuItem(string workingDir)
2456
return item;
2557
}
2658

27-
public static ToolStripMenuItem InitDirectoryFile(string path)
59+
public static ToolStripMenuItem GetInitDirectoryFileMenuItem(string path)
2860
{
2961
var item = new ToolStripMenuItem(Env.CreateFolderSpecificFileText);
3062
item.Click += (sender, args) =>
@@ -34,23 +66,23 @@ public static ToolStripMenuItem InitDirectoryFile(string path)
3466
return item;
3567
}
3668

37-
public static ToolStripMenuItem OpenGlobalSetting()
69+
public static ToolStripMenuItem GetOpenGlobalSettingMenuItem()
3870
{
3971
var item = new ToolStripMenuItem(Env.OpenGlobalSettingFileText);
4072
item.Click += (sender, args) =>
4173
{
42-
var path = Path.Combine(Env.GetAppFolder(), Env.GlobalSettingFileName);
74+
var path = Path.Combine(Reg.GetAppFolder(), Env.GlobalSettingFileName);
4375
Cmd.RunAsInvoker(path, "");
4476
};
4577
return item;
4678
}
4779

48-
public static ToolStripMenuItem OpenApp()
80+
public static ToolStripMenuItem GetOpenAppMenuItem()
4981
{
5082
var item = new ToolStripMenuItem(Env.OpenAppText);
5183
item.Click += (sender, args) =>
5284
{
53-
Cmd.RunAsInvoker(Util.Reg.GetExePath(), "");
85+
Cmd.RunAsInvoker(Reg.GetExePath(), "");
5486
};
5587
return item;
5688
}

0 commit comments

Comments
 (0)