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 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
|
// Copyright 2024 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/glic/widget/glic_window_controller_impl.h"
#include <algorithm>
#include "base/check.h"
#include "base/check_deref.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/notimplemented.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/glic/browser_ui/scoped_glic_button_indicator.h"
#include "chrome/browser/glic/fre/glic_fre_controller.h"
#include "chrome/browser/glic/fre/glic_fre_dialog_view.h"
#include "chrome/browser/glic/glic_enabling.h"
#include "chrome/browser/glic/glic_keyed_service.h"
#include "chrome/browser/glic/glic_metrics.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/glic_profile_manager.h"
#include "chrome/browser/glic/host/glic.mojom-data-view.h"
#include "chrome/browser/glic/host/glic.mojom.h"
#include "chrome/browser/glic/host/host.h"
#include "chrome/browser/glic/host/webui_contents_container.h"
#include "chrome/browser/glic/resources/grit/glic_browser_resources.h"
#include "chrome/browser/glic/widget/browser_conditions.h"
#include "chrome/browser/glic/widget/glic_view.h"
#include "chrome/browser/glic/widget/glic_widget.h"
#include "chrome/browser/glic/widget/glic_window_animator.h"
#include "chrome/browser/glic/widget/glic_window_config.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/tabs/public/tab_dialog_manager.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/chrome_widget_sublevel.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/tab_strip_region_view.h"
#include "chrome/browser/ui/views/tabs/glic_button.h"
#include "chrome/browser/ui/views/tabs/tab_strip_action_container.h"
#include "chrome/browser/ui/views/tabs/window_finder.h"
#include "chrome/common/chrome_features.h"
#include "components/tabs/public/tab_interface.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_types.h"
#include "ui/display/display.h"
#include "ui/display/display_finder.h"
#include "ui/display/display_observer.h"
#include "ui/display/screen.h"
#include "ui/events/event_observer.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/event_monitor.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/widget/native_widget.h"
#include "ui/views/widget/widget_observer.h"
#if BUILDFLAG(IS_WIN)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/win/event_creation_utils.h"
#include "ui/display/win/screen_win.h"
#include "ui/views/win/hwnd_util.h"
#endif // BUILDFLAG(IS_WIN)
namespace glic {
DEFINE_CUSTOM_ELEMENT_EVENT_TYPE(kGlicWidgetAttached);
namespace {
// Default value for adding a buffer to the attachment zone.
constexpr static int kAttachmentBuffer = 20;
constexpr static int kInitialPositionBuffer = 4;
constexpr static int kMaxWidgetSize = 16'384;
constexpr static int kDraggableAreaHeight = 44;
constexpr static base::TimeDelta kAnimationDuration = base::Milliseconds(300);
mojom::PanelState CreatePanelState(bool widget_visible,
Browser* attached_browser) {
mojom::PanelState panel_state;
if (!widget_visible) {
panel_state.kind = mojom::PanelState_Kind::kHidden;
} else if (attached_browser) {
panel_state.kind = mojom::PanelState_Kind::kAttached;
panel_state.window_id = attached_browser->session_id().id();
} else {
panel_state.kind = mojom::PanelState_Kind::kDetached;
}
return panel_state;
}
GlicButton* GetGlicButton(const Browser& browser) {
return browser.window()
->AsBrowserView()
->tab_strip_region_view()
->GetGlicButton();
}
display::Display GetDisplayForOpeningDetached() {
// Get the Display for the most recently active browser. If there was no
// recently active browser, use the primary display.
Browser* last_active_browser = BrowserList::GetInstance()->GetLastActive();
if (last_active_browser) {
std::optional<display::Display> widget_display =
last_active_browser->GetBrowserView().GetWidget()->GetNearestDisplay();
if (widget_display) {
return *widget_display;
}
}
return display::Screen::GetScreen()->GetPrimaryDisplay();
}
// True if |bounds| is an allowed position the Widget can be shown in.
bool IsWidgetLocationAllowed(const gfx::Rect& bounds) {
const std::vector<display::Display>& displays =
display::Screen::GetScreen()->GetAllDisplays();
// Calculate inset corners to allow part of the widget to be off screen.
std::array<gfx::Point, 4> inset_points = {
// top-left: Allow 40% on left and |kInitialPositionBuffer| on top.
gfx::Point(bounds.x() + bounds.width() * .4,
bounds.y() + kInitialPositionBuffer),
// top-right: Allow 40% on right and |kInitialPositionBuffer| on top.
gfx::Point(bounds.right() - bounds.width() * .4,
bounds.y() + kInitialPositionBuffer),
// bottom-left: Allow 40% on left and 70% on bottom.
gfx::Point(bounds.x() + bounds.width() * .4,
bounds.bottom() - bounds.height() * .7),
// bottom-right: Allow 40% on right and 70% on bottom.
gfx::Point(bounds.right() - bounds.width() * .4,
bounds.bottom() - bounds.height() * .7),
};
// Check that all four points are on an existing display.
return std::ranges::all_of(inset_points, [&](gfx::Point p) {
return display::FindDisplayContainingPoint(displays, p) != displays.end();
});
}
std::optional<int> GetOptionalIntPreference(PrefService* prefs,
std::string_view path) {
const PrefService::Preference& pref =
CHECK_DEREF(prefs->FindPreference(path));
if (pref.IsDefaultValue()) {
return std::nullopt;
}
return pref.GetValue()->GetInt();
}
// Get the previous position or none if the window has not been dragged before.
std::optional<gfx::Point> GetPreviousPositionFromPrefs(PrefService* prefs) {
if (!prefs) {
return std::nullopt;
}
auto x_pos = GetOptionalIntPreference(prefs, prefs::kGlicPreviousPositionX);
auto y_pos = GetOptionalIntPreference(prefs, prefs::kGlicPreviousPositionY);
if (!x_pos.has_value() || !y_pos.has_value()) {
return std::nullopt;
}
return gfx::Point(x_pos.value(), y_pos.value());
}
} // namespace
// Helper class for observing mouse and key events from native window.
class GlicWindowControllerImpl::WindowEventObserver : public ui::EventObserver {
public:
WindowEventObserver(glic::GlicWindowController* glic_window_controller,
glic::GlicView* glic_view)
: glic_window_controller_(glic_window_controller), glic_view_(glic_view) {
event_monitor_ = views::EventMonitor::CreateWindowMonitor(
this, glic_view->GetWidget()->GetNativeWindow(),
{
ui::EventType::kMousePressed,
ui::EventType::kMouseReleased,
ui::EventType::kMouseDragged,
ui::EventType::kTouchReleased,
ui::EventType::kTouchPressed,
ui::EventType::kTouchMoved,
ui::EventType::kTouchCancelled,
});
}
WindowEventObserver(const WindowEventObserver&) = delete;
WindowEventObserver& operator=(const WindowEventObserver&) = delete;
~WindowEventObserver() override = default;
void OnEvent(const ui::Event& event) override {
#if BUILDFLAG(IS_WIN)
if (event.IsTouchEvent()) {
// If we get a touch event, send the corresponding mouse event so that
// drag drop of the floaty window will work with touch screens. This is a
// bit hacky; it would be better to have non client hit tests for the
// draggable area return HT_CAPTION but that requires the web client to
// set the draggable areas correctly, and not include the buttons in the
// titlebar. See crbug.com/388000848.
const ui::TouchEvent* touch_event = event.AsTouchEvent();
gfx::Point touch_location = touch_event->location();
auto touch_screen_point =
views::View::ConvertPointToScreen(glic_view_, touch_location);
auto* host = glic_view_->GetWidget()->GetNativeWindow()->GetHost();
host->ConvertDIPToPixels(&touch_screen_point);
if (event.type() == ui::EventType::kTouchPressed) {
POINT cursor_location = touch_screen_point.ToPOINT();
::SetCursorPos(cursor_location.x, cursor_location.y);
touch_down_in_draggable_area_ =
glic_view_->IsPointWithinDraggableArea(touch_location);
if (touch_down_in_draggable_area_) {
ui::SendMouseEvent(touch_screen_point, MOUSEEVENTF_LEFTDOWN);
ui::SendMouseEvent(touch_screen_point, MOUSEEVENTF_MOVE);
}
}
if (!touch_down_in_draggable_area_) {
// If we're not in a potential touch drag of the window, ignore touch
// events.
return;
}
if (event.type() == ui::EventType::kTouchCancelled ||
event.type() == ui::EventType::kTouchReleased) {
touch_down_in_draggable_area_ = false;
ui::SendMouseEvent(touch_screen_point, MOUSEEVENTF_LEFTUP);
}
if (event.type() == ui::EventType::kTouchMoved) {
ui::SendMouseEvent(touch_screen_point, MOUSEEVENTF_MOVE);
}
return;
}
#endif // BUILDFLAG(IS_WIN)
gfx::Point mouse_location = event_monitor_->GetLastMouseLocation();
views::View::ConvertPointFromScreen(glic_view_, &mouse_location);
if (event.type() == ui::EventType::kMousePressed) {
mouse_down_in_draggable_area_ =
glic_view_->IsPointWithinDraggableArea(mouse_location);
initial_press_loc_ = mouse_location;
}
if (event.type() == ui::EventType::kMouseReleased ||
event.type() == ui::EventType::kMouseExited) {
mouse_down_in_draggable_area_ = false;
initial_press_loc_ = gfx::Point();
}
// Window should only be dragged if a corresponding mouse drag event was
// initiated in the draggable area.
if (mouse_down_in_draggable_area_ &&
event.type() == ui::EventType::kMouseDragged &&
glic_window_controller_->ShouldStartDrag(initial_press_loc_,
mouse_location)) {
glic_window_controller_->HandleWindowDragWithOffset(
initial_press_loc_.OffsetFromOrigin());
}
}
private:
raw_ptr<glic::GlicWindowController> glic_window_controller_;
raw_ptr<glic::GlicView> glic_view_;
std::unique_ptr<views::EventMonitor> event_monitor_;
// Tracks whether the mouse is pressed and was initially within a draggable
// area of the window.
bool mouse_down_in_draggable_area_ = false;
#if BUILDFLAG(IS_WIN)
// Tracks whether a touch pressed event occurred within the draggable area. If
// so, subsequent touch events will trigger corresponding mouse events so that
// window drag works.
bool touch_down_in_draggable_area_ = false;
#endif // BUILDFLAG(IS_WIN)
// Tracks the initial kMousePressed location of a potential drag.
gfx::Point initial_press_loc_;
};
GlicWindowControllerImpl::GlicWindowControllerImpl(
Profile* profile,
signin::IdentityManager* identity_manager,
GlicKeyedService* glic_service,
GlicEnabling* enabling)
: profile_(profile),
fre_controller_(
std::make_unique<GlicFreController>(profile, identity_manager)),
window_finder_(std::make_unique<WindowFinder>()),
glic_service_(glic_service),
enabling_(enabling) {
if (window_config_.ShouldResetOnStart()) {
previous_position_.reset();
} else {
previous_position_ = GetPreviousPositionFromPrefs(profile_->GetPrefs());
}
application_hotkey_manager_ = MakeApplicationHotkeyManager(GetWeakPtr());
host_observation_.Observe(&glic_service_->host());
}
GlicWindowControllerImpl::~GlicWindowControllerImpl() = default;
void GlicWindowControllerImpl::WebClientInitializeFailed() {
if (state_ == State::kWaitingForGlicToLoad) {
// TODO(crbug.com/388328847): The web client failed to initialize. Decide
// what the fallback behavior is. Additionally, we probably need some kind
// of timeout and/or loading indicator if loading takes too much time. For
// now, show the UI anyway, which should be helpful in development.
LOG(ERROR)
<< "Glic web client failed to initialize, it won't work properly.";
glic_service_->metrics()->set_show_start_time(base::TimeTicks());
GlicLoadedAndReadyToDisplay();
}
}
void GlicWindowControllerImpl::LoginPageCommitted() {
login_page_committed_ = true;
if (state_ == State::kWaitingForGlicToLoad && !host().IsReady()) {
// TODO(crbug.com/388328847): Temporarily allow showing the UI when a login
// page is reached.
glic_service_->metrics()->set_show_start_time(base::TimeTicks());
GlicLoadedAndReadyToDisplay();
}
}
// Monitoring the glic widget.
void GlicWindowControllerImpl::OnWidgetActivationChanged(views::Widget* widget,
bool active) {
if (GetGlicWidget() != widget) {
return;
}
if (!active && do_focus_loss_announcement_) {
widget->widget_delegate()->SetAccessibleTitle(
l10n_util::GetStringUTF16(IDS_GLIC_WINDOW_TITLE));
GetGlicView()->GetViewAccessibility().AnnounceAlert(
l10n_util::GetStringFUTF16(
IDS_GLIC_WINDOW_FIRST_FOCUS_LOST_ANNOUNCEMENT,
LocalHotkeyManager::GetConfigurableAccelerator(
LocalHotkeyManager::Hotkey::kFocusToggle)
.GetShortcutText()));
do_focus_loss_announcement_ = false;
}
window_activation_callback_list_.Notify(active);
}
// Monitoring the glic widget.
void GlicWindowControllerImpl::OnWidgetDestroyed(views::Widget* widget) {
// This is used to handle the case where the native window is closed
// directly (e.g., Windows context menu close on the title bar).
// Conceptually this should synchronously call Close(), but the Widget
// implementation currently does not support this.
if (GetGlicWidget() == widget) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&GlicWindowControllerImpl::Close,
weak_ptr_factory_.GetWeakPtr()));
}
}
void GlicWindowControllerImpl::OnWidgetBoundsChanged(
views::Widget* widget,
const gfx::Rect& new_bounds) {
if (in_move_loop_ && !AlwaysDetached()) {
// While in a move loop, look for nearby browsers to toggle the drop to
// attach indicator.
HandleGlicButtonIndicator();
}
modal_dialog_host_observers_.Notify(
&web_modal::ModalDialogHostObserver::OnPositionRequiresUpdate);
}
void GlicWindowControllerImpl::OnWidgetUserResizeStarted() {
user_resizing_ = true;
glic_service_->metrics()->OnWidgetUserResizeStarted();
if (GlicWebClientAccess* client = host().GetPrimaryWebClient()) {
client->ManualResizeChanged(true);
}
}
void GlicWindowControllerImpl::OnWidgetUserResizeEnded() {
glic_service_->metrics()->OnWidgetUserResizeEnded();
if (GlicWebClientAccess* client = host().GetPrimaryWebClient()) {
client->ManualResizeChanged(false);
}
if (GetGlicView()) {
GetGlicView()->UpdatePrimaryDraggableAreaOnResize();
}
if (GetGlicWidget()) {
glic_size_ = GetGlicWidget()->GetClientAreaBoundsInScreen().size();
SaveWidgetPosition(/*user_modified=*/true);
}
glic_window_animator_->ResetLastTargetSize();
user_resizing_ = false;
}
void GlicWindowControllerImpl::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t changed_metrics) {
MaybeAdjustSizeForDisplay(/*animate=*/false);
AdjustPositionIfNeeded();
}
void GlicWindowControllerImpl::ShowAfterSignIn(base::WeakPtr<Browser> browser) {
Toggle(browser.get(), true,
// Prefer the source that triggered the sign-in, but if that's not
// available, report it as coming from the sign-in flow.
opening_source_.value_or(mojom::InvocationSource::kAfterSignIn));
}
void GlicWindowControllerImpl::Toggle(BrowserWindowInterface* bwi,
bool prevent_close,
mojom::InvocationSource source) {
// If `bwi` is non-null, the glic button was clicked on a specific window and
// glic should be attached to that window. Otherwise glic was invoked from the
// hotkey or other OS-level entrypoint.
Browser* new_attached_browser =
bwi ? bwi->GetBrowserForMigrationOnly() : nullptr;
// Show the FRE if not yet completed, and if we have a browser to use.
if (fre_controller_->ShouldShowFreDialog()) {
if (!fre_controller_->CanShowFreDialog(new_attached_browser)) {
// If the FRE is blocked because it is already showing, we should instead
// dismiss it. This allows the glic button to be used to toggle the
// presence of the FRE.
fre_controller_->DismissFreIfOpenOnActiveTab(new_attached_browser);
return;
}
fre_controller_->ShowFreDialog(new_attached_browser, source);
return;
}
mojom::PanelState panel_state = ComputePanelState();
bool is_detached = panel_state.kind == mojom::PanelState_Kind::kDetached;
// In the case where the user invokes the hotkey, or the status tray glic
// icon and the most recently used window for the glic profile is active,
// treat this as if the user clicked the glic button on that window if
// Chrome is currently in the foreground and we aren't in detached state.
if (!new_attached_browser && !is_detached) {
Browser* last_active_browser = BrowserList::GetInstance()->GetLastActive();
if (last_active_browser &&
IsBrowserGlicAttachable(profile_, last_active_browser) &&
IsBrowserInForeground(last_active_browser)) {
new_attached_browser = last_active_browser;
}
}
if (!AlwaysDetached()) {
ToggleWhenNotAlwaysDetached(new_attached_browser, prevent_close, source);
return;
}
auto maybe_close = [this, prevent_close] {
if (!prevent_close) {
Close();
}
};
// If floaty is closed, open floaty
if (state_ == State::kClosed) {
Show(new_attached_browser, source);
return;
}
#if BUILDFLAG(IS_WIN)
// Clicking status tray on Windows makes floaty not active so always close.
if (source == mojom::InvocationSource::kOsButton) {
Close();
return;
}
#endif // BUILDFLAG(IS_WIN)
// If floaty is focused or the source is the top button, close it.
// If floaty is unfocused and open, focus it.
if (IsActive() ||
(source == mojom::InvocationSource::kTopChromeButton &&
!base::FeatureList::IsEnabled(features::kGlicZOrderChanges))) {
maybe_close();
} else if (state_ == State::kOpen) {
// TODO(crbug.com/404601783): Bring focus to the textbox.
GetGlicWidget()->Activate();
GetGlicView()->GetWebContents()->Focus();
}
}
void GlicWindowControllerImpl::ToggleWhenNotAlwaysDetached(
Browser* new_attached_browser,
bool prevent_close,
mojom::InvocationSource source) {
auto maybe_close = [this, prevent_close] {
if (!prevent_close) {
Close();
}
};
// Pressing the button or the hotkey when the window is open, or waiting to
// load should close it. The latter is required because otherwise if there
// were an error loading the backend (or if it just took a long time) then the
// button/hotkey would become unresponsive.
//
// In the future, when the WebUI can send its status back to the controller
// via mojom, we could explicitly restrict the second case to loading,
// offline, and error states.
if (state_ == State::kOpen || state_ == State::kWaitingForGlicToLoad) {
if (new_attached_browser) {
if (new_attached_browser == attached_browser_) {
// Button was clicked on same browser: close.
// There are three ways for this to happen. Normally the glic window
// obscures the glic button. Either the user used keyboard navigation to
// click the glic button, the user clicked the button early and the
// button click was eventually processed asynchronously after the button
// was obscured, or the user invokes the glic hotkey while glic is
// attached to the active window.
maybe_close();
} else {
// Button clicked on a different browser: attach to that one.
MovePositionToBrowserGlicButton(*new_attached_browser,
/*animate=*/true);
AttachToBrowser(*new_attached_browser, AttachChangeReason::kInit);
}
return;
}
// Everything else in this block handles the case where the user invokes the
// hotkey and the most recently used window from the glic profile is not
// active.
// Already attached?
if (attached_browser_) {
bool should_close = IsActive();
#if BUILDFLAG(IS_WIN)
// On Windows, clicking the system tray icon de-activates the active
// window, so fall back to checking if `attached_browser_` is visible for
// both the hot key and system tray click cases.
should_close = attached_browser_->window()
->GetNativeWindow()
->GetHost()
->GetNativeWindowOcclusionState() ==
aura::Window::OcclusionState::VISIBLE;
#endif // BUILDFLAG(IS_WIN)
if (should_close) {
// Hotkey when glic active and attached: close.
maybe_close();
return;
}
// Hotkey when glic is inactive and attached:
if (attached_browser_->IsActive()) {
// Hotkey when glic inactive but attached to active browser: close.
// Note: this should not be possible, since if the attached browser is
// active, new_attached_browser must not have been null.
maybe_close();
} else {
// Hotkey when neither attached browser nor glic are active: open
// detached.
}
return;
}
// Hotkey invoked when glic is already detached.
maybe_close();
} else if (state_ != State::kClosed) {
// Currently in the process of showing the widget, allow that to finish.
return;
} else {
Show(new_attached_browser, source);
}
}
void GlicWindowControllerImpl::FocusIfOpen() {
if (IsShowing() && !IsActive()) {
GetGlicWidget()->Activate();
GetGlicView()->GetWebContents()->Focus();
}
}
void GlicWindowControllerImpl::ShowDetachedForTesting() {
glic::GlicProfileManager::GetInstance()->SetActiveGlic(glic_service_);
Show(nullptr, mojom::InvocationSource::kOsHotkey);
}
void GlicWindowControllerImpl::SetPreviousPositionForTesting(
gfx::Point position) {
previous_position_ = position;
}
Host& GlicWindowControllerImpl::host() const {
return glic_service_->host();
}
void GlicWindowControllerImpl::Show(Browser* browser,
mojom::InvocationSource source) {
// At this point State must be kClosed, and all glic window state must be
// unset.
CHECK(!attached_browser_);
opening_source_ = source;
if (source == mojom::InvocationSource::kTopChromeButton &&
window_config_.ShouldResetOnOpen()) {
previous_position_.reset();
base::RecordAction(
base::UserMetricsAction("Glic.Widget.ResetPositionOnOpen"));
}
if (window_config_.ShouldResetOnNewSession()) {
previous_position_.reset();
}
window_config_.SetLastOpenTime();
if (!glic_service_->GetAuthController().CheckAuthBeforeShowSync(
base::BindOnce(&GlicWindowControllerImpl::ShowAfterSignIn,
weak_ptr_factory_.GetWeakPtr(),
browser ? browser->AsWeakPtr() : nullptr))) {
return;
}
glic_window_animator_ = std::make_unique<GlicWindowAnimator>(this);
SetWindowState(State::kWaitingForGlicToLoad);
glic_service_->metrics()->OnGlicWindowOpen(/*attached=*/browser, source);
glic_service_->GetAuthController().OnGlicWindowOpened();
glic_service_->metrics()->set_show_start_time(base::TimeTicks::Now());
host().CreateContents();
host().NotifyWindowIntentToShow();
SetupGlicWidget(browser);
// Notify the web client that the panel will open, and wait for the response
// to actually show the window. Note that we have to call
// `NotifyIfPanelStateChanged()` first, so that the host will receive the
// correct panel state.
NotifyIfPanelStateChanged();
host().PanelWillOpen(source);
if (login_page_committed_) {
// This indicates that we've warmed the web client and it has hit a login
// page. See LoginPageCommitted.
GlicLoadedAndReadyToDisplay();
} else {
// This adds dragging functionality to special case panels (e.g. error,
// offline, loading).
SetDraggingAreasAndWatchForMouseEvents();
}
std::optional<display::Display> display =
GetGlicWidget()->GetNearestDisplay();
gfx::Point glic_center_point =
GetGlicWidget()->GetWindowBoundsInScreen().CenterPoint();
glic_service_->metrics()->OnGlicWindowShown(display, glic_center_point);
}
void GlicWindowControllerImpl::SetupGlicWidget(Browser* browser) {
auto initial_bounds = GetInitialBounds(browser);
glic_window_hotkey_manager_ = MakeGlicWindowHotkeyManager(GetWeakPtr());
glic_widget_ = GlicWidget::Create(profile_, initial_bounds,
glic_window_hotkey_manager_->GetWeakPtr(),
user_resizable_);
glic_widget_observation_.Observe(glic_widget_.get());
SetupGlicWidgetAccessibilityText();
glic_window_hotkey_manager_->InitializeAccelerators();
if (AlwaysDetached()) {
SetGlicWindowToFloatingMode(true);
} else if (!browser) {
// Detached window when no always detach. Be sure to set its state first
// before showing it.
// TODO(crbug.com/410629338): Reimplement attachment.
}
glic_widget_->Show();
// This is needed in case of theme difference between OS and chrome.
GetGlicWidget()->ThemeChanged();
if (browser && !AlwaysDetached()) {
// Attached window if needed.
AttachToBrowser(*browser, AttachChangeReason::kInit);
}
// This is used to handle the case where the native window is closed
// directly (e.g., Windows context menu close on the title bar). It fixes the
// bug where the window position was not restored after closing with the
// context menu close menu item.
GetGlicWidget()->MakeCloseSynchronous(base::BindOnce(
&GlicWindowControllerImpl::CloseWithReason, base::Unretained(this)));
// Immediately hook up the WebView to the WebContents.
GetGlicView()->SetWebContents(host().webui_contents());
GetGlicView()->UpdateBackgroundColor();
// Add capability to show web modal dialogs (e.g. Data Controls Dialogs for
// enterprise users) via constrained_window APIs.
web_modal::WebContentsModalDialogManager::CreateForWebContents(
host().webui_contents());
web_modal::WebContentsModalDialogManager::FromWebContents(
host().webui_contents())
->SetDelegate(this);
}
void GlicWindowControllerImpl::SetupGlicWidgetAccessibilityText() {
auto* widget_delegate = glic_widget_->widget_delegate();
if (opening_source_ == mojom::InvocationSource::kFre) {
widget_delegate->SetAccessibleTitle(l10n_util::GetStringFUTF16(
IDS_GLIC_WINDOW_TITLE_FIRST_LOAD,
LocalHotkeyManager::GetConfigurableAccelerator(
LocalHotkeyManager::Hotkey::kFocusToggle)
.GetShortcutText()));
do_focus_loss_announcement_ = true;
} else {
widget_delegate->SetAccessibleTitle(
l10n_util::GetStringUTF16(IDS_GLIC_WINDOW_TITLE));
}
}
void GlicWindowControllerImpl::SetGlicWindowToFloatingMode(bool floating) {
GetGlicWidget()->SetZOrderLevel(floating ? ui::ZOrderLevel::kFloatingWindow
: ui::ZOrderLevel::kNormal);
#if BUILDFLAG(IS_MAC)
GetGlicWidget()->SetActivationIndependence(floating);
GetGlicWidget()->SetVisibleOnAllWorkspaces(floating);
GetGlicWidget()->SetCanAppearInExistingFullscreenSpaces(floating);
#endif
}
gfx::Rect GlicWindowControllerImpl::GetInitialBounds(Browser* browser) {
if (browser && !AlwaysDetached()) {
return GetInitialAttachedBounds(*browser);
}
gfx::Size target_size = GetLastRequestedSizeClamped();
// Reset previous position if it results in an invalid location.
if (previous_position_.has_value() &&
!IsWidgetLocationAllowed({previous_position_.value(), target_size})) {
previous_position_.reset();
}
// Use the previous position if there is one.
if (previous_position_.has_value()) {
return {previous_position_.value(), target_size};
}
std::optional<gfx::Rect> bounds_with_browser =
GetInitialDetachedBoundsFromBrowser(browser, target_size);
return bounds_with_browser.value_or(
GetInitialDetachedBoundsNoBrowser(target_size));
}
gfx::Rect GlicWindowControllerImpl::GetInitialDetachedBoundsNoBrowser(
const gfx::Size& target_size) {
// Get the default position offset equal distances from the top right corner
// of the work area (which excludes system UI such as the taskbar).
display::Display display = GetDisplayForOpeningDetached();
gfx::Point top_right = display.work_area().top_right();
int initial_x =
top_right.x() - target_size.width() - kDefaultDetachedTopRightDistance;
int initial_y = top_right.y() + kDefaultDetachedTopRightDistance;
return {{initial_x, initial_y}, target_size};
}
gfx::Rect GlicWindowControllerImpl::GetInitialAttachedBounds(Browser& browser) {
GlicButton* glic_button = GetGlicButton(browser);
CHECK(glic_button);
// If summoned from the tab strip button. This will always show up attached
// because it is tied to a views::View object within the current browser
// window.
gfx::Rect glic_window_widget_initial_rect = glic_button->GetBoundsWithInset();
// Ensure that we start with a non-empty rect (see DCHECK in
// NativeWidgetNSWindowBridge::SetInitialBounds).
if (glic_window_widget_initial_rect.IsEmpty()) {
glic_window_widget_initial_rect.set_width(
std::max(glic_window_widget_initial_rect.width(), 1));
glic_window_widget_initial_rect.set_height(
std::max(glic_window_widget_initial_rect.height(), 1));
}
return glic_window_widget_initial_rect;
}
std::optional<gfx::Rect>
GlicWindowControllerImpl::GetInitialDetachedBoundsFromBrowser(
Browser* browser,
const gfx::Size& target_size) {
if (!browser) {
return std::nullopt;
}
// Set the origin so the top right of glic meets the bottom left of the glic
// button.
GlicButton* glic_button = GetGlicButton(*browser);
CHECK(glic_button);
gfx::Rect glic_button_inset_bounds = glic_button->GetBoundsWithInset();
gfx::Point origin(glic_button_inset_bounds.x() - target_size.width() -
kInitialPositionBuffer,
glic_button_inset_bounds.bottom() + kInitialPositionBuffer);
gfx::Rect bounds = {origin, target_size};
return IsWidgetLocationAllowed(bounds) ? std::make_optional(bounds)
: std::nullopt;
}
void GlicWindowControllerImpl::ClientReadyToShow(
const mojom::OpenPanelInfo& open_info) {
DVLOG(1) << "Glic client ready to show " << open_info.web_client_mode;
glic_service_->metrics()->set_starting_mode(open_info.web_client_mode);
glic_service_->metrics()->OnGlicWindowOpenAndReady();
if (open_info.panelSize.has_value()) {
Resize(*open_info.panelSize, open_info.resizeDuration, base::DoNothing());
}
EnableDragResize(open_info.can_user_resize);
if (state_ == State::kWaitingForGlicToLoad) {
GlicLoadedAndReadyToDisplay();
}
}
void GlicWindowControllerImpl::GlicLoadedAndReadyToDisplay() {
login_page_committed_ = false;
if (state_ == State::kClosed || state_ == State::kOpen) {
return;
}
// Update the background color after showing the webview so the transition
// isn't visible. This will be the widget background color the user sees next
// time.
GetGlicView()->UpdateBackgroundColor();
// In the case that the open animation was skipped, the web view should still
// be visible now.
SetWindowState(State::kOpen);
// Whenever the glic window is shown, it should have focus. The following line
// of code appears to be necessary but not sufficient and there are still some
// edge cases.
// TODO(crbug.com/390637019): Fully fix and remove this comment.
GetGlicView()->GetWebContents()->Focus();
SetDraggingAreasAndWatchForMouseEvents();
NotifyIfPanelStateChanged();
}
void GlicWindowControllerImpl::SetDraggingAreasAndWatchForMouseEvents() {
if (window_event_observer_) {
return;
}
window_event_observer_ =
std::make_unique<WindowEventObserver>(this, GetGlicView());
if (!draggable_area_) {
// Set the draggable area to the top bar of the window.
GetGlicView()->SetDraggableAreas(
{{0, 0, GetGlicView()->width(), kDraggableAreaHeight}});
}
}
GlicView* GlicWindowControllerImpl::GetGlicView() {
if (!GetGlicWidget()) {
return nullptr;
}
return static_cast<GlicView*>(GetGlicWidget()->GetContentsView());
}
base::WeakPtr<views::View> GlicWindowControllerImpl::GetGlicViewAsView() {
if (auto* view = GetGlicView()) {
return view->GetWeakPtr();
}
return nullptr;
}
GlicWidget* GlicWindowControllerImpl::GetGlicWidget() {
return glic_widget_.get();
}
content::WebContents* GlicWindowControllerImpl::GetFreWebContents() {
return fre_controller_->GetWebContents();
}
gfx::Point GlicWindowControllerImpl::GetTopRightPositionForAttachedGlicWindow(
GlicButton* glic_button) {
// The widget should be placed so its top right corner matches the visible top
// right corner of the glic button.
return glic_button->GetBoundsWithInset().top_right();
}
void GlicWindowControllerImpl::AttachedBrowserDidClose(
BrowserWindowInterface* browser) {
Close();
}
void GlicWindowControllerImpl::Attach() {
if (!GetGlicWidget()) {
return;
}
Browser* browser = glic::FindBrowserForAttachment(profile_);
if (!browser) {
return;
}
MovePositionToBrowserGlicButton(*browser, /*animate=*/true);
if (AlwaysDetached()) {
return;
}
AttachToBrowser(*browser, AttachChangeReason::kMenu);
}
void GlicWindowControllerImpl::Detach() {
if (state_ != State::kOpen || !attached_browser_) {
return;
}
if (!AlwaysDetached()) {
// TODO(crbug.com/410629338): Reimplement attachment.
}
NOTIMPLEMENTED();
}
void GlicWindowControllerImpl::DetachFinished() {
SetWindowState(State::kOpen);
}
void GlicWindowControllerImpl::AttachToBrowser(Browser& browser,
AttachChangeReason reason) {
CHECK(!AlwaysDetached());
CHECK(GetGlicWidget());
attached_browser_ = &browser;
glic_service_->metrics()->OnAttachedToBrowser(reason);
BrowserView* browser_view = browser.window()->AsBrowserView();
CHECK(browser_view);
if (!IsActive()) {
GetGlicWidget()->Activate();
}
NotifyIfPanelStateChanged();
SetGlicWindowToFloatingMode(false);
browser_close_subscription_ = browser.RegisterBrowserDidClose(
base::BindRepeating(&GlicWindowControllerImpl::AttachedBrowserDidClose,
base::Unretained(this)));
// Trigger custom event for testing.
views::ElementTrackerViews::GetInstance()->NotifyCustomEvent(
kGlicWidgetAttached, GetGlicButton(browser));
}
void GlicWindowControllerImpl::Resize(const gfx::Size& size,
base::TimeDelta duration,
base::OnceClosure callback) {
glic_size_ = size;
glic_service_->metrics()->OnGlicWindowResize();
const bool in_resizable_state =
state_ == State::kOpen || state_ == State::kWaitingForGlicToLoad;
// TODO(https://crbug.com/379164689): Drive resize animations for error states
// from the browser. For now, we allow animations during the waiting state.
// TODO(https://crbug.com/392668958): If the widget is ready and asks for a
// resize before the opening animation is finished, we will stop the current
// animation and resize to the final size. Investigate a smoother way to
// animate this transition.
if (in_resizable_state && !user_resizing_) {
glic_window_animator_->AnimateSize(GetLastRequestedSizeClamped(), duration,
std::move(callback));
} else {
// If the glic window is closed, or the widget isn't ready (e.g. because
// it's currently still animating closed) immediately post the callback.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
}
void GlicWindowControllerImpl::EnableDragResize(bool enabled) {
user_resizable_ = enabled;
if (!GetGlicWidget()) {
return;
}
if (base::FeatureList::IsEnabled(features::kGlicZOrderChanges)) {
// Drag-resizability implies text mode, which isn't floating, while
// non-resizability implies audio mode, which is floating.
SetGlicWindowToFloatingMode(!enabled);
}
MaybeSetWidgetCanResize();
GetGlicView()->UpdateBackgroundColor();
glic_window_animator_->MaybeAnimateToTargetSize();
}
void GlicWindowControllerImpl::MaybeSetWidgetCanResize() {
if (!GetGlicWidget()) {
return;
}
if (GetGlicWidget()->widget_delegate()->CanResize() == user_resizable_ ||
glic_window_animator_->IsAnimating()) {
// If the resize state is already correct or the widget is animating do not
// update the resize state.
return;
}
#if BUILDFLAG(IS_WIN)
// On Windows when resize is enabled there is an invisible border added
// around the client area. We need to make the widget larger or smaller to
// keep the visible client area the same size.
gfx::Rect previous_client_bounds =
GetGlicWidget()->GetClientAreaBoundsInScreen();
#endif // BUILDFLAG(IS_WIN)
// Update resize state on widget delegate.
GetGlicWidget()->widget_delegate()->SetCanResize(user_resizable_);
#if BUILDFLAG(IS_WIN)
if (user_resizable_) {
// Resizable so the widget area is larger than the client area.
gfx::Rect new_widget_bounds =
GetGlicWidget()->VisibleToWidgetBounds(previous_client_bounds);
GetGlicWidget()->SetBoundsConstrained(new_widget_bounds);
} else {
// Not resizable so the client and widget areas are the same.
GetGlicWidget()->SetBoundsConstrained(previous_client_bounds);
}
#endif // BUILDFLAG(IS_WIN)
}
gfx::Size GlicWindowControllerImpl::GetSize() {
if (!GetGlicWidget()) {
return gfx::Size();
}
return GetGlicWidget()->GetSize();
}
void GlicWindowControllerImpl::SetDraggableAreas(
const std::vector<gfx::Rect>& draggable_areas) {
GlicView* glic_view = GetGlicView();
if (!glic_view) {
return;
}
glic_view->SetDraggableAreas(draggable_areas);
}
void GlicWindowControllerImpl::SetMinimumWidgetSize(const gfx::Size& size) {
if (!GetGlicWidget()) {
return;
}
glic_widget_->SetMinimumSize(size);
}
void GlicWindowControllerImpl::CloseWithReason(
views::Widget::ClosedReason reason) {
Close();
}
void GlicWindowControllerImpl::Close() {
if (state_ == State::kClosed) {
return;
}
// Save the widge position on close so we can restore in the same position.
SaveWidgetPosition(/*user_modified=*/false);
window_config_.SetLastCloseTime();
glic_window_animator_.reset();
std::optional<display::Display> display =
GetGlicWidget()->GetNearestDisplay();
gfx::Point glic_center_point =
GetGlicWidget()->GetWindowBoundsInScreen().CenterPoint();
glic_service_->metrics()->OnGlicWindowClose(display, glic_center_point);
base::UmaHistogramEnumeration("Glic.PanelWebUiState.FinishState2",
host().GetPrimaryWebUiState());
SetWindowState(State::kClosed);
attached_browser_ = nullptr;
draggable_area_ = std::nullopt;
window_event_observer_.reset();
browser_close_subscription_.reset();
glic_window_hotkey_manager_.reset();
glic_widget_observation_.Reset();
glic_widget_.reset();
scoped_glic_button_indicator_.reset();
user_resizing_ = false;
NotifyIfPanelStateChanged();
window_activation_callback_list_.Notify(false);
modal_dialog_host_observers_.Notify(
&web_modal::ModalDialogHostObserver::OnHostDestroying);
web_modal::WebContentsModalDialogManager::FromWebContents(
host().webui_contents())
->SetDelegate(nullptr);
host().PanelWasClosed();
if (base::FeatureList::IsEnabled(features::kGlicUnloadOnClose)) {
host().Shutdown();
}
}
void GlicWindowControllerImpl::SaveWidgetPosition(bool user_modified) {
if (!GetGlicWidget() || !GetGlicWidget()->IsVisible()) {
return;
}
if (window_config_.ShouldSetPostionOnDrag() && !user_modified &&
!previous_position_.has_value()) {
profile_->GetPrefs()->ClearPref(prefs::kGlicPreviousPositionX);
profile_->GetPrefs()->ClearPref(prefs::kGlicPreviousPositionY);
return;
}
previous_position_ = GetGlicWidget()->GetClientAreaBoundsInScreen().origin();
profile_->GetPrefs()->SetInteger(prefs::kGlicPreviousPositionX,
previous_position_->x());
profile_->GetPrefs()->SetInteger(prefs::kGlicPreviousPositionY,
previous_position_->y());
}
void GlicWindowControllerImpl::ShowTitleBarContextMenuAt(gfx::Point event_loc) {
#if BUILDFLAG(IS_WIN)
views::View::ConvertPointToScreen(GetGlicView(), &event_loc);
event_loc = display::win::GetScreenWin()->DIPToScreenPoint(event_loc);
views::ShowSystemMenuAtScreenPixelLocation(views::HWNDForView(GetGlicView()),
event_loc);
#endif // BUILDFLAG(IS_WIN)
}
bool GlicWindowControllerImpl::ShouldStartDrag(
const gfx::Point& initial_press_loc,
const gfx::Point& mouse_location) {
// Determine if the mouse has moved beyond a minimum elasticity distance
// in any direction from the starting point.
static const int kMinimumDragDistance = 10;
int x_offset = abs(mouse_location.x() - initial_press_loc.x());
int y_offset = abs(mouse_location.y() - initial_press_loc.y());
return sqrt(pow(static_cast<float>(x_offset), 2) +
pow(static_cast<float>(y_offset), 2)) > kMinimumDragDistance;
}
void GlicWindowControllerImpl::HandleWindowDragWithOffset(
gfx::Vector2d mouse_offset) {
// This code isn't set up to handle nested run loops. Nested run loops will
// lead to crashes.
if (!in_move_loop_) {
in_move_loop_ = true;
#if BUILDFLAG(IS_MAC)
GetGlicWidget()->SetCapture(nullptr);
#endif
const views::Widget::MoveLoopSource move_loop_source =
views::Widget::MoveLoopSource::kMouse;
if (!AlwaysDetached()) {
// Set glic to a floating z-order while dragging so browsers brought into
// focus by HandleGlicButtonIndicator won't show in front of glic.
GetGlicWidget()->SetZOrderLevel(ui::ZOrderLevel::kFloatingWindow);
}
GetGlicWidget()->RunMoveLoop(
mouse_offset, move_loop_source,
views::Widget::MoveLoopEscapeBehavior::kDontHide);
in_move_loop_ = false;
scoped_glic_button_indicator_.reset();
// Only handle positioning if glic wasn't closed during the drag.
if (state_ == State::kClosed) {
return;
}
// Dragging stops animations. This makes sure we honor the last resize
// request.
glic_window_animator_->MaybeAnimateToTargetSize();
MaybeAdjustSizeForDisplay(/*animate=*/false);
AdjustPositionIfNeeded();
SaveWidgetPosition(/*user_modified=*/true);
if (!AlwaysDetached()) {
// set glic z-order back to normal after drag is done.
GetGlicWidget()->SetZOrderLevel(ui::ZOrderLevel::kNormal);
// Check whether `GetGlicWidget()` is in a position to attach to a
// browser window.
OnDragComplete();
}
}
}
const mojom::PanelState& GlicWindowControllerImpl::GetPanelState() const {
return panel_state_;
}
void GlicWindowControllerImpl::AdjustPositionIfNeeded() {
if (!GetGlicWidget()) {
return;
}
// Always have at least `kMinimumVisible` px visible from glic window in
// both vertical and horizontal directions.
constexpr int kMinimumVisible = 40;
const auto widget_size = GetGlicWidget()->GetSize();
const int horizontal_buffer = widget_size.width() - kMinimumVisible;
const int vertical_buffer = widget_size.height() - kMinimumVisible;
// Adjust bounds of visible area screen to allow part of glic to go off
// screen.
auto workarea = GetGlicWidget()->GetWorkAreaBoundsInScreen();
workarea.Outset(gfx::Outsets::VH(vertical_buffer, horizontal_buffer));
auto rect = GetGlicWidget()->GetRestoredBounds();
rect.AdjustToFit(workarea);
GetGlicWidget()->SetBounds(rect);
}
void GlicWindowControllerImpl::OnDragComplete() {
Browser* browser = FindBrowserForAttachment();
// No browser within attachment range.
if (!browser) {
return;
}
// Attach to the found browser.
MovePositionToBrowserGlicButton(*browser, /*animate=*/true);
AttachToBrowser(*browser, AttachChangeReason::kDrag);
}
void GlicWindowControllerImpl::HandleGlicButtonIndicator() {
Browser* browser = FindBrowserForAttachment();
// No browser within attachment range so reset indicators
if (!browser) {
scoped_glic_button_indicator_.reset();
return;
}
GlicButton* glic_button = GetGlicButton(*browser);
// If there isn't an existing scoped indicator for this button, create one.
if (!scoped_glic_button_indicator_ ||
scoped_glic_button_indicator_->GetGlicButton() != glic_button) {
// Bring the browser to the front.
browser->GetBrowserView().GetWidget()->Activate();
scoped_glic_button_indicator_ =
std::make_unique<ScopedGlicButtonIndicator>(glic_button);
}
}
Browser* GlicWindowControllerImpl::FindBrowserForAttachment() {
// The profile must have started off as Glic enabled since a Glic widget is
// open but it may have been disabled at runtime by policy. In this edge-case,
// prevent reattaching back to a window (as it no longer has a GlicButton).
if (!GlicEnabling::IsEnabledForProfile(profile_)) {
return nullptr;
}
gfx::Point glic_top_right =
GetGlicWidget()->GetWindowBoundsInScreen().top_right();
// Loops through all browsers in activation order with the latest accessed
// browser first.
for (Browser* browser : BrowserList::GetInstance()->OrderedByActivation()) {
if (!IsBrowserGlicAttachable(profile_, browser)) {
continue;
}
// If the profile is enabled, the Glic button must be available.
auto* tab_strip_region_view =
browser->GetBrowserView().tab_strip_region_view();
CHECK(tab_strip_region_view);
CHECK(tab_strip_region_view->GetGlicButton());
// Define attachment zone as the right of the tab strip. It either is the
// width of the widget or 1/3 of the tab strip, whichever is smaller.
gfx::Rect attachment_zone = tab_strip_region_view->GetBoundsInScreen();
int width = std::min(attachment_zone.width() / 3,
GlicWidget::GetInitialSize().width());
attachment_zone.SetByBounds(attachment_zone.right() - width,
attachment_zone.y() - kAttachmentBuffer,
attachment_zone.right() + kAttachmentBuffer,
attachment_zone.bottom());
// If both the left center of the attachment zone and glic button right
// center are occluded, don't consider for attachment.
if (IsBrowserOccludedAtPoint(browser, attachment_zone.left_center()) &&
IsBrowserOccludedAtPoint(browser, tab_strip_region_view->GetGlicButton()
->GetBoundsInScreen()
.right_center())) {
continue;
}
if (attachment_zone.Contains(glic_top_right)) {
return browser;
}
}
// No browser found near glic.
return nullptr;
}
void GlicWindowControllerImpl::MovePositionToBrowserGlicButton(
const Browser& browser,
bool animate) {
if (!GetGlicWidget()) {
return;
}
// If the profile's been disabled (e.g. by policy) the window's Glic button
// will be removed so we can't anchor to it. We could work around this by
// keeping the button but disabling and making it invisible but this is an
// edge-case, not sure it's worth the effort.
if (!GlicEnabling::IsEnabledForProfile(browser.profile())) {
return;
}
if (AlwaysDetached() && !IsActive()) {
GetGlicWidget()->Activate();
}
// This will stack with a move animation below.
MaybeAdjustSizeForDisplay(animate);
GlicButton* glic_button = GetGlicButton(browser);
CHECK(glic_button);
gfx::Point window_target_top_left;
if (AlwaysDetached()) {
gfx::Point button_bottom_left =
glic_button->GetBoundsWithInset().bottom_left();
window_target_top_left =
gfx::Point(button_bottom_left.x() -
GetGlicWidget()->GetWindowBoundsInScreen().width(),
button_bottom_left.y());
} else {
gfx::Point top_right =
GetTopRightPositionForAttachedGlicWindow(glic_button);
window_target_top_left = gfx::Point(
top_right.x() - GetGlicWidget()->GetWindowBoundsInScreen().width(),
top_right.y());
}
// Avoid conversions between pixels and DIP on non 1.0 scale factor displays
// changing widget width and height.
base::TimeDelta duration =
animate ? kAnimationDuration : base::Milliseconds(0);
glic_window_animator_->AnimatePosition(window_target_top_left, duration,
base::DoNothing());
NotifyIfPanelStateChanged();
}
void GlicWindowControllerImpl::AddStateObserver(StateObserver* observer) {
state_observers_.AddObserver(observer);
}
void GlicWindowControllerImpl::RemoveStateObserver(StateObserver* observer) {
state_observers_.RemoveObserver(observer);
}
void GlicWindowControllerImpl::NotifyIfPanelStateChanged() {
auto new_state = ComputePanelState();
if (new_state != panel_state_) {
panel_state_ = new_state;
state_observers_.Notify(&StateObserver::PanelStateChanged, panel_state_,
attached_browser_);
}
}
mojom::PanelState GlicWindowControllerImpl::ComputePanelState() const {
return CreatePanelState(IsShowing(), attached_browser_);
}
bool GlicWindowControllerImpl::IsActive() {
return IsShowing() && GetGlicWidget() && GetGlicWidget()->IsActive();
}
bool GlicWindowControllerImpl::IsShowing() const {
return !(state_ == State::kClosed);
}
bool GlicWindowControllerImpl::IsPanelOrFreShowing() const {
return IsShowing() || fre_controller_->IsShowingDialog();
}
bool GlicWindowControllerImpl::IsAttached() const {
return attached_browser_ != nullptr;
}
bool GlicWindowControllerImpl::IsDetached() const {
return IsShowing() && !IsAttached();
}
base::CallbackListSubscription
GlicWindowControllerImpl::AddWindowActivationChangedCallback(
WindowActivationChangedCallback callback) {
return window_activation_callback_list_.Add(std::move(callback));
}
void GlicWindowControllerImpl::Preload() {
if (!host().contents_container()) {
host().CreateContents();
host().webui_contents()->Resize(GetInitialBounds(nullptr));
}
}
void GlicWindowControllerImpl::PreloadFre() {
if (fre_controller_->ShouldShowFreDialog()) {
fre_controller_->TryPreload();
}
}
void GlicWindowControllerImpl::Reload() {
if (GetFreWebContents()) {
GetFreWebContents()->GetController().Reload(
content::ReloadType::BYPASSING_CACHE,
/*check_for_repost=*/false);
}
if (auto* webui_contents = host().webui_contents()) {
webui_contents->GetController().Reload(content::ReloadType::BYPASSING_CACHE,
/*check_for_repost=*/false);
}
}
bool GlicWindowControllerImpl::IsWarmed() const {
return !!host().contents_container();
}
base::WeakPtr<GlicWindowController> GlicWindowControllerImpl::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void GlicWindowControllerImpl::Shutdown() {
// Hide first, then clean up (but do not animate).
Close();
fre_controller_->Shutdown();
window_activation_callback_list_.Notify(false);
}
bool GlicWindowControllerImpl::IsBrowserOccludedAtPoint(Browser* browser,
gfx::Point point) {
std::set<gfx::NativeWindow> exclude = {
GetGlicView()->GetWidget()->GetNativeWindow()};
gfx::NativeWindow window =
window_finder_->GetLocalProcessWindowAtPoint(point, exclude);
if (browser->GetBrowserView().GetWidget()->GetNativeWindow() != window) {
return true;
}
return false;
}
gfx::Size GlicWindowControllerImpl::GetLastRequestedSizeClamped() const {
gfx::Size min = GlicWidget::GetInitialSize();
if (glic_widget_) {
gfx::Size widget_min = glic_widget_->GetMinimumSize();
if (!widget_min.IsEmpty()) {
min = widget_min;
}
}
gfx::Size max(kMaxWidgetSize, kMaxWidgetSize);
gfx::Size result = glic_size_.value_or(min);
result.SetToMax(min);
result.SetToMin(max);
return result;
}
void GlicWindowControllerImpl::MaybeAdjustSizeForDisplay(bool animate) {
if (state_ == State::kOpen || state_ == State::kWaitingForGlicToLoad) {
const auto target_size = GetLastRequestedSizeClamped();
if (target_size != glic_window_animator_->GetCurrentTargetBounds().size()) {
glic_window_animator_->AnimateSize(
target_size, animate ? kAnimationDuration : base::Milliseconds(0),
base::DoNothing());
}
}
}
void GlicWindowControllerImpl::SetWindowState(State new_state) {
if (state_ == new_state) {
return;
}
state_ = new_state;
if (IsWindowOpenAndReady()) {
glic_service_->metrics()->OnGlicWindowOpenAndReady();
}
}
bool GlicWindowControllerImpl::IsWindowOpenAndReady() {
return host().IsReady() && state_ == State::kOpen;
}
GlicWindowController::State GlicWindowControllerImpl::state() const {
return state_;
}
bool GlicWindowControllerImpl::IsDragging() {
return in_move_loop_;
}
Profile* GlicWindowControllerImpl::profile() {
return profile_;
}
GlicWindowAnimator* GlicWindowControllerImpl::window_animator() {
return glic_window_animator_.get();
}
GlicFreController* GlicWindowControllerImpl::fre_controller() {
return fre_controller_.get();
}
// Return the Browser to which the panel is attached, or null if detached.
Browser* GlicWindowControllerImpl::attached_browser() {
return attached_browser_;
}
web_modal::WebContentsModalDialogHost*
GlicWindowControllerImpl::GetWebContentsModalDialogHost() {
return this;
}
gfx::Size GlicWindowControllerImpl::GetMaximumDialogSize() {
if (!glic_widget_) {
return gfx::Size();
}
return glic_widget_->GetClientAreaBoundsInScreen().size();
}
gfx::NativeView GlicWindowControllerImpl::GetHostView() const {
if (!glic_widget_) {
return gfx::NativeView();
}
return glic_widget_->GetNativeView();
}
gfx::Point GlicWindowControllerImpl::GetDialogPosition(
const gfx::Size& dialog_size) {
if (!glic_widget_) {
return gfx::Point();
}
gfx::Rect client_area_bounds = glic_widget_->GetClientAreaBoundsInScreen();
return gfx::Point((client_area_bounds.width() - dialog_size.width()) / 2, 0);
}
bool GlicWindowControllerImpl::ShouldDialogBoundsConstrainedByHost() {
// Allows web modal dialogs to extend beyond the boundary of glic window.
// These web modals are usually larger than the glic window.
return false;
}
void GlicWindowControllerImpl::AddObserver(
web_modal::ModalDialogHostObserver* observer) {
modal_dialog_host_observers_.AddObserver(observer);
}
void GlicWindowControllerImpl::RemoveObserver(
web_modal::ModalDialogHostObserver* observer) {
modal_dialog_host_observers_.RemoveObserver(observer);
}
} // namespace glic
|