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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
ash / quick_insert / quick_insert_controller.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/quick_insert/quick_insert_controller.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include "ash/accessibility/accessibility_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/clipboard_history_controller.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/quick_insert/model/quick_insert_action_type.h"
#include "ash/quick_insert/model/quick_insert_caps_lock_position.h"
#include "ash/quick_insert/model/quick_insert_emoji_history_model.h"
#include "ash/quick_insert/model/quick_insert_emoji_suggester.h"
#include "ash/quick_insert/model/quick_insert_mode_type.h"
#include "ash/quick_insert/model/quick_insert_model.h"
#include "ash/quick_insert/model/quick_insert_search_results_section.h"
#include "ash/quick_insert/quick_insert_action_on_next_focus_request.h"
#include "ash/quick_insert/quick_insert_asset_fetcher.h"
#include "ash/quick_insert/quick_insert_asset_fetcher_impl.h"
#include "ash/quick_insert/quick_insert_caps_lock_bubble_controller.h"
#include "ash/quick_insert/quick_insert_client.h"
#include "ash/quick_insert/quick_insert_copy_media.h"
#include "ash/quick_insert/quick_insert_insert_media_request.h"
#include "ash/quick_insert/quick_insert_paste_request.h"
#include "ash/quick_insert/quick_insert_rich_media.h"
#include "ash/quick_insert/quick_insert_search_result.h"
#include "ash/quick_insert/quick_insert_suggestions_controller.h"
#include "ash/quick_insert/quick_insert_transform_case.h"
#include "ash/quick_insert/search/quick_insert_search_controller.h"
#include "ash/quick_insert/views/quick_insert_caps_lock_state_view.h"
#include "ash/quick_insert/views/quick_insert_icons.h"
#include "ash/quick_insert/views/quick_insert_positioning.h"
#include "ash/quick_insert/views/quick_insert_view.h"
#include "ash/quick_insert/views/quick_insert_view_delegate.h"
#include "ash/quick_insert/views/quick_insert_widget.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/wm/window_util.h"
#include "base/check.h"
#include "base/check_is_test.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/overloaded.h"
#include "base/hash/sha1.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/window.h"
#include "ui/base/emoji/emoji_panel_helper.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/ime/ash/ime_keyboard.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/base/ime/ash/text_input_target.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/display/screen.h"
#include "ui/events/ash/keyboard_capability.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
namespace ash {
namespace {
bool g_feature_tour_enabled = true;
// When spoken feedback is enabled, closing the widget after an insert is
// delayed by this amount.
constexpr base::TimeDelta kCloseWidgetDelay = base::Milliseconds(200);
// Time to wait for a focus event after triggering caps lock.
constexpr base::TimeDelta kCapsLockRequestTimeout = base::Seconds(1);
constexpr int kCapsLockMinimumTopDisplayCount = 5;
constexpr float kCapsLockRatioThresholdForTop = 0.8;
constexpr float kCapsLockRatioThresholdForBottom = 0.2;
constexpr std::string_view kSupportUrl =
"https://support.google.com/chromebook?p=dugong";
ui::TextInputClient* GetFocusedTextInputClient() {
const ui::InputMethod* input_method =
IMEBridge::Get()->GetInputContextHandler()->GetInputMethod();
if (!input_method || !input_method->GetTextInputClient()) {
return nullptr;
}
return input_method->GetTextInputClient();
}
// Gets the current caret bounds in universal screen coordinates in DIP. Returns
// an empty rect if there is no active caret or the caret bounds can't be
// determined (e.g. no focused input field).
gfx::Rect GetCaretBounds() {
if (ui::TextInputClient* client = GetFocusedTextInputClient()) {
return client->GetCaretBounds();
}
return gfx::Rect();
}
// Gets the current cursor point in universal screen coordinates in DIP.
gfx::Point GetCursorPoint() {
return display::Screen::GetScreen()->GetCursorScreenPoint();
}
// Gets the bounds of the current focused window in universal screen coordinates
// in DIP. Returns an empty rect if there is no currently focused window.
gfx::Rect GetFocusedWindowBounds() {
return window_util::GetFocusedWindow()
? window_util::GetFocusedWindow()->GetBoundsInScreen()
: gfx::Rect();
}
input_method::ImeKeyboard& GetImeKeyboard() {
auto* input_method_manager = input_method::InputMethodManager::Get();
CHECK(input_method_manager);
input_method::ImeKeyboard* ime_keyboard =
input_method_manager->GetImeKeyboard();
CHECK(ime_keyboard);
return *ime_keyboard;
}
// The user can ask to insert rich media, a clipboard item, or insert nothing.
using InsertionContent = std::
variant<QuickInsertRichMedia, QuickInsertClipboardResult, std::monostate>;
InsertionContent GetInsertionContentForResult(
const QuickInsertSearchResult& result) {
using ReturnType = InsertionContent;
return std::visit(
base::Overloaded{
[](const QuickInsertTextResult& data) -> ReturnType {
return QuickInsertTextMedia(data.primary_text);
},
[](const QuickInsertEmojiResult& data) -> ReturnType {
return QuickInsertTextMedia(data.text);
},
[](const QuickInsertClipboardResult& data) -> ReturnType {
return data;
},
[](const QuickInsertBrowsingHistoryResult& data) -> ReturnType {
return QuickInsertLinkMedia(data.url,
base::UTF16ToUTF8(data.title));
},
[](const QuickInsertGifResult& data) -> ReturnType {
return QuickInsertImageMedia(data.full_url, data.full_dimensions,
data.content_description);
},
[](const QuickInsertLocalFileResult& data) -> ReturnType {
return QuickInsertLocalFileMedia(data.file_path);
},
[](const QuickInsertDriveFileResult& data) -> ReturnType {
return QuickInsertLinkMedia(data.url,
base::UTF16ToUTF8(data.title));
},
[](const QuickInsertCategoryResult& data) -> ReturnType {
return std::monostate();
},
[](const QuickInsertSearchRequestResult& data) -> ReturnType {
return std::monostate();
},
[](const QuickInsertEditorResult& data) -> ReturnType {
return std::monostate();
},
[](const QuickInsertLobsterResult& data) -> ReturnType {
return std::monostate();
},
[](const QuickInsertNewWindowResult& data) -> ReturnType {
return std::monostate();
},
[](const QuickInsertCapsLockResult& data) -> ReturnType {
return std::monostate();
},
[](const QuickInsertCaseTransformResult& data) -> ReturnType {
return std::monostate();
},
},
result);
}
std::vector<QuickInsertSearchResultsSection>
CreateSingleSectionForCategoryResults(
QuickInsertSectionType section_type,
std::vector<QuickInsertSearchResult> results) {
if (results.empty()) {
return {};
}
return {QuickInsertSearchResultsSection(section_type, std::move(results),
/*has_more_results=*/false)};
}
std::u16string TransformText(std::u16string_view text,
QuickInsertCaseTransformResult::Type type) {
switch (type) {
case QuickInsertCaseTransformResult::Type::kUpperCase:
return QuickInsertTransformToUpperCase(text);
case QuickInsertCaseTransformResult::Type::kLowerCase:
return QuickInsertTransformToLowerCase(text);
case QuickInsertCaseTransformResult::Type::kTitleCase:
return QuickInsertTransformToTitleCase(text);
}
NOTREACHED();
}
void OpenLink(const GURL& url) {
NewWindowDelegate::GetPrimary()->OpenUrl(
url, NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
}
void OpenFile(const base::FilePath& path) {
NewWindowDelegate::GetPrimary()->OpenFile(path);
}
GURL GetUrlForNewWindow(QuickInsertNewWindowResult::Type type) {
switch (type) {
case QuickInsertNewWindowResult::Type::kDoc:
return GURL("https://docs.new");
case QuickInsertNewWindowResult::Type::kSheet:
return GURL("https://sheets.new");
case QuickInsertNewWindowResult::Type::kSlide:
return GURL("https://slides.new");
case QuickInsertNewWindowResult::Type::kChrome:
return GURL("chrome://newtab");
}
}
ui::EmojiPickerCategory EmojiResultTypeToCategory(
QuickInsertEmojiResult::Type type) {
switch (type) {
case QuickInsertEmojiResult::Type::kEmoji:
return ui::EmojiPickerCategory::kEmojis;
case QuickInsertEmojiResult::Type::kSymbol:
return ui::EmojiPickerCategory::kSymbols;
case QuickInsertEmojiResult::Type::kEmoticon:
return ui::EmojiPickerCategory::kEmoticons;
}
}
QuickInsertSectionType GetSectionTypeForCategorySuggestion(
QuickInsertCategory category) {
switch (category) {
case QuickInsertCategory::kUnitsMaths:
case QuickInsertCategory::kDatesTimes:
return QuickInsertSectionType::kExamples;
case QuickInsertCategory::kGifs:
return QuickInsertSectionType::kFeaturedGifs;
default:
return QuickInsertSectionType::kNone;
}
}
} // namespace
QuickInsertController::QuickInsertController()
: caps_lock_bubble_controller_(&GetImeKeyboard()),
asset_fetcher_(std::make_unique<QuickInsertAssetFetcherImpl>(this)),
search_controller_(kBurnInPeriod) {}
QuickInsertController::~QuickInsertController() {
// `widget_` depends on `this`. Destroy the widget synchronously to avoid a
// dangling pointer.
if (widget_) {
widget_->CloseNow();
}
}
void QuickInsertController::RegisterProfilePrefs(PrefRegistrySimple* registry) {
QuickInsertFeatureTour::RegisterProfilePrefs(registry);
QuickInsertSessionMetrics::RegisterProfilePrefs(registry);
}
void QuickInsertController::DisableFeatureTourForTesting() {
CHECK_IS_TEST();
g_feature_tour_enabled = false;
}
void QuickInsertController::SetClient(QuickInsertClient* client) {
// `QuickInsertSearchController` may depend on the current client via
// `StartSearch`. Stop the search before changing the `client`. This may send
// a `StopSearch` call to the current `client_`.
search_controller_.StopSearch();
client_ = client;
}
void QuickInsertController::OnClientPrefsSet(PrefService* prefs) {
if (client_ == nullptr) {
return;
}
search_controller_.LoadEmojiLanguagesFromPrefs(prefs);
}
void QuickInsertController::ToggleWidget(
const base::TimeTicks trigger_event_timestamp) {
// Show the feature tour if it's the first time this feature is used.
if (PrefService* prefs = GetPrefs();
g_feature_tour_enabled && prefs &&
feature_tour_.MaybeShowForFirstUse(
prefs,
client_->IsEligibleForEditor()
? QuickInsertFeatureTour::EditorStatus::kEligible
: QuickInsertFeatureTour::EditorStatus::kNotEligible,
base::BindRepeating(OpenLink, GURL(kSupportUrl)),
base::BindRepeating(&QuickInsertController::ShowWidgetPostFeatureTour,
weak_ptr_factory_.GetWeakPtr()))) {
return;
}
if (widget_) {
CloseWidget();
} else {
ShowWidget(trigger_event_timestamp, WidgetTriggerSource::kDefault);
}
}
std::vector<QuickInsertCategory>
QuickInsertController::GetAvailableCategories() {
return session_ == nullptr ? std::vector<QuickInsertCategory>{}
: session_->model.GetAvailableCategories();
}
void QuickInsertController::GetZeroStateSuggestedResults(
SuggestedResultsCallback callback) {
CHECK(client_);
suggestions_controller_.GetSuggestions(*client_, session_->model,
std::move(callback));
}
void QuickInsertController::GetResultsForCategory(
QuickInsertCategory category,
SearchResultsCallback callback) {
CHECK(client_);
suggestions_controller_.GetSuggestionsForCategory(
*client_, category,
base::BindRepeating(CreateSingleSectionForCategoryResults,
GetSectionTypeForCategorySuggestion(category))
.Then(std::move(callback)));
}
void QuickInsertController::StartSearch(
std::u16string_view query,
std::optional<QuickInsertCategory> category,
SearchResultsCallback callback) {
CHECK(session_);
CHECK(client_);
search_controller_.StartSearch(
client_, query, std::move(category), GetAvailableCategories(),
!session_->model.is_caps_lock_enabled(),
session_->model.GetMode() == QuickInsertModeType::kHasSelection,
std::move(callback));
}
void QuickInsertController::StopSearch() {
search_controller_.StopSearch();
}
void QuickInsertController::StartEmojiSearch(
std::u16string_view query,
EmojiSearchResultsCallback callback) {
search_controller_.StartEmojiSearch(GetPrefs(), query, std::move(callback));
}
void QuickInsertController::CloseWidgetThenInsertResultOnNextFocus(
const QuickInsertSearchResult& result) {
InsertResultOnNextFocus(result);
client_->Announce(
l10n_util::GetStringUTF16(IDS_PICKER_INSERTION_ANNOUNCEMENT_TEXT));
if (Shell::Get()->accessibility_controller()->spoken_feedback().enabled()) {
close_widget_delay_timer_.Start(
FROM_HERE, kCloseWidgetDelay,
base::BindOnce(&QuickInsertController::CloseWidget,
weak_ptr_factory_.GetWeakPtr()));
} else {
CloseWidget();
}
}
void QuickInsertController::OpenResult(const QuickInsertSearchResult& result) {
return std::visit(
base::Overloaded{
[](const QuickInsertTextResult& data) { NOTREACHED(); },
[](const QuickInsertEmojiResult& data) { NOTREACHED(); },
[](const QuickInsertGifResult& data) { NOTREACHED(); },
[](const QuickInsertClipboardResult& data) { NOTREACHED(); },
[&](const QuickInsertBrowsingHistoryResult& data) {
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kOpenLink);
OpenLink(data.url);
},
[&](const QuickInsertLocalFileResult& data) {
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kOpenFile);
OpenFile(data.file_path);
},
[&](const QuickInsertDriveFileResult& data) {
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kOpenLink);
OpenLink(data.url);
},
[](const QuickInsertCategoryResult& data) { NOTREACHED(); },
[](const QuickInsertSearchRequestResult& data) { NOTREACHED(); },
[](const QuickInsertEditorResult& data) { NOTREACHED(); },
[](const QuickInsertLobsterResult& data) { NOTREACHED(); },
[&](const QuickInsertNewWindowResult& data) {
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kCreate);
OpenLink(GetUrlForNewWindow(data.type));
},
[&](const QuickInsertCapsLockResult& data) {
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kFormat);
caps_lock_request_ =
std::make_unique<QuickInsertActionOnNextFocusRequest>(
widget_->GetInputMethod(), kCapsLockRequestTimeout,
base::BindOnce(
[](bool enabled) {
GetImeKeyboard().SetCapsLockEnabled(enabled);
},
data.enabled),
base::BindOnce(
[](bool enabled) {
GetImeKeyboard().SetCapsLockEnabled(enabled);
},
data.enabled));
},
[&](const QuickInsertCaseTransformResult& data) {
if (!session_) {
return;
}
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kFormat);
std::u16string_view selected_text = session_->model.selected_text();
InsertResultOnNextFocus(QuickInsertTextResult(
TransformText(selected_text, data.type),
QuickInsertTextResult::Source::kCaseTransform));
},
},
result);
}
void QuickInsertController::ShowEmojiPicker(ui::EmojiPickerCategory category,
std::u16string_view query) {
ui::ShowEmojiPanelInSpecificMode(category,
ui::EmojiPickerFocusBehavior::kAlwaysShow,
base::UTF16ToUTF8(query));
}
void QuickInsertController::ShowEditor(
std::optional<std::string> preset_query_id,
std::optional<std::string> freeform_text) {
if (!show_editor_callback_.is_null()) {
std::move(show_editor_callback_)
.Run(std::move(preset_query_id), std::move(freeform_text));
}
}
// TODO: b:370885630 - Considers making selected_text as an argument of this
// method.
void QuickInsertController::ShowLobster(
std::optional<std::string> freeform_text) {
if (!show_lobster_callback_.is_null()) {
std::move(show_lobster_callback_)
.Run(session_ != nullptr && session_->model.selected_text() != u""
? base::UTF16ToUTF8(session_->model.selected_text())
: std::move(freeform_text));
}
}
QuickInsertAssetFetcher* QuickInsertController::GetAssetFetcher() {
return asset_fetcher_.get();
}
QuickInsertSessionMetrics& QuickInsertController::GetSessionMetrics() {
return session_->session_metrics;
}
QuickInsertActionType QuickInsertController::GetActionForResult(
const QuickInsertSearchResult& result) {
CHECK(session_);
const QuickInsertModeType mode = session_->model.GetMode();
return std::visit(
base::Overloaded{[mode](const QuickInsertTextResult& data) {
CHECK(mode == QuickInsertModeType::kNoSelection ||
mode == QuickInsertModeType::kHasSelection);
return QuickInsertActionType::kInsert;
},
[mode](const QuickInsertEmojiResult& data) {
CHECK(mode == QuickInsertModeType::kNoSelection ||
mode == QuickInsertModeType::kHasSelection);
return QuickInsertActionType::kInsert;
},
[mode](const QuickInsertGifResult& data) {
CHECK(mode == QuickInsertModeType::kNoSelection ||
mode == QuickInsertModeType::kHasSelection);
return QuickInsertActionType::kInsert;
},
[mode](const QuickInsertClipboardResult& data) {
CHECK(mode == QuickInsertModeType::kNoSelection ||
mode == QuickInsertModeType::kHasSelection);
return QuickInsertActionType::kInsert;
},
[mode](const QuickInsertBrowsingHistoryResult& data) {
return mode == QuickInsertModeType::kUnfocused
? QuickInsertActionType::kOpen
: QuickInsertActionType::kInsert;
},
[mode](const QuickInsertLocalFileResult& data) {
return mode == QuickInsertModeType::kUnfocused
? QuickInsertActionType::kOpen
: QuickInsertActionType::kInsert;
},
[mode](const QuickInsertDriveFileResult& data) {
return mode == QuickInsertModeType::kUnfocused
? QuickInsertActionType::kOpen
: QuickInsertActionType::kInsert;
},
[](const QuickInsertCategoryResult& data) {
return QuickInsertActionType::kDo;
},
[](const QuickInsertSearchRequestResult& data) {
return QuickInsertActionType::kDo;
},
[](const QuickInsertEditorResult& data) {
return QuickInsertActionType::kCreate;
},
[](const QuickInsertLobsterResult& data) {
return QuickInsertActionType::kCreate;
},
[](const QuickInsertNewWindowResult& data) {
return QuickInsertActionType::kDo;
},
[](const QuickInsertCapsLockResult& data) {
return QuickInsertActionType::kDo;
},
[&](const QuickInsertCaseTransformResult& data) {
return QuickInsertActionType::kDo;
}},
result);
}
std::vector<QuickInsertEmojiResult> QuickInsertController::GetSuggestedEmoji() {
CHECK(session_);
return session_->emoji_suggester.GetSuggestedEmoji();
}
bool QuickInsertController::IsGifsEnabled() {
CHECK(session_);
return session_->model.IsGifsEnabled();
}
PrefService* QuickInsertController::GetPrefs() {
return Shell::Get()->session_controller()->GetLastActiveUserPrefService();
}
QuickInsertModeType QuickInsertController::GetMode() {
CHECK(session_);
return session_->model.GetMode();
}
void QuickInsertController::OnViewIsDeleting(views::View* view) {
view_observation_.Reset();
session_.reset();
}
scoped_refptr<network::SharedURLLoaderFactory>
QuickInsertController::GetSharedURLLoaderFactory() {
return client_->GetSharedURLLoaderFactory();
}
void QuickInsertController::FetchFileThumbnail(
const base::FilePath& path,
const gfx::Size& size,
FetchFileThumbnailCallback callback) {
client_->FetchFileThumbnail(path, size, std::move(callback));
}
QuickInsertController::Session::Session(
PrefService* prefs,
ui::TextInputClient* focused_client,
input_method::ImeKeyboard* ime_keyboard,
QuickInsertModel::EditorStatus editor_status,
QuickInsertModel::LobsterStatus lobster_status,
QuickInsertEmojiSuggester::GetNameCallback get_name)
: model(prefs, focused_client, ime_keyboard, editor_status, lobster_status),
emoji_history_model(prefs),
emoji_suggester(&emoji_history_model, std::move(get_name)),
session_metrics(prefs) {
session_metrics.OnStartSession(focused_client);
feature_usage_metrics.StartUsage();
}
QuickInsertController::Session::~Session() {
feature_usage_metrics.StopUsage();
}
void QuickInsertController::ShowWidget(base::TimeTicks trigger_event_timestamp,
WidgetTriggerSource trigger_source) {
ui::TextInputClient* focused_text_input_client = GetFocusedTextInputClient();
show_editor_callback_ = client_->CacheEditorContext();
show_lobster_callback_ = client_->CacheLobsterContext(
/*support_image_insertion=*/focused_text_input_client &&
focused_text_input_client->CanInsertImage());
input_method::ImeKeyboard& keyboard = GetImeKeyboard();
if (focused_text_input_client &&
focused_text_input_client->GetTextInputType() ==
ui::TEXT_INPUT_TYPE_PASSWORD) {
bool should_enable = !keyboard.IsCapsLockEnabled();
keyboard.SetCapsLockEnabled(should_enable);
return;
}
session_ = std::make_unique<Session>(
GetPrefs(), focused_text_input_client, &keyboard,
show_editor_callback_.is_null()
? QuickInsertModel::EditorStatus::kDisabled
: QuickInsertModel::EditorStatus::kEnabled,
show_lobster_callback_.is_null()
? QuickInsertModel::LobsterStatus::kDisabled
: QuickInsertModel::LobsterStatus::kEnabled,
base::BindRepeating(
[](base::WeakPtr<QuickInsertController> weak_controller,
std::string_view emoji) -> std::string {
if (weak_controller == nullptr) {
return "";
}
return weak_controller->search_controller_.GetEmojiName(emoji);
},
weak_ptr_factory_.GetWeakPtr()));
const gfx::Rect anchor_bounds = GetQuickInsertAnchorBounds(
GetCaretBounds(), GetCursorPoint(), GetFocusedWindowBounds());
if (trigger_source == WidgetTriggerSource::kFeatureTour &&
session_->model.GetMode() == QuickInsertModeType::kUnfocused) {
widget_ = QuickInsertWidget::CreateCentered(this, anchor_bounds,
trigger_event_timestamp);
} else {
widget_ =
QuickInsertWidget::Create(this, anchor_bounds, trigger_event_timestamp);
}
widget_->Show();
view_observation_.Observe(widget_->GetContentsView());
}
void QuickInsertController::CloseWidget() {
if (!widget_) {
return;
}
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kAbandoned);
widget_->Close();
}
void QuickInsertController::ShowWidgetPostFeatureTour() {
ShowWidget(base::TimeTicks::Now(), WidgetTriggerSource::kFeatureTour);
}
std::optional<QuickInsertWebPasteTarget>
QuickInsertController::GetWebPasteTarget() {
return client_ ? client_->GetWebPasteTarget() : std::nullopt;
}
void QuickInsertController::InsertResultOnNextFocus(
const QuickInsertSearchResult& result) {
if (!widget_) {
return;
}
// Update emoji history in prefs the result is an emoji/symbol/emoticon.
CHECK(session_);
if (auto* data = std::get_if<QuickInsertEmojiResult>(&result);
data != nullptr && session_->model.should_do_learning()) {
session_->emoji_history_model.UpdateRecentEmoji(
EmojiResultTypeToCategory(data->type), base::UTF16ToUTF8(data->text));
}
std::visit(
base::Overloaded{
[&](QuickInsertRichMedia media) {
ui::InputMethod* input_method = widget_->GetInputMethod();
if (input_method == nullptr) {
return;
}
// This cancels the previous request if there was one.
insert_media_request_ = std::make_unique<
QuickInsertInsertMediaRequest>(
input_method, media, kInsertMediaTimeout,
base::BindOnce(
[](base::WeakPtr<QuickInsertController> weak_controller) {
return weak_controller
? weak_controller->GetWebPasteTarget()
: std::nullopt;
},
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&QuickInsertController::OnInsertCompleted,
weak_ptr_factory_.GetWeakPtr(), media));
},
[&](QuickInsertClipboardResult data) {
// This cancels the previous request if there was one.
paste_request_ = std::make_unique<QuickInsertPasteRequest>(
ClipboardHistoryController::Get(),
aura::client::GetFocusClient(widget_->GetNativeView()),
data.item_id);
},
[](std::monostate) { NOTREACHED(); },
},
GetInsertionContentForResult(result));
session_->session_metrics.SetOutcome(
QuickInsertSessionMetrics::SessionOutcome::kInsertedOrCopied);
}
void QuickInsertController::OnInsertCompleted(
const QuickInsertRichMedia& media,
QuickInsertInsertMediaRequest::Result result) {
// Fallback to copying to the clipboard on failure.
if (result != QuickInsertInsertMediaRequest::Result::kSuccess) {
CopyMediaToClipboard(media);
}
}
QuickInsertCapsLockPosition QuickInsertController::GetCapsLockPosition() {
// Always put the caps lock entry point at the top if the user has caps lock
// enabled, since it is they will likely want to disable it.
if (GetImeKeyboard().IsCapsLockEnabled()) {
return QuickInsertCapsLockPosition::kTop;
}
PrefService* prefs = GetPrefs();
if (prefs == nullptr) {
return QuickInsertCapsLockPosition::kTop;
}
int caps_lock_displayed_count =
prefs->GetInteger(prefs::kQuickInsertCapsLockDisplayedCountPrefName);
int caps_lock_selected_count =
prefs->GetInteger(prefs::kQuickInsertLockSelectedCountPrefName);
float caps_lock_selected_ratio =
static_cast<float>(caps_lock_selected_count) / caps_lock_displayed_count;
if (caps_lock_displayed_count < kCapsLockMinimumTopDisplayCount ||
caps_lock_selected_ratio >= kCapsLockRatioThresholdForTop) {
return QuickInsertCapsLockPosition::kTop;
}
if (caps_lock_selected_ratio >= kCapsLockRatioThresholdForBottom) {
return QuickInsertCapsLockPosition::kMiddle;
}
return QuickInsertCapsLockPosition::kBottom;
}
} // namespace ash