From 3085c78bcfb3152f6e52ef0f786f86d5d57eaa7a Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 9 Feb 2018 21:38:21 -0500 Subject: [PATCH] implement selectFormat selector --- atom/browser/ui/file_dialog_mac.mm | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/atom/browser/ui/file_dialog_mac.mm b/atom/browser/ui/file_dialog_mac.mm index f634da7b40f..de25e45281a 100644 --- a/atom/browser/ui/file_dialog_mac.mm +++ b/atom/browser/ui/file_dialog_mac.mm @@ -14,10 +14,42 @@ #include "base/mac/scoped_cftyperef.h" #include "base/strings/sys_string_conversions.h" +@interface PopUpButtonHandler : NSObject +@property (nonatomic, weak) NSSavePanel *savePanel; +@property (nonatomic, weak) 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 { +static PopUpButtonHandler *popUpButtonHandler; + void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) { NSMutableSet* file_type_set = [NSMutableSet set]; for (size_t i = 0; i < filters.size(); ++i) { @@ -43,6 +75,9 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) { [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)]; @@ -55,6 +90,7 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) { 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];