-
Notifications
You must be signed in to change notification settings - Fork 1
/
Macapi.Dialogs.pas
275 lines (230 loc) · 8.17 KB
/
Macapi.Dialogs.pas
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
(* macOS Dialogs. Simplified. *)
(* Unit by Lily Stilson [2019] *)
(* Licence: MIT *)
unit Macapi.Dialogs;
interface
uses
System.SysUtils, System.Types, System.Classes, System.TypInfo, FMX.Dialogs,
MacApi.AppKit, MacApi.Foundation, MacApi.CocoaTypes, Macapi.Helpers, MacApi.ObjCRuntime, Macapi.ObjectiveC;
type
{PopUpButtonHandler = interface(NSObject)
property
end;}
/// <summary>Converts standard Delphi array of Strtings to MacApi NSArray</summary>
function ArrayToNSArray(const Arr: TArray<String>): NSArray;
procedure selectFormat(sender: Pointer);
function FileTypeAView(Types: NSArray): NSView;
/// <summary>Simplified NSOpenPanel Invoker. Available flags: 'multiple', 'others', 'create', 'hidden'.</summary>
function InvokeNSOpenPanel(const Title: String; const InitialDir: String;
const Flags: String; const AllowedFileTypes: TArray<String>;
var ADir: String): Boolean;
/// <summary>Simplified NSSavePanel Invoker. Available flags: 'others', 'create', 'hidden'.</summary>
function InvokeNSSavePanel(const Title: String; const InitialDir: String;
const Flags: String; const FileTypesDescription: TArray<String>; const AllowedFileTypes: TArray<String>;
var ADir: String): Boolean;
/// <summary>Simplified NSOpenPanel Invoker in Directory selection mode. Available flags: 'multiple', 'create', 'hidden'.</summary>
function InvokeNSDirOpenPanel(const Title: String; const InitialDir: String;
const Flags: String; var ADir: String): Boolean;
var
SavePanel: NSSavePanel;
OpenPanel: NSOpenPanel;
FOwner: Pointer;
implementation
function ArrayToNSArray(const Arr: TArray<String>): NSArray;
var
LArgsPtrs : TArray<Pointer>;
begin
SetLength(LArgsPtrs, Length(Arr));
for var i := 0 to Length(Arr) - 1 do
begin
LArgsPtrs[i] := NSObjectToID(StrToNSStr(Arr[i]));
end;
Result:= TNSArray.Wrap(TNSArray.OCClass.arrayWithObjects(@LArgsPtrs[0], Length(LArgsPtrs)));
end;
procedure selectFormat(sender: Pointer);
begin
if TNSPopUpButton.Wrap(SavePanel.accessoryView.subviews.objectAtIndex(1)).indexOfSelectedItem = 0 then
begin
//ShowMessage('selected item = ' + NSStrToStr(TNSPopUpButton.Wrap(SavePanel.accessoryView.subviews.objectAtIndex(1)).itemTitleAtIndex(0)));
SavePanel.setNameFieldStringValue(StrToNSStr(Format('%s.%s', [NSStrToStr(SavePanel.nameFieldStringValue.stringByDeletingPathExtension), 'aep'])));
SavePanel.setAllowedFileTypes(ArrayToNSArray(['aep']));
end;
if TNSPopUpButton.Wrap(SavePanel.accessoryView.subviews.objectAtIndex(1)).indexOfSelectedItem = 1 then
begin
//ShowMessage('selected item = ' + NSStrToStr(TNSPopUpButton.Wrap(SavePanel.accessoryView.subviews.objectAtIndex(1)).itemTitleAtIndex(1)));
SavePanel.setNameFieldStringValue(StrToNSStr(Format('%s.%s', [NSStrToStr(SavePanel.nameFieldStringValue.stringByDeletingPathExtension), 'aer'])));
SavePanel.setAllowedFileTypes(ArrayToNSArray(['aer']));
end;
end;
function FileTypeAView(Types: NSArray): NSView;
var
AccessoryView: NSView;
ViewLabel: NSTextField;
FormatSelector: NSPopUpButton;
begin
AccessoryView := TNSView.Wrap(TNSView.OCClass.alloc);
AccessoryView.initWithFrame(NSMakeRect(0, 0, 300, 32));
ViewLabel := TNSTextField.Wrap(TNSTextField.OCClass.alloc);
ViewLabel.initWithFrame(NSMakeRect(0, 0, 60, 22));
ViewLabel.setEditable(False);
ViewLabel.setStringValue(StrToNSStr('Format:'));
ViewLabel.setBordered(False);
ViewLabel.setBezeled(False);
ViewLabel.setDrawsBackground(False);
FormatSelector := TNSPopUpButton.Wrap(TNSPopUpButton.OCClass.alloc);
FormatSelector.initWithFrame(NSMakeRect(50, 2, 240, 22), False);
FormatSelector.addItemsWithTitles(Types);
FormatSelector.setAction(sel_getUid('selectFormat:'));
FormatSelector.setTarget(nil); //Here must be NSApplicationDelegate, but it is not accessable
AccessoryView.addSubview(ViewLabel);
AccessoryView.addSubview(FormatSelector);
Result := AccessoryView;
end;
function InvokeNSOpenPanel(const Title: String; const InitialDir: String;
const Flags: String; const AllowedFileTypes: TArray<String>;
var ADir: String): Boolean;
var
IResult: NSInteger;
NSInitialDir: NSURL;
multiple, others, create, hidden: Boolean;
begin
{$REGION 'Flags Assignment'}
if Flags.Contains('multiple') then
multiple := True
else
multiple := False;
if Flags.Contains('others') then
others := True
else
others := False;
if Flags.Contains('create') then
create := True
else
create := False;
if Flags.Contains('hidden') then
hidden := True
else
hidden := False;
{$ENDREGION}
Result := False;
OpenPanel := TNSOpenPanel.Wrap(TNSOpenPanel.OCClass.openPanel);
OpenPanel.setAllowsMultipleSelection(multiple);
OpenPanel.setAllowsOtherFileTypes(others);
OpenPanel.setCanCreateDirectories(create);
OpenPanel.setCanSelectHiddenExtension(hidden);
OpenPanel.setAllowedFileTypes(ArrayToNSArray(AllowedFileTypes));
if InitialDir <> '' then
begin
NSInitialDir := TNSURL.Create;
NSInitialDir.initFileURLWithPath(StrToNSStr(InitialDir));
OpenPanel.setDirectoryURL(NSInitialDir);
end;
if Title <> '' then
OpenPanel.setTitle(StrToNSStr(Title));
OpenPanel.retain;
try
IResult := OpenPanel.runModal;
if IResult = NSOKButton then
ADir := NSStrToStr(TNSUrl.Wrap(OpenPanel.URLs.objectAtIndex(0)).relativePath);
Result := True;
finally
OpenPanel.Release;
end;
end;
function InvokeNSSavePanel(const Title: String; const InitialDir: String;
const Flags: String; const FileTypesDescription: TArray<String>; const AllowedFileTypes: TArray<String>;
var ADir: String): Boolean;
var
IResult: NSInteger;
NSInitialDir: NSURL;
others, create, hidden: Boolean;
begin
{$REGION 'Flags Assignment'}
if Flags.Contains('others') then
others := True
else
others := False;
if Flags.Contains('create') then
create := True
else
create := False;
if Flags.Contains('hidden') then
hidden := True
else
hidden := False;
//FOwner := Owner;
{$ENDREGION}
Result := False;
SavePanel := TNSSavePanel.Wrap(TNSSavePanel.OCClass.savePanel);
SavePanel.setAllowsOtherFileTypes(others);
SavePanel.setCanCreateDirectories(create);
SavePanel.setCanSelectHiddenExtension(hidden);
SavePanel.setAllowedFileTypes(ArrayToNSArray(AllowedFileTypes));
SavePanel.setAccessoryView(FileTypeAView(ArrayToNSArray(FileTypesDescription)));
if InitialDir <> '' then
begin
NSInitialDir := TNSURL.Create;
NSInitialDir.initFileURLWithPath(StrToNSStr(InitialDir));
SavePanel.setDirectoryURL(NSInitialDir);
end;
if Title <> '' then
SavePanel.setTitle(StrToNSStr(Title));
SavePanel.retain;
try
IResult := SavePanel.runModal;
if IResult = NSOKButton then
ADir := NSStrToStr(SavePanel.URL.relativePath);
Result := True;
finally
SavePanel.Release;
end;
end;
function InvokeNSDirOpenPanel(const Title: String; const InitialDir: String;
const Flags: String; var ADir: String): Boolean;
var
NSInitialDir: NSURL;
LDlgResult: NSInteger;
multiple, create, hidden: Boolean;
begin
{$REGION 'Flags Assignment'}
if Flags.Contains('multiple') then
multiple := True
else
multiple := False;
if Flags.Contains('create') then
create := True
else
create := False;
if Flags.Contains('hidden') then
hidden := True
else
hidden := False;
{$ENDREGION}
Result := False;
OpenPanel := TNSOpenPanel.Wrap(TNSOpenPanel.OCClass.openPanel);
OpenPanel.setAllowsMultipleSelection(multiple);
OpenPanel.setCanCreateDirectories(create);
OpenPanel.setCanSelectHiddenExtension(hidden);
OpenPanel.setCanChooseFiles(False);
OpenPanel.setCanChooseDirectories(True);
if InitialDir <> '' then
begin
NSInitialDir := TNSURL.Create;
NSInitialDir.initFileURLWithPath(StrToNSStr(InitialDir));
OpenPanel.setDirectoryURL(NSInitialDir);
end;
if Title <> '' then
OpenPanel.setTitle(StrToNSStr(Title));
OpenPanel.retain;
try
LDlgResult := OpenPanel.runModal;
if LDlgResult = NSOKButton then
begin
ADir := string(TNSUrl.Wrap(OpenPanel.URLs.objectAtIndex(0)).relativePath.UTF8String);
Result := True;
end;
finally
OpenPanel.release;
end;
end;
end.