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 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_VIEWS_WIDGET_WIDGET_H_
#define UI_VIEWS_WIDGET_WIDGET_H_
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/safety_checks.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "build/build_config.h"
#include "ui/accessibility/platform/ax_mode_observer.h"
#include "ui/base/class_property.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-forward.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_types.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/base/ui_base_types.h"
#include "ui/color/color_provider_key.h"
#include "ui/color/color_provider_source.h"
#include "ui/color/color_provider_utils.h"
#include "ui/display/display.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/event_source.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/native_theme_observer.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/widget/native_widget_delegate.h"
#include "ui/views/window/client_view.h"
#include "ui/views/window/non_client_view.h"
#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/platform_session_manager.h"
#endif
namespace base {
class TimeDelta;
}
namespace gfx {
class Insets;
class Point;
class Rect;
} // namespace gfx
namespace ui {
class Accelerator;
class ColorProvider;
class Compositor;
class GestureRecognizer;
class InputMethod;
class Layer;
class OSExchangeData;
class ThemeProvider;
class AXPlatform;
} // namespace ui
namespace ui_devtools {
class BubbleLocking;
}
namespace views {
class DesktopWindowTreeHost;
class NativeWidget;
class NonClientFrameView;
class SublevelManager;
class TooltipManager;
class View;
class WidgetDelegate;
class WidgetObserver;
class WidgetRemovalsObserver;
namespace internal {
class NativeWidgetPrivate;
class RootView;
} // namespace internal
enum class CloseRequestResult { kCanClose, kCannotClose };
////////////////////////////////////////////////////////////////////////////////
// Widget class
//
// Encapsulates the platform-specific rendering, event receiving and widget
// management aspects of the UI framework.
//
// Owns a RootView and thus a View hierarchy. Can contain child Widgets.
// Widget is a platform-independent type that communicates with a platform or
// context specific NativeWidget implementation.
//
// All widgets should use ownership = CLIENT_OWNS_WIDGET. The client code that
// creates the widget should hold onto a std::unique_ptr<Widget>. The proper
// way to close the Widget is to reset the unique_ptr.
//
// The Close() and CloseWithReason() methods are problematic because they
// asynchronously close the widget. This means that client code has to handle
// the edge case of: widget is closed, but not destroyed. Use
// MakeCloseSynchronous() to allow the client to intercept these calls
// and reset the unique_ptr. Note that the point of
// MakeCloseSynchronous() is to intercept calls to Close() from code in
// //ui that client code cannot control (such as DialogDelegate). This also
// allows client code to have a single destruction path for widgets, which
// simplifies logic for code that should be written exactly once, such as
// logging. If Client code does not rely on DialogDelegate or similar helpers
// that call Widget::Close(), then MakeCloseSynchronous is unnecessary.
//
// Aside 1: Clients are responsible for handling the case where the parent
// widget is destroyed. There are common helpers like TabDialogManager that
// will do this.
//
// Aside 2: There will always be the edge case of NATIVE_WIDGET destroyed while
// Widget is alive. This is rare and most clients do not need to handle this.
// For clients that do care about this, the best way to detect this right now
// is WidgetObserver::OnWidgetDestroying.
//
// See documentation of MakeCloseSynchronous for an example.
//
// Deprecated but kept for historical context --------------------------------
// A special note on ownership:
//
// Depending on the value of the InitParams' ownership field, the Widget
// either owns or is owned by its NativeWidget:
//
// ownership = NATIVE_WIDGET_OWNS_WIDGET (default)
// The Widget instance is owned by its NativeWidget. When the NativeWidget
// is destroyed (in response to a native destruction message), it deletes
// the Widget from its destructor.
// ownership = WIDGET_OWNS_NATIVE_WIDGET (non-default)
// The Widget instance owns its NativeWidget. This state implies someone
// else wants to control the lifetime of this object. When they destroy
// the Widget it is responsible for destroying the NativeWidget (from its
// destructor). This is often used to place a Widget in a std::unique_ptr<>
// or on the stack in a test.
class VIEWS_EXPORT Widget : public internal::NativeWidgetDelegate,
public ui::EventSource,
public FocusTraversable,
public ui::NativeThemeObserver,
public ui::ColorProviderSource,
public ui::PropertyHandler,
public ui::AXModeObserver,
public ui::metadata::MetaDataProvider {
// Do not remove this macro!
// The macro is maintained by the memory safety team.
// `Widget` is used extensively and has a complicated lifetime. This macro
// enables additional lifetime checks.
ADVANCED_MEMORY_SAFETY_CHECKS();
public:
METADATA_HEADER_BASE(Widget);
using Widgets = std::set<raw_ptr<Widget, SetExperimental>>;
using ShapeRects = std::vector<gfx::Rect>;
using PaintAsActiveCallbackList = base::RepeatingClosureList;
enum class FrameType {
kDefault, // Use whatever the default would be.
kForceCustom, // Force the custom frame.
kForceNative // Force the native frame.
};
// Result from RunMoveLoop().
enum class MoveLoopResult {
// The move loop completed successfully.
kSuccessful,
// The user canceled the move loop.
kCanceled
};
// Source that initiated the move loop.
enum class MoveLoopSource {
kMouse,
kTouch,
};
// Behavior when escape is pressed during a move loop.
enum class MoveLoopEscapeBehavior {
// Indicates the window should be hidden.
kHide,
// Indicates the window should not be hidden.
kDontHide,
};
// Type of visibility change transition that should animate.
enum VisibilityTransition {
ANIMATE_SHOW = 0x1,
ANIMATE_HIDE = 0x2,
ANIMATE_BOTH = ANIMATE_SHOW | ANIMATE_HIDE,
ANIMATE_NONE = 0x4,
};
// Represents the reason a Widget was closed, if it is known.
//
// For backwards compatibility, we default to kUnspecified when
// Widget::Close() is called. Note that we do not currently handle close
// reason for menu or for the main Chrome browser, as we have no reason to
// specifically differentiate those yet.
//
// Add additional values as needed. Do not change any existing values, as this
// enum is logged to UMA.
enum class ClosedReason {
kUnspecified = 0, // No reason was given for the widget closing.
kEscKeyPressed = 1, // The ESC key was pressed to cancel the widget.
kCloseButtonClicked = 2, // The [X] button was explicitly clicked.
kLostFocus = 3, // The widget destroyed itself when it lost focus.
kCancelButtonClicked = 4, // The widget's cancel button was clicked.
kAcceptButtonClicked = 5, // The widget's done/accept button was clicked.
kMaxValue = kAcceptButtonClicked
};
// This struct had unused fields that were removed, but may be of interest to
// future users:
// - force_show_in_taskbar: https://crrev.com/c/6356649
// - native_theme: https://crrev.com/c/6356535
// - wants_mouse_events_when_inactive: https://crrev.com/c/6354158
struct VIEWS_EXPORT InitParams {
enum Type {
TYPE_WINDOW, // A decorated Window, like a frame window.
// Widgets of TYPE_WINDOW will have a NonClientView.
TYPE_WINDOW_FRAMELESS, // An undecorated Window.
TYPE_CONTROL, // A control, like a button.
TYPE_POPUP, // An undecorated Window, with transient properties.
TYPE_MENU, // An undecorated Window, with transient properties
// specialized to menus.
TYPE_TOOLTIP,
TYPE_BUBBLE,
TYPE_DRAG, // An undecorated Window, used during a drag-and-drop to
// show the drag image.
};
enum class WindowOpacity {
// Infer fully opaque or not. For WinAura, top-level windows that are not
// of TYPE_WINDOW are translucent so that they can be made to fade in.
// For LinuxAura, only windows that are TYPE_DRAG are translucent. In all
// other cases, windows are fully opaque.
kInferred,
// Fully opaque.
kOpaque,
// Possibly translucent/transparent. Widgets that fade in or out using
// SetOpacity() but do not make use of an alpha channel should use
// kInferred.
kTranslucent,
};
enum class Activatable {
// Infer whether the window should be activatable from the window type.
kDefault,
kYes,
kNo
};
enum Ownership {
// The client (caller) manages the lifetime of the Widget, typically via
// std::unique_ptr<Widget>. This is the preferred ownership mode.
//
// If you encounter problems with this ownership mode, please file a bug.
//
// - The Widget remains valid even after the platform window
// (HWND, NSWindow, etc.) is closed.
// - Widget API calls are safe after the platform window closes, but
// most will become no-ops (e.g., Show() will do nothing).
// - The NativeWidget is destroyed when the platform window closes.
// - When the client destroys the Widget, a close request is sent to the
// platform window (if it's still open).
CLIENT_OWNS_WIDGET,
// The NativeWidget manages the lifetime of the Widget. The Widget is
// destroyed when the corresponding NativeWidget is destroyed.
//
// DEPRECATED: Prone to memory issues. A Widget* can be invalidated
// at any time, leading to dangling pointers. This does not fit typical
// C++ memory management idioms.
NATIVE_WIDGET_OWNS_WIDGET,
// The Widget owns the NativeWidget. The NativeWidget is destroyed when
// the corresponding Widget is destroyed.
//
// DEPRECATED: Causes problems with platform window shutdown. The OS
// usually does not expect the NativeWidget to be destroyed immediately
// when the platform window is closed. For example, if the platform window
// has a close animation, it must remain valid until the animation
// finishes to avoid prematurely destroying the compositor and its layer.
// This would also cause other platform-specific issues
// (e.g., crbug.com/40619853).
WIDGET_OWNS_NATIVE_WIDGET,
};
enum class ShadowType {
kDefault, // Use default shadow setting. It will be one of
// the settings below depending on InitParams::type
// and the native widget's type.
kNone, // Don't draw any shadow.
kDrop, // Draw a drop shadow that emphasizes Z-order
// relationship to other windows.
};
// The preferred constructor. Must specify the ownership mode. The ownership
// mode will eventually go away and will implicitly be CLIENT_OWNS_WIDGET.
// This is here for migration purposes.
explicit InitParams(Ownership ownership, Type type = TYPE_WINDOW);
InitParams(InitParams&& other);
~InitParams();
InitParams& operator=(InitParams&& rhs) = default;
// Returns the activatablity based on |activatable|, but also handles the
// case where |activatable| is |kDefault|.
bool CanActivate() const;
// Returns the z-order level, based on the overriding |z_order| but also
// taking into account special levels due to |type|.
ui::ZOrderLevel EffectiveZOrderLevel() const;
// Returns whether the widget should be initialized as headless by checking
// if |headless_mode| or the associated top level widget's |is_headless_|
// are set.
bool ShouldInitAsHeadless() const;
// Sets the parent view using a parent Widget. This will set the `parent`
// field correctly.
void SetParent(Widget* parent_widget);
// Sets the parent view with the given gfx::NativeView directly. This is the
// same as directly assigning the `parent` field.
// TODO(crbug.com/392029296): Make the `parent` field private and favor this
// setter and/or the previous setter.
void SetParent(gfx::NativeView parent_view);
Type type;
// If null, a default implementation will be constructed. The default
// implementation deletes itself when the Widget closes.
raw_ptr<WidgetDelegate> delegate = nullptr;
// Internal name. Propagated to the NativeWidget. Useful for debugging.
std::string name;
// False if this widget behaves like a top-level widget, true otherwise. A
// top-level widget has its own focus and IME state, independent of any
// other widget. A widget for which child is true should have a parent; if
// it doesn't, it will not handle keyboard events or IME input at all.
// TODO(crbug.com/40120838): DCHECK(parent || !child)
bool child = false;
// If kTranslucent, the widget may be fully or partially transparent.
// If kOpaque, we can perform optimizations based on the widget being fully
// opaque. Default is based on ViewsDelegate::GetOpacityForInitParams().
// Defaults to kOpaque for non-window widgets. Translucent windows may not
// always be supported, e.g., resizable windows cannot be translucent
// on Windows.
WindowOpacity opacity = WindowOpacity::kInferred;
bool accept_events = true;
Activatable activatable = Activatable::kDefault;
// The class of window and its overall z-order level. This level is visible
// to other applications in the system. A value other than `kNormal` will
// create an "always on top" widget.
std::optional<ui::ZOrderLevel> z_order;
// The z-order sublevel that is invisible to other applications in the
// system. Widgets of the same `z_order` are stacked in the order specified
// by their sub-levels.
int sublevel = 0;
bool visible_on_all_workspaces = false;
// See Widget class comment above.
Ownership ownership;
ShadowType shadow_type = ShadowType::kDefault;
// A hint about the size of the shadow if the type is ShadowType::kDrop. May
// be ignored on some platforms. No value indicates no preference.
std::optional<int> shadow_elevation;
// Specifies the desired corner radius for the window, in pixels. This is
// handled by the OS windowing system, and the support varies:
// - ChromeOS Ash & macOS: Fully effective; the specified radius is used.
// - Windows 11: Partially effective; if a value is set positive, it enables
// system-managed rounded corners via the DWMWCP_ROUND window style. The
// actual radius is determined by the OS, not this specific value.
// - Windows 10 & other platforms: Has no effect.
// Alternatively, you can set WindowOpacity to kTranslucent and use
// views::RoundedRectBackground. This has limitations (see `opacity`).
std::optional<int> corner_radius;
// Specifies that the system default caption and icon should not be
// rendered, and that the client area should be equivalent to the window
// area. Only used on some platforms (Windows and Linux).
bool remove_standard_frame = false;
// Whether the widget should be maximized or minimized.
ui::mojom::WindowShowState show_state =
ui::mojom::WindowShowState::kDefault;
// The native *view* (not native *window*) to which this widget should be
// parented. If this widget has a parent, then:
// * If that parent closes, this widget is closed too
// * If that parent is hidden, this widget is hidden too
// * This widget is stacked above the parent widget (always on Mac, usually
// elsewhere)
// * This widget's initial bounds are constrained to the parent widget's
// bounds, which prevents window restoration from placing windows
// offscreen
// Note: on some platforms (Mac) this directly implies a parent-child
// relationship in the backing native windows, but on Aura platforms it does
// not necessarily.
//
// Windows with no parent window are permitted, although in Aura these
// windows instead need a "context". On Aura systems, if a widget has no
// parent set, its backing aura::Window is parented to the Aura root window.
//
// TODO(crbug.com/40120838): It makes no sense that this is a
// NativeView instead of a NativeWindow. On Aura, NativeView and
// NativeWindow are synonyms, and NativeWidgetAura immediately treats the
// provided NativeView as an aura::Window; on Mac, the NativeView is
// immediately converted to an NSWindow (ie a gfx::NativeWindow) and used
// that way throughout. This should simply be a NativeWindow - windows are
// parented to other windows, not to views, and it being a view confuses
// the concept with bubble anchoring a la BubbleDialogDelegateView.
//
// TODO(crbug.com/392029296): Make this field private and only set via the
// setters above.
gfx::NativeView parent = gfx::NativeView();
// Specifies the initial bounds of the Widget. Default is empty, which means
// the NativeWidget may specify a default size. If the parent is specified
// and the widget type is not WINDOW_TYPE_POPUP, `bounds` is in the parent's
// coordinate system. Otherwise, it's in screen coordinates.
// TODO(crbug.com/40287810): can we use screen coordinates universally?
gfx::Rect bounds;
#if BUILDFLAG(IS_CHROMEOS)
// If specified and the `bounds` is inside the specified display, the widget
// will be created on this display. Otherwise, the display matching the
// `bounds` will be used.
std::optional<int64_t> display_id;
#endif
// The initial workspace of the Widget. Default is "", which means the
// current workspace.
std::string workspace;
// If set, this value is used as the Widget's NativeWidget implementation.
// The Widget will not construct a default one.
raw_ptr<NativeWidget> native_widget = nullptr;
// Aura-only. Provides a DesktopWindowTreeHost implementation to use instead
// of the default one.
// TODO(beng): Figure out if there's a better way to expose this, e.g. get
// rid of NW subclasses and do this all via message handling.
raw_ptr<DesktopWindowTreeHost> desktop_window_tree_host = nullptr;
// Only used by NativeWidgetAura. Specifies the type of layer for the
// aura::Window.
ui::LayerType layer_type = ui::LAYER_TEXTURED;
// Only used by Aura. Provides a context window whose RootWindow is
// consulted during widget creation to determine where in the Window
// hierarchy this widget should be placed. (This is separate from |parent|;
// if you pass a RootWindow to |parent|, your window will be parented to
// |parent|. If you pass a RootWindow to |context|, we ask that RootWindow
// where it wants your window placed.) Nullptr is not allowed on Windows and
// Linux. Nullptr is allowed on Chrome OS, which will place the window on
// the default desktop for new windows.
gfx::NativeWindow context = gfx::NativeWindow();
#if BUILDFLAG(IS_WIN)
// If true, force the window not to be shown in the taskbar, even for
// window types that do appear in the taskbar by default.
bool dont_show_in_taskbar = false;
// If true, adds the WS_SYSMENU style to TYPE_WINDOW_FRAMELESS windows.
bool force_system_menu_for_frameless = false;
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_LINUX)
// Only used by X11, for root level windows. Specifies the res_name and
// res_class fields, respectively, of the WM_CLASS window property. Controls
// window grouping and desktop file matching in Linux window managers.
std::string wm_role_name;
std::string wm_class_name;
std::string wm_class_class;
// Only used by Wayland, for root level windows.
std::string wayland_app_id;
#endif // BUILDFLAG(IS_LINUX)
// If true then the widget uses software compositing.
bool force_software_compositing = false;
// If set, the widget was created in headless mode.
bool headless_mode = false;
// If set, the window size will follow the content preferred size.
bool autosize = false;
#if BUILDFLAG(IS_OZONE)
// Specifies whether the Widget should use a desktop native widget.
// true: desktop native widget will always be used
// false: desktop native widget will never be used
// unspecified: depends on experiment kOzoneBubblesUsePlatformWidgets and
// whether the platform actually supports desktop native subwindows (see
// ozone_platform.h `supports_subwindows_as_accelerated_widgets`)
// See crbug.com/1280332
std::optional<bool> use_accelerated_widget_override;
#endif
// Contains any properties with which the native widget should be
// initialized prior to adding it to the window hierarchy. All the
// properties in |init_properties_container| will be moved to the native
// widget.
ui::PropertyHandler init_properties_container;
#if BUILDFLAG(IS_OZONE)
// Only used by Wayland for root level windows. Specifies whether this
// window should request the wayland compositor to send key events,
// even if it matches with the compositor's keyboard shortcuts.
bool inhibit_keyboard_shortcuts = false;
// Used by Ozone platforms that implement support for display server backed
// session management. E.g: Wayland with xdg-session-management protocol.
std::optional<ui::PlatformSessionWindowData> session_data;
#endif
#if BUILDFLAG(IS_MAC)
// If set to true, tags the widget as an invisible overlay widget that
// allows the Views tree to be broken up into distinct NSViews for use by
// immersive fullscreen. Not for general use.
bool is_overlay = false;
// If set to true, enable system default show and hide animations.
bool animation_enabled = false;
#endif
};
// Represents a lock held on the widget's ShouldPaintAsActive() state. As
// long as at least one lock is held, the widget will paint as active.
// Multiple locks can exist for the same widget, and a lock can outlive its
// associated widget. See Widget::LockPaintAsActive().
class PaintAsActiveLock {
public:
PaintAsActiveLock(const PaintAsActiveLock&) = delete;
PaintAsActiveLock& operator=(const PaintAsActiveLock&) = delete;
virtual ~PaintAsActiveLock();
protected:
PaintAsActiveLock();
};
Widget();
explicit Widget(InitParams params);
Widget(const Widget&) = delete;
Widget& operator=(const Widget&) = delete;
~Widget() override;
// Creates a decorated window Widget with the specified properties. The
// returned Widget is owned by its NativeWidget; see Widget class comment for
// details.
// The std::unique_ptr variant requires that delegate->owned_by_widget().
static Widget* CreateWindowWithParent(WidgetDelegate* delegate,
gfx::NativeView parent,
const gfx::Rect& bounds = gfx::Rect());
static Widget* CreateWindowWithParent(
std::unique_ptr<WidgetDelegate> delegate,
gfx::NativeView parent,
const gfx::Rect& bounds = gfx::Rect());
// Creates a decorated window Widget in the same desktop context as |context|.
// The returned Widget is owned by its NativeWidget; see Widget class comment
// for details.
// The std::unique_ptr variant requires that delegate->owned_by_widget().
static Widget* CreateWindowWithContext(WidgetDelegate* delegate,
gfx::NativeWindow context,
const gfx::Rect& bounds = gfx::Rect());
static Widget* CreateWindowWithContext(
std::unique_ptr<WidgetDelegate> delegate,
gfx::NativeWindow context,
const gfx::Rect& bounds = gfx::Rect());
// Closes all Widgets that aren't identified as "secondary widgets". Called
// during application shutdown when the last non-secondary widget is closed.
static void CloseAllSecondaryWidgets();
// Retrieves the Widget implementation associated with the given
// NativeView or Window, or NULL if the supplied handle has no associated
// Widget.
static Widget* GetWidgetForNativeView(gfx::NativeView native_view);
static Widget* GetWidgetForNativeWindow(gfx::NativeWindow native_window);
// Retrieves the top level widget in a native view hierarchy
// starting at |native_view|. Top level widget is a widget with TYPE_WINDOW,
// TYPE_PANEL, TYPE_WINDOW_FRAMELESS, POPUP or MENU and has its own
// focus manager. This may be itself if the |native_view| is top level,
// or NULL if there is no toplevel in a native view hierarchy.
static Widget* GetTopLevelWidgetForNativeView(gfx::NativeView native_view);
// Returns all Widgets in |native_view|'s hierarchy, including itself if
// it is one.
static Widgets GetAllChildWidgets(gfx::NativeView native_view);
// Returns all Widgets owned by |native_view| (including child widgets, but
// not including itself).
static Widgets GetAllOwnedWidgets(gfx::NativeView native_view);
// https://crbug.com/391414831: This is only used by some views
// implementation details for content::WebContents glue, and for ChromeOS.
// New use cases should not be added. Use Reparent() instead.
// Re-parent a NativeView and notify all Widgets in |native_view|'s hierarchy
// of the change.
static void ReparentNativeView(gfx::NativeView native_view,
gfx::NativeView new_parent);
// Returns the preferred size of the contents view of this window based on
// its localized size data. The width in cols is held in a localized string
// resource identified by |col_resource_id|, the height in the same fashion.
// TODO(beng): This should eventually live somewhere else, probably closer to
// ClientView.
static int GetLocalizedContentsWidth(int col_resource_id);
static int GetLocalizedContentsHeight(int row_resource_id);
static gfx::Size GetLocalizedContentsSize(int col_resource_id,
int row_resource_id);
// Returns true if the specified type requires a NonClientView.
static bool RequiresNonClientView(InitParams::Type type);
static bool IsWindowCompositingSupported();
// Initializes the widget, and in turn, the native widget. |params| should be
// moved to Init() by the caller.
void Init(InitParams params);
// Returns the gfx::NativeView associated with this Widget.
gfx::NativeView GetNativeView() const;
// Returns the gfx::NativeWindow associated with this Widget. This may return
// NULL on some platforms if the widget was created with a type other than
// TYPE_WINDOW or TYPE_PANEL.
gfx::NativeWindow GetNativeWindow() const;
// Returns the nearest display intersecting this Widget. Widget must be
// initialized.
std::optional<display::Display> GetNearestDisplay();
// Add/remove observer.
void AddObserver(WidgetObserver* observer);
void RemoveObserver(WidgetObserver* observer);
bool HasObserver(const WidgetObserver* observer) const;
// Add/remove removals observer.
void AddRemovalsObserver(WidgetRemovalsObserver* observer);
void RemoveRemovalsObserver(WidgetRemovalsObserver* observer);
bool HasRemovalsObserver(const WidgetRemovalsObserver* observer) const;
// Returns the accelerator given a command id. Returns false if there is
// no accelerator associated with a given id, which is a common condition.
virtual bool GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const;
// Sets a new parent and notifies all Widgets in this widget's hierarchy of
// the change.
void Reparent(Widget* parent);
// Forwarded from the RootView so that the widget can do any cleanup.
void ViewHierarchyChanged(const ViewHierarchyChangedDetails& details);
// Called right before changing the widget's parent NativeView to do any
// cleanup.
void NotifyNativeViewHierarchyWillChange();
// Called after changing the widget's parent NativeView. Notifies the RootView
// about the change.
void NotifyNativeViewHierarchyChanged();
// Called immediately before removing |view| from this widget.
void NotifyWillRemoveView(View* view);
// Returns the top level widget in a hierarchy (see is_top_level() for
// the definition of top level widget.) Will return NULL if called
// before the widget is attached to the top level widget's hierarchy.
//
// If you want to get the absolute primary application window, accounting for
// e.g. bubble and menu anchoring, use GetPrimaryWindowWidget() instead.
Widget* GetTopLevelWidget();
const Widget* GetTopLevelWidget() const;
// Returns the widget of the primary window this widget is associated with,
// such as an application window, accounting for anchoring and other
// relationships not accounted for in GetTopLevelWidget().
//
// Equivalent to GetTopLevelWidget() by default; override in derived classes
// that require additional logic.
virtual Widget* GetPrimaryWindowWidget();
const Widget* GetPrimaryWindowWidget() const;
// Gets/Sets the WidgetDelegate.
WidgetDelegate* widget_delegate() const { return widget_delegate_.get(); }
// Sets the specified view as the contents of this Widget. There can only
// be one contents view child of this Widget's RootView. This view is sized to
// fit the entire size of the RootView. The RootView takes ownership of this
// View, unless it is passed in as a raw pointer and set as not being
// parent-owned. Prefer using SetContentsView(std::unique_ptr) over passing a
// raw pointer for new code.
template <typename T>
T* SetContentsView(std::unique_ptr<T> view) {
DCHECK(!view->owned_by_client())
<< "This should only be called if the client is passing over the "
"ownership of |view|.";
T* raw_pointer = view.get();
SetContentsView(view.release());
return raw_pointer;
}
void SetContentsView(View* view);
// NOTE: This may not be the same view as WidgetDelegate::GetContentsView().
// See RootView::GetContentsView().
View* GetContentsView();
// This returns the client content view that corresponds to the view returned
// from WidgetDelegate::GetContentsView(). Alternatively, if
// Widget::SetContentView() was explicitly called, this will return that view.
View* GetClientContentsView();
// Returns the bounds of the Widget in screen coordinates.
gfx::Rect GetWindowBoundsInScreen() const;
// Returns the bounds of the Widget's client area in screen coordinates.
gfx::Rect GetClientAreaBoundsInScreen() const;
// Retrieves the restored bounds for the window.
gfx::Rect GetRestoredBounds() const;
// Retrieves the current workspace for the window. (On macOS: an opaque
// binary blob that encodes the workspace and other window state. On ChromeOS,
// this returns empty string if this widget is a window that appears on all
// desks.)
std::string GetWorkspace() const;
// Sizes and/or places the widget to the specified bounds, size or position.
// `bounds` is in screen coordinates for top-level (is_top_level() == true)
// widgets and WINDOW_TYPE_POPUP widgets. Other widgets use its parent
// widget's client area coordinates.
// TODO(crbug.com/40287810): can we use screen coordinates universally?
void SetBounds(const gfx::Rect& bounds);
void SetSize(const gfx::Size& size);
// Retrieves the restored size for the window.
gfx::Size GetSize() const;
// Returns the insets that each widget implementation can customize. It
// returns empty insets by default.
virtual gfx::Insets GetCustomInsetsInDIP() const;
// Sizes the window to the specified size and centers it.
void CenterWindow(const gfx::Size& size);
// Like SetBounds(), but ensures the Widget is fully visible on screen or
// parent widget, resizing and/or repositioning as necessary.
void SetBoundsConstrained(const gfx::Rect& bounds);
// Sets whether animations that occur when visibility is changed are enabled.
// Default is true.
void SetVisibilityChangedAnimationsEnabled(bool value);
// Sets the duration of visibility change animations.
void SetVisibilityAnimationDuration(const base::TimeDelta& duration);
// Sets the visibility transitions that should animate.
// Default behavior is to animate both show and hide.
void SetVisibilityAnimationTransition(VisibilityTransition transition);
// Whether calling RunMoveLoop() is supported for the widget.
bool IsMoveLoopSupported() const;
// Starts a nested run loop that moves the window. This can be used to
// start a window move operation from a mouse or touch event. This returns
// when the move completes. |drag_offset| is the offset from the top left
// corner of the window to the point where the cursor is dragging, and is used
// to offset the bounds of the window from the cursor.
MoveLoopResult RunMoveLoop(const gfx::Vector2d& drag_offset,
MoveLoopSource source,
MoveLoopEscapeBehavior escape_behavior);
// Stops a previously started move loop. This is not immediate.
void EndMoveLoop();
// Places the widget in front of the specified widget in z-order.
void StackAboveWidget(Widget* widget);
void StackAbove(gfx::NativeView native_view);
void StackAtTop();
// Returns true if widget is above the specified window in z-order.
bool IsStackedAbove(gfx::NativeView native_view);
// Sets a shape on the widget. Passing a NULL |shape| reverts the widget to
// be rectangular.
void SetShape(std::unique_ptr<ShapeRects> shape);
// Equivalent to CloseWithReason(ClosedReason::kUnspecified).
// DEPRECATED: Please use CLIENT_OWNS_WIDGET and reset the unique_ptr<Widget>
// instead. Use MakeCloseSynchronous() to intercept unexpected calls
// to Close().
void Close();
// Hides the widget, then closes it after a return to the message loop,
// specifying the reason for it having been closed.
// Note that while you can pass ClosedReason::kUnspecified, it is highly
// discouraged and only supported for backwards-compatibility with Close().
// DEPRECATED: Please use CLIENT_OWNS_WIDGET and reset the unique_ptr<Widget>
// instead. Use MakeCloseSynchronous() to intercept unexpected calls
// to Close().
// Aside, note that depending on platform, platform settings, and widget
// InitParams::Ownership, closing is sometimes synchronous and sometimes
// asynchronous. Yet another reason to prefer CLIENT_OWNS_WIDGET and
// MakeCloseSynchronous(), as that guarantees that Close() is always
// synchronous.
void CloseWithReason(ClosedReason closed_reason);
// This method is used by clients to intercept calls to Close() from other
// code in //ui such as DialogDelegate. The only valid use case is to allow
// clients to implement a synchronous version of Close() by resetting the
// unique_ptr.
//
// widget_->MakeCloseSynchronous(
// base::BindOnce(&Client::CloseWidget, this));
//
// // Called by the implementation of DialogDelegate when the user clicks the
// // close/cancel buttons, or presses `esc`.
// void Client::CloseWidget(Widget::CloseReason reason) {
// LogExactlyOnceOnWidgetDestruction(reason);
// widget_.reset();
// }
//
// // If the client wants to close the widget, it can also do so.
// Client::ClientCloseWidget() {
// CloseWidget(CloseReason::kUnspecified);
// }
void MakeCloseSynchronous(
base::OnceCallback<void(ClosedReason)> override_close);
// A UI test which tries to asynchronously examine a widget (e.g. the pixel
// tests) will fail if the widget is closed before that. This can happen
// easily with widgets that close on focus loss coupled with tests being run
// in parallel, since one test's widget can be closed by the appearance of
// another test's. This method can be used to temporarily disable
// Widget::Close() for such asynchronous cases.
//
// DEPRECATED. Don't use this. Avoid asynchronously closing to begin with. See
// MakeCloseSynchronous() for more details.
void SetBlockCloseForTesting(bool block_close) { block_close_ = block_close; }
// TODO(beng): Move off public API.
// Closes the widget immediately. Compare to |Close|. This will destroy the
// window handle associated with this Widget, so should not be called from
// any code that expects it to be valid beyond this call.
void CloseNow();
// Whether the widget has been asked to close itself. In particular this is
// set to true after Close() has been invoked on the NativeWidget.
bool IsClosed() const;
// Returns the reason the widget was closed, if it was specified.
ClosedReason closed_reason() const { return closed_reason_; }
// Shows the widget. The widget is activated if during initialization the
// can_activate flag in the InitParams structure is set to true.
void Show();
// Hides the widget.
void Hide();
// Like Show(), but does not activate the window. Tests may be flaky on Mac:
// Mac browsertests do not have an activation policy so the widget may be
// activated.
void ShowInactive();
// Unlike Show/Hide above, this function is idempotent. Calling
// SetVisible(true) when IsVisible() == true is a no-op.
void SetVisible(bool visible);
// Activates the widget, assuming it already exists and is visible.
void Activate();
// Deactivates the widget, making the next window in the Z order the active
// window.
void Deactivate();
// Returns whether the Widget is the currently active window.
virtual bool IsActive() const;
// Returns true if views style follows the widget activation state. The
// default implementation simply calls CanActivate().
virtual bool ShouldViewsStyleFollowWidgetActivation() const;
// Sets the z-order of the widget. This only applies to top-level widgets.
void SetZOrderLevel(ui::ZOrderLevel order);
// Gets the z-order of the widget. This only applies to top-level widgets.
ui::ZOrderLevel GetZOrderLevel() const;
// Sets the z-order sublevel of the widget. This applies to both top-level
// and non top-level widgets.
void SetZOrderSublevel(int sublevel);
// Gets the z-order sublevel of the widget. This applies to both top-level
// and non top-level widgets.
int GetZOrderSublevel() const;
#if BUILDFLAG(IS_MAC)
// Sets the widget as being "activation independent". This sets two
// properties:
//
// - If Chromium is hidden (from the Dock menu or programmatically), the
// widget is not forced to be hidden as well.
// - The widget can be interacted with without causing Chromium to be
// activated.
//
// To accomplish this, the activation independence state of all ancestor
// widgets is set as well.
//
// The notion of "activation independence" only makes sense if the widget
// floats above all other apps, so this property must only be set on a widget
// that has a z-order of ui::ZOrderLevel::kFloatingWindow. This is enforced.
void SetActivationIndependence(bool independence);
#endif
// Sets the widget to be visible on all work spaces.
void SetVisibleOnAllWorkspaces(bool always_visible);
// Is this widget currently visible on all workspaces?
// A call to SetVisibleOnAllWorkspaces(true) won't necessarily mean
// IsVisbleOnAllWorkspaces() == true (for example, when the platform doesn't
// support workspaces).
bool IsVisibleOnAllWorkspaces() const;
// Maximizes/minimizes/restores the window.
void Maximize();
void Minimize();
void Restore();
// Shows a menu with controls beyond minimize/maximize/restore. Only
// implemented on Linux.
void ShowWindowControlsMenu(const gfx::Point& point);
// Whether or not the window is maximized or minimized.
virtual bool IsMaximized() const;
bool IsMinimized() const;
// Accessors for fullscreen state.
// The `target_display_id` may only be specified if `fullscreen` is true, and
// indicates a specific display to become fullscreen on (note that this may
// move a fullscreen widget from one display to another).
void SetFullscreen(bool fullscreen,
int64_t target_display_id = display::kInvalidDisplayId);
bool IsFullscreen() const;
// macOS: Sets whether the window can share fullscreen windows' spaces.
void SetCanAppearInExistingFullscreenSpaces(
bool can_appear_in_existing_fullscreen_spaces);
// Sets the opacity of the widget. This may allow widgets behind the widget
// in the Z-order to become visible, depending on the capabilities of the
// underlying windowing system.
void SetOpacity(float opacity);
// Sets the aspect ratio of the widget's client view, which will be maintained
// during interactive resizing. Note that for widgets that have a client view
// that is framed by custom-drawn borders / window frame / etc, the widget
// size will be chosen so that the aspect ratio of client view, not the entire
// widget, will be `aspect_ratio`.
//
// Once set, some platforms ensure the content will only size to integer
// multiples of |aspect_ratio|.
void SetAspectRatio(const gfx::SizeF& aspect_ratio);
// Flashes the frame of the window to draw attention to it. Currently only
// implemented on Windows for non-Aura.
void FlashFrame(bool flash);
// Returns the View at the root of the View hierarchy contained by this
// Widget.
View* GetRootView();
const View* GetRootView() const;
// A secondary widget is one that is automatically closed (via Close()) when
// all non-secondary widgets are closed.
// Default is true.
// TODO(beng): This is an ugly API, should be handled implicitly via
// transience.
void set_is_secondary_widget(bool is_secondary_widget) {
is_secondary_widget_ = is_secondary_widget;
}
bool is_secondary_widget() const { return is_secondary_widget_; }
// Returns whether the Widget is mapped by the window server. It doesn't
// necessarily mean the window's pixels are currently visible on a physical
// display to the user.
// Example: a mapped Widget on a hidden virtual desktop returns true for
// IsVisible(), but is not physically visible to the user.
//
// On some platforms (e.g., macOS), this is asynchronously updated, i.e.
// calling Show() or Hide() will not synchronously update this.
bool IsVisible() const;
// Returns true if the Widget is physically visible to the user on any screen.
// This is implemented only on macOS and Windows. On other platforms this is
// equivalent to IsVisible().
//
// On some platforms (e.g., macOS), this is asynchronously updated, i.e.
// calling Show() or Hide() will not synchronously update this.
bool IsVisibleOnScreen() const;
// Returns the ThemeProvider that provides theme resources for this Widget.
virtual const ui::ThemeProvider* GetThemeProvider() const;
// Returns a custom theme object suitable for use in a
// ColorProviderKey. If this is null, the window has no custom theme.
virtual ui::ColorProviderKey::ThemeInitializerSupplier* GetCustomTheme()
const;
ui::NativeTheme* GetNativeTheme() {
return const_cast<ui::NativeTheme*>(
static_cast<const Widget*>(this)->GetNativeTheme());
}
virtual const ui::NativeTheme* GetNativeTheme() const;
// Returns the FocusManager for this widget.
// Note that all widgets in a widget hierarchy share the same focus manager.
FocusManager* GetFocusManager();
const FocusManager* GetFocusManager() const;
// Returns the ui::InputMethod for this widget.
ui::InputMethod* GetInputMethod();
// Returns the SublevelManager for this widget.
SublevelManager* GetSublevelManager();
// Starts a drag operation for the specified view. This blocks until the drag
// operation completes or is cancelled by calling `CancelShellDrag()`.
// |view| can be NULL.
// If the view is non-NULL it can be accessed during the drag by calling
// dragged_view(). If the view has not been deleted during the drag,
// OnDragDone() is called on it. |location| is in the widget's coordinate
// system. |view| must be hosted by this widget.
void RunShellDrag(View* view,
std::unique_ptr<ui::OSExchangeData> data,
const gfx::Point& location,
int operation,
ui::mojom::DragEventSource source);
// Cancels a currently running drag operation for the specified view. |view|
// can be NULL.
void CancelShellDrag(View* view);
// Returns the view that requested the current drag operation via
// RunShellDrag(), or NULL if there is no such view or drag operation.
View* dragged_view() {
return const_cast<View*>(const_cast<const Widget*>(this)->dragged_view());
}
const View* dragged_view() const { return dragged_view_; }
// Adds the specified |rect| in client area coordinates to the rectangle to be
// redrawn.
virtual void SchedulePaintInRect(const gfx::Rect& rect);
// Schedule a layout to occur. This is called by RootView, client code should
// not need to call this.
void ScheduleLayout();
// Called when the contents view receives an `InvalidateLayout()`.
void OnRootViewLayoutInvalidated();
// Sets the currently visible cursor.
void SetCursor(const ui::Cursor& cursor);
// Returns true if and only if mouse events are enabled.
bool IsMouseEventsEnabled() const;
// Sets/Gets a native window property on the underlying native window object.
// Returns NULL if the property does not exist. Setting the property value to
// NULL removes the property.
void SetNativeWindowProperty(const char* name, void* value);
void* GetNativeWindowProperty(const char* name) const;
// Tell the window to update its title from the delegate.
void UpdateWindowTitle();
// Tell the window to update its icon from the delegate.
void UpdateWindowIcon();
// Shows the platform specific emoji picker for this widget.
void ShowEmojiPanel();
// Retrieves the focus traversable for this widget.
FocusTraversable* GetFocusTraversable();
// Notifies the view hierarchy contained in this widget that theme resources
// changed.
void ThemeChanged();
// Notifies the view hierarchy contained in this widget that the device scale
// factor changed.
void DeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor);
void SetFocusTraversableParent(FocusTraversable* parent);
void SetFocusTraversableParentView(View* parent_view);
// Clear native focus set to the Widget's NativeWidget.
void ClearNativeFocus();
void set_frame_type(FrameType frame_type) { frame_type_ = frame_type; }
FrameType frame_type() const { return frame_type_; }
// Creates an appropriate NonClientFrameView for this widget. The
// WidgetDelegate is given the first opportunity to create one, followed by
// the NativeWidget implementation. If both return NULL, a default one is
// created.
virtual std::unique_ptr<NonClientFrameView> CreateNonClientFrameView();
// Whether we should be using a native frame.
bool ShouldUseNativeFrame() const;
// Determines whether the window contents should be rendered transparently
// (for example, so that they can overhang onto the window title bar).
bool ShouldWindowContentsBeTransparent() const;
// Tell the window that something caused the frame type to change.
void FrameTypeChanged();
NonClientView* non_client_view() {
return const_cast<NonClientView*>(
const_cast<const Widget*>(this)->non_client_view());
}
const NonClientView* non_client_view() const { return non_client_view_; }
ClientView* client_view() {
return const_cast<ClientView*>(
const_cast<const Widget*>(this)->client_view());
}
const ClientView* client_view() const {
// non_client_view_ may be NULL, especially during creation.
return non_client_view_ ? non_client_view_->client_view() : nullptr;
}
// Returns the compositor for this Widget, note that this may change during
// the Widget's lifetime (e.g. when switching monitors on Chrome OS).
ui::Compositor* GetCompositor() {
return const_cast<ui::Compositor*>(
const_cast<const Widget*>(this)->GetCompositor());
}
const ui::Compositor* GetCompositor() const;
// Returns the widget's layer, if any.
ui::Layer* GetLayer() {
return const_cast<ui::Layer*>(const_cast<const Widget*>(this)->GetLayer());
}
const ui::Layer* GetLayer() const;
// Reorders the widget's child NativeViews which are associated to the view
// tree (eg via a NativeViewHost) to match the z-order of the views in the
// view tree. The z-order of views with layers relative to views with
// associated NativeViews is used to reorder the NativeView layers. This
// method assumes that the widget's child layers which are owned by a view are
// already in the correct z-order relative to each other and does no
// reordering if there are no views with an associated NativeView.
void ReorderNativeViews();
// Called by a View when the status of it's layer or one of the views
// descendants layer status changes.
void LayerTreeChanged();
const NativeWidget* native_widget() const;
NativeWidget* native_widget();
internal::NativeWidgetPrivate* native_widget_private() {
return native_widget_.get();
}
const internal::NativeWidgetPrivate* native_widget_private() const {
return native_widget_.get();
}
// Sets capture to the specified view. This makes it so that all mouse, touch
// and gesture events go to |view|. If |view| is NULL, the widget still
// obtains event capture, but the events will go to the view they'd normally
// go to.
void SetCapture(View* view);
// Releases capture.
void ReleaseCapture();
// Returns true if the widget has capture.
bool HasCapture();
void set_auto_release_capture(bool auto_release_capture) {
auto_release_capture_ = auto_release_capture;
}
// Returns the font used for tooltips.
TooltipManager* GetTooltipManager();
const TooltipManager* GetTooltipManager() const;
void set_focus_on_creation(bool focus_on_creation) {
focus_on_creation_ = focus_on_creation;
}
// Returns the parent of this widget. Note that
// * A top-level widget is not necessarily the root and may have a parent.
// * A child widget shares the same visual style, e.g. the dark/light theme,
// with its parent.
// * The native widget may change a widget's parent.
// * The native view's parent might or might not be the parent's native view.
// * For a desktop widget with a non-desktop parent, this value might be
// nullptr during shutdown.
Widget* parent() { return parent_.get(); }
const Widget* parent() const { return parent_.get(); }
// True if the widget is considered top level widget. Top level widget
// is a widget of TYPE_WINDOW, TYPE_PANEL, TYPE_WINDOW_FRAMELESS, BUBBLE,
// POPUP or MENU, and has a focus manager and input method object associated
// with it. TYPE_CONTROL and TYPE_TOOLTIP is not considered top level.
bool is_top_level() const { return is_top_level_; }
// True if widget was created in headless mode.
bool is_headless() const { return is_headless_; }
// True if the window size will follow the content preferred size.
bool is_autosized() const { return is_autosized_; }
// True when window movement via mouse interaction with the frame is disabled.
bool movement_disabled() const { return movement_disabled_; }
void set_movement_disabled(bool disabled) { movement_disabled_ = disabled; }
// Returns the work area bounds of the screen the Widget belongs to.
gfx::Rect GetWorkAreaBoundsInScreen() const;
// Creates and dispatches synthesized mouse move event using the current
// mouse location to refresh hovering status in the widget.
void SynthesizeMouseMoveEvent();
// Returns the gesture recognizer which can handle touch/gesture events on
// this.
ui::GestureRecognizer* GetGestureRecognizer();
// Returns the associated gesture consumer.
ui::GestureConsumer* GetGestureConsumer();
// Called when the delegate's CanResize or CanMaximize changes.
void OnSizeConstraintsChanged();
// Notification that our owner is closing.
// NOTE: this is not invoked for aura as it's currently not needed there.
// Under aura menus close by way of activation getting reset when the owner
// closes.
virtual void OnOwnerClosing();
// Returns the internal name for this Widget and NativeWidget.
std::string GetName() const;
// Registers |callback| to be called whenever the "paint as active" state
// changes.
base::CallbackListSubscription RegisterPaintAsActiveChangedCallback(
PaintAsActiveCallbackList::CallbackType callback);
// Prevents the widget from being rendered as inactive during the lifetime of
// the returned lock. Multiple locks can exist with disjoint lifetimes. The
// returned lock can safely outlive the associated widget.
std::unique_ptr<PaintAsActiveLock> LockPaintAsActive();
// Undoes LockPaintAsActive(). This should never be called outside of
// PaintAsActiveLock destructor.
void UnlockPaintAsActive();
// Returns true if the window should paint as active.
bool ShouldPaintAsActive() const;
// Called when the ShouldPaintAsActive() of parent changes.
void OnParentShouldPaintAsActiveChanged();
// Notifies registered callbacks and the native widget of changes to
// the ShouldPaintAsActive() state.
void NotifyPaintAsActiveChanged();
base::WeakPtr<Widget> GetWeakPtr();
// Overridden from NativeWidgetDelegate:
bool IsModal() const override;
bool IsDialogBox() const override;
bool CanActivate() const override;
bool IsNativeWidgetInitialized() const override;
bool OnNativeWidgetActivationChanged(bool active) override;
bool ShouldHandleNativeWidgetActivationChanged(bool active) override;
void OnNativeFocus() override;
void OnNativeBlur() override;
void OnNativeWidgetVisibilityChanged(bool visible) override;
void OnNativeWidgetVisibilityOnScreenChanged(bool visible) override;
void OnNativeWidgetCreated() override;
void OnNativeWidgetDestroying() override;
void OnNativeWidgetDestroyed() override;
void OnNativeWidgetParentChanged(gfx::NativeView parent) override;
gfx::Size GetMinimumSize() const override;
gfx::Size GetMaximumSize() const override;
void OnNativeWidgetMove() override;
void OnNativeWidgetSizeChanged(const gfx::Size& new_size) override;
void OnNativeWidgetUserResizeStarted() override;
void OnNativeWidgetUserResizeEnded() override;
void OnNativeWidgetWorkspaceChanged() override;
void OnNativeWidgetWindowShowStateChanged() override;
void OnNativeWidgetBeginUserBoundsChange() override;
void OnNativeWidgetEndUserBoundsChange() override;
void OnNativeWidgetAddedToCompositor() override;
void OnNativeWidgetRemovingFromCompositor() override;
bool HasFocusManager() const override;
void OnNativeWidgetPaint(const ui::PaintContext& context) override;
int GetNonClientComponent(const gfx::Point& point) override;
void OnKeyEvent(ui::KeyEvent* event) override;
void OnMouseEvent(ui::MouseEvent* event) override;
void OnMouseCaptureLost() override;
void OnScrollEvent(ui::ScrollEvent* event) override;
void OnGestureEvent(ui::GestureEvent* event) override;
bool ExecuteCommand(int command_id) override;
bool HasHitTestMask() const override;
void GetHitTestMask(SkPath* mask) const override;
Widget* AsWidget() override;
const Widget* AsWidget() const override;
bool SetInitialFocus(ui::mojom::WindowShowState show_state) override;
bool ShouldDescendIntoChildForEventHandling(
ui::Layer* root_layer,
gfx::NativeView child,
ui::Layer* child_layer,
const gfx::Point& location) override;
void LayoutRootViewIfNecessary() override;
// Overridden from ui::EventSource:
ui::EventSink* GetEventSink() override;
// Overridden from FocusTraversable:
FocusSearch* GetFocusSearch() override;
FocusTraversable* GetFocusTraversableParent() override;
View* GetFocusTraversableParentView() override;
// Overridden from ui::NativeThemeObserver:
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
// Overridden from ui::AXModeObsever
// TODO(crbug.com/325137417): We might need to add an override for when the
// mode is removed, but currently we don't have the support for this in
// ViewAccessibility. Add the override once logic to remove a mode is added to
// ViewAccessibility.
void OnAXModeAdded(ui::AXMode mode) override;
// Sets an override for `color_mode` when `GetColorProvider()` is requested.
// e.g. if set to kDark, colors will always be for the dark theme.
void SetColorModeOverride(
std::optional<ui::ColorProviderKey::ColorMode> color_mode);
// ui::ColorProviderSource:
const ui::ColorProvider* GetColorProvider() const override;
ui::RendererColorMap GetRendererColorMap(
ui::ColorProviderKey::ColorMode color_mode,
ui::ColorProviderKey::ForcedColors forced_colors) const override;
// Set the native theme from which this widget gets color from for testing.
void SetNativeThemeForTest(ui::NativeTheme* native_theme) {
SetNativeTheme(native_theme);
native_theme_set_for_testing_ = true;
}
ui::ColorProviderKey GetColorProviderKeyForTesting() const;
// Causes IsFullscreen() to also check parent state, since this widget is
// logically part of the same window as the parent.
void SetCheckParentForFullscreen();
// Returns the current ownership model of the widget.
InitParams::Ownership ownership() const { return ownership_; }
bool native_widget_active() const { return native_widget_active_; }
// Called to enable or disable screenshots of this widget.
void SetAllowScreenshots(bool allow);
bool AreScreenshotsAllowed();
// Called when we become / stop being `child_widget`'s parent.
void OnChildAdded(Widget* child_widget);
void OnChildRemoved(Widget* child_widget);
void UpdateAccessibleNameForRootView();
void UpdateAccessibleURLForRootView(const GURL& url);
protected:
// Creates the RootView to be used within this Widget. Subclasses may override
// to create custom RootViews that do specialized event processing.
// TODO(beng): Investigate whether or not this is needed.
virtual internal::RootView* CreateRootView();
// Provided to allow the NativeWidget implementations to destroy the RootView
// _before_ the focus manager/tooltip manager.
// TODO(beng): remove once we fold those objects onto this one.
void DestroyRootView();
// Notification that a drag will start. Default implementation does nothing.
virtual void OnDragWillStart();
// Notification that the drag performed by RunShellDrag() has completed.
virtual void OnDragComplete();
// Set the native theme from which this widget gets color from.
void SetNativeTheme(ui::NativeTheme* native_theme);
// The following methods are used by the property access system described in
// the comments on views::View. They follow the required naming convention in
// order to allow them to be visible via the metadata.
// TODO(kylixrd): Refactor code to use these methods directly.
int GetX() const;
int GetY() const;
int GetWidth() const;
int GetHeight() const;
bool GetVisible() const;
void SetX(int x);
void SetY(int y);
void SetWidth(int width);
void SetHeight(int height);
// ui::ColorProviderSource:
ui::ColorProviderKey GetColorProviderKey() const override;
private:
// Type of ways to ignore activation changes.
enum class DisableActivationChangeHandlingType {
kNone = 0, // Don't ignore any activation changes.
kIgnore, // Ignore both activation and deactivation changes.
kIgnoreDeactivationOnly, // Ignore only deactivation changes.
};
class PaintAsActiveLockImpl;
friend class ButtonTest;
friend class ComboboxTest;
friend class PaintAsActiveLockImpl;
friend class TextfieldTest;
friend class ViewAuraTest;
friend class ui_devtools::BubbleLocking;
friend void DisableActivationChangeHandlingForTests();
// Sets/gets the type of disabling widget activation change handling.
static void SetDisableActivationChangeHandling(
DisableActivationChangeHandlingType new_type) {
g_disable_activation_change_handling_ = new_type;
}
static DisableActivationChangeHandlingType
GetDisableActivationChangeHandling() {
return g_disable_activation_change_handling_;
}
// Helper for Init() to handle accessibility-specific work.
void InitAccessibility();
// Persists the window's restored position and "show" state using the
// window delegate.
void SaveWindowPlacement();
// Invokes SaveWindowPlacement() if the native widget has been initialized.
// This is called at times when the native widget may not have been
// initialized.
void SaveWindowPlacementIfNeeded();
// Sizes and positions the window just after it is created.
void SetInitialBounds(const gfx::Rect& bounds);
// Sizes and positions the frameless window just after it is created.
void SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds);
// The actual heavy-lifting for setting a widget's parent is handled at the
// NativeWidget layer. This just updates some book-keeping.
void HandleNativeWidgetReparented(Widget* parent);
// Returns the bounds and "show" state from the delegate. Returns true if
// the delegate wants to use a specified bounds.
bool GetSavedWindowPlacement(gfx::Rect* bounds,
ui::mojom::WindowShowState* show_state);
// Returns the Views whose layers are parented directly to the Widget's
// layer in reverse z-order (i.e views later in the returned vector have a
// higher z-order).
const View::Views& GetViewsWithLayersInZOrder();
// If a descendent of |root_view_| is focused, then clear the focus.
void ClearFocusFromWidget();
// Notifies the parent that a window-modal child's visibility changed.
// This function is a no-op if the parent does not exist or if this widget is
// not a window modal.
void MaybeNotifyWindowModalVisibilityChanged(bool visible);
// This holds logic that needs to called synchronously after showing, before
// the native widget asynchronously invokes OnNativeWidgetVisibilityChanged().
void HandleShowRequested();
// This holds the logic for handling of the destroying and destroyed
// notifications.
void HandleWidgetDestroying();
void HandleWidgetDestroyed();
// This is called by a task posted by OnRootViewLayoutInvalidated().
// Resize the widget to delegate's desired bounds.
void ResizeToDelegateDesiredBounds();
static DisableActivationChangeHandlingType
g_disable_activation_change_handling_;
base::WeakPtr<internal::NativeWidgetPrivate> native_widget_ = nullptr;
// This unique pointer is only set when WIDGET_OWNS_NATIVE_WIDGET so that we
// can destroy the NativeWidget. Except for managing lifetime for
// WIDGET_OWNS_NATIVE_WIDGET, the NativeWidget should always be referenced
// through the |native_widget_| weak ptr.
std::unique_ptr<internal::NativeWidgetPrivate> owned_native_widget_;
base::ObserverList<WidgetObserver> observers_;
base::ObserverList<WidgetRemovalsObserver>::Unchecked removals_observers_;
// Weak pointer to the Widget's delegate. If a NULL delegate is supplied
// to Init() a default WidgetDelegate is created.
base::WeakPtr<WidgetDelegate> widget_delegate_;
// The parent of this widget. This is the widget that associates with
// the |params.parent| supplied to Init(). If no parent is given or the native
// view parent has no associating Widget, this value will be nullptr.
// For a desktop widget with a non-desktop parent, this value might be nullptr
// during shutdown.
base::WeakPtr<Widget> parent_ = nullptr;
// This boolean is true when the widget tree is being traversed for
// WidgetObserver event broadcasting. It is used to CHECK that the widget tree
// is not modified during the traversal.
bool is_traversing_widget_tree_ = false;
// The root of the View hierarchy attached to this window.
// WARNING: see warning in tooltip_manager_ for ordering dependencies with
// this and tooltip_manager_.
std::unique_ptr<internal::RootView> root_view_;
// The View that provides the non-client area of the window (title bar,
// window controls, sizing borders etc). To use an implementation other than
// the default, this class must be sub-classed and this value set to the
// desired implementation before calling |InitWindow()|.
raw_ptr<NonClientView> non_client_view_ = nullptr;
// The focus manager keeping track of focus for this Widget and any of its
// children. NULL for non top-level widgets.
// WARNING: RootView's destructor calls into the FocusManager. As such, this
// must be destroyed AFTER root_view_. This is enforced in DestroyRootView().
std::unique_ptr<FocusManager> focus_manager_;
// The sublevel manager that ensures that the children are stacked in the
// order specified by their InitParams::sublevel.
std::unique_ptr<SublevelManager> sublevel_manager_;
// Valid for the lifetime of RunShellDrag(), indicates the view the drag
// started from.
raw_ptr<View> dragged_view_ = nullptr;
// See class documentation for Widget above for a note about ownership.
InitParams::Ownership ownership_ = InitParams::NATIVE_WIDGET_OWNS_WIDGET;
// See set_is_secondary_widget().
bool is_secondary_widget_ = true;
// If set, overrides this value is used instead of the one from NativeTheme
// when constructing a ColorProvider.
std::optional<ui::ColorProviderKey::ColorMode> color_mode_override_;
// The current frame type in use by this window. Defaults to
// FrameType::kDefault.
FrameType frame_type_ = FrameType::kDefault;
// Tracks whether the native widget is active.
bool native_widget_active_ = false;
// Count of paint-as-active locks on this widget. See LockPaintAsActive().
size_t paint_as_active_refcount_ = 0;
// Callbacks to notify when the ShouldPaintAsActive() changes.
PaintAsActiveCallbackList paint_as_active_callbacks_;
// Lock on the parent widget when this widget is active.
// When this widget is destroyed, the lock is automatically released.
std::unique_ptr<PaintAsActiveLock> parent_paint_as_active_lock_;
// Subscription to parent's ShouldPaintAsActive() change.
base::CallbackListSubscription parent_paint_as_active_subscription_;
// Set to true if the widget is in the process of closing.
bool widget_closed_ = false;
// Set to true after OnWidgetDestroyed called.
bool native_widget_destroyed_ = false;
// The reason the widget was closed.
// Note that this may be ClosedReason::kUnspecified if the deprecated Close()
// method was called rather than CloseWithReason().
ClosedReason closed_reason_ = ClosedReason::kUnspecified;
// The saved "show" state for this window. See note in SetInitialBounds
// that explains why we save this.
ui::mojom::WindowShowState saved_show_state_ =
ui::mojom::WindowShowState::kDefault;
// The restored bounds used for the initial show. This is only used if
// |saved_show_state_| is maximized. initial_restored_bounds_ is in DIP units
// and is converted to pixels in DesktopWindowTreeHostWin::Show.
gfx::Rect initial_restored_bounds_;
// Focus is automatically set to the view provided by the delegate
// when the widget is shown. Set this value to false to override
// initial focus for the widget.
bool focus_on_creation_ = true;
// See |is_top_level()| accessor.
bool is_top_level_ = false;
// Tracks whether native widget has been initialized.
bool native_widget_initialized_ = false;
// TODO(beng): Remove NativeWidgetGtk's dependence on these:
// If true, the mouse is currently down.
bool is_mouse_button_pressed_ = false;
// If set, the widget was created in headless mode.
bool is_headless_ = false;
// If set, the window size will follow the content preferred size.
bool is_autosized_ = false;
// True if capture losses should be ignored.
bool ignore_capture_loss_ = false;
// True if allow saving window placement.
bool save_window_placement_allowed_ = true;
// TODO(beng): Remove NativeWidgetGtk's dependence on these:
// The following are used to detect duplicate mouse move events and not
// deliver them. Displaying a window may result in the system generating
// duplicate move events even though the mouse hasn't moved.
bool last_mouse_event_was_move_ = false;
gfx::Point last_mouse_event_position_;
// True if event capture should be released on a mouse up event. Default is
// true.
bool auto_release_capture_ = true;
// See description in GetViewsWithLayers().
View::Views views_with_layers_;
// Does |views_with_layers_| need updating?
bool views_with_layers_dirty_ = false;
// True when window movement via mouse interaction with the frame should be
// disabled.
bool movement_disabled_ = false;
// Block the widget from closing.
bool block_close_ = false;
// The native theme this widget is using.
// If nullptr, defaults to use the regular native theme.
raw_ptr<ui::NativeTheme> native_theme_ = nullptr;
// A flag that prevents the widget from updating its instance of
// `native_theme_`. This is necessary during testing as theme updates may
// trigger a reset of the explicitly set test theme.
bool native_theme_set_for_testing_ = false;
// By default, widgets are assumed to correspond to windows. If a parent
// widget is fullscreen, then the child widget is a popup which is not
// fullscreen. However, on macOS some child widgets logically correspond to
// the same window. Their fullscreen state should inherit from their parents.
bool check_parent_for_fullscreen_ = false;
// Replaces the implementation of Close() and CloseWithReason().
base::OnceCallback<void(ClosedReason)> override_close_;
base::ScopedObservation<ui::NativeTheme, ui::NativeThemeObserver>
native_theme_observation_{this};
base::ScopedObservation<ui::AXPlatform, ui::AXModeObserver>
ax_mode_observation_{this};
// Indicates whether there is an autosize task in the task queue. Also used to
// cancel the autosize task in testing.
base::WeakPtrFactory<Widget> autosize_task_factory_{this};
base::WeakPtrFactory<Widget> weak_ptr_factory_{this};
};
} // namespace views
#endif // UI_VIEWS_WIDGET_WIDGET_H_
|