-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathShowDesignerCommand.cs
More file actions
166 lines (143 loc) · 5.98 KB
/
ShowDesignerCommand.cs
File metadata and controls
166 lines (143 loc) · 5.98 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
using System;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
namespace Terminal.Gui.Designer
{
[Cmdlet("Show", "TUIDesigner")]
public class ShowDesignerCommand : PSCmdlet
{
protected override void ProcessRecord()
{
var state = new DesignerState();
Application.Init();
var top = Application.Top;
var fileNameStatus = new StatusItem(Key.Unknown, "Unsaved", () => { });
var versionStatus = new StatusItem(Key.Unknown, base.MyInvocation.MyCommand.Module.Version.ToString(), () => { });
var statusBar = new StatusBar(new StatusItem[] { fileNameStatus, versionStatus });
Action<bool> save = (saveAs) =>
{
if (string.IsNullOrEmpty(state.FileName) || saveAs)
{
var dialog = new SaveDialog("Save file", "Save designer file");
dialog.AllowedFileTypes = new string[] { ".ps1" };
Application.Run(dialog);
if (dialog.FilePath.IsEmpty || Directory.Exists(dialog.FilePath.ToString())) return;
state.FileName = dialog.FilePath.ToString();
fileNameStatus.Title = state.FileName;
}
fileNameStatus.Title = fileNameStatus.Title.TrimEnd("*");
statusBar.SetNeedsDisplay();
var powerShellIntegration = new PowerShellIntegration(state);
try
{
powerShellIntegration.Save();
}
catch (Exception ex)
{
MessageBox.ErrorQuery("Failed", "Failed to save. " + ex.Message, "Ok");
}
};
top.Add(new MenuBar(new MenuBarItem[] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_Open", "", () => {
var dialog = new OpenDialog("Open file", "Open designer file");
dialog.CanChooseDirectories = false;
dialog.CanChooseFiles = true;
dialog.AllowsMultipleSelection = false;
dialog.AllowedFileTypes = new [] {".ps1"};
Application.Run(dialog);
if (dialog.FilePath.IsEmpty)
{
return;
}
try
{
state.FileName = dialog.FilePath.ToString();
var powerShellIntegration = new PowerShellIntegration(state);
powerShellIntegration.Load();
fileNameStatus.Title = state.FileName;
}
catch (Exception ex)
{
MessageBox.ErrorQuery("Failed", "Failed to load Window: " + ex.Message, "Ok");
}
}),
new MenuItem ("_Save", "", () => {
save(false);
}),
new MenuItem ("Save As", "", () => {
save(true);
}),
new MenuItem ("_Quit", "", () => {
try
{
Application.Shutdown();
}
catch {}
})
}),
new MenuBarItem("_Help", new [] {
new MenuItem("_About", "", () => MessageBox.Query("About", $"PowerShell Terminal GUI Designer\nVersion: {base.MyInvocation.MyCommand.Module.Version.ToString()}\n", "Ok")),
new MenuItem("_Docs", "", () => {
var psi = new ProcessStartInfo
{
FileName = "https://www.github.com/ironmansoftware/terminal-gui-designer",
UseShellExecute = true
};
Process.Start (psi);
})
})
}));
var controls = new ControlListView(state)
{
X = 0,
Y = 1,
Width = Dim.Percent(20),
Height = Dim.Percent(30)
};
var properties = new PropertyListView(state)
{
X = 0,
Y = Pos.Bottom(controls),
Width = Dim.Percent(20),
Height = Dim.Percent(50)
};
var definedControls = new DefinedControlsListView(state)
{
X = 0,
Y = Pos.Bottom(properties),
Width = Dim.Percent(20),
Height = Dim.Fill() - 1
};
var designer = state.Window;
designer.X = Pos.Right(controls);
designer.Y = 1;
designer.Height = Dim.Fill() - 1;
designer.Width = Dim.Percent(80);
designer.SetNeedsDisplay();
state.ControlSelected += (sender, args) =>
{
properties.SetTargetView(args.SelectedControl);
};
state.ControlRemoved += (sender, args) =>
{
properties.SetTargetView(null);
};
state.Dirty += (sender, args) =>
{
if (!fileNameStatus.Title.EndsWith("*"))
{
fileNameStatus.Title += "*";
statusBar.SetNeedsDisplay();
}
};
top.Add(controls, properties, designer, definedControls, statusBar);
try
{
Application.Run();
}
catch { }
}
}
}