1
- // LAF OS Library
2
- // Copyright (C) 2020-2022 Igara Studio S.A.
1
+ // laf-dlgs
2
+ // Copyright (C) 2020-2024 Igara Studio S.A.
3
3
// Copyright (C) 2012-2018 David Capello
4
4
//
5
5
// This file is released under the terms of the MIT license.
8
8
#include < Cocoa/Cocoa.h>
9
9
#include < vector>
10
10
11
+ #include " dlgs/file_dialog.h"
12
+
11
13
#include " base/fs.h"
12
- #include " os/common/file_dialog.h"
13
- #include " os/keys.h"
14
- #include " os/native_cursor.h"
15
- #include " os/osx/native_dialogs.h"
16
- #include " os/window.h"
17
14
18
15
#include < map>
19
16
26
23
// etc.
27
24
//
28
25
// So what we have to do if the main menu doesn't provide the standard
29
- // Edit selectors? (which is our case with our MenuItemOSX and MenuOSX
30
- // implementations) :
26
+ // " Edit" selectors? Which can be our case with laf-os MenuItemOSX and
27
+ // MenuOSX implementations:
31
28
//
32
- // 1. Before we open the NSSavePanel, find the Edit menu that was
33
- // specified by the user with os::MenuItem::setAsStandardEditMenuItem()
34
- // 2. Replace the Edit menu with a custom made one with the
35
- // standard Edit menu prepared especially with selectors
36
- // (undo:, redo:, cut:, etc.)
29
+ // 1. Before we open the NSSavePanel, this class receives the current
30
+ // "Edit" menu that was specified by the user from
31
+ // FileDialog::Spec::editNSMenuItem.
32
+ // 2. Replaces the "Edit" menu with a custom made one with the
33
+ // standard Edit menu prepared especially with selectors (undo:,
34
+ // redo:, cut:, etc.)
37
35
// 3. Each menu item that contains a standard keyboard shortcut
38
36
// (Command+C, Command+V, etc.) must be modified, because those
39
37
// shortcuts are now used by this new Edit menu. So we remove the
40
38
// keyEquivalent of each one. This is necessary only when one of
41
39
// those items are outside the replaced Edit menu (e.g. if we have
42
40
// Command+A to select all in other menu like Select > All, instead
43
41
// of Edit > Select All)
44
- // 4. After the NSSavePanel is closed, we restore all keyEquivalent
42
+ // 4. After the NSSavePanel is closed, it restores all keyEquivalent
45
43
// shortcuts and the old Edit menu.
46
44
//
47
45
class OSXEditMenuHack {
@@ -121,20 +119,16 @@ void disableMenuItemsWithKeyEquivalent(NSMenu* menu,
121
119
122
120
} // anonymous namespace
123
121
124
- namespace os {
125
- extern NSMenuItem * g_standardEditMenuItem;
126
- }
127
-
128
122
@interface OpenSaveHelper : NSObject {
129
123
@private
130
124
NSSavePanel * panel;
131
- os::Window * window;
125
+ NSWindow * window;
132
126
int result;
133
127
std::function<void ()> fileTypeChangeObserver;
134
128
}
135
129
- (id )init ;
136
130
- (void )setPanel : (NSSavePanel *)panel ;
137
- - (void )setWindow : (os::Window *)window ;
131
+ - (void )setWindow : (NSWindow *)window ;
138
132
- (void )runModal ;
139
133
- (int )result ;
140
134
- (void )fileTypeChange ;
@@ -146,6 +140,8 @@ @implementation OpenSaveHelper
146
140
- (id )init
147
141
{
148
142
if (self = [super init ]) {
143
+ panel = nil ;
144
+ window = nil ;
149
145
result = NSFileHandlingPanelCancelButton ;
150
146
}
151
147
return self;
@@ -156,18 +152,14 @@ - (void)setPanel:(NSSavePanel*)newPanel
156
152
panel = newPanel;
157
153
}
158
154
159
- - (void )setWindow : (os::Window *)newWindow
155
+ - (void )setWindow : (NSWindow *)newWindow
160
156
{
161
157
window = newWindow;
162
158
}
163
159
164
160
// This is executed in the main thread.
165
161
- (void )runModal
166
162
{
167
- os::NativeCursor oldCursor = window->nativeCursor ();
168
- window->setCursor (os::NativeCursor::Arrow);
169
-
170
- OSXEditMenuHack hack (os::g_standardEditMenuItem);
171
163
[[[NSApplication sharedApplication ] mainMenu ] setAutoenablesItems: NO ];
172
164
173
165
#ifndef __MAC_10_6 // runModalForTypes is deprecated in 10.6
@@ -184,10 +176,11 @@ - (void)runModal
184
176
result = [panel runModal ];
185
177
}
186
178
187
- window->setCursor (oldCursor);
188
- NSWindow * nsWindow = (__bridge NSWindow *)window->nativeHandle ();
189
- [nsWindow makeKeyAndOrderFront: nil ];
190
179
[[[NSApplication sharedApplication ] mainMenu ] setAutoenablesItems: YES ];
180
+
181
+ // Re-active the parent key window.
182
+ if (window)
183
+ [window makeKeyAndOrderFront: nil ];
191
184
}
192
185
193
186
- (int )result
@@ -208,11 +201,13 @@ - (void)setFileTypeChangeObserver:(std::function<void()>)observer
208
201
209
202
@end
210
203
211
- namespace os {
204
+ namespace dlgs {
212
205
213
- class FileDialogOSX : public CommonFileDialog {
206
+ class FileDialogOSX : public FileDialog {
214
207
public:
215
- FileDialogOSX () {
208
+ FileDialogOSX (const Spec& spec) {
209
+ if (spec.editNSMenuItem )
210
+ m_editMenuItem = (__bridge NSMenuItem *)spec.editNSMenuItem ;
216
211
}
217
212
218
213
std::string fileName () override {
@@ -227,7 +222,7 @@ void setFileName(const std::string& filename) override {
227
222
m_filename = filename;
228
223
}
229
224
230
- Result show (Window * window) override {
225
+ Result show (void * window) override {
231
226
Result retValue = Result::Cancel;
232
227
@autoreleasepool {
233
228
NSSavePanel * panel = nil ;
@@ -282,7 +277,8 @@ Result show(Window* window) override {
282
277
283
278
OpenSaveHelper* helper = [OpenSaveHelper new ];
284
279
[helper setPanel: panel];
285
- [helper setWindow: window];
280
+ if (window)
281
+ [helper setWindow: (__bridge NSWindow *)window];
286
282
287
283
// Configure the file type popup/combobox.
288
284
if (m_popup) {
@@ -323,7 +319,10 @@ Result show(Window* window) override {
323
319
}];
324
320
}
325
321
326
- [helper performSelectorOnMainThread: @selector (runModal ) withObject: nil waitUntilDone: YES ];
322
+ {
323
+ OSXEditMenuHack hack (m_editMenuItem);
324
+ [helper performSelectorOnMainThread: @selector (runModal ) withObject: nil waitUntilDone: YES ];
325
+ }
327
326
328
327
if ([helper result ] == NSFileHandlingPanelOKButton ) {
329
328
if (m_type == Type::OpenFiles) {
@@ -385,15 +384,14 @@ Result show(Window* window) override {
385
384
// easily (configuring its target/action properties + accessing its
386
385
// selected item).
387
386
NSPopUpButton * m_popup = nullptr ;
388
- };
389
387
390
- NativeDialogsOSX::NativeDialogsOSX ()
391
- {
392
- }
388
+ // Pointer to the "Edit" menu used with OSXEditMenuHack.
389
+ NSMenuItem * m_editMenuItem = nullptr ;
390
+ };
393
391
394
- FileDialogRef NativeDialogsOSX::makeFileDialog ( )
392
+ FileDialogRef FileDialog::makeOSX ( const Spec& spec )
395
393
{
396
- return make_ref<FileDialogOSX>();
394
+ return base:: make_ref<FileDialogOSX>(spec );
397
395
}
398
396
399
- } // namespace os
397
+ } // namespace dlgs
0 commit comments