Blender V5.3
fsmenu_system_macos.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#import "Foundation/Foundation.h"
11
12#include "BLI_fileops.hh"
13#include "BLI_path_utils.hh"
14#include "BLI_string.hh"
15
16#include "BLT_translation.hh"
17#include "UI_resources.hh"
18
19#include "fsmenu.hh"
20
21namespace blender {
22
23struct FSMenu;
24
26 const char *name,
27 const char *default_path,
28 const int icon,
29 const char *home,
30 const FSMenuCategory category = FS_CATEGORY_OTHER,
31 const FSMenuInsert insert_flag = FS_INSERT_LAST)
32{
33 char path[FILE_MAXDIR];
34 SNPRINTF(path, default_path, home);
35
36 fsmenu_insert_entry(fsmenu, category, path, name, icon, insert_flag);
37}
38
39void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
40{
41 /* We store some known macOS system paths and corresponding icons
42 * and names in the FS_CATEGORY_OTHER (not displayed directly) category. */
44 fsmenu, FS_CATEGORY_OTHER, "/Library/Fonts/", N_("Fonts"), ICON_FILE_FONT, FS_INSERT_LAST);
47 "/Applications/",
48 N_("Applications"),
49 ICON_FILE_FOLDER,
51
52 const char *home = BLI_dir_home();
53 if (home) {
54 fsmenu_macos_insert_entry(fsmenu, nullptr, "%s/", ICON_HOME, home);
55 fsmenu_macos_insert_entry(fsmenu, N_("Desktop"), "%s/Desktop/", ICON_DESKTOP, home);
56 fsmenu_macos_insert_entry(fsmenu, N_("Documents"), "%s/Documents/", ICON_DOCUMENTS, home);
57 fsmenu_macos_insert_entry(fsmenu, N_("Downloads"), "%s/Downloads/", ICON_IMPORT, home);
58 fsmenu_macos_insert_entry(fsmenu, N_("Movies"), "%s/Movies/", ICON_FILE_MOVIE, home);
59 fsmenu_macos_insert_entry(fsmenu, N_("Music"), "%s/Music/", ICON_FILE_SOUND, home);
60 fsmenu_macos_insert_entry(fsmenu, N_("Pictures"), "%s/Pictures/", ICON_FILE_IMAGE, home);
61 fsmenu_macos_insert_entry(fsmenu, N_("Fonts"), "%s/Library/Fonts/", ICON_FILE_FONT, home);
62 }
63
64 NSFileManager *file_manager = [NSFileManager defaultManager];
65
66 NSArray *resource_keys =
67 @[ NSURLVolumeLocalizedNameKey, NSURLVolumeIsLocalKey, NSURLVolumeIsRemovableKey ];
68
69 NSArray *mounted_volume_urls = [file_manager
70 mountedVolumeURLsIncludingResourceValuesForKeys:resource_keys
71 options:NSVolumeEnumerationSkipHiddenVolumes];
72
73 for (NSURL *volume_url in mounted_volume_urls) {
74 NSError *error = nil;
75 NSDictionary *resources_values = [volume_url resourceValuesForKeys:resource_keys error:&error];
76
77 if (!error) {
78 NSString *volume_name = resources_values[NSURLVolumeLocalizedNameKey];
79 NSNumber *volume_is_local = resources_values[NSURLVolumeIsLocalKey];
80 NSNumber *volume_is_ejectable = resources_values[NSURLVolumeIsRemovableKey];
81
82 /* Set icon for regular, removable or network drive. */
83 int icon = ICON_DISK_DRIVE;
84 if (!volume_is_local.boolValue) {
85 icon = ICON_NETWORK_DRIVE;
86 }
87 else if (volume_is_ejectable.boolValue) {
88 icon = ICON_EXTERNAL_DRIVE;
89 }
90
93 volume_url.path.UTF8String,
94 volume_name.UTF8String,
95 icon,
97 }
98 }
99
100 /* iCloud Drive support, shown as a network volume, similarly to how it's displayed in Finder. */
101 if (home) {
103 N_("iCloud Drive"),
104 "%s/Library/Mobile Documents/com~apple~CloudDocs/",
105 ICON_NETWORK_DRIVE,
106 home,
109 }
110
111 /* The LSSharedFileList API has been deprecated, and no replacement has been provided to obtain
112 * the user's Finder Favorites items from other applications. Ignore these deprecation warnings.
113 * It is unknown when this API will be fully removed from macOS. */
114 if (read_bookmarks) {
115#pragma GCC diagnostic push
116#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
117 LSSharedFileListRef shared_list = LSSharedFileListCreate(
118 nullptr, kLSSharedFileListFavoriteItems, nullptr);
119
120 UInt32 seed;
121 NSArray *paths_array = (__bridge NSArray *)LSSharedFileListCopySnapshot(shared_list, &seed);
122
123 for (id item in paths_array) {
124 LSSharedFileListItemRef item_ref = (__bridge LSSharedFileListItemRef)item;
125
126 CFURLRef cf_url = nullptr;
127 OSErr err = LSSharedFileListItemResolve(item_ref,
128 kLSSharedFileListNoUserInteraction |
129 kLSSharedFileListDoNotMountVolumes,
130 &cf_url,
131 nullptr);
132 if (err != noErr || !cf_url) {
133 continue;
134 }
135
136 NSURL *url = (__bridge NSURL *)cf_url;
137 NSString *path = [url path];
138
139 if (!path) {
140 CFRelease(cf_url);
141 continue;
142 }
143
144 // Exclude "all my files" and empty paths
145 if (![path containsString:@"myDocuments.cannedSearch"] && [path length] > 0) {
146 fsmenu_insert_entry(fsmenu,
148 [path UTF8String],
149 nullptr,
150 ICON_FILE_FOLDER,
152 }
153
154 CFRelease(cf_url);
155 }
156
157 [paths_array release];
158 CFRelease(shared_list);
159#pragma GCC diagnostic pop
160 }
161}
162
163} // namespace blender
File and directory operations.
#define FILE_MAXDIR
#define SNPRINTF(dst, format,...)
CCL_NAMESPACE_BEGIN struct Options options
float length(VecOp< float, D >) RET
BMesh Iterators.
void fsmenu_macos_insert_entry(FSMenu *fsmenu, const char *name, const char *default_path, const int icon, const char *home, const FSMenuCategory category=FS_CATEGORY_OTHER, const FSMenuInsert insert_flag=FS_INSERT_LAST)
const char * BLI_dir_home()
Definition storage.cc:99
void fsmenu_insert_entry(FSMenu *fsmenu, FSMenuCategory category, const char *path, const char *name, int icon, FSMenuInsert flag)
Definition fsmenu.cc:229
@ FS_CATEGORY_SYSTEM_BOOKMARKS
void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
#define N_(msgid)