File: partial_translate_bubble_view.h

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 (331 lines) | stat: -rw-r--r-- 13,861 bytes parent folder | download | duplicates (5)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_TRANSLATE_PARTIAL_TRANSLATE_BUBBLE_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_TRANSLATE_PARTIAL_TRANSLATE_BUBBLE_VIEW_H_

#include <map>
#include <memory>
#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/ui/translate/partial_translate_bubble_model.h"
#include "chrome/browser/ui/translate/source_language_combobox_model.h"
#include "chrome/browser/ui/translate/target_language_combobox_model.h"
#include "chrome/browser/ui/views/location_bar/location_bar_bubble_delegate_view.h"
#include "components/language/core/common/language_experiments.h"
#include "components/translate/core/common/translate_errors.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/actions/actions.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane.h"
#include "ui/views/controls/tabbed_pane/tabbed_pane_listener.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/window/non_client_view.h"

namespace views {
class Combobox;
class LabelButton;
class View;
}  // namespace views

// The bubble shown when translating a partial text (e.g. selecting a some
// text from the contents).
class PartialTranslateBubbleView : public LocationBarBubbleDelegateView,
                                   public ui::SimpleMenuModel::Delegate,
                                   public views::TabbedPaneListener {
  METADATA_HEADER(PartialTranslateBubbleView, LocationBarBubbleDelegateView)

 public:
  // Item IDs for the option button's menu.
  enum OptionsMenuItem { CHANGE_TARGET_LANGUAGE, CHANGE_SOURCE_LANGUAGE };

  // Element IDs for ui::ElementTracker
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kIdentifier);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kSourceLanguageTab);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kTargetLanguageTab);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kCloseButton);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kOptionsMenuButton);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kChangeTargetLanguage);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kTargetLanguageCombobox);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kTargetLanguageDoneButton);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kChangeSourceLanguage);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kSourceLanguageCombobox);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kSourceLanguageDoneButton);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kErrorMessage);

  PartialTranslateBubbleView(base::WeakPtr<actions::ActionItem> action_item,
                             views::View* anchor_view,
                             std::unique_ptr<PartialTranslateBubbleModel> model,
                             content::WebContents* web_contents,
                             base::OnceClosure on_closing);

  PartialTranslateBubbleView(const PartialTranslateBubbleView&) = delete;
  PartialTranslateBubbleView& operator=(const PartialTranslateBubbleView&) =
      delete;

  ~PartialTranslateBubbleView() override;

  PartialTranslateBubbleModel* model() { return model_.get(); }

  // LocationBarBubbleDelegateView:
  void Init() override;
  View* GetInitiallyFocusedView() override;
  bool ShouldShowCloseButton() const override;
  bool ShouldShowWindowTitle() const override;
  void WindowClosing() override;
  bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;
  void OnWidgetDestroying(views::Widget* widget) override;

  // ui::SimpleMenuModel::Delegate:
  void ExecuteCommand(int command_id, int event_flags) override;

  // Returns the current view state.
  PartialTranslateBubbleModel::ViewState GetViewState() const;

  // Initialize the bubble in the correct view state when it is shown.
  void SetViewState(PartialTranslateBubbleModel::ViewState view_state,
                    translate::TranslateErrors error_type);

  // Update the source language combobox's selected index to match the current
  // index in the model. These values desynchronize when a request does not
  // specify a source language, such as with initial translations from the menu,
  // or when "Detected Language" is used.
  void MaybeUpdateSourceLanguageCombobox();

  // LocationBarBubbleDelegateView:
  void CloseBubble() override;

 private:
  // IDs used by PartialTranslateBubbleViewTest to simulate button presses.
  enum ButtonID {
    BUTTON_ID_DONE = 1,
    BUTTON_ID_TRY_AGAIN,
    BUTTON_ID_OPTIONS_MENU,
    BUTTON_ID_CLOSE,
    BUTTON_ID_RESET,
    BUTTON_ID_FULL_PAGE_TRANSLATE
  };

  friend class PartialTranslateBubbleViewTest;
  FRIEND_TEST_ALL_PREFIXES(PartialTranslateBubbleViewTest,
                           TargetLanguageTabDoesntTriggerTranslate);
  FRIEND_TEST_ALL_PREFIXES(PartialTranslateBubbleViewTest,
                           TabSelectedAfterTranslation);
  FRIEND_TEST_ALL_PREFIXES(PartialTranslateBubbleViewTest,
                           UpdateLanguageTabsFromResponse);
  FRIEND_TEST_ALL_PREFIXES(PartialTranslateBubbleViewTest,
                           SourceLanguageTabUpdatesViewState);
  FRIEND_TEST_ALL_PREFIXES(PartialTranslateBubbleViewTest,
                           SourceLanguageTabSelectedLogged);
  FRIEND_TEST_ALL_PREFIXES(PartialTranslateBubbleViewTest,
                           TranslateFullPageButton);

  // views::TabbedPaneListener:
  void TabSelectedAt(int index) override;

  // Returns the current child view.
  views::View* GetCurrentView() const;

  // Triggers options menu.
  void ShowOptionsMenu(views::Button* source);

  // Handles the event when the user changes an index of a combobox.
  void SourceLanguageChanged();
  void TargetLanguageChanged();

  // Updates the visibilities of child views according to the current view type.
  void UpdateChildVisibilities();

  // Creates the view used before/during/after translate.
  std::unique_ptr<views::View> CreateView();

  // AddTab function requires a view element to be shown below each tab.
  // This function creates an empty view so no extra white space below the tab.
  std::unique_ptr<views::View> CreateEmptyPane();

  // Creates the 'error' view for Button UI.
  std::unique_ptr<views::View> CreateViewError();

  // Creates the 'error' view skeleton UI with no title.
  std::unique_ptr<views::View> CreateViewErrorNoTitle(
      std::unique_ptr<views::Button> button);

  // Creates the 'waiting' view that shows an empty bubble with a throbber.
  std::unique_ptr<views::View> CreateViewWaiting();

  // Creates source language label and combobox for Tab UI advanced view. Caller
  // takes ownership of the returned view.
  std::unique_ptr<views::View> CreateViewAdvancedSource();

  // Creates target language label and combobox for Tab UI advanced view. Caller
  // takes ownership of the returned view.
  std::unique_ptr<views::View> CreateViewAdvancedTarget();

  // Creates the 'advanced' view to show source/target language combobox. Caller
  // takes ownership of the returned view.
  std::unique_ptr<views::View> CreateViewAdvanced(
      std::unique_ptr<views::Combobox> combobox,
      std::unique_ptr<views::Label> language_title_label,
      std::unique_ptr<views::Button> advanced_reset_button,
      std::unique_ptr<views::Button> advanced_done_button);

  // Creates a translate icon for when the bottom branding isn't showing. This
  // should only be used on non-Chrome-branded builds.
  std::unique_ptr<views::ImageView> CreateTranslateIcon();

  // Creates a three dot options menu button.
  std::unique_ptr<views::Button> CreateOptionsMenuButton();

  // Creates a close button.
  std::unique_ptr<views::Button> CreateCloseButton();

  // Sets the window title. The window title still needs to be set, even when it
  // is not shown, for accessibility purposes.
  void SetWindowTitle(PartialTranslateBubbleModel::ViewState view_state);

  // Finds and saves the width of the bubble's largest child view, excluding
  // |translate_view_|. This value is needed to properly resize
  // |partial_text_label_| as the width of the tabbed pane changes with changes
  // in selected languages.
  void ComputeLargestViewStateWidth();

  // Updates the view state. Whenever the view state is updated, the title needs
  // to be updated for accessibility.
  void UpdateViewState(PartialTranslateBubbleModel::ViewState view_state);

  // Switches the view type.
  void SwitchView(PartialTranslateBubbleModel::ViewState view_state);

  // Handles tab switching on when the view type switches.
  void SwitchTabForViewState(PartialTranslateBubbleModel::ViewState view_state);

  // Switches to the error view.
  void SwitchToErrorView(translate::TranslateErrors error_type);

  // Updates the advanced view.
  void UpdateAdvancedView();

  // Actions for button presses shared with accelerators.
  void ShowTranslated();
  void ShowOriginal();
  void ConfirmAdvancedOptions();

  // Returns whether or not the current language selection is different from the
  // initial language selection in an advanced view.
  bool DidLanguageSelectionChange(
      PartialTranslateBubbleModel::ViewState view_state);

  // Handles the reset button in advanced view under Tab UI.
  void ResetLanguage();

  // Updates the body text for the bubble based on the view state (either the
  // source text if we're pre-translate or translating, or the target text if
  // we're done translating).
  void UpdateTextForViewState(
      PartialTranslateBubbleModel::ViewState view_state);

  // Update the names of the source/target language tabs.
  void UpdateLanguageTabNames();

  void UpdateInsets(PartialTranslateBubbleModel::ViewState state);

  // Function bound to the "Translate full page" button.
  void TranslateFullPage();

  // Update the alignment of |partial_text_label_| to match the direction of
  // the locale being used.
  void SetTextAlignmentForLocaleTextDirection(std::string locale);

  // Forces announcement of translation state and conditionally also accounces
  // the translated text.
  void AnnounceForAccessibility(
      PartialTranslateBubbleModel::ViewState view_state);

  static PartialTranslateBubbleView* partial_translate_bubble_view_;

  raw_ptr<views::View, DanglingUntriaged> translate_view_waiting_ = nullptr;
  raw_ptr<views::View, DanglingUntriaged> translate_view_ = nullptr;
  raw_ptr<views::View, DanglingUntriaged> error_view_ = nullptr;
  raw_ptr<views::View, DanglingUntriaged> advanced_view_source_ = nullptr;
  raw_ptr<views::View, DanglingUntriaged> advanced_view_target_ = nullptr;

  raw_ptr<views::Throbber, DanglingUntriaged> throbber_;

  raw_ptr<views::Combobox, DanglingUntriaged> source_language_combobox_ =
      nullptr;
  raw_ptr<views::Combobox, DanglingUntriaged> target_language_combobox_ =
      nullptr;

  raw_ptr<views::TabbedPane, DanglingUntriaged> tabbed_pane_ = nullptr;
  raw_ptr<views::View, DanglingUntriaged> tab_view_top_row_ = nullptr;
  raw_ptr<views::Label, DanglingUntriaged> partial_text_label_ = nullptr;

  raw_ptr<views::LabelButton, DanglingUntriaged> advanced_reset_button_source_ =
      nullptr;
  raw_ptr<views::LabelButton, DanglingUntriaged> advanced_reset_button_target_ =
      nullptr;
  raw_ptr<views::LabelButton, DanglingUntriaged> advanced_done_button_source_ =
      nullptr;
  raw_ptr<views::LabelButton, DanglingUntriaged> advanced_done_button_target_ =
      nullptr;

  // Default source/target language without user interaction.
  size_t previous_source_language_index_;
  size_t previous_target_language_index_;

  // Whether or not user changed target language and triggered translation from
  // the advanced options.
  bool target_language_changed_ = false;

  std::unique_ptr<ui::SimpleMenuModel> options_menu_model_;
  std::unique_ptr<views::MenuRunner> options_menu_runner_;

  std::unique_ptr<PartialTranslateBubbleModel> model_;

  translate::TranslateErrors error_type_;

  std::unique_ptr<WebContentMouseHandler> mouse_handler_;

  std::u16string text_selection_;

  // The width of the largest non-|translate_view_| child view at
  // initialization. The default minimum width is set to 300dp to provide a more
  // consistent experience between different UI languages - for high density
  // languages the width would otherwise be very narrow.
  int largest_view_state_width_ = 300;

  // The threshold for character volume of |partial_text_label_|. If the volume
  // is larger than the threshold, use the preset maximum width allowable for
  // the bubble. Otherwise, resize normally.
  const size_t char_threshold_for_max_width_ = 1300;

  // The max allowable width for the bubble, used when the character volume of
  // |partial_text_label_| exceeds |char_threshold_for_max_width_|. This is
  // necessary to accommodate the size of the label in cases of largest possible
  // character volume. It follows that this is based off of
  // translate::kDesktopPartialTranslateTextSelectionMaxCharacters and should be
  // updated alongside it.
  const int bubble_max_width_ = 550;

  base::OnceClosure on_closing_;

  raw_ptr<content::WebContents> web_contents_;

  // The action item that this bubble is associated with. This bubble updates
  // the action item's "IsShowingBubbleProperty" as needed.
  const base::WeakPtr<actions::ActionItem> action_item_ = nullptr;
};

#endif  // CHROME_BROWSER_UI_VIEWS_TRANSLATE_PARTIAL_TRANSLATE_BUBBLE_VIEW_H_