| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import <Cocoa/Cocoa.h> | |
| 6 | |
| 7 #import "base/mac/scoped_nsobject.h" | |
| 8 #import "chrome/browser/web_applications/web_app_mac.h" | |
| 9 #import "ui/base/l10n/l10n_util_mac.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h" | |
| 13 #include "chrome/grit/generated_resources.h" | |
| 14 #include "components/strings/grit/components_strings.h" | |
| 15 #include "ui/gfx/image/image.h" | |
| 16 | |
| 17 @interface CrCreateAppShortcutCheckboxObserver : NSObject { | |
| 18 @private | |
| 19 NSButton* checkbox_; | |
| 20 NSButton* continueButton_; | |
| 21 } | |
| 22 | |
| 23 - (id)initWithCheckbox:(NSButton*)checkbox | |
| 24 continueButton:(NSButton*)continueButton; | |
| 25 - (void)startObserving; | |
| 26 - (void)stopObserving; | |
| 27 @end | |
| 28 | |
| 29 @implementation CrCreateAppShortcutCheckboxObserver | |
| 30 | |
| 31 - (id)initWithCheckbox:(NSButton*)checkbox | |
| 32 continueButton:(NSButton*)continueButton { | |
| 33 if ((self = [super init])) { | |
| 34 checkbox_ = checkbox; | |
| 35 continueButton_ = continueButton; | |
| 36 } | |
| 37 return self; | |
| 38 } | |
| 39 | |
| 40 - (void)startObserving { | |
| 41 [checkbox_ addObserver:self forKeyPath:@"cell.state" options:0 context:nil]; | |
| 42 } | |
| 43 | |
| 44 - (void)stopObserving { | |
| 45 [checkbox_ removeObserver:self forKeyPath:@"cell.state"]; | |
| 46 } | |
| 47 | |
| 48 - (void)observeValueForKeyPath:(NSString*)keyPath | |
| 49 ofObject:(id)object | |
| 50 change:(NSDictionary*)change | |
| 51 context:(void*)context { | |
| 52 [continueButton_ setEnabled:([checkbox_ state] == NSOnState)]; | |
| 53 } | |
| 54 | |
| 55 @end | |
| 56 | |
| 57 namespace { | |
| 58 | |
| 59 // Called when the app's ShortcutInfo (with icon) is loaded when creating app | |
| 60 // shortcuts. | |
| 61 void CreateAppShortcutInfoLoaded( | |
| 62 Profile* profile, | |
| 63 const extensions::Extension* app, | |
| 64 const base::Callback<void(bool)>& close_callback, | |
| 65 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) { | |
| 66 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); | |
| 67 | |
| 68 NSButton* continue_button = [alert | |
| 69 addButtonWithTitle:l10n_util::GetNSString(IDS_CREATE_SHORTCUTS_COMMIT)]; | |
| 70 [continue_button setKeyEquivalent:kKeyEquivalentReturn]; | |
| 71 | |
| 72 NSButton* cancel_button = | |
| 73 [alert addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)]; | |
| 74 [cancel_button setKeyEquivalent:kKeyEquivalentEscape]; | |
| 75 | |
| 76 [alert setMessageText:l10n_util::GetNSString(IDS_CREATE_SHORTCUTS_LABEL)]; | |
| 77 [alert setAlertStyle:NSInformationalAlertStyle]; | |
| 78 | |
| 79 base::scoped_nsobject<NSButton> application_folder_checkbox( | |
| 80 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
| 81 [application_folder_checkbox setButtonType:NSSwitchButton]; | |
| 82 [application_folder_checkbox | |
| 83 setTitle:l10n_util::GetNSString(IDS_CREATE_SHORTCUTS_APP_FOLDER_CHKBOX)]; | |
| 84 [application_folder_checkbox setState:NSOnState]; | |
| 85 [application_folder_checkbox sizeToFit]; | |
| 86 | |
| 87 base::scoped_nsobject<CrCreateAppShortcutCheckboxObserver> checkbox_observer( | |
| 88 [[CrCreateAppShortcutCheckboxObserver alloc] | |
| 89 initWithCheckbox:application_folder_checkbox | |
| 90 continueButton:continue_button]); | |
| 91 [checkbox_observer startObserving]; | |
| 92 | |
| 93 [alert setAccessoryView:application_folder_checkbox]; | |
| 94 | |
| 95 const int kIconPreviewSizePixels = 128; | |
| 96 const int kIconPreviewTargetSize = 64; | |
| 97 const gfx::Image* icon = shortcut_info->favicon.GetBest( | |
| 98 kIconPreviewSizePixels, kIconPreviewSizePixels); | |
| 99 | |
| 100 if (icon && !icon->IsEmpty()) { | |
| 101 NSImage* icon_image = icon->ToNSImage(); | |
| 102 [icon_image | |
| 103 setSize:NSMakeSize(kIconPreviewTargetSize, kIconPreviewTargetSize)]; | |
| 104 [alert setIcon:icon_image]; | |
| 105 } | |
| 106 | |
| 107 bool dialog_accepted = false; | |
| 108 if ([alert runModal] == NSAlertFirstButtonReturn && | |
| 109 [application_folder_checkbox state] == NSOnState) { | |
| 110 dialog_accepted = true; | |
| 111 CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER, | |
| 112 web_app::ShortcutLocations(), profile, app); | |
| 113 } | |
| 114 | |
| 115 [checkbox_observer stopObserving]; | |
| 116 | |
| 117 if (!close_callback.is_null()) | |
| 118 close_callback.Run(dialog_accepted); | |
| 119 } | |
| 120 | |
| 121 } // namespace | |
| 122 | |
| 123 namespace chrome { | |
| 124 | |
| 125 void ShowCreateChromeAppShortcutsDialog( | |
| 126 gfx::NativeWindow /*parent_window*/, | |
| 127 Profile* profile, | |
| 128 const extensions::Extension* app, | |
| 129 const base::Callback<void(bool)>& close_callback) { | |
| 130 web_app::GetShortcutInfoForApp( | |
| 131 app, profile, | |
| 132 base::Bind(&CreateAppShortcutInfoLoaded, profile, app, close_callback)); | |
| 133 } | |
| 134 | |
| 135 } // namespace chrome | |
| OLD | NEW |