Merge pull request #11873 from electron/add-file-format-picker
add accessory view for format picker
This commit is contained in:
commit
647f0f3a57
1 changed files with 56 additions and 0 deletions
|
@ -14,10 +14,42 @@
|
||||||
#include "base/mac/scoped_cftyperef.h"
|
#include "base/mac/scoped_cftyperef.h"
|
||||||
#include "base/strings/sys_string_conversions.h"
|
#include "base/strings/sys_string_conversions.h"
|
||||||
|
|
||||||
|
@interface PopUpButtonHandler : NSObject
|
||||||
|
@property (nonatomic, strong) NSSavePanel *savePanel;
|
||||||
|
@property (nonatomic, strong) NSArray *fileTypes;
|
||||||
|
- (instancetype)initWithPanel:(NSSavePanel *)panel andTypes:(NSArray *)types;
|
||||||
|
- (void)selectFormat:(id)sender;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation PopUpButtonHandler
|
||||||
|
- (instancetype)initWithPanel:(NSSavePanel *)panel andTypes:(NSArray *)types {
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
_savePanel = panel;
|
||||||
|
_fileTypes = types;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)selectFormat:(id)sender {
|
||||||
|
NSPopUpButton *button = (NSPopUpButton *)sender;
|
||||||
|
NSInteger selectedItemIndex = [button indexOfSelectedItem];
|
||||||
|
NSString *nameFieldString = [[self savePanel] nameFieldStringValue];
|
||||||
|
NSString *trimmedNameFieldString = [nameFieldString stringByDeletingPathExtension];
|
||||||
|
NSString *extension = [[self fileTypes] objectAtIndex: selectedItemIndex];
|
||||||
|
|
||||||
|
NSString *nameFieldStringWithExt = [NSString stringWithFormat:@"%@.%@", trimmedNameFieldString, extension];
|
||||||
|
[[self savePanel] setNameFieldStringValue:nameFieldStringWithExt];
|
||||||
|
[[self savePanel] setAllowedFileTypes:@[extension]];
|
||||||
|
}
|
||||||
|
@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];
|
NSMutableSet* file_type_set = [NSMutableSet set];
|
||||||
for (size_t i = 0; i < filters.size(); ++i) {
|
for (size_t i = 0; i < filters.size(); ++i) {
|
||||||
|
@ -25,6 +57,7 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
|
||||||
for (size_t j = 0; j < filter.second.size(); ++j) {
|
for (size_t j = 0; j < filter.second.size(); ++j) {
|
||||||
// If we meet a '*' file extension, we allow all the file types and no
|
// If we meet a '*' file extension, we allow all the file types and no
|
||||||
// need to set the specified file types.
|
// need to set the specified file types.
|
||||||
|
|
||||||
if (filter.second[j] == "*") {
|
if (filter.second[j] == "*") {
|
||||||
[dialog setAllowsOtherFileTypes:YES];
|
[dialog setAllowsOtherFileTypes:YES];
|
||||||
return;
|
return;
|
||||||
|
@ -41,6 +74,29 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
|
||||||
file_types = [file_type_set allObjects];
|
file_types = [file_type_set allObjects];
|
||||||
|
|
||||||
[dialog setAllowedFileTypes:file_types];
|
[dialog setAllowedFileTypes:file_types];
|
||||||
|
|
||||||
|
if (!popUpButtonHandler)
|
||||||
|
popUpButtonHandler = [[PopUpButtonHandler alloc] initWithPanel:dialog andTypes:file_types];
|
||||||
|
|
||||||
|
// add file format picker
|
||||||
|
NSView *accessoryView = [[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 200, 32.0)];
|
||||||
|
NSTextField *label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 60, 22)];
|
||||||
|
|
||||||
|
[label setEditable:NO];
|
||||||
|
[label setStringValue:@"Format:"];
|
||||||
|
[label setBordered:NO];
|
||||||
|
[label setBezeled:NO];
|
||||||
|
[label setDrawsBackground:NO];
|
||||||
|
|
||||||
|
NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(50.0, 2, 140, 22.0) pullsDown:NO];
|
||||||
|
[popupButton addItemsWithTitles:file_types];
|
||||||
|
[popupButton setTarget:popUpButtonHandler];
|
||||||
|
[popupButton setAction:@selector(selectFormat:)];
|
||||||
|
|
||||||
|
[accessoryView addSubview:label];
|
||||||
|
[accessoryView addSubview:popupButton];
|
||||||
|
|
||||||
|
[dialog setAccessoryView:accessoryView];
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetupDialog(NSSavePanel* dialog,
|
void SetupDialog(NSSavePanel* dialog,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue