-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add copy clean link to the macOS application menu bar
- Loading branch information
1 parent
7d527cc
commit 4bffcdd
Showing
16 changed files
with
254 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* Copyright (c) 2022 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_BRAVE_APP_CONTROLLER_MAC_H_ | ||
#define BRAVE_BROWSER_BRAVE_APP_CONTROLLER_MAC_H_ | ||
|
||
#import "chrome/browser/app_controller_mac.h" | ||
|
||
// Manages logic to switch hotkey between copy and copy clean link item. | ||
@interface BraveAppController : AppController { | ||
NSMenuItem* _copyMenuItem; | ||
NSMenuItem* _copyCleanLinkMenuItem; | ||
absl::optional<bool> _hasSelectedURLForTesting; | ||
} | ||
|
||
// Testing API. | ||
- (void)setCopyMenuItemForTesting:(NSMenuItem*)menuItem; | ||
- (void)setCopyCleanLinkMenuItemForTesting:(NSMenuItem*)menuItem; | ||
- (void)setSelectedURLForTesting:(bool)selected; | ||
|
||
@end | ||
|
||
#endif // BRAVE_BROWSER_BRAVE_APP_CONTROLLER_MAC_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* Copyright (c) 2022 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#import "brave/browser/brave_app_controller_mac.h" | ||
|
||
#include "brave/app/brave_command_ids.h" | ||
#include "brave/browser/ui/browser_commands.h" | ||
#include "chrome/app/chrome_command_ids.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/browser/ui/browser_commands.h" | ||
#include "chrome/browser/ui/browser_finder.h" | ||
#include "chrome/grit/generated_resources.h" | ||
|
||
@implementation BraveAppController | ||
|
||
- (void)mainMenuCreated { | ||
[super mainMenuCreated]; | ||
|
||
NSMenu* editMenu = [[[NSApp mainMenu] itemWithTag:IDC_EDIT_MENU] submenu]; | ||
_copyMenuItem = [editMenu itemWithTag:IDC_CONTENT_CONTEXT_COPY]; | ||
DCHECK(_copyMenuItem); | ||
[[_copyMenuItem menu] setDelegate:self]; | ||
_copyCleanLinkMenuItem = [editMenu itemWithTag:IDC_COPY_CLEAN_LINK]; | ||
DCHECK(_copyCleanLinkMenuItem); | ||
[[_copyCleanLinkMenuItem menu] setDelegate:self]; | ||
} | ||
|
||
- (void)dealloc { | ||
[[_copyMenuItem menu] setDelegate:nil]; | ||
[[_copyCleanLinkMenuItem menu] setDelegate:nil]; | ||
[super dealloc]; | ||
} | ||
|
||
- (Browser*)getBrowser { | ||
return chrome::FindBrowserWithProfile([self lastProfileIfLoaded]); | ||
} | ||
|
||
- (BOOL)shouldShowCleanLinkItem { | ||
if (_hasSelectedURLForTesting.has_value()) { | ||
return _hasSelectedURLForTesting.value(); | ||
} | ||
return brave::HasSelectedURL([self getBrowser]); | ||
} | ||
|
||
- (void)setKeyEquivalentToItem:(NSMenuItem*)item { | ||
auto* hootkeyItem = | ||
item == _copyMenuItem ? _copyMenuItem : _copyCleanLinkMenuItem; | ||
auto* noHootkeyItem = | ||
item == _copyMenuItem ? _copyCleanLinkMenuItem : _copyMenuItem; | ||
|
||
[hootkeyItem setKeyEquivalent:@"c"]; | ||
[hootkeyItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand]; | ||
|
||
[noHootkeyItem setKeyEquivalent:@""]; | ||
[noHootkeyItem setKeyEquivalentModifierMask:0]; | ||
} | ||
|
||
- (void)menuNeedsUpdate:(NSMenu*)menu { | ||
if (menu != [_copyMenuItem menu] && menu != [_copyCleanLinkMenuItem menu]) { | ||
[super menuNeedsUpdate:menu]; | ||
return; | ||
} | ||
if ([self shouldShowCleanLinkItem]) { | ||
[_copyCleanLinkMenuItem setHidden:NO]; | ||
[self setKeyEquivalentToItem:_copyCleanLinkMenuItem]; | ||
} else { | ||
[_copyCleanLinkMenuItem setHidden:YES]; | ||
[self setKeyEquivalentToItem:_copyMenuItem]; | ||
} | ||
} | ||
|
||
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item { | ||
NSInteger tag = [item tag]; | ||
if (tag == IDC_COPY_CLEAN_LINK) { | ||
return [self shouldShowCleanLinkItem]; | ||
} | ||
return [super validateUserInterfaceItem:item]; | ||
} | ||
|
||
- (void)commandDispatch:(id)sender { | ||
NSInteger tag = [sender tag]; | ||
if (tag == IDC_COPY_CLEAN_LINK) { | ||
chrome::ExecuteCommand([self getBrowser], IDC_COPY_CLEAN_LINK); | ||
return; | ||
} | ||
|
||
[super commandDispatch:sender]; | ||
} | ||
|
||
- (void)setCopyMenuItemForTesting:(NSMenuItem*)menuItem { | ||
_copyMenuItem = menuItem; | ||
} | ||
|
||
- (void)setCopyCleanLinkMenuItemForTesting:(NSMenuItem*)menuItem { | ||
_copyCleanLinkMenuItem = menuItem; | ||
} | ||
- (void)setSelectedURLForTesting:(bool)value { | ||
_hasSelectedURLForTesting = value; | ||
} | ||
|
||
@end // @implementation BraveAppController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright (c) 2022 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
#import "brave/browser/brave_app_controller_mac.h" | ||
#include "content/public/test/browser_task_environment.h" | ||
#include "testing/platform_test.h" | ||
|
||
class BraveAppControllerTest : public PlatformTest { | ||
protected: | ||
BraveAppControllerTest() {} | ||
|
||
void SetUp() override { | ||
PlatformTest::SetUp(); | ||
braveAppController_.reset([[BraveAppController alloc] init]); | ||
copyMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | ||
action:0 | ||
keyEquivalent:@""]); | ||
[braveAppController_ setCopyMenuItemForTesting:copyMenuItem_]; | ||
|
||
copyCleanLinkMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | ||
action:0 | ||
keyEquivalent:@""]); | ||
[braveAppController_ | ||
setCopyCleanLinkMenuItemForTesting:copyCleanLinkMenuItem_]; | ||
} | ||
|
||
void TearDown() override { | ||
[braveAppController_ setCopyMenuItemForTesting:nil]; | ||
[braveAppController_ setCopyCleanLinkMenuItemForTesting:nil]; | ||
PlatformTest::TearDown(); | ||
} | ||
|
||
void CheckHotkeysOnCopyItem() { | ||
[braveAppController_ setSelectedURLForTesting:false]; | ||
|
||
[braveAppController_ menuNeedsUpdate:[copyMenuItem_ menu]]; | ||
|
||
EXPECT_TRUE([[copyMenuItem_ keyEquivalent] isEqualToString:@"c"]); | ||
EXPECT_EQ([copyMenuItem_ keyEquivalentModifierMask], | ||
NSEventModifierFlagCommand); | ||
|
||
EXPECT_TRUE([[copyCleanLinkMenuItem_ keyEquivalent] isEqualToString:@""]); | ||
EXPECT_EQ([copyCleanLinkMenuItem_ keyEquivalentModifierMask], 0UL); | ||
EXPECT_TRUE([copyCleanLinkMenuItem_ isHidden]); | ||
} | ||
|
||
void CheckHotkeysOnCleanLinkItem() { | ||
[braveAppController_ setSelectedURLForTesting:true]; | ||
|
||
[braveAppController_ menuNeedsUpdate:[copyMenuItem_ menu]]; | ||
|
||
EXPECT_TRUE([[copyMenuItem_ keyEquivalent] isEqualToString:@""]); | ||
EXPECT_EQ([copyMenuItem_ keyEquivalentModifierMask], 0UL); | ||
|
||
EXPECT_TRUE([[copyCleanLinkMenuItem_ keyEquivalent] isEqualToString:@"c"]); | ||
EXPECT_EQ([copyCleanLinkMenuItem_ keyEquivalentModifierMask], | ||
NSEventModifierFlagCommand); | ||
EXPECT_FALSE([copyCleanLinkMenuItem_ isHidden]); | ||
} | ||
|
||
base::scoped_nsobject<BraveAppController> braveAppController_; | ||
base::scoped_nsobject<NSMenuItem> copyMenuItem_; | ||
base::scoped_nsobject<NSMenuItem> copyCleanLinkMenuItem_; | ||
content::BrowserTaskEnvironment task_environment_; | ||
}; | ||
|
||
TEST_F(BraveAppControllerTest, OnlyCopyItem) { | ||
CheckHotkeysOnCopyItem(); | ||
} | ||
|
||
TEST_F(BraveAppControllerTest, CleanLinkItemAdded) { | ||
CheckHotkeysOnCleanLinkItem(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* Copyright (c) 2022 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "chrome/browser/chrome_browser_main_mac.h" | ||
#import "brave/browser/brave_app_controller_mac.h" | ||
|
||
#define AppController BraveAppController | ||
#include "src/chrome/browser/chrome_browser_main_mac.mm" |
7 changes: 4 additions & 3 deletions
7
chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
chromium_src/ui/views/controls/textfield/textfield_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
chromium_src/ui/views/controls/textfield/textfield_controller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters