-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced copy text from image feature on macOS
fix brave/brave-browser#27513 "Copy Text From Image" entry is added to render view's context menu. It gets bitmap data from renderer via LocalFrame::GetImage() mojom interface, and then passed it to TextRecognitionDialog to extract and show text from that image.
- Loading branch information
Showing
25 changed files
with
627 additions
and
6 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
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,126 @@ | ||
/* Copyright (c) 2023 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 "brave/browser/ui/views/text_recognition_dialog_view.h" | ||
|
||
#include <memory> | ||
|
||
#include "base/bind.h" | ||
#include "base/strings/utf_string_conversions.h" | ||
#include "base/task/task_traits.h" | ||
#include "base/task/thread_pool.h" | ||
#include "brave/components/l10n/common/localization_util.h" | ||
#include "brave/components/text_recognition/browser/text_recognition.h" | ||
#include "brave/grit/brave_generated_resources.h" | ||
#include "components/constrained_window/constrained_window_views.h" | ||
#include "ui/base/clipboard/scoped_clipboard_writer.h" | ||
#include "ui/base/metadata/metadata_impl_macros.h" | ||
#include "ui/gfx/geometry/insets.h" | ||
#include "ui/views/controls/label.h" | ||
#include "ui/views/controls/scroll_view.h" | ||
#include "ui/views/layout/flex_layout.h" | ||
#include "ui/views/widget/widget.h" | ||
#include "ui/views/window/dialog_client_view.h" | ||
|
||
namespace brave { | ||
|
||
void ShowTextRecognitionDialog(content::WebContents* web_contents, | ||
const SkBitmap& image) { | ||
constrained_window::ShowWebModalDialogViews( | ||
new TextRecognitionDialogView(image), web_contents) | ||
->Show(); | ||
} | ||
|
||
} // namespace brave | ||
|
||
TextRecognitionDialogView::TextRecognitionDialogView(const SkBitmap& image) { | ||
SetModalType(ui::MODAL_TYPE_CHILD); | ||
SetButtons(ui::DIALOG_BUTTON_OK); | ||
SetButtonLabel(ui::DIALOG_BUTTON_OK, | ||
brave_l10n::GetLocalizedResourceUTF16String( | ||
IDS_TEXT_RECOG_DIALOG_CLOSE_BUTTON)); | ||
SetShowCloseButton(false); | ||
|
||
SetLayoutManager(std::make_unique<views::FlexLayout>()) | ||
->SetOrientation(views::LayoutOrientation::kVertical) | ||
.SetMainAxisAlignment(views::LayoutAlignment::kStart) | ||
.SetInteriorMargin(gfx::Insets::TLBR(24, 26, 0, 26)); | ||
|
||
header_label_ = AddChildView(std::make_unique<views::Label>()); | ||
const int size_diff = 14 - views::Label::GetDefaultFontList().GetFontSize(); | ||
header_label_->SetFontList( | ||
views::Label::GetDefaultFontList() | ||
.DeriveWithSizeDelta(size_diff) | ||
.DeriveWithWeight(gfx::Font::Weight::SEMIBOLD)); | ||
header_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | ||
header_label_->SetProperty(views::kMarginsKey, | ||
gfx::Insets::TLBR(0, 0, 10, 0)); | ||
|
||
if (image.empty()) { | ||
UpdateContents({}); | ||
return; | ||
} | ||
|
||
StartExtractingText(image); | ||
} | ||
|
||
TextRecognitionDialogView::~TextRecognitionDialogView() = default; | ||
|
||
void TextRecognitionDialogView::StartExtractingText(const SkBitmap& image) { | ||
header_label_->SetText(brave_l10n::GetLocalizedResourceUTF16String( | ||
IDS_TEXT_RECOG_DIALOG_HEADER_IN_PROGRESS)); | ||
|
||
base::ThreadPool::PostTaskAndReplyWithResult( | ||
FROM_HERE, | ||
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, | ||
base::BindOnce(&GetTextFromImage, image), | ||
base::BindOnce(&TextRecognitionDialogView::OnGetTextFromImage, | ||
weak_factory_.GetWeakPtr())); | ||
} | ||
|
||
void TextRecognitionDialogView::OnGetTextFromImage( | ||
const std::vector<std::string>& text) { | ||
UpdateContents(text); | ||
AdjustWidgetSize(); | ||
} | ||
|
||
void TextRecognitionDialogView::UpdateContents( | ||
const std::vector<std::string>& text) { | ||
if (text.empty()) { | ||
header_label_->SetText(brave_l10n::GetLocalizedResourceUTF16String( | ||
IDS_TEXT_RECOG_DIALOG_HEADER_FAILED)); | ||
return; | ||
} | ||
|
||
header_label_->SetText(brave_l10n::GetLocalizedResourceUTF16String( | ||
IDS_TEXT_RECOG_DIALOG_HEADER_COMPLETE)); | ||
|
||
// Treat each string in |text| as a separated line string. | ||
std::string unified_string; | ||
for (const auto& t : text) { | ||
unified_string += t; | ||
unified_string += '\n'; | ||
} | ||
|
||
ui::ScopedClipboardWriter(ui::ClipboardBuffer::kCopyPaste) | ||
.WriteText(base::UTF8ToUTF16(unified_string)); | ||
|
||
auto* scroll_view = AddChildView(std::make_unique<views::ScrollView>()); | ||
scroll_view->SetProperty(views::kMarginsKey, gfx::Insets::VH(0, 10)); | ||
scroll_view->ClipHeightTo(0, 350); | ||
auto* label = scroll_view->SetContents( | ||
std::make_unique<views::Label>(base::UTF8ToUTF16(unified_string))); | ||
|
||
label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | ||
label->SetSelectable(true); | ||
label->SetMultiLine(true); | ||
} | ||
|
||
void TextRecognitionDialogView::AdjustWidgetSize() { | ||
GetWidget()->SetSize(GetDialogClientView()->GetPreferredSize()); | ||
} | ||
|
||
BEGIN_METADATA(TextRecognitionDialogView, views::DialogDelegateView) | ||
END_METADATA |
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,45 @@ | ||
/* Copyright (c) 2023 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_UI_VIEWS_TEXT_RECOGNITION_DIALOG_VIEW_H_ | ||
#define BRAVE_BROWSER_UI_VIEWS_TEXT_RECOGNITION_DIALOG_VIEW_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "ui/base/metadata/metadata_header_macros.h" | ||
#include "ui/views/window/dialog_delegate.h" | ||
|
||
class SkBitmap; | ||
|
||
namespace views { | ||
class Label; | ||
} // namespace views | ||
|
||
class TextRecognitionDialogView : public views::DialogDelegateView { | ||
public: | ||
METADATA_HEADER(TextRecognitionDialogView); | ||
|
||
explicit TextRecognitionDialogView(const SkBitmap& image); | ||
TextRecognitionDialogView(const TextRecognitionDialogView&) = delete; | ||
TextRecognitionDialogView& operator=(const TextRecognitionDialogView&) = | ||
delete; | ||
~TextRecognitionDialogView() override; | ||
|
||
private: | ||
void StartExtractingText(const SkBitmap& image); | ||
void OnGetTextFromImage(const std::vector<std::string>& text); | ||
|
||
// Show |text| in this dialog and copy it to clipboard. | ||
void UpdateContents(const std::vector<std::string>& text); | ||
void AdjustWidgetSize(); | ||
|
||
raw_ptr<views::Label> header_label_ = nullptr; | ||
base::WeakPtrFactory<TextRecognitionDialogView> weak_factory_{this}; | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_UI_VIEWS_TEXT_RECOGNITION_DIALOG_VIEW_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
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
Oops, something went wrong.