// Copyright (c) 2024 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/. // Based on Chromium code subject to the following license: // Copyright 2021 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "brave/browser/ui/commander/window_command_source.h" #include #include #include #include #include "brave/browser/ui/commander/entity_match.h" #include "brave/browser/ui/commander/fuzzy_finder.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_commands.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window/public/global_browser_collection.h" #include "components/grit/brave_components_strings.h" #include "ui/base/l10n/l10n_util.h" namespace commander { namespace { // Activates `browser` if it's still present. void SwitchToBrowser(base::WeakPtr browser) { if (browser.get()) { browser->GetBrowserForMigrationOnly()->window()->Show(); } } // Merges all tabs from `source` into `target`, if they are both present. void MergeBrowsers(base::WeakPtr source, base::WeakPtr target) { if (!source.get() || !target.get()) { return; } size_t source_count = source->GetTabStripModel()->count(); std::vector indices(source_count); std::iota(indices.begin(), indices.end(), 0); chrome::MoveTabsToExistingWindow(source.get(), target.get(), indices); } // Returns browser windows whose titles fuzzy match `input`. If input is empty, // returns all eligible browser windows with score reflecting MRU order. // `browser_to_exclude` is excluded from the list, as are all browser windows // from a different profile unless `match_profile` is false. std::unique_ptr CreateSwitchWindowItem(const WindowMatch& match) { auto item = match.ToCommandItem(); item->command = base::BindOnce(&SwitchToBrowser, match.browser->GetWeakPtr()); return item; } std::unique_ptr CreateMergeWindowItem( BrowserWindowInterface* source, const WindowMatch& target) { auto item = target.ToCommandItem(); item->command = base::BindOnce(&MergeBrowsers, source->GetWeakPtr(), target.browser->GetWeakPtr()); return item; } CommandSource::CommandResults SwitchCommandsForWindowsMatching( BrowserWindowInterface* browser_to_exclude, const std::u16string& input) { CommandSource::CommandResults results; for (auto& match : WindowsMatchingInput(browser_to_exclude, input)) { results.push_back(CreateSwitchWindowItem(match)); } return results; } CommandSource::CommandResults MergeCommandsForWindowsMatching( BrowserWindowInterface* source_browser, const std::u16string& input) { CommandSource::CommandResults results; for (auto& match : WindowsMatchingInput(source_browser, input, true)) { results.push_back(CreateMergeWindowItem(source_browser, match)); } return results; } } // namespace WindowCommandSource::WindowCommandSource() = default; WindowCommandSource::~WindowCommandSource() = default; CommandSource::CommandResults WindowCommandSource::GetCommands( const std::u16string& input, BrowserWindowInterface* browser) const { CommandSource::CommandResults results; if (GlobalBrowserCollection::GetInstance()->GetSize() < 2) { return results; } FuzzyFinder finder(input); std::vector ranges; std::u16string open_title = l10n_util::GetStringUTF16(IDS_COMMANDER_SWITCH_TO_WINDOW); std::u16string merge_title = l10n_util::GetStringUTF16(IDS_COMMANDER_MERGE_WINDOW_INTO); double score = finder.Find(open_title, ranges); if (score > 0) { auto verb = std::make_unique(open_title, score, ranges); verb->command = std::make_pair( open_title, base::BindRepeating(&SwitchCommandsForWindowsMatching, base::Unretained(browser))); results.push_back(std::move(verb)); } score = finder.Find(merge_title, ranges); if (score > 0 && browser->GetType() != BrowserWindowInterface::Type::TYPE_DEVTOOLS) { auto verb = std::make_unique(merge_title, score, ranges); verb->command = std::make_pair( merge_title, base::BindRepeating(&MergeCommandsForWindowsMatching, base::Unretained(browser))); results.push_back(std::move(verb)); } return results; } } // namespace commander