-
Notifications
You must be signed in to change notification settings - Fork 0
/
dln.d
359 lines (291 loc) · 9.78 KB
/
dln.d
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
module dln;
import dlangui;
import dlangui.core.logger;
import dlangui.dialogs.filedlg;
import dlangui.dialogs.dialog;
import dlangui.dml.dmlhighlight;
import dlangui.widgets.metadata;
import std.array : replaceFirst;
import std.algorithm;
import std.process;
import std.stdio;
import std.array;
import std.file;
import std.path;
import dlangui.dialogs.settingsdialog;
import settings;
import appSettings;
mixin APP_ENTRY_POINT;
enum IDEActions : int {
About = 1010000,
Lang
}
dstring SAMPLE_SOURCE_CODE = q{import std.stdio;
import std.path;
import std.file;
import std.string;
import std.algorithm;
import std.array;
import std.conv;
import std.typecons;
import std.traits;
struct A {
int a;
string b;
}
int main() {
int x=3;
writeln("Ok! x is ", typeid(x));
writeln("allMembers=", [ __traits(allMembers, A) ] );
return 0;
}
};
const Action ACTION_ABOUT = new Action(IDEActions.About, "MENU_ABOUT"c, "document-about", KeyCode.KEY_A, KeyFlag.Control);
const Action ACTION_LANG = new Action(IDEActions.Lang, "MENU_LANG"c, "document-lang", KeyCode.KEY_L, KeyFlag.Control);
string BUTTONS_CODE = q{
HorizontalLayout {
layoutWidth: FILL_PARENT
layoutHeight: 20
alignment: HCenter
Button { id: btnRun; text: "compile/run";
margins: Rect { left: 60; right: 0; top: 20; bottom: 20}
layoutWidth: 180; layoutHeight: 20 }
Button { id: btnReset; text: "reset"; padding: 0;
margins: Rect { left: 80; right: 0; top: 20; bottom: 20 }
layoutWidth: 200; layoutHeight: 20 }
Button { id: btnSave; text: "save"; padding: 0;
margins: Rect { left: 80; right: 0; top: 20; bottom: 20 }
layoutWidth: 200; layoutHeight: 20 }
}
};
immutable string RESULT_WIDGET = `
MultilineTextWidget {
/* this widget can be accessed via id myLabel1
e.g. w.childById!TextWidget("myLabel1")
*/
id: logTxtWidget
text: ""
padding: 5
layoutWidth: FILL_PARENT
layoutHeight: 100
//readonly: true
}
`;
string cacheFile;
class DMLSourceEdit : SourceEdit {
this(string ID) {
super(ID);
content.syntaxSupport = new DMLSyntaxSupport("");
setTokenHightlightColor(TokenCategory.Comment, 0x008000); // green
setTokenHightlightColor(TokenCategory.Keyword, 0x0000FF); // blue
setTokenHightlightColor(TokenCategory.String, 0xa31515); // brown
setTokenHightlightColor(TokenCategory.Integer, 0xa315C0); //
setTokenHightlightColor(TokenCategory.Float, 0xa315C0); //
setTokenHightlightColor(TokenCategory.Error, 0xFF0000); // red
setTokenHightlightColor(TokenCategory.Op, 0x503000);
setTokenHightlightColor(TokenCategory.Identifier_Class, 0x000080); // blue
}
this() {
this("Dln");
}
}
class MainFrame : AppFrame {
MenuItem mainMenuItems;
string settingsFile;
AppSettings _settings;
override protected void initialize() {
_appName = "Dln";
super.initialize();
}
this(Window window) {
settingsFile = buildNormalizedPath(settingsDir, "dlnrc.json");
_settings = new AppSettings(settingsFile);
cacheFile = buildNormalizedPath(settingsDir, "dln_ini.d");
applySettings(_settings);
super();
}
~this() {
// if (_dcdInterface) {
// destroy(_dcdInterface);
// _dcdInterface = null;
// }
}
/// create main menu
override protected MainMenu createMainMenu() {
mainMenuItems = new MenuItem();
MenuItem aboutMenu = new MenuItem(new Action(1, "MENU_ABOUT"));
aboutMenu.add(ACTION_ABOUT);
mainMenuItems.add(aboutMenu);
MenuItem langMenu = new MenuItem(new Action(2, "MENU_LANG"));
langMenu.add(ACTION_LANG);
mainMenuItems.add(langMenu);
MainMenu mainMenu = new MainMenu(mainMenuItems);
return mainMenu;
}
void applySettings(AppSettings settings) {
Platform.instance.uiLanguage = settings.uiLanguage;
//Log.d("settings.uiLanguage=");
//Log.d(settings.uiLanguage);
requestLayout();
}
override bool handleAction(const Action a) {
if (!a) {
return false;
}
switch (a.id) {
case IDEActions.About:
window.showMessageBox(UIString.fromId("ABOUT"), UIString.fromId("ABOUT_TEXT"));
return true;
case IDEActions.Lang:
showPreferences();
return true;
default:
return super.handleAction(a);
}
//return false;
}
override protected ToolBarHost createToolbars() {
ToolBarHost res = new ToolBarHost();
ToolBar tb;
tb = res.getOrAddToolbar("About");
tb.addButtons(ACTION_ABOUT);
tb = res.getOrAddToolbar("Lang");
tb.addButtons(ACTION_LANG);
return res;
}
string _filename;
void openSourceFile(string filename) {
import std.file;
// TODO
if (exists(filename)) {
_filename = filename;
window.windowCaption = toUTF32(filename);
_editor.load(filename);
}
}
void saveSourceFile(string filename) {
if (filename.length == 0)
filename = _filename;
import std.file;
_filename = filename;
window.windowCaption = toUTF32(filename);
_editor.save(filename);
// window.showMessageBox(UIString.fromId("Info"), UIString.fromRaw("Saved to: " ~ filename));
}
bool onCanClose() {
// todo
return true;
}
void showPreferences() {
Setting s = _settings.copySettings();
SettingsDialog dlg = new SettingsDialog(UIString.fromId("HEADER_SETTINGS"), window, s, createSettingsPages());
dlg.dialogResult = delegate(Dialog dlg, const Action result) {
if (result.id == ACTION_APPLY.id) {
_settings.applySettings(s);
applySettings(_settings);
_settings.save();
window.showMessageBox(UIString.fromRaw("Info"), UIString.fromRaw("Saved to: " ~ settingsFile));
}
};
dlg.show();
}
protected DMLSourceEdit _editor;
// protected ScrollWidget _preview;
void setText(Widget w, string text) {
w.text(toUTF32(text.replace("\r\n", "\n")));
}
override protected Widget createBody() {
auto dockHost = new DockHost();
dockHost.layoutWidth = FILL_PARENT;
dockHost.layoutHeight = FILL_PARENT;
dockHost.alignment = Align.Top;
auto ml = parseML(RESULT_WIDGET);
auto topFrame = parseML(BUTTONS_CODE);
_editor = new DMLSourceEdit();
_editor.text = getCacheText();
_editor.layoutHeight = 200;
auto resultDockWin = new DockWindow("result"); // false: don't show close button
resultDockWin.bodyWidget = ml;
resultDockWin.dockAlignment = DockAlignment.Bottom;
resultDockWin.layoutHeight = makePercentSize(40);
resultDockWin.layoutWidth = FILL_PARENT;
resultDockWin.caption.text = UIString.fromId("RESULT") ~ ":";
auto topDockWin = new DockWindow("top");
topDockWin.bodyWidget = topFrame;
topDockWin.dockAlignment = DockAlignment.Top;
topDockWin.layoutHeight = makePercentSize(10);
topDockWin.layoutWidth = FILL_PARENT;
topDockWin.caption.text = UIString.fromId("BUTTONS") ~ ":";
dockHost.addDockedWindow(topDockWin);
dockHost.bodyWidget = _editor;
dockHost.addDockedWindow(resultDockWin);
auto logTxtWidget = ml.childById!TextWidget("logTxtWidget");
auto btnSave = topFrame.childById!Button("btnSave");
btnSave.text = UIString.fromId("SAVE");
btnSave.click = delegate(Widget srcWidget) {
auto fp = new File(cacheFile, "w");
fp.write(_editor.text);
setText(logTxtWidget, "Saved: " ~ cacheFile);
return true;
};
auto btnRun = topFrame.childById!Button("btnRun");
btnRun.text = UIString.fromId("COMPILE_AND_RUN");
btnRun.click = delegate(Widget srcWidget) {
auto tmpDir = std.file.tempDir();
auto tmp_filename = buildPath(tmpDir, "aa.d");
this.saveSourceFile(tmp_filename);
auto tmp_exeFile = tmp_filename ~ ".exe";
logTxtWidget.text = " ";
try {
auto result0 = execute(["dmd", tmp_filename, "-of=" ~ tmp_exeFile],
null, Config.suppressConsole);
setText(logTxtWidget, result0.output);
Log.i("dmd result: ", result0.output);
if (result0.status != 0) {
return false;
}
} catch (Exception e) {
setText(logTxtWidget, "编译出错:" ~ e.toString());
return false;
}
if (exists(tmp_exeFile)) {
try {
auto result1 = execute([tmp_exeFile], null, Config.suppressConsole);
setText(logTxtWidget, "\n\n" ~ result1.output);
if (result1.status != 0) {
return false;
}
} catch (Exception e) {
setText(logTxtWidget, UIString.fromId("ERROR").to!string ~ ":" ~ e.toString());
return false;
}
}
return true;
};
auto btnReset = topFrame.childById!Button("btnReset");
btnReset.text = UIString.fromId("RESET");
btnReset.click = delegate(Widget srcWidget) {
_editor.text = getCacheText();
return true;
};
return dockHost;
}
}
dstring getCacheText() {
if (exists(cacheFile)) {
return readText(cacheFile).to!dstring;
} else {
return SAMPLE_SOURCE_CODE;
}
}
extern (C) int UIAppMain(string[] args) {
embeddedResourceList.addResources(embedResourcesFromList!("resources.list")());
FontManager.fontGamma = 0.8;
FontManager.hintingMode = HintingMode.Normal;
Window window = Platform.instance.createWindow("Dlang Learn"d, null,
WindowFlag.Resizable, 500, 500);
window.windowIcon = drawableCache.getImage("dlangui-logo1");
window.mainWidget = new MainFrame(window);
window.show();
return Platform.instance.enterMessageLoop();
}