зеркало из https://github.com/electron/electron.git
Merge pull request #11959 from yuya-oc/filter-for-mac-dialog
show file filter name for accessory view of file dialog
This commit is contained in:
Коммит
0e5aaab0b5
|
@ -15,72 +15,99 @@
|
||||||
#include "base/strings/sys_string_conversions.h"
|
#include "base/strings/sys_string_conversions.h"
|
||||||
|
|
||||||
@interface PopUpButtonHandler : NSObject
|
@interface PopUpButtonHandler : NSObject
|
||||||
@property (nonatomic, strong) NSSavePanel *savePanel;
|
|
||||||
@property (nonatomic, strong) NSArray *fileTypes;
|
@property(nonatomic, assign) NSSavePanel* savePanel;
|
||||||
- (instancetype)initWithPanel:(NSSavePanel *)panel andTypes:(NSArray *)types;
|
@property(nonatomic, strong) NSArray* fileTypesList;
|
||||||
|
|
||||||
|
- (instancetype)initWithPanel:(NSSavePanel*)panel
|
||||||
|
andTypesList:(NSArray*)typesList;
|
||||||
- (void)selectFormat:(id)sender;
|
- (void)selectFormat:(id)sender;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation PopUpButtonHandler
|
@implementation PopUpButtonHandler
|
||||||
- (instancetype)initWithPanel:(NSSavePanel *)panel andTypes:(NSArray *)types {
|
|
||||||
|
- (instancetype)initWithPanel:(NSSavePanel*)panel
|
||||||
|
andTypesList:(NSArray*)typesList {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
_savePanel = panel;
|
[self setSavePanel:panel];
|
||||||
_fileTypes = types;
|
[self setFileTypesList:typesList];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)selectFormat:(id)sender {
|
- (void)selectFormat:(id)sender {
|
||||||
NSPopUpButton *button = (NSPopUpButton *)sender;
|
NSPopUpButton* button = (NSPopUpButton*)sender;
|
||||||
NSInteger selectedItemIndex = [button indexOfSelectedItem];
|
NSInteger selectedItemIndex = [button indexOfSelectedItem];
|
||||||
NSString *nameFieldString = [[self savePanel] nameFieldStringValue];
|
NSArray* list = [self fileTypesList];
|
||||||
NSString *trimmedNameFieldString = [nameFieldString stringByDeletingPathExtension];
|
NSArray* fileTypes = [list objectAtIndex:selectedItemIndex];
|
||||||
NSString *extension = [[self fileTypes] objectAtIndex: selectedItemIndex];
|
|
||||||
|
|
||||||
NSString *nameFieldStringWithExt = [NSString stringWithFormat:@"%@.%@", trimmedNameFieldString, extension];
|
// If we meet a '*' file extension, we allow all the file types and no
|
||||||
[[self savePanel] setNameFieldStringValue:nameFieldStringWithExt];
|
// need to set the specified file types.
|
||||||
[[self savePanel] setAllowedFileTypes:@[extension]];
|
if ([fileTypes count] == 0 || [fileTypes containsObject:@"*"])
|
||||||
|
[[self savePanel] setAllowedFileTypes:nil];
|
||||||
|
else
|
||||||
|
[[self savePanel] setAllowedFileTypes:fileTypes];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
// Manages the PopUpButtonHandler.
|
||||||
|
@interface AtomAccessoryView : NSView
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation AtomAccessoryView
|
||||||
|
|
||||||
|
- (void)dealloc {
|
||||||
|
auto* popupButton = static_cast<NSPopUpButton*>([[self subviews] objectAtIndex: 1]);
|
||||||
|
[[popupButton target] release];
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
namespace file_dialog {
|
namespace file_dialog {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
static PopUpButtonHandler *popUpButtonHandler;
|
|
||||||
|
|
||||||
void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
|
void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
|
||||||
NSMutableSet* file_type_set = [NSMutableSet set];
|
NSMutableArray* file_types_list = [NSMutableArray array];
|
||||||
for (size_t i = 0; i < filters.size(); ++i) {
|
NSMutableArray* filter_names = [NSMutableArray array];
|
||||||
const Filter& filter = filters[i];
|
|
||||||
for (size_t j = 0; j < filter.second.size(); ++j) {
|
|
||||||
// If we meet a '*' file extension, we allow all the file types and no
|
|
||||||
// need to set the specified file types.
|
|
||||||
|
|
||||||
if (filter.second[j] == "*") {
|
// Create array to keep file types and their name.
|
||||||
[dialog setAllowsOtherFileTypes:YES];
|
for (const Filter& filter : filters) {
|
||||||
return;
|
NSMutableSet* file_type_set = [NSMutableSet set];
|
||||||
}
|
base::ScopedCFTypeRef<CFStringRef> name_cf(
|
||||||
base::ScopedCFTypeRef<CFStringRef> ext_cf(
|
base::SysUTF8ToCFStringRef(filter.first));
|
||||||
base::SysUTF8ToCFStringRef(filter.second[j]));
|
[filter_names addObject:base::mac::CFToNSCast(name_cf.get())];
|
||||||
|
for (const std::string& ext : filter.second) {
|
||||||
|
base::ScopedCFTypeRef<CFStringRef> ext_cf(base::SysUTF8ToCFStringRef(ext));
|
||||||
[file_type_set addObject:base::mac::CFToNSCast(ext_cf.get())];
|
[file_type_set addObject:base::mac::CFToNSCast(ext_cf.get())];
|
||||||
}
|
}
|
||||||
|
[file_types_list addObject:[file_type_set allObjects]];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Passing empty array to setAllowedFileTypes will cause exception.
|
// Passing empty array to setAllowedFileTypes will cause exception.
|
||||||
NSArray* file_types = nil;
|
NSArray* file_types = nil;
|
||||||
if ([file_type_set count])
|
NSUInteger count = [file_types_list count];
|
||||||
file_types = [file_type_set allObjects];
|
if (count > 0) {
|
||||||
|
file_types = [[file_types_list objectAtIndex:0] allObjects];
|
||||||
|
// If we meet a '*' file extension, we allow all the file types and no
|
||||||
|
// need to set the specified file types.
|
||||||
|
if ([file_types count] == 0 || [file_types containsObject:@"*"])
|
||||||
|
file_types = nil;
|
||||||
|
}
|
||||||
[dialog setAllowedFileTypes:file_types];
|
[dialog setAllowedFileTypes:file_types];
|
||||||
|
|
||||||
if (!popUpButtonHandler)
|
if (count <= 1)
|
||||||
popUpButtonHandler = [[PopUpButtonHandler alloc] initWithPanel:dialog andTypes:file_types];
|
return; // don't add file format picker
|
||||||
|
|
||||||
// add file format picker
|
// Add file format picker.
|
||||||
NSView *accessoryView = [[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 200, 32.0)];
|
AtomAccessoryView* accessoryView =
|
||||||
NSTextField *label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 60, 22)];
|
[[AtomAccessoryView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 200, 32.0)];
|
||||||
|
NSTextField* label =
|
||||||
|
[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 60, 22)];
|
||||||
|
|
||||||
[label setEditable:NO];
|
[label setEditable:NO];
|
||||||
[label setStringValue:@"Format:"];
|
[label setStringValue:@"Format:"];
|
||||||
|
@ -88,15 +115,18 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
|
||||||
[label setBezeled:NO];
|
[label setBezeled:NO];
|
||||||
[label setDrawsBackground:NO];
|
[label setDrawsBackground:NO];
|
||||||
|
|
||||||
NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(50.0, 2, 140, 22.0) pullsDown:NO];
|
NSPopUpButton* popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(50.0, 2, 140, 22.0)
|
||||||
[popupButton addItemsWithTitles:file_types];
|
pullsDown:NO];
|
||||||
|
PopUpButtonHandler* popUpButtonHandler = [[PopUpButtonHandler alloc] initWithPanel:dialog
|
||||||
|
andTypesList:file_types_list];
|
||||||
|
[popupButton addItemsWithTitles:filter_names];
|
||||||
[popupButton setTarget:popUpButtonHandler];
|
[popupButton setTarget:popUpButtonHandler];
|
||||||
[popupButton setAction:@selector(selectFormat:)];
|
[popupButton setAction:@selector(selectFormat:)];
|
||||||
|
|
||||||
[accessoryView addSubview:label];
|
[accessoryView addSubview:[label autorelease]];
|
||||||
[accessoryView addSubview:popupButton];
|
[accessoryView addSubview:[popupButton autorelease]];
|
||||||
|
|
||||||
[dialog setAccessoryView:accessoryView];
|
[dialog setAccessoryView:[accessoryView autorelease]];
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetupDialog(NSSavePanel* dialog,
|
void SetupDialog(NSSavePanel* dialog,
|
||||||
|
@ -118,6 +148,7 @@ void SetupDialog(NSSavePanel* dialog,
|
||||||
NSString* default_dir = nil;
|
NSString* default_dir = nil;
|
||||||
NSString* default_filename = nil;
|
NSString* default_filename = nil;
|
||||||
if (!settings.default_path.empty()) {
|
if (!settings.default_path.empty()) {
|
||||||
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||||
if (base::DirectoryExists(settings.default_path)) {
|
if (base::DirectoryExists(settings.default_path)) {
|
||||||
default_dir = base::SysUTF8ToNSString(settings.default_path.value());
|
default_dir = base::SysUTF8ToNSString(settings.default_path.value());
|
||||||
} else {
|
} else {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче