This change allows a couple of more places to be migrated to `BrowserWindowInterface`. Chromium changes: https://chromium.googlesource.com/chromium/src/+/e8d0df41d25900497716dd3a4b070c6436fa1a97 commit e8d0df41d25900497716dd3a4b070c6436fa1a97 Author: Kun Wang <kunwang@microsoft.com> Date: Wed Apr 15 23:35:39 2026 -0700 [bedrock] Migrate FindLastActiveWithProfile Step 1 Bug: 494010890 Change-Id: Ie0385deeda2e2f28baf75a810ada0becf5807c78 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7753884 Reviewed-by: Thomas Lukaszewicz <tluk@chromium.org> Reviewed-by: Qikai Zhong <qikaizhong@microsoft.com> Commit-Queue: Kun Wang <kunwang@microsoft.com> Cr-Commit-Position: refs/heads/main@{#1615659}
129 lines
4.7 KiB
C++
129 lines
4.7 KiB
C++
// 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 <memory>
|
|
#include <numeric>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#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<BrowserWindowInterface> browser) {
|
|
if (browser.get()) {
|
|
browser->GetBrowserForMigrationOnly()->window()->Show();
|
|
}
|
|
}
|
|
|
|
// Merges all tabs from `source` into `target`, if they are both present.
|
|
void MergeBrowsers(base::WeakPtr<BrowserWindowInterface> source,
|
|
base::WeakPtr<BrowserWindowInterface> target) {
|
|
if (!source.get() || !target.get()) {
|
|
return;
|
|
}
|
|
size_t source_count = source->GetTabStripModel()->count();
|
|
std::vector<int> 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<CommandItem> CreateSwitchWindowItem(const WindowMatch& match) {
|
|
auto item = match.ToCommandItem();
|
|
item->command = base::BindOnce(&SwitchToBrowser, match.browser->GetWeakPtr());
|
|
return item;
|
|
}
|
|
|
|
std::unique_ptr<CommandItem> 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<gfx::Range> 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<CommandItem>(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<CommandItem>(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
|