File: remove_suggestion_bubble.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 4,639 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/omnibox/remove_suggestion_bubble.h"

#include <memory>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "chrome/grit/generated_resources.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/search_engines/template_url_service.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

class RemoveSuggestionBubbleDialogDelegateView
    : public views::BubbleDialogDelegateView {
  METADATA_HEADER(RemoveSuggestionBubbleDialogDelegateView,
                  views::BubbleDialogDelegateView)

 public:
  RemoveSuggestionBubbleDialogDelegateView(
      TemplateURLService* template_url_service,
      views::View* anchor_view,
      const AutocompleteMatch& match,
      base::OnceClosure remove_closure)
      : views::BubbleDialogDelegateView(anchor_view,
                                        views::BubbleBorder::TOP_RIGHT),
        match_(match),
        remove_closure_(std::move(remove_closure)) {
    DCHECK(template_url_service);
    DCHECK(match_.SupportsDeletion());

    SetButtonLabel(ui::mojom::DialogButton::kOk,
                   l10n_util::GetStringUTF16(IDS_REMOVE));
    SetButtonLabel(ui::mojom::DialogButton::kCancel,
                   l10n_util::GetStringUTF16(IDS_CANCEL));
    SetModalType(ui::mojom::ModalType::kWindow);

    auto* layout_manager = SetLayoutManager(std::make_unique<views::BoxLayout>(
        views::BoxLayout::Orientation::kVertical));
    layout_manager->set_cross_axis_alignment(
        views::BoxLayout::CrossAxisAlignment::kStart);
    // TODO(tommycli): Replace this with the real spacing from UX.
    layout_manager->set_between_child_spacing(16);

    // Get the Search Provider name associated with this match.
    std::u16string search_provider_short_name;
    const TemplateURL* template_url =
        match.GetTemplateURL(template_url_service, false);
    // If the match has no associated Search Provider, get the default one,
    // although this may still fail if it's forbidden by policy.
    if (!template_url) {
      template_url = template_url_service->GetDefaultSearchProvider();
    }
    if (template_url) {
      search_provider_short_name =
          template_url->AdjustedShortNameForLocaleDirection();
    }

    views::Label* description_label =
        new views::Label(l10n_util::GetStringFUTF16(
            IDS_OMNIBOX_REMOVE_SUGGESTION_BUBBLE_DESCRIPTION,
            search_provider_short_name));
    description_label->SetMultiLine(true);
    description_label->SetHorizontalAlignment(
        gfx::HorizontalAlignment::ALIGN_LEFT);
    AddChildViewRaw(description_label);

    // TODO(tommycli): Indent and set a smaller font per UX suggestions.
    views::Label* url_label = new views::Label(match.contents);
    url_label->SetMultiLine(true);
    url_label->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
    AddChildViewRaw(url_label);
  }

  // views::DialogDelegateView:
  bool Accept() override {
    std::move(remove_closure_).Run();
    return true;
  }

  // views::View:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override {
    // TODO(tommycli): Replace with the real width from UX.
    return gfx::Size(500,
                     GetLayoutManager()->GetPreferredHeightForWidth(this, 500));
  }

  // views::WidgetDelegate:
  std::u16string GetWindowTitle() const override {
    return l10n_util::GetStringUTF16(
        IDS_OMNIBOX_REMOVE_SUGGESTION_BUBBLE_TITLE);
  }

 private:
  AutocompleteMatch match_;
  base::OnceClosure remove_closure_;
};

BEGIN_METADATA(RemoveSuggestionBubbleDialogDelegateView)
END_METADATA

void ShowRemoveSuggestion(TemplateURLService* template_url_service,
                          views::View* anchor_view,
                          const AutocompleteMatch& match,
                          base::OnceClosure remove_closure) {
  views::BubbleDialogDelegateView::CreateBubble(
      std::make_unique<RemoveSuggestionBubbleDialogDelegateView>(
          template_url_service, anchor_view, match, std::move(remove_closure)))
      ->Show();
}