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 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/time/calendar_view.h"
#include <memory>
#include <string>
#include "ash/ash_element_identifiers.h"
#include "ash/bubble/bubble_utils.h"
#include "ash/constants/ash_features.h"
#include "ash/glanceables/common/glanceables_progress_bar_view.h"
#include "ash/public/cpp/ash_typography.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/icon_button.h"
#include "ash/style/pill_button.h"
#include "ash/style/typography.h"
#include "ash/system/model/clock_model.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/time/calendar_event_list_view.h"
#include "ash/system/time/calendar_metrics.h"
#include "ash/system/time/calendar_month_view.h"
#include "ash/system/time/calendar_up_next_view.h"
#include "ash/system/time/calendar_utils.h"
#include "ash/system/time/calendar_view_controller.h"
#include "ash/system/time/date_helper.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tri_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_bubble.h"
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/animation_throughput_reporter.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/vector2d_f.h"
#include "ui/gfx/interpolated_transform.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/scrollbar/scroll_bar.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
// TODO(http://b/361693496): Remove this after the original issue fixed.
#include "components/crash/core/common/crash_key.h"
namespace ash {
using BoundsType = CalendarView::CalendarSlidingSurfaceBoundsType;
namespace {
using AnimatingCrashKey = crash_reporter::CrashKeyString<8>;
// The paddings in each view.
constexpr int kContentVerticalPadding = 20;
constexpr int kContentHorizontalPadding = 20;
constexpr int kMonthVerticalPadding = 10;
constexpr int kLabelVerticalPadding = 10;
constexpr int kLabelTextInBetweenPadding = 4;
const int kWeekRowHorizontalPadding =
kContentHorizontalPadding - calendar_utils::kDateHorizontalPadding;
constexpr int kExpandedCalendarPadding = 10;
constexpr int kChevronPadding = calendar_utils::kColumnSetPadding - 1;
constexpr int kChevronInBetweenPadding = 16;
constexpr int kMonthHeaderLabelTopPadding = 14;
constexpr int kMonthHeaderLabelBottomPadding = 2;
constexpr int kEventListViewHorizontalOffset = 1;
constexpr int kTitleLeftPadding = 8;
// For `calendar_header_view`.
constexpr int kButtonInBetweenPadding = 12;
constexpr int kHeaderViewHeight = 32;
constexpr auto kHeaderIconButtonMargin =
gfx::Insets::TLBR(0, 0, 0, kButtonInBetweenPadding);
constexpr auto kHeaderLabelBorder = gfx::Insets::VH(4, 10);
constexpr auto kHeaderViewMargin = gfx::Insets::TLBR(16, 16, 0, 16);
// The border of `MonthHeaderView` when time management glanceables are enabled.
constexpr auto kMonthHeaderBorder = gfx::Insets::TLBR(14, 0, 2, 0);
// Adds a gap between the bottom visible row in the scrollview and the top of
// the event list view when open.
constexpr int kCalendarEventListViewOpenMargin = 8;
// The offset for `month_label_` to make it align with `month_header`.
constexpr int kMonthLabelPaddingOffset = -1;
// The cool-down time for calling `UpdateOnScreenMonthMap()` after scrolling.
constexpr base::TimeDelta kScrollingSettledTimeout = base::Milliseconds(500);
// The max number of rows in a month.
constexpr int kMaxRowsInOneMonth = 6;
// Duration of the delay for starting header animation.
constexpr base::TimeDelta kDelayHeaderAnimationDuration =
base::Milliseconds(200);
// Duration of events moving animation.
constexpr base::TimeDelta kAnimationDurationForEventsMoving =
base::Milliseconds(400);
// Duration of closing events panel animation.
constexpr base::TimeDelta kAnimationDurationForClosingEvents =
base::Milliseconds(200);
// Duration of `event_list_view_` fade in animation delay.
constexpr base::TimeDelta kEventListAnimationStartDelay =
base::Milliseconds(100);
// Duration of `up_next_view_` fade in animation delay.
constexpr base::TimeDelta kUpNextAnimationStartDelay = base::Milliseconds(50);
// The cool-down time for enabling animation.
constexpr base::TimeDelta kAnimationDisablingTimeout = base::Milliseconds(500);
// Periodic time delay for checking upcoming events.
constexpr base::TimeDelta kCheckUpcomingEventsDelay = base::Seconds(15);
// The multiplier used to reduce velocity of flings on the calendar view.
// Without this, CalendarView will scroll a few years per fast swipe.
constexpr float kCalendarScrollFlingMultiplier = 0.25f;
constexpr char kMonthViewScrollOneMonthAnimationHistogram[] =
"Ash.CalendarView.ScrollOneMonth.MonthView.AnimationSmoothness";
constexpr char kLabelViewScrollOneMonthAnimationHistogram[] =
"Ash.CalendarView.ScrollOneMonth.LabelView.AnimationSmoothness";
constexpr char kHeaderViewScrollOneMonthAnimationHistogram[] =
"Ash.CalendarView.ScrollOneMonth.HeaderView.AnimationSmoothness";
constexpr char kContentViewResetToTodayAnimationHistogram[] =
"Ash.CalendarView.ResetToToday.ContentView.AnimationSmoothness";
constexpr char kHeaderViewResetToTodayAnimationHistogram[] =
"Ash.CalendarView.ResetToToday.HeaderView.AnimationSmoothness";
constexpr char kContentViewFadeInCurrentMonthAnimationHistogram[] =
"Ash.CalendarView.FadeInCurrentMonth.ContentView.AnimationSmoothness";
constexpr char kHeaderViewFadeInCurrentMonthAnimationHistogram[] =
"Ash.CalendarView.FadeInCurrentMonth.HeaderView.AnimationSmoothness";
constexpr char kOnMonthChangedAnimationHistogram[] =
"Ash.CalendarView.OnMonthChanged.AnimationSmoothness";
constexpr char kCloseEventListAnimationHistogram[] =
"Ash.CalendarView.CloseEventList.EventListView.AnimationSmoothness";
constexpr char kCloseEventListCalendarSlidingSurfaceAnimationHistogram[] =
"Ash.CalendarView.CloseEventList.CalendarSlidingSurface."
"AnimationSmoothness";
constexpr char kMonthViewOpenEventListAnimationHistogram[] =
"Ash.CalendarView.OpenEventList.MonthView.AnimationSmoothness";
constexpr char kLabelViewOpenEventListAnimationHistogram[] =
"Ash.CalendarView.OpenEventList.LabelView.AnimationSmoothness";
constexpr char kEventListViewOpenEventListAnimationHistogram[] =
"Ash.CalendarView.OpenEventList.EventListView.AnimationSmoothness";
constexpr char kCalendarSlidingSurfaceOpenEventListAnimationHistogram[] =
"Ash.CalendarView.OpenEventList.CalendarSlidingSurface.AnimationSmoothness";
constexpr char kUpNextViewOpenEventListAnimationHistogram[] =
"Ash.CalendarView.OpenEventList.UpNextView.AnimationSmoothness";
constexpr char kFadeInUpNextViewAnimationHistogram[] =
"Ash.CalendarView.FadeInUpNextView.AnimationSmoothness";
constexpr char kFadeOutUpNextViewAnimationHistogram[] =
"Ash.CalendarView.FadeOutUpNextView.AnimationSmoothness";
// Configures the TriView used for the title.
void ConfigureTitleTriView(TriView* tri_view, TriView::Container container) {
std::unique_ptr<views::BoxLayout> layout;
switch (container) {
case TriView::Container::START:
case TriView::Container::END: {
const int left_padding =
container == TriView::Container::START ? kTitleLeftPadding : 0;
const int right_padding =
container == TriView::Container::END ? kTitleRightPadding : 0;
layout = std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal,
gfx::Insets::TLBR(0, left_padding, 0, right_padding),
kTitleItemBetweenSpacing);
layout->set_main_axis_alignment(
views::BoxLayout::MainAxisAlignment::kCenter);
layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kCenter);
break;
}
case TriView::Container::CENTER:
tri_view->SetFlexForContainer(TriView::Container::CENTER, 1.f);
layout = std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical);
layout->set_main_axis_alignment(
views::BoxLayout::MainAxisAlignment::kCenter);
layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kStretch);
break;
}
tri_view->SetContainerLayout(container, std::move(layout));
tri_view->SetMinSize(container,
gfx::Size(0, kUnifiedDetailedViewTitleRowHeight));
}
std::unique_ptr<views::Label> CreateHeaderView(const std::u16string& month) {
return views::Builder<views::Label>(
bubble_utils::CreateLabel(TypographyToken::kCrosDisplay7, month,
cros_tokens::kCrosSysOnSurface))
.SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_TO_HEAD)
.SetTextContext(CONTEXT_CALENDAR_LABEL)
.SetAutoColorReadabilityEnabled(false)
.Build();
}
std::unique_ptr<views::Label> CreateHeaderYearView(const std::u16string& year) {
const int label_padding = kLabelTextInBetweenPadding;
return views::Builder<views::Label>(
bubble_utils::CreateLabel(TypographyToken::kCrosDisplay7, year,
cros_tokens::kCrosSysOnSurface))
.SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_TO_HEAD)
.SetTextContext(CONTEXT_CALENDAR_LABEL)
.SetAutoColorReadabilityEnabled(false)
.SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(0, label_padding)))
.Build();
}
int GetExpandedCalendarPadding() {
return kExpandedCalendarPadding;
}
void StopViewLayerAnimation(views::View* view) {
view->layer()->GetAnimator()->StopAnimating();
}
void UpdateCachedAnimatingState(AnimatingCrashKey& key, bool running) {
key.Set(running ? "True" : "False");
}
// The overridden `Label` view used in `CalendarView`.
class CalendarLabel : public views::Label {
public:
explicit CalendarLabel(const std::u16string& text) : views::Label(text) {
views::Label::SetEnabledColor(calendar_utils::GetPrimaryTextColor());
views::Label::SetAutoColorReadabilityEnabled(false);
}
CalendarLabel(const CalendarLabel&) = delete;
CalendarLabel& operator=(const CalendarLabel&) = delete;
~CalendarLabel() override = default;
void OnThemeChanged() override {
views::Label::OnThemeChanged();
views::Label::SetEnabledColor(calendar_utils::GetPrimaryTextColor());
}
};
// The month view header which contains the title of each week day.
class MonthHeaderView : public views::View {
METADATA_HEADER(MonthHeaderView, views::View)
public:
MonthHeaderView() {
views::TableLayout* layout =
SetLayoutManager(std::make_unique<views::TableLayout>());
calendar_utils::SetUpWeekColumns(layout);
layout->AddRows(1, views::TableLayout::kFixedSize);
for (const std::u16string& week_day :
DateHelper::GetInstance()->week_titles()) {
auto label =
views::Builder<views::Label>(
bubble_utils::CreateLabel(TypographyToken::kCrosButton1, week_day,
cros_tokens::kCrosSysOnSurface))
.Build();
label->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_CENTER);
label->SetBorder((views::CreateEmptyBorder(
features::AreAnyGlanceablesTimeManagementViewsEnabled()
? kMonthHeaderBorder
: gfx::Insets::VH(calendar_utils::kDateVerticalPadding, 0))));
label->SetElideBehavior(gfx::NO_ELIDE);
label->SetSubpixelRenderingEnabled(false);
AddChildView(std::move(label));
}
}
MonthHeaderView(const MonthHeaderView& other) = delete;
MonthHeaderView& operator=(const MonthHeaderView& other) = delete;
~MonthHeaderView() override = default;
};
// Resets the `view`'s opacity and position.
void ResetLayer(views::View* view) {
view->layer()->SetOpacity(1.0f);
view->layer()->SetTransform(gfx::Transform());
}
BEGIN_METADATA(MonthHeaderView)
END_METADATA
} // namespace
// The label for each month that's within the scroll view.
class CalendarView::MonthHeaderLabelView : public views::View {
METADATA_HEADER(MonthHeaderLabelView, views::View)
public:
MonthHeaderLabelView(LabelType type,
CalendarViewController* calendar_view_controller)
: month_label_(AddChildView(CreateHeaderView(std::u16string()))) {
// The layer is required in animation.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
switch (type) {
case PREVIOUS:
month_name_ = calendar_view_controller->GetPreviousMonthName();
break;
case CURRENT:
month_name_ = calendar_view_controller->GetOnScreenMonthName();
break;
case NEXT:
month_name_ = calendar_view_controller->GetNextMonthName();
break;
case NEXTNEXT:
month_name_ =
calendar_view_controller->GetNextMonthName(/*num_months=*/2);
break;
}
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
month_label_->SetText(month_name_);
month_label_->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(kMonthHeaderLabelTopPadding,
kContentHorizontalPadding + kMonthLabelPaddingOffset,
kMonthHeaderLabelBottomPadding, 0)));
}
MonthHeaderLabelView(const MonthHeaderLabelView&) = delete;
MonthHeaderLabelView& operator=(const MonthHeaderLabelView&) = delete;
~MonthHeaderLabelView() override = default;
private:
// The name of the month.
std::u16string month_name_;
// The month label in the view.
const raw_ptr<views::Label> month_label_ = nullptr;
};
BEGIN_METADATA(CalendarView, MonthHeaderLabelView)
END_METADATA
CalendarView::ScrollContentsView::ScrollContentsView(
CalendarViewController* controller)
: controller_(controller),
stylus_event_handler_(this),
current_month_(controller_->GetOnScreenMonthName()) {}
void CalendarView::ScrollContentsView::OnMonthChanged() {
current_month_ = controller_->GetOnScreenMonthName();
}
void CalendarView::ScrollContentsView::OnEvent(ui::Event* event) {
views::View::OnEvent(event);
if (controller_->GetOnScreenMonthName() == current_month_) {
return;
}
OnMonthChanged();
if (event->IsMouseWheelEvent()) {
calendar_metrics::RecordScrollSource(
calendar_metrics::CalendarViewScrollSource::kByMouseWheel);
}
if (event->IsScrollGestureEvent()) {
calendar_metrics::RecordScrollSource(
calendar_metrics::CalendarViewScrollSource::kByGesture);
}
if (event->IsFlingScrollEvent()) {
calendar_metrics::RecordScrollSource(
calendar_metrics::CalendarViewScrollSource::kByFling);
}
}
void CalendarView::ScrollContentsView::OnStylusEvent(
const ui::TouchEvent& event) {
if (controller_->GetOnScreenMonthName() == current_month_) {
return;
}
OnMonthChanged();
calendar_metrics::RecordScrollSource(
calendar_metrics::CalendarViewScrollSource::kByStylus);
}
CalendarView::ScrollContentsView::StylusEventHandler::StylusEventHandler(
ScrollContentsView* content_view)
: content_view_(content_view) {
Shell::Get()->AddPreTargetHandler(this);
}
CalendarView::ScrollContentsView::StylusEventHandler::~StylusEventHandler() {
Shell::Get()->RemovePreTargetHandler(this);
}
void CalendarView::ScrollContentsView::StylusEventHandler::OnTouchEvent(
ui::TouchEvent* event) {
if (event->pointer_details().pointer_type == ui::EventPointerType::kPen) {
content_view_->OnStylusEvent(*event);
}
}
BEGIN_METADATA(CalendarView, ScrollContentsView)
END_METADATA
CalendarHeaderView::CalendarHeaderView(const std::u16string& month,
const std::u16string& year)
: header_(AddChildView(CreateHeaderView(month))),
header_year_(AddChildView(CreateHeaderYearView(year))) {
// The layer is required in animation.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
}
CalendarHeaderView::~CalendarHeaderView() = default;
void CalendarHeaderView::UpdateHeaders(const std::u16string& month,
const std::u16string& year) {
header_->SetText(month);
header_year_->SetText(year);
}
BEGIN_METADATA(CalendarHeaderView)
END_METADATA
CalendarView::CalendarView(bool use_glanceables_container_style)
: GlanceableTrayChildBubble(use_glanceables_container_style),
calendar_view_controller_(std::make_unique<CalendarViewController>()),
scrolling_settled_timer_(
FROM_HERE,
kScrollingSettledTimeout,
base::BindRepeating(&CalendarView::UpdateOnScreenMonthMap,
base::Unretained(this))),
header_animation_restart_timer_(
FROM_HERE,
kAnimationDisablingTimeout,
base::BindRepeating(
[](CalendarView* calendar_view) {
if (!calendar_view) {
return;
}
calendar_view->set_should_header_animate(true);
},
base::Unretained(this))),
months_animation_restart_timer_(
FROM_HERE,
kAnimationDisablingTimeout,
base::BindRepeating(
[](CalendarView* calendar_view) {
if (!calendar_view) {
return;
}
calendar_view->set_should_months_animate(true);
},
base::Unretained(this))) {
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
SetFocusBehavior(FocusBehavior::ALWAYS);
// Focusable nodes must have an accessible name and valid role.
// TODO(crbug.com/1348930): Review the accessible name and role.
GetViewAccessibility().SetRole(ax::mojom::Role::kPane);
GetViewAccessibility().SetName(std::string(GetClassName()),
ax::mojom::NameFrom::kAttribute);
views::View* calendar_header_view = nullptr;
if (features::AreAnyGlanceablesTimeManagementViewsEnabled()) {
calendar_header_view = CreateCalendarHeaderRow();
} else {
CreateCalendarTitleRow();
}
// Adds the progress bar to layout when initialization to avoid changing the
// layout while reading the bounds of it.
progress_bar_ = AddChildView(std::make_unique<GlanceablesProgressBarView>());
progress_bar_->SetPreferredSize(gfx::Size(0, kTitleRowProgressBarHeight));
progress_bar_->UpdateProgressBarVisibility(/*visible=*/false);
// Adds the calendar month header view and up/down buttons after the progress
// bar for non-Glanceables calendar view.
if (!features::AreAnyGlanceablesTimeManagementViewsEnabled()) {
TriView* tri_view =
TrayPopupUtils::CreateDefaultRowView(/*use_wide_layout=*/false);
tri_view->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(kLabelVerticalPadding, kContentHorizontalPadding, 0,
kContentHorizontalPadding - kChevronPadding)));
tri_view->AddView(TriView::Container::START, CreateMonthHeaderContainer());
tri_view->AddView(TriView::Container::END, CreateButtonContainer());
AddChildViewRaw(tri_view);
}
// Add month header.
auto month_header = std::make_unique<MonthHeaderView>();
month_header->SetBorder(
views::CreateEmptyBorder(gfx::Insets::VH(0, kWeekRowHorizontalPadding)));
AddChildView(std::move(month_header));
// Add scroll view.
scroll_view_ = AddChildView(std::make_unique<views::ScrollView>());
scroll_view_->SetAllowKeyboardScrolling(false);
scroll_view_->SetBackgroundColor(std::nullopt);
ClipScrollViewHeight(ScrollViewState::FULL_HEIGHT);
scroll_view_->SetDrawOverflowIndicator(false);
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
scroll_view_->vertical_scroll_bar()->SetFlingMultiplier(
kCalendarScrollFlingMultiplier);
scroll_view_->SetFocusBehavior(FocusBehavior::NEVER);
on_contents_scrolled_subscription_ =
scroll_view_->AddContentsScrolledCallback(base::BindRepeating(
&CalendarView::OnContentsScrolled, base::Unretained(this)));
content_view_ = scroll_view_->SetContents(
std::make_unique<ScrollContentsView>(calendar_view_controller_.get()));
content_view_->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
content_view_->SetBorder(views::CreateEmptyBorder(
gfx::Insets::VH(kContentVerticalPadding, kWeekRowHorizontalPadding)));
// Focusable nodes must have an accessible name and valid role.
// TODO(crbug.com/1348930): Review the accessible name and role.
content_view_->GetViewAccessibility().SetRole(ax::mojom::Role::kPane);
content_view_->GetViewAccessibility().SetName(
std::string(GetClassName()), ax::mojom::NameFrom::kAttribute);
content_view_->SetFocusBehavior(FocusBehavior::ALWAYS);
// Set up layer for animations.
content_view_->SetPaintToLayer();
content_view_->layer()->SetFillsBoundsOpaquely(false);
if (calendar_utils::IsMultiCalendarEnabled()) {
calendar_list_model_->FetchCalendars();
}
SetMonthViews();
// Container used for animating the event list view and / or the up next view.
calendar_sliding_surface_ = AddChildView(std::make_unique<views::View>());
calendar_sliding_surface_->SetUseDefaultFillLayout(true);
// We manipulate this layer with translations which can take it off the screen
// so for the animations to work we need to control its positioning.
calendar_sliding_surface_->SetProperty(views::kViewIgnoredByLayoutKey, true);
// This layer is required for animations.
calendar_sliding_surface_->SetPaintToLayer();
calendar_sliding_surface_->layer()->SetFillsBoundsOpaquely(false);
// Override the default focus order so the calendar contents (which contains
// the current date view) and the UI within calendar sliding surfaces get
// focused before the "Today" button in the calendar view header.
scroll_view_->InsertBeforeInFocusList(
features::AreAnyGlanceablesTimeManagementViewsEnabled()
? calendar_header_view
: tri_view_);
calendar_sliding_surface_->InsertAfterInFocusList(scroll_view_);
scoped_calendar_model_observer_.Observe(calendar_model_.get());
scoped_calendar_view_controller_observer_.Observe(
calendar_view_controller_.get());
scoped_view_observer_.AddObservation(scroll_view_.get());
scoped_view_observer_.AddObservation(content_view_.get());
scoped_view_observer_.AddObservation(this);
check_upcoming_events_timer_.Start(
FROM_HERE, kCheckUpcomingEventsDelay,
base::BindRepeating(&CalendarView::MaybeShowUpNextView,
base::Unretained(this)));
SetProperty(views::kElementIdentifierKey, kCalendarViewElementId);
}
CalendarView::~CalendarView() {
is_destroying_ = true;
RestoreHeadersStatus();
RestoreMonthStatus();
// Removes child views including month views and event list to remove their
// dependency from `CalendarViewController`, since these views are destructed
// after the controller.
if (event_list_view_) {
calendar_sliding_surface_->RemoveChildViewT(event_list_view_.get());
event_list_view_ = nullptr;
}
StopUpNextTimer();
RemoveUpNextView();
up_next_view_ = nullptr;
content_view_->RemoveAllChildViews();
}
views::View* CalendarView::CreateCalendarHeaderRow() {
auto* calendar_header_view =
TrayPopupUtils::CreateDefaultRowView(/*use_wide_layout=*/false);
calendar_header_view->SetBorder(views::CreateEmptyBorder(kHeaderViewMargin));
calendar_header_view->AddView(TriView::Container::START,
CreateMonthHeaderContainer());
auto* today_button = new IconButton(
base::BindRepeating(&CalendarView::ResetToTodayWithAnimation,
base::Unretained(this)),
IconButton::Type::kMediumFloating, &kGlanceablesCalendarTodayIcon,
IDS_ASH_CALENDAR_INFO_BUTTON_ACCESSIBLE_DESCRIPTION);
today_button->SetBackgroundColor(cros_tokens::kCrosSysBaseElevated);
today_button->SetProperty(views::kMarginsKey, kHeaderIconButtonMargin);
today_button->GetViewAccessibility().SetName(l10n_util::GetStringFUTF16(
IDS_ASH_CALENDAR_INFO_BUTTON_ACCESSIBLE_DESCRIPTION,
calendar_utils::GetMonthDayYear(base::Time::Now())));
today_button->SetTooltipText(
l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_TODAY_BUTTON_TOOLTIP));
calendar_header_view->AddView(TriView::Container::END, today_button);
calendar_header_view->AddView(TriView::Container::END,
CreateButtonContainer());
// Resets the insets of `calendar_header_view` since it has a default value
// when constructed.
calendar_header_view->SetInsets(gfx::Insets(0));
calendar_header_view->SetContainerBorder(
TriView::Container::START, views::CreateEmptyBorder(kHeaderLabelBorder));
calendar_header_view->SetMinHeight(kHeaderViewHeight);
return AddChildViewRaw(calendar_header_view);
}
void CalendarView::CreateCalendarTitleRow() {
DCHECK(!tri_view_);
tri_view_ =
AddChildViewAt(std::make_unique<TriView>(kUnifiedTopShortcutSpacing), 0);
ConfigureTitleTriView(tri_view_.get(), TriView::Container::START);
ConfigureTitleTriView(tri_view_.get(), TriView::Container::CENTER);
ConfigureTitleTriView(tri_view_.get(), TriView::Container::END);
auto* title_label = TrayPopupUtils::CreateDefaultLabel();
title_label->SetText(l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_TITLE));
title_label->SetEnabledColor(cros_tokens::kCrosSysOnSurface);
ash::TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosTitle1,
*title_label);
tri_view_->AddView(TriView::Container::CENTER, title_label);
// Adds the buttons to the end of the `tri_view_`.
tri_view_->SetContainerVisible(TriView::Container::END, /*visible=*/true);
if (calendar_utils::IsDisabledByAdmin()) {
DCHECK(!managed_button_);
managed_button_ = tri_view_->AddView(
TriView::Container::END,
std::make_unique<IconButton>(
base::BindRepeating([]() {
Shell::Get()->system_tray_model()->client()->ShowEnterpriseInfo();
}),
IconButton::Type::kMedium, &kSystemTrayManagedIcon,
IDS_ASH_CALENDAR_DISABLED_BY_ADMIN));
}
DCHECK(!reset_to_today_button_);
reset_to_today_button_ = new PillButton(
base::BindRepeating(&CalendarView::ResetToTodayWithAnimation,
base::Unretained(this)),
l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_INFO_BUTTON),
PillButton::Type::kDefaultWithoutIcon, /*icon=*/nullptr);
reset_to_today_button_->GetViewAccessibility().SetName(
l10n_util::GetStringFUTF16(
IDS_ASH_CALENDAR_INFO_BUTTON_ACCESSIBLE_DESCRIPTION,
calendar_utils::GetMonthDayYear(base::Time::Now())));
reset_to_today_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_TODAY_BUTTON_TOOLTIP));
tri_view_->AddView(TriView::Container::END, reset_to_today_button_);
DCHECK(!settings_button_);
settings_button_ = new IconButton(
base::BindRepeating([]() {
calendar_metrics::RecordSettingsButtonPressed();
ClockModel* model = Shell::Get()->system_tray_model()->clock();
if (Shell::Get()->session_controller()->ShouldEnableSettings()) {
model->ShowDateSettings();
} else if (model->can_set_time()) {
model->ShowSetTimeDialog();
}
}),
IconButton::Type::kMedium, &vector_icons::kSettingsOutlineIcon,
IDS_ASH_CALENDAR_SETTINGS);
if (!TrayPopupUtils::CanOpenWebUISettings()) {
settings_button_->SetEnabled(false);
}
settings_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_SETTINGS_TOOLTIP));
tri_view_->AddView(TriView::Container::END, settings_button_);
DeprecatedLayoutImmediately();
}
views::View* CalendarView::CreateMonthHeaderContainer() {
auto* header_container = new views::View();
header_container->SetLayoutManager(std::make_unique<views::FillLayout>());
header_ = header_container->AddChildView(std::make_unique<CalendarHeaderView>(
calendar_view_controller_->GetOnScreenMonthName(),
calendar_utils::GetYear(
calendar_view_controller_->currently_shown_date())));
temp_header_ =
header_container->AddChildView(std::make_unique<CalendarHeaderView>(
calendar_view_controller_->GetPreviousMonthName(),
calendar_utils::GetYear(
calendar_view_controller_->currently_shown_date())));
// The `temp_header_` only shows up during the header animation.
temp_header_->SetVisible(false);
return header_container;
}
views::View* CalendarView::CreateButtonContainer() {
auto* button_container = new views::View();
views::BoxLayout* button_container_layout =
button_container->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
button_container_layout->set_main_axis_alignment(
views::BoxLayout::MainAxisAlignment::kEnd);
// Aligns button with the calendar dates in the `TableLayout`.
button_container_layout->set_between_child_spacing(
features::AreAnyGlanceablesTimeManagementViewsEnabled()
? kButtonInBetweenPadding
: kChevronInBetweenPadding);
up_button_ = button_container->AddChildView(std::make_unique<IconButton>(
base::BindRepeating(&CalendarView::OnMonthArrowButtonActivated,
base::Unretained(this), /*up=*/true),
IconButton::Type::kMediumFloating, &vector_icons::kCaretUpIcon,
IDS_ASH_CALENDAR_UP_BUTTON_ACCESSIBLE_DESCRIPTION));
down_button_ = button_container->AddChildView(std::make_unique<IconButton>(
base::BindRepeating(&CalendarView::OnMonthArrowButtonActivated,
base::Unretained(this), /*up=*/false),
IconButton::Type::kMediumFloating, &vector_icons::kCaretDownIcon,
IDS_ASH_CALENDAR_DOWN_BUTTON_ACCESSIBLE_DESCRIPTION));
return button_container;
}
void CalendarView::SetMonthViews() {
previous_label_ = AddLabelWithId(LabelType::PREVIOUS);
previous_month_ =
AddMonth(calendar_view_controller_->GetPreviousMonthFirstDayUTC(1));
current_label_ = AddLabelWithId(LabelType::CURRENT);
current_month_ =
AddMonth(calendar_view_controller_->GetOnScreenMonthFirstDayUTC());
next_label_ = AddLabelWithId(LabelType::NEXT);
next_month_ = AddMonth(calendar_view_controller_->GetNextMonthFirstDayUTC(1));
next_next_label_ = AddLabelWithId(LabelType::NEXTNEXT);
next_next_month_ = AddMonth(
calendar_view_controller_->GetNextMonthFirstDayUTC(/*num_months=*/2));
}
int CalendarView::GetPositionOfCurrentMonth() const {
// Compute the position, because this information may be required before
// layout.
return kContentVerticalPadding +
previous_label_->GetPreferredSize().height() +
previous_month_->GetPreferredSize().height() +
current_label_->GetPreferredSize().height();
}
int CalendarView::GetPositionOfToday() const {
return GetPositionOfCurrentMonth() +
calendar_view_controller_->GetTodayRowTopHeight();
}
int CalendarView::GetPositionOfSelectedDate() const {
DCHECK(calendar_view_controller_->selected_date().has_value());
const int row_height = calendar_view_controller_->selected_date_row_index() *
calendar_view_controller_->row_height() +
GetExpandedCalendarPadding();
// The selected date should be either in the current month or the next month.
if (calendar_view_controller_->IsSelectedDateInCurrentMonth()) {
return GetPositionOfCurrentMonth() + row_height;
}
return GetPositionOfCurrentMonth() +
current_month_->GetPreferredSize().height() +
next_label_->GetPreferredSize().height() + row_height;
}
int CalendarView::GetSingleVisibleRowHeight() const {
return calendar_view_controller_->row_height() +
kCalendarEventListViewOpenMargin;
}
void CalendarView::SetHeaderAndContentViewOpacity(float opacity) {
header_->layer()->SetOpacity(opacity);
content_view_->layer()->SetOpacity(opacity);
}
void CalendarView::SetShouldMonthsAnimateAndScrollEnabled(bool enabled) {
set_should_months_animate(enabled);
is_resetting_scroll_ = !enabled;
calendar_view_controller_->set_is_date_cell_clickable(enabled);
scroll_view_->SetVerticalScrollBarMode(
enabled ? views::ScrollView::ScrollBarMode::kHiddenButEnabled
: views::ScrollView::ScrollBarMode::kDisabled);
}
void CalendarView::ResetToTodayWithAnimation() {
if (!should_months_animate_) {
return;
}
calendar_metrics::RecordResetToTodayPressed();
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/false);
auto content_reporter = calendar_metrics::CreateAnimationReporter(
content_view_, kContentViewResetToTodayAnimationHistogram);
auto header_reporter = calendar_metrics::CreateAnimationReporter(
header_, kHeaderViewResetToTodayAnimationHistogram);
// Fades out on-screen month. When animation ends sets date to today by
// calling `ResetToToday` and fades in updated views after.
is_reset_to_today_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(&CalendarView::OnResetToTodayAnimationComplete,
weak_factory_.GetWeakPtr()))
.OnAborted(base::BindOnce(&CalendarView::OnResetToTodayAnimationComplete,
weak_factory_.GetWeakPtr()))
.Once()
.SetDuration(calendar_utils::kResetToTodayFadeAnimationDuration)
.SetOpacity(header_, 0.0f)
.SetOpacity(content_view_, 0.0f);
}
void CalendarView::ResetToToday() {
if (event_list_view_) {
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
set_should_months_animate(false);
}
// Updates month to today's date without animating header.
{
base::AutoReset<bool> is_updating_month(&should_header_animate_, false);
calendar_view_controller_->UpdateMonth(base::Time::Now());
}
content_view_->RemoveChildViewT(previous_label_.get());
content_view_->RemoveChildViewT(previous_month_.get());
content_view_->RemoveChildViewT(current_label_.get());
content_view_->RemoveChildViewT(current_month_.get());
content_view_->RemoveChildViewT(next_label_.get());
content_view_->RemoveChildViewT(next_month_.get());
content_view_->RemoveChildViewT(next_next_label_.get());
content_view_->RemoveChildViewT(next_next_month_.get());
// Before adding new label and month views, reset the `scroll_view_` to 0
// position. Otherwise after all the views are deleted the 'scroll_view_`'s
// position is still pointing to the position of the previous `current_month_`
// view which is outside of the view. This will cause the scroll view show
// nothing on the screen and get stuck there (can not scroll up and down any
// more).
{
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(), 0);
}
SetMonthViews();
ScrollToToday();
MaybeResetContentViewFocusBehavior();
if (event_list_view_) {
// `ShowEventListView()` also updates the selected view.
DCHECK(current_month_->has_today());
calendar_view_controller_->ShowEventListView(
calendar_view_controller_->todays_date_cell_view(), base::Time::Now(),
calendar_view_controller_->today_row());
months_animation_restart_timer_.Reset();
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/true);
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
}
}
void CalendarView::UpdateOnScreenMonthMap() {
base::Time current_date = calendar_view_controller_->currently_shown_date();
base::Time start_time = calendar_utils::GetStartOfMonthUTC(
current_date + calendar_utils::GetTimeDifference(current_date));
on_screen_month_.clear();
on_screen_month_[start_time] =
calendar_model_->FindFetchingStatus(start_time);
// Checks if `next_month_` is in the visible view. If so, adds it to
// `on_screen_month_` if not already present. Otherwise updates the fetching
// status. This is needed since a refetching request may be sent when this
// function is called and we need to update the fetching status to toggle the
// visibility of the loading bar.
if (scroll_view_->GetVisibleRect().bottom() >= next_month_->y()) {
base::Time next_start_time =
calendar_utils::GetStartOfNextMonthUTC(start_time);
on_screen_month_[next_start_time] =
calendar_model_->FindFetchingStatus(next_start_time);
// Checks if `next_next_month_` is in the visible view.
if (scroll_view_->GetVisibleRect().bottom() >= next_next_month_->y()) {
base::Time next_next_start_time =
calendar_utils::GetStartOfNextMonthUTC(next_start_time);
on_screen_month_[next_next_start_time] =
calendar_model_->FindFetchingStatus(next_next_start_time);
}
}
MaybeUpdateLoadingBarVisibility();
calendar_view_controller_->CalendarLoaded();
}
bool CalendarView::EventsFetchComplete() {
for (auto& it : on_screen_month_) {
// Return false if there's an on-screen month that hasn't finished fetching
// or re-fetching.
if (it.second == CalendarModel::kFetching ||
it.second == CalendarModel::kRefetching) {
return false;
}
}
return true;
}
void CalendarView::MaybeCreateUpNextView() {
if (up_next_view_) {
return;
}
up_next_view_ = calendar_sliding_surface_->AddChildView(
std::make_unique<CalendarUpNextView>(
calendar_view_controller_.get(),
base::BindRepeating(&CalendarView::OpenEventListForTodaysDate,
base::Unretained(this))));
}
void CalendarView::MaybeUpdateLoadingBarVisibility() {
bool visible;
if (calendar_utils::IsMultiCalendarEnabled()) {
visible = !(!calendar_list_model_->get_fetch_in_progress() &&
EventsFetchComplete());
} else {
visible = !EventsFetchComplete();
}
progress_bar_->UpdateProgressBarVisibility(
/*visible=*/visible);
}
void CalendarView::FadeInCurrentMonth() {
if (!should_months_animate_) {
return;
}
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/false);
content_view_->SetPaintToLayer();
content_view_->layer()->SetFillsBoundsOpaquely(false);
SetHeaderAndContentViewOpacity(/*opacity=*/0.0f);
auto content_reporter = calendar_metrics::CreateAnimationReporter(
content_view_, kContentViewFadeInCurrentMonthAnimationHistogram);
auto header_reporter = calendar_metrics::CreateAnimationReporter(
header_, kHeaderViewFadeInCurrentMonthAnimationHistogram);
is_reset_to_today_fade_in_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(
base::BindOnce(&CalendarView::OnResetToTodayFadeInAnimationComplete,
weak_factory_.GetWeakPtr()))
.OnAborted(
base::BindOnce(&CalendarView::OnResetToTodayFadeInAnimationComplete,
weak_factory_.GetWeakPtr()))
.Once()
.SetDuration(calendar_utils::kResetToTodayFadeAnimationDuration)
.SetOpacity(header_, 1.0f)
.SetOpacity(content_view_, 1.0f);
}
void CalendarView::UpdateHeaders() {
header_->UpdateHeaders(
calendar_view_controller_->GetOnScreenMonthName(),
calendar_utils::GetYear(
calendar_view_controller_->currently_shown_date()));
}
void CalendarView::RestoreHeadersStatus() {
header_->layer()->GetAnimator()->StopAnimating();
header_->layer()->SetOpacity(1.0f);
header_->layer()->SetTransform(gfx::Transform());
temp_header_->layer()->GetAnimator()->StopAnimating();
temp_header_->SetVisible(false);
scrolling_settled_timer_.Reset();
if (!should_header_animate_) {
header_animation_restart_timer_.Reset();
}
}
void CalendarView::RestoreMonthStatus() {
current_month_->layer()->GetAnimator()->StopAnimating();
current_label_->layer()->GetAnimator()->StopAnimating();
previous_month_->layer()->GetAnimator()->StopAnimating();
previous_label_->layer()->GetAnimator()->StopAnimating();
next_label_->layer()->GetAnimator()->StopAnimating();
next_month_->layer()->GetAnimator()->StopAnimating();
next_next_label_->layer()->GetAnimator()->StopAnimating();
next_next_month_->layer()->GetAnimator()->StopAnimating();
ResetLayer(current_month_);
ResetLayer(current_label_);
ResetLayer(previous_label_);
ResetLayer(previous_month_);
ResetLayer(next_label_);
ResetLayer(next_month_);
ResetLayer(next_next_label_);
ResetLayer(next_next_month_);
if (!should_months_animate_) {
months_animation_restart_timer_.Reset();
}
}
void CalendarView::ScrollToToday() {
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
if (event_list_view_) {
scroll_view_->ScrollToPosition(
scroll_view_->vertical_scroll_bar(),
GetPositionOfToday() + GetExpandedCalendarPadding());
return;
}
scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(),
GetPositionOfToday());
}
bool CalendarView::IsDateCellViewFocused() {
// For tests, in which the view is not in a Widget.
if (!GetFocusManager()) {
return false;
}
auto* focused_view = GetFocusManager()->GetFocusedView();
if (!focused_view) {
return false;
}
return views::IsViewClass<CalendarDateCellView>(focused_view);
}
bool CalendarView::IsAnimating() {
return header_->layer()->GetAnimator()->is_animating() ||
current_month_->layer()->GetAnimator()->is_animating() ||
content_view_->layer()->GetAnimator()->is_animating() ||
(event_list_view_ &&
event_list_view_->layer()->GetAnimator()->is_animating()) ||
calendar_sliding_surface_->layer()->GetAnimator()->is_animating();
}
void CalendarView::MaybeResetContentViewFocusBehavior() {
if (IsDateCellViewFocused() ||
content_view_->GetFocusBehavior() == FocusBehavior::ALWAYS) {
return;
}
content_view_->SetFocusBehavior(FocusBehavior::ALWAYS);
}
void CalendarView::OnViewBoundsChanged(views::View* observed_view) {
// When in the tablet mode and the display rotates and the `event_list_view_`
// is shown, `event_list_view_` should update its height to fill out the
// remaining space.
if (observed_view == this && event_list_view_) {
SetCalendarSlidingSurfaceBounds(BoundsType::EVENT_LIST_VIEW_BOUNDS);
return;
}
// For screen density or orientation changes, we need to redraw the up next
// views position and adjust the scroll view height accordingly.
if (observed_view == this && IsUpNextViewVisible()) {
SetCalendarSlidingSurfaceBounds(BoundsType::UP_NEXT_VIEW_BOUNDS);
ClipScrollViewHeight(ScrollViewState::UP_NEXT_SHOWING);
return;
}
if (observed_view != scroll_view_) {
return;
}
// The CalendarView is created and lives without being added to the view tree
// for a while. The first time OnViewBoundsChanged is called is the sign that
// the view has actually been added to a view hierarchy, and it is time to
// make some changes which depend on the view belonging to a widget.
scoped_view_observer_.RemoveObservation(observed_view);
// Initializes the view to auto scroll to `GetPositionOfToday` or the first
// row of today's month.
ScrollToToday();
// If the view was shown via keyboard shortcut, the widget will be focusable.
// Request focus to enable the user to quickly press enter to see todays
// events. If the view was not shown via keyboard, this will be a no-op.
RequestFocus();
// Reset the timer here to invoke `UpdateOnScreenMonthMap()` manually after
// 'kScrollingSettledTimeout` since layout will be finalized after a few
// iterations and `on_screen_month_` only wants the final result.
scrolling_settled_timer_.Reset();
}
void CalendarView::OnViewFocused(View* observed_view) {
if (observed_view == this) {
content_view_->RequestFocus();
SetFocusBehavior(FocusBehavior::NEVER);
return;
}
if (observed_view != content_view_ || IsDateCellViewFocused()) {
return;
}
auto* focus_manager = GetFocusManager();
previous_month_->EnableFocus();
current_month_->EnableFocus();
next_month_->EnableFocus();
next_next_month_->EnableFocus();
// If the event list is showing, focus on the first cell in the current row or
// today's cell if today is in this row.
if (calendar_view_controller_->is_event_list_showing()) {
focus_manager->SetFocusedView(
current_month_->focused_cells()[calendar_view_controller_
->GetExpandedRowIndex()]);
AdjustDateCellVoxBounds();
content_view_->SetFocusBehavior(FocusBehavior::NEVER);
return;
}
FocusPreferredDateCellViewOrFirstVisible(/*prefer_today=*/true);
}
views::View* CalendarView::AddLabelWithId(LabelType type, bool add_at_front) {
auto label = std::make_unique<MonthHeaderLabelView>(
type, calendar_view_controller_.get());
if (add_at_front) {
return content_view_->AddChildViewAt(std::move(label), 0);
}
return content_view_->AddChildView(std::move(label));
}
CalendarMonthView* CalendarView::AddMonth(base::Time month_first_date,
bool add_at_front) {
auto month = std::make_unique<CalendarMonthView>(
month_first_date, calendar_view_controller_.get());
month->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(kMonthVerticalPadding, 0, kMonthVerticalPadding, 0)));
if (add_at_front) {
return content_view_->AddChildViewAt(std::move(month), 0);
} else {
return content_view_->AddChildView(std::move(month));
}
}
void CalendarView::OnMonthChanged() {
// The header animation without event list view is handled in the
// `ScrollOneMonthWithAnimation` method.
if (!should_header_animate_ || !event_list_view_) {
UpdateHeaders();
RestoreHeadersStatus();
return;
}
set_should_header_animate(false);
const std::u16string month =
calendar_view_controller_->GetOnScreenMonthName();
const std::u16string year = calendar_utils::GetYear(
calendar_view_controller_->currently_shown_date());
gfx::Transform header_moving = GetHeaderMovingAndPrepareAnimation(
is_scrolling_up_, kOnMonthChangedAnimationHistogram, month, year);
is_header_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(
[](base::WeakPtr<CalendarView> calendar_view) {
if (!calendar_view) {
return;
}
calendar_view->is_header_animation_running_ = false;
calendar_view->UpdateHeaders();
calendar_view->temp_header_->SetVisible(false);
calendar_view->header_->layer()->SetOpacity(1.0f);
calendar_view->header_->layer()->SetTransform(gfx::Transform());
calendar_view->set_should_header_animate(true);
calendar_view->reset_scrolling_settled_timer();
},
weak_factory_.GetWeakPtr()))
.OnAborted(base::BindOnce(
[](base::WeakPtr<CalendarView> calendar_view) {
if (!calendar_view) {
return;
}
calendar_view->is_header_animation_running_ = false;
calendar_view->temp_header_->SetVisible(false);
calendar_view->UpdateHeaders();
calendar_view->RestoreHeadersStatus();
},
weak_factory_.GetWeakPtr()))
.Once()
.SetDuration(calendar_utils::kAnimationDurationForMoving)
.SetTransform(header_, header_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(temp_header_, gfx::Transform(), gfx::Tween::EASE_OUT_2)
.At(kDelayHeaderAnimationDuration)
.SetDuration(calendar_utils::kAnimationDurationForVisibility)
.SetOpacity(header_, 0.0f)
.At(kDelayHeaderAnimationDuration +
calendar_utils::kAnimationDurationForVisibility)
.SetDuration(calendar_utils::kAnimationDurationForVisibility)
.SetOpacity(temp_header_, 1.0f);
}
void CalendarView::OnEventsFetched(const CalendarModel::FetchingStatus status,
const base::Time start_time) {
if (base::Contains(on_screen_month_, start_time)) {
on_screen_month_[start_time] = status;
}
MaybeUpdateLoadingBarVisibility();
// Only show up next for events that are the same month as `base::Time::Now`
// and if the user hasn't scrolled which is checked in
// `MaybeShowUpNextView()`.
if (start_time == calendar_utils::GetStartOfMonthUTC(
base::Time::NowFromSystemTime().UTCMidnight())) {
MaybeShowUpNextView();
}
}
void CalendarView::OpenEventList() {
// Don't show the the `event_list_` view for unlogged in users.
if (!calendar_utils::ShouldFetchCalendarData()) {
return;
}
// If the event list is already open or if any animation is occurring do not
// let the user open the EventListView. It is ok to show the EventListView if
// the animation cooldown is active.
if (event_list_view_ || is_calendar_view_scrolling_ || IsAnimating()) {
return;
}
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
// Updates `scroll_view_`'s accessible name with the selected date.
std::optional<base::Time> selected_date =
calendar_view_controller_->selected_date();
scroll_view_->GetViewAccessibility().SetName(
l10n_util::GetStringFUTF16(
IDS_ASH_CALENDAR_CONTENT_ACCESSIBLE_DESCRIPTION,
calendar_utils::GetMonthNameAndYear(
calendar_view_controller_->currently_shown_date()),
calendar_utils::GetMonthDayYear(selected_date.value())),
ax::mojom::NameFrom::kAttribute);
event_list_view_ = calendar_sliding_surface_->AddChildView(
std::make_unique<CalendarEventListView>(calendar_view_controller_.get()));
event_list_view_->SetFocusBehavior(FocusBehavior::NEVER);
const int previous_surface_y = calendar_sliding_surface_->y();
SetCalendarSlidingSurfaceBounds(BoundsType::EVENT_LIST_VIEW_BOUNDS);
set_should_months_animate(false);
calendar_view_controller_->set_is_date_cell_clickable(false);
gfx::Vector2dF moving_up_location = gfx::Vector2dF(
0, -GetPositionOfSelectedDate() + scroll_view_->GetVisibleRect().y());
gfx::Transform month_moving;
month_moving.Translate(moving_up_location);
// If the `up_next_view_` is showing, then we want to start the animation from
// there, otherwise we start from the bottom of the screen.
const int y_transform_start_position =
IsUpNextViewVisible()
? previous_surface_y - calendar_sliding_surface_->y()
: calendar_sliding_surface_->y();
std::unique_ptr<ui::InterpolatedTranslation> list_view_sliding_up =
std::make_unique<ui::InterpolatedTranslation>(
gfx::PointF(0.f, y_transform_start_position), gfx::PointF());
// Tracks animation smoothness. For now, we only track animation smoothness
// for 1 month and 1 label since all 2 month views and 2 label views are
// similar and perform the same animation. If this is not the case in the
// future, we should add additional metrics for the rest.
auto month_reporter = calendar_metrics::CreateAnimationReporter(
current_month_, kMonthViewOpenEventListAnimationHistogram);
auto label_reporter = calendar_metrics::CreateAnimationReporter(
current_label_, kLabelViewOpenEventListAnimationHistogram);
auto event_list_reporter = calendar_metrics::CreateAnimationReporter(
event_list_view_, kEventListViewOpenEventListAnimationHistogram);
auto calendar_sliding_surface_reporter =
calendar_metrics::CreateAnimationReporter(
calendar_sliding_surface_,
kCalendarSlidingSurfaceOpenEventListAnimationHistogram);
is_event_list_open_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(&CalendarView::OnOpenEventListAnimationComplete,
weak_factory_.GetWeakPtr()))
.OnAborted(base::BindOnce(&CalendarView::OnOpenEventListAnimationComplete,
weak_factory_.GetWeakPtr()))
.Once()
.SetDuration(calendar_utils::kAnimationDurationForMoving)
.SetTransform(current_month_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(current_label_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_label_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_month_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_next_label_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_next_month_, month_moving, gfx::Tween::EASE_OUT_2)
.At(base::Milliseconds(0))
.SetDuration(kAnimationDurationForEventsMoving)
.SetInterpolatedTransform(calendar_sliding_surface_,
std::move(list_view_sliding_up),
gfx::Tween::EASE_IN_OUT_2);
if (IsUpNextViewVisible()) {
auto up_next_reporter = calendar_metrics::CreateAnimationReporter(
up_next_view_, kUpNextViewOpenEventListAnimationHistogram);
// Fade in `event_list_view_` and fade out `up_next_view_`.
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.Once()
.SetOpacity(event_list_view_, 0.f)
.SetOpacity(up_next_view_, 1.f)
.At(base::Milliseconds(0))
.SetDuration(kAnimationDurationForClosingEvents)
.SetOpacity(event_list_view_, 1.f)
.SetOpacity(up_next_view_, 0.f, gfx::Tween::EASE_IN);
}
}
void CalendarView::CloseEventList() {
// Don't allow the EventListView to close if an animation is
// occurring. It is ok to animate the EventListView if the animation cooldown
// is active.
if (IsAnimating()) {
return;
}
calendar_metrics::RecordEventListClosed();
// Updates `scroll_view_`'s accessible name without the selected date.
scroll_view_->GetViewAccessibility().SetName(
l10n_util::GetStringFUTF16(
IDS_ASH_CALENDAR_BUBBLE_ACCESSIBLE_DESCRIPTION,
calendar_utils::GetMonthDayYearWeek(
calendar_view_controller_->currently_shown_date())),
ax::mojom::NameFrom::kAttribute);
// Increase the scroll height before the animation starts, so that it's
// already full height when animating `event_list_view_` sliding down.
ClipScrollViewHeight(IsUpNextViewVisible() ? ScrollViewState::UP_NEXT_SHOWING
: ScrollViewState::FULL_HEIGHT);
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
calendar_view_controller_->set_is_date_cell_clickable(false);
// Move EventListView to the top of the up next view if showing, or off the
// bottom of the CalendarView.
const int previous_surface_y = calendar_sliding_surface_->y();
SetCalendarSlidingSurfaceBounds(IsUpNextViewVisible()
? BoundsType::UP_NEXT_VIEW_BOUNDS
: BoundsType::CALENDAR_BOTTOM_BOUNDS);
std::unique_ptr<ui::InterpolatedTranslation> list_view_sliding_down =
std::make_unique<ui::InterpolatedTranslation>(
gfx::PointF(0.f, previous_surface_y - calendar_sliding_surface_->y()),
gfx::PointF());
auto event_list_reporter = calendar_metrics::CreateAnimationReporter(
event_list_view_, kCloseEventListAnimationHistogram);
auto calendar_sliding_surface_reporter =
calendar_metrics::CreateAnimationReporter(
calendar_sliding_surface_,
kCloseEventListCalendarSlidingSurfaceAnimationHistogram);
is_event_list_close_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(&CalendarView::OnCloseEventListAnimationComplete,
weak_factory_.GetWeakPtr()))
.OnAborted(
base::BindOnce(&CalendarView::OnCloseEventListAnimationComplete,
weak_factory_.GetWeakPtr()))
.Once()
.SetDuration(kAnimationDurationForClosingEvents)
.SetInterpolatedTransform(calendar_sliding_surface_,
std::move(list_view_sliding_down),
gfx::Tween::FAST_OUT_SLOW_IN)
// Fade out the event list view.
.At(kEventListAnimationStartDelay)
.SetDuration(kAnimationDurationForClosingEvents)
.SetOpacity(event_list_view_, 0.f, gfx::Tween::FAST_OUT_SLOW_IN);
// If `up_next_view_` should be shown, fades out the up next view if the user
// has scrolled. Otherwise fades in.
if (!calendar_view_controller_->UpcomingEvents().empty()) {
user_has_scrolled_ ? FadeOutUpNextView() : FadeInUpNextView();
}
}
void CalendarView::OnSelectedDateUpdated() {
// If the event list is already open and the date cell is focused, moves the
// focusing ring to the close button.
if (event_list_view_ && IsDateCellViewFocused()) {
RequestFocusForEventListCloseButton();
}
}
void CalendarView::OnCalendarLoaded() {
// We might have some cached upcoming events so we can show the
// `up_next_view_` as soon as the calendar has loaded i.e. before waiting for
// the event fetch to complete. Don't display the up next view if user has
// scrolled.
MaybeShowUpNextView();
}
void CalendarView::ScrollUpOneMonth() {
is_scrolling_up_ = true;
calendar_view_controller_->UpdateMonth(
calendar_view_controller_->GetPreviousMonthFirstDayUTC(1));
content_view_->RemoveChildViewT(next_next_label_.get());
content_view_->RemoveChildViewT(next_next_month_.get());
next_next_label_ = next_label_;
next_next_month_ = next_month_;
next_label_ = current_label_;
next_month_ = current_month_;
current_label_ = previous_label_;
current_month_ = previous_month_;
previous_month_ =
AddMonth(calendar_view_controller_->GetPreviousMonthFirstDayUTC(1),
/*add_at_front=*/true);
if (IsDateCellViewFocused()) {
previous_month_->EnableFocus();
}
previous_label_ = AddLabelWithId(LabelType::PREVIOUS,
/*add_at_front=*/true);
// After adding a new month in the content, the current position stays the
// same but below the added view the each view's position has changed to
// [original position + new month's height]. So we need to add the height of
// the newly added month to keep the current view's position.
int added_height = previous_month_->GetPreferredSize().height() +
previous_label_->GetPreferredSize().height();
int position = added_height + scroll_view_->GetVisibleRect().y();
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(), position);
MaybeResetContentViewFocusBehavior();
if (current_month_->has_events()) {
calendar_view_controller_->EventsDisplayedToUser();
}
}
void CalendarView::ScrollDownOneMonth() {
is_scrolling_up_ = false;
// Renders the next month if the next month label is moving up and passing
// the top of the visible area, or the next month body's bottom is passing
// the bottom of the visible area.
int removed_height = previous_month_->GetPreferredSize().height() +
previous_label_->GetPreferredSize().height();
calendar_view_controller_->UpdateMonth(
calendar_view_controller_->GetNextMonthFirstDayUTC(1));
content_view_->RemoveChildViewT(previous_label_.get());
content_view_->RemoveChildViewT(previous_month_.get());
previous_label_ = current_label_;
previous_month_ = current_month_;
current_label_ = next_label_;
current_month_ = next_month_;
next_label_ = next_next_label_;
next_month_ = next_next_month_;
next_next_label_ = AddLabelWithId(LabelType::NEXTNEXT);
next_next_month_ = AddMonth(
calendar_view_controller_->GetNextMonthFirstDayUTC(/*num_months=*/2));
if (IsDateCellViewFocused()) {
next_next_month_->EnableFocus();
}
// Same as adding previous views. We need to remove the height of the
// deleted month to keep the current view's position.
int position = scroll_view_->GetVisibleRect().y() - removed_height;
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(), position);
MaybeResetContentViewFocusBehavior();
if (current_month_->has_events()) {
calendar_view_controller_->EventsDisplayedToUser();
}
}
void CalendarView::ScrollOneMonthAndAutoScroll(bool scroll_up) {
if (is_resetting_scroll_) {
return;
}
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
RestoreMonthStatus();
if (scroll_up) {
ScrollUpOneMonth();
} else {
ScrollDownOneMonth();
}
scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(),
GetPositionOfCurrentMonth());
// Starts to fade out `up_next_view_` after `scroll_view_` has been updated.
FadeOutUpNextView();
}
void CalendarView::ScrollOneMonthWithAnimation(bool scroll_up) {
user_has_scrolled_ = true;
is_scrolling_up_ = scroll_up;
if (event_list_view_) {
// If it is animating to open this `event_list_view_`, disable the up/down
// buttons.
if (!should_months_animate_ || !should_header_animate_) {
return;
}
ScrollOneRowWithAnimation(scroll_up);
return;
}
// If there's already an existing animation, restores each layer's visibility
// and position.
if (!should_months_animate_ || !should_header_animate_) {
set_should_months_animate(false);
set_should_header_animate(false);
RestoreHeadersStatus();
is_resetting_scroll_ = false;
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
ScrollOneMonthAndAutoScroll(scroll_up);
return;
}
if (is_resetting_scroll_) {
return;
}
// Starts to show the month and header animation.
SetShouldMonthsAnimateAndScrollEnabled(false);
set_should_header_animate(false);
gfx::Vector2dF moving_up_location = gfx::Vector2dF(
0, previous_month_->GetPreferredSize().height() +
current_label_->GetPreferredSize().height() +
(scroll_view_->GetVisibleRect().y() - current_month_->y()));
gfx::Vector2dF moving_down_location = gfx::Vector2dF(
0, -current_month_->GetPreferredSize().height() -
next_label_->GetPreferredSize().height() +
(scroll_view_->GetVisibleRect().y() - current_month_->y()));
gfx::Transform month_moving;
month_moving.Translate(scroll_up ? moving_up_location : moving_down_location);
const std::u16string temp_month =
scroll_up ? calendar_view_controller_->GetPreviousMonthName()
: calendar_view_controller_->GetNextMonthName();
const std::u16string temp_year = calendar_utils::GetYear(
scroll_up ? calendar_view_controller_->GetPreviousMonthFirstDayUTC(
/*num_months=*/1)
: calendar_view_controller_->GetNextMonthFirstDayUTC(
/*num_months=*/1));
gfx::Transform header_moving = GetHeaderMovingAndPrepareAnimation(
scroll_up, kHeaderViewScrollOneMonthAnimationHistogram, temp_month,
temp_year);
// Tracks animation smoothness. For now, we only track animation smoothness
// for 1 month and 1 label since all 3 month views and 3 label views are
// similar and perform the same animation. If this is not the case in the
// future, we should add additional metrics for the rest.
auto month_reporter = calendar_metrics::CreateAnimationReporter(
current_month_, kMonthViewScrollOneMonthAnimationHistogram);
auto label_reporter = calendar_metrics::CreateAnimationReporter(
current_label_, kLabelViewScrollOneMonthAnimationHistogram);
UpdateAnimationCrashKeys();
// Stop animating the views that will be involved in the month scroll
// animation. It handles the edge case that the calendar view's children get
// recreated by the abortion callback of the existing animation interrupted by
// the month scroll animation launched below.
StopViewLayerAnimation(current_label_);
StopViewLayerAnimation(current_month_);
StopViewLayerAnimation(header_);
StopViewLayerAnimation(next_label_);
StopViewLayerAnimation(next_month_);
StopViewLayerAnimation(next_next_label_);
StopViewLayerAnimation(next_next_month_);
StopViewLayerAnimation(previous_label_);
StopViewLayerAnimation(previous_month_);
StopViewLayerAnimation(temp_header_);
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnEnded(base::BindOnce(&CalendarView::OnScrollMonthAnimationComplete,
weak_factory_.GetWeakPtr(), scroll_up))
.OnAborted(base::BindOnce(&CalendarView::OnScrollMonthAnimationComplete,
weak_factory_.GetWeakPtr(), scroll_up))
.Once()
.SetDuration(calendar_utils::kAnimationDurationForMonthMoving)
.SetTransform(current_month_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(current_label_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(previous_month_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(previous_label_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_month_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_label_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_next_month_, month_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(next_next_label_, month_moving, gfx::Tween::EASE_OUT_2)
.At(kDelayHeaderAnimationDuration)
.SetDuration(calendar_utils::kAnimationDurationForMoving)
.SetTransform(header_, header_moving, gfx::Tween::EASE_OUT_2)
.SetTransform(temp_header_, gfx::Transform(), gfx::Tween::EASE_OUT_2)
.At(kDelayHeaderAnimationDuration)
.SetDuration(calendar_utils::kAnimationDurationForVisibility)
.SetOpacity(header_, 0.0f)
.At(kDelayHeaderAnimationDuration +
calendar_utils::kAnimationDurationForVisibility)
.SetDuration(calendar_utils::kAnimationDurationForVisibility)
.SetOpacity(temp_header_, 1.0f);
}
gfx::Transform CalendarView::GetHeaderMovingAndPrepareAnimation(
bool scroll_up,
const std::string& animation_name,
const std::u16string& temp_month,
const std::u16string& temp_year) {
const int header_height = header_->GetPreferredSize().height();
const gfx::Vector2dF header_moving_location =
gfx::Vector2dF(0, (header_height / 2) * (scroll_up ? 1 : -1));
gfx::Transform header_moving;
header_moving.Translate(header_moving_location);
// Tracks animation smoothness.
auto header_reporter =
calendar_metrics::CreateAnimationReporter(header_, animation_name);
// Update the temp header label with the new header's month and year.
temp_header_->UpdateHeaders(temp_month, temp_year);
temp_header_->layer()->SetOpacity(0.0f);
gfx::Transform initial_state;
initial_state.Translate(
gfx::Vector2dF(0, (header_height / 2) * (scroll_up ? -1 : 1)));
temp_header_->layer()->SetTransform(initial_state);
temp_header_->SetVisible(true);
return header_moving;
}
void CalendarView::ScrollOneRowWithAnimation(bool scroll_up) {
if (is_resetting_scroll_) {
return;
}
is_scrolling_up_ = scroll_up;
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
// Scrolls to the last row of the previous month if it's currently on the
// first row and scrolling up.
if (scroll_up && calendar_view_controller_->GetExpandedRowIndex() == 0) {
ScrollUpOneMonth();
SetExpandedRowThenDisableScroll(current_month_->last_row_index());
return;
}
// Scrolls to the first row of the next month if it's currently on the
// last row and scrolling down.
if (!scroll_up && calendar_view_controller_->GetExpandedRowIndex() ==
current_month_->last_row_index()) {
ScrollDownOneMonth();
SetExpandedRowThenDisableScroll(0);
return;
}
SetExpandedRowThenDisableScroll(
calendar_view_controller_->GetExpandedRowIndex() + (scroll_up ? -1 : 1));
return;
}
void CalendarView::OnEvent(ui::Event* event) {
// If it's animating, do not respond to any keyboard navigation for focus. All
// other keyboard event (e.g. keyboard shortcut to close the calendar view,
// shortcut to open the quick settings view, etc.) won't be affected.
if (IsAnimating()) {
event->StopPropagation();
return;
}
if (!event->IsKeyEvent()) {
GlanceableTrayChildBubble::OnEvent(event);
return;
}
auto* key_event = event->AsKeyEvent();
auto key_code = key_event->key_code();
auto* focus_manager = GetFocusManager();
bool is_tab_key_pressed =
key_event->type() == ui::EventType::kKeyPressed &&
views::FocusManager::IsTabTraversalKeyEvent(*key_event);
if (is_tab_key_pressed) {
RecordCalendarKeyboardNavigation(
calendar_metrics::CalendarKeyboardNavigationSource::kTab);
}
if (!IsDateCellViewFocused()) {
GlanceableTrayChildBubble::OnEvent(event);
return;
}
// When tab key is pressed, stops focusing on any `CalendarDateCellView` and
// goes to the next focusable button in the header.
if (is_tab_key_pressed) {
// Focus the whole scroll view so `focus_manager->AdvanceFocus()` moves the
// focus out of the scroll view, to the next view in the focus order. This
// avoids focusing the next date cell view.
scroll_view_->SetFocusBehavior(FocusBehavior::ALWAYS);
scroll_view_->RequestFocus();
current_month_->DisableFocus();
previous_month_->DisableFocus();
next_month_->DisableFocus();
next_next_month_->DisableFocus();
GlanceableTrayChildBubble::OnEvent(event);
// Should move the focus out of the scroll view (the whole scroll view
// temporarily grabbed focus in place of the initially focused date cell
// view).
focus_manager->AdvanceFocus(/*reverse=*/key_event->IsShiftDown());
scroll_view_->SetFocusBehavior(FocusBehavior::NEVER);
event->StopPropagation();
content_view_->SetFocusBehavior(FocusBehavior::ALWAYS);
return;
}
if (key_event->type() != ui::EventType::kKeyPressed ||
(key_code != ui::VKEY_UP && key_code != ui::VKEY_DOWN &&
key_code != ui::VKEY_LEFT && key_code != ui::VKEY_RIGHT)) {
GlanceableTrayChildBubble::OnEvent(event);
return;
}
switch (key_code) {
case ui::VKEY_UP:
case ui::VKEY_DOWN: {
RecordCalendarKeyboardNavigation(
calendar_metrics::CalendarKeyboardNavigationSource::kArrowKeys);
auto* current_focusable_view = focus_manager->GetFocusedView();
// Enable the scroll bar mode, in case it is disabled when the event list
// is showing.
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
// Moving 7 (`kDateInOneWeek`) steps will focus on the cell which is right
// above or below the current cell, since each row has 7 days.
for (int count = 0; count < calendar_utils::kDateInOneWeek; count++) {
auto* next_focusable_view = focus_manager->GetNextFocusableView(
current_focusable_view, GetWidget(),
/*reverse=*/key_code == ui::VKEY_UP,
/*dont_loop=*/false);
current_focusable_view = next_focusable_view;
// Sometimes the position of the upper row cells, which should be
// focused next, are above (and hidden behind) the header buttons. So
// this loop skips those buttons.
while (
current_focusable_view &&
!views::IsViewClass<CalendarDateCellView>(current_focusable_view)) {
current_focusable_view = focus_manager->GetNextFocusableView(
current_focusable_view, GetWidget(),
/*reverse=*/key_code == ui::VKEY_UP,
/*dont_loop=*/false);
}
}
focus_manager->SetFocusedView(current_focusable_view);
// After focusing on the new cell the view should have scrolled already
// if needed, but there's an offset compared with scrolled by
// `ScrollOneRowWithAnimation`. Manually scroll the view then disable the
// scroll bar mode if the even list is showing.
if (event_list_view_) {
const int current_height =
scroll_view_->GetVisibleRect().y() - GetPositionOfCurrentMonth();
SetExpandedRowThenDisableScroll(
current_height / calendar_view_controller_->row_height());
}
AdjustDateCellVoxBounds();
return;
}
case ui::VKEY_LEFT:
case ui::VKEY_RIGHT: {
RecordCalendarKeyboardNavigation(
calendar_metrics::CalendarKeyboardNavigationSource::kArrowKeys);
// Enable the scroll bar mode, in case it is disabled when the event list
// is showing.
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
bool is_reverse = base::i18n::IsRTL() ? key_code == ui::VKEY_RIGHT
: key_code == ui::VKEY_LEFT;
focus_manager->AdvanceFocus(/*reverse=*/is_reverse);
// After focusing on the new cell the view should have scrolled already
// if needed, but there's an offset compared with scrolled by
// `ScrollOneRowWithAnimation`. Manually scroll the view then disable the
// scroll bar mode if the even list is showing.
if (event_list_view_) {
const int current_height =
scroll_view_->GetVisibleRect().y() - GetPositionOfCurrentMonth();
SetExpandedRowThenDisableScroll(
current_height / calendar_view_controller_->row_height());
}
AdjustDateCellVoxBounds();
return;
}
default:
NOTREACHED();
}
}
void CalendarView::SetExpandedRowThenDisableScroll(int row_index) {
DCHECK(event_list_view_);
calendar_view_controller_->set_expanded_row_index(row_index);
const int row_height = calendar_view_controller_->GetExpandedRowIndex() *
calendar_view_controller_->row_height();
scroll_view_->ScrollToPosition(
scroll_view_->vertical_scroll_bar(),
GetPositionOfCurrentMonth() + row_height + GetExpandedCalendarPadding());
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
}
void CalendarView::OnContentsScrolled() {
base::AutoReset<bool> set_is_scrolling(&is_calendar_view_scrolling_, true);
// The scroll position is reset because it's adjusting the position when
// adding or removing views from the `scroll_view_`. It should scroll to the
// position we want, so we don't need to check the visible area position.
if (is_resetting_scroll_) {
return;
}
user_has_scrolled_ = true;
base::AutoReset<bool> disable_header_animation(&should_header_animate_,
false);
// Reset the timer to update the `on_screen_month_` map after scrolling.
scrolling_settled_timer_.Reset();
// Scrolls to the previous month if the current label is moving down and
// passing the top of the visible area.
if (scroll_view_->GetVisibleRect().y() <= current_label_->y()) {
ScrollUpOneMonth();
} else if (scroll_view_->GetVisibleRect().y() >= next_label_->y()) {
ScrollDownOneMonth();
}
// Fades out `up_next_view_` after the user has scrolled.
FadeOutUpNextView();
}
void CalendarView::OnMonthArrowButtonActivated(bool up,
const ui::Event& event) {
calendar_metrics::RecordMonthArrowButtonActivated(up, event);
ScrollOneMonthWithAnimation(up);
content_view_->OnMonthChanged();
}
void CalendarView::AdjustDateCellVoxBounds() {
auto* focused_view = GetFocusManager()->GetFocusedView();
DCHECK(views::IsViewClass<CalendarDateCellView>(focused_view));
// When the Chrome Vox focusing box is in a `ScrollView`, the hidden content
// height, which is `scroll_view_->GetVisibleRect().y()` should also be added.
// Otherwise the position of the Chrome Vox box is off.
gfx::Rect bounds = focused_view->GetBoundsInScreen();
focused_view->GetViewAccessibility().SetBounds(
gfx::RectF(bounds.x(), bounds.y() + scroll_view_->GetVisibleRect().y(),
bounds.width(), bounds.height()));
}
void CalendarView::OnScrollMonthAnimationComplete(bool scroll_up) {
set_should_header_animate(true);
SetShouldMonthsAnimateAndScrollEnabled(true);
ScrollOneMonthAndAutoScroll(scroll_up);
temp_header_->SetVisible(false);
header_->layer()->SetOpacity(1.0f);
header_->layer()->SetTransform(gfx::Transform());
}
void CalendarView::OnOpenEventListAnimationComplete() {
is_event_list_open_animation_running_ = false;
if (is_destroying_) {
return;
}
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
// Scrolls to the next month if the selected date is in the `next_month_`, so
// that the `current_month_`is updated to the next month.
if (!calendar_view_controller_->IsSelectedDateInCurrentMonth()) {
ScrollDownOneMonth();
}
// If still not in this month, it's in the `next_next_month_`. Doing this in a
// while loop may cause a potential infinite loop. For example when the time
// difference is not calculated or applied correctly, which may cause some
// dates cannot be found in the months.
if (!calendar_view_controller_->IsSelectedDateInCurrentMonth()) {
ScrollDownOneMonth();
}
base::AutoReset<bool> is_resetting_scrolling(&is_resetting_scroll_, true);
RestoreMonthStatus();
scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(),
GetPositionOfSelectedDate());
// If the selected date is not on the same row with todays date, the
// `scroll_view_` should scroll, and `user_has_scrolled_` should be true to
// hide the `up_next_view_` when the `event_list_view_` is closed.
if (!calendar_view_controller_->IsSelectedDateInCurrentMonth() ||
calendar_view_controller_->selected_date_row_index() !=
calendar_view_controller_->today_row() - 1) {
user_has_scrolled_ = true;
}
// Clip the height to a bit more than the height of a row.
ClipScrollViewHeight(ScrollViewState::EVENT_LIST_SHOWING);
if (up_next_view_) {
// Once the animation is complete, the `up_next_view_` needs to be invisible
// otherwise ChromeVox will pick it up.
up_next_view_->SetVisible(false);
}
if (!should_months_animate_) {
months_animation_restart_timer_.Reset();
}
calendar_view_controller_->set_is_date_cell_clickable(true);
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
calendar_view_controller_->OnEventListOpened();
// Moves focusing ring to the close button of the event list if it's opened
// from the date cell view focus or from the `up_next_view_`.
if (IsDateCellViewFocused() || up_next_view_) {
RequestFocusForEventListCloseButton();
}
up_button_->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_CALENDAR_UP_BUTTON_EVENT_LIST_ACCESSIBLE_DESCRIPTION));
down_button_->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_CALENDAR_DOWN_BUTTON_EVENT_LIST_ACCESSIBLE_DESCRIPTION));
}
void CalendarView::OnCloseEventListAnimationComplete() {
is_event_list_close_animation_running_ = false;
if (is_destroying_) {
return;
}
// GetFocusManager() can be nullptr if `CalendarView` is destroyed when the
// closing animation hasn't finished.
auto* focused_view =
GetFocusManager() ? GetFocusManager()->GetFocusedView() : nullptr;
// Restore focus before removing `event_list_view_`. This is necessary because
// showing `event_list_view_` scrolls the `scroll_view_` with custom padding
// which is hard to detect after the fact. If `event_list_view_` doesn't
// exist, it's not clear the padding exists, and this can result in the wrong
// CalendarDateCellView being focused.
if (focused_view && Contains(focused_view)) {
FocusPreferredDateCellViewOrFirstVisible(/*prefer_today=*/false);
}
calendar_sliding_surface_->RemoveChildViewT(event_list_view_.get());
event_list_view_ = nullptr;
calendar_view_controller_->OnEventListClosed();
calendar_view_controller_->set_is_date_cell_clickable(true);
MaybeShowUpNextView();
up_button_->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_CALENDAR_UP_BUTTON_ACCESSIBLE_DESCRIPTION));
down_button_->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_CALENDAR_DOWN_BUTTON_ACCESSIBLE_DESCRIPTION));
}
void CalendarView::RequestFocusForEventListCloseButton() {
DCHECK(event_list_view_);
event_list_view_->RequestCloseButtonFocus();
current_month_->DisableFocus();
previous_month_->DisableFocus();
next_month_->DisableFocus();
next_next_month_->DisableFocus();
content_view_->SetFocusBehavior(FocusBehavior::ALWAYS);
}
void CalendarView::OnResetToTodayAnimationComplete() {
is_reset_to_today_animation_running_ = false;
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/true);
ResetToToday();
FadeInCurrentMonth();
// There's a corner case when the `current_month_` doesn't change,
// the `on_screen_month_` map won't be updated since
// `OnMonthChanged` won't be called and the timer won't be reset. So
// we manually call the timer to update `on_screen_month_`.
reset_scrolling_settled_timer();
}
void CalendarView::OnResetToTodayFadeInAnimationComplete() {
is_reset_to_today_fade_in_animation_running_ = false;
set_should_header_animate(true);
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/true);
scroll_view_->SetVerticalScrollBarMode(
event_list_view_ ? views::ScrollView::ScrollBarMode::kDisabled
: views::ScrollView::ScrollBarMode::kHiddenButEnabled);
SetHeaderAndContentViewOpacity(/*opacity=*/1.0f);
// Resets `user_has_scrolled_` and `check_upcoming_events_timer_` since after
// resetting to today, `up_next_view_` state should be reset.
user_has_scrolled_ = false;
check_upcoming_events_timer_.Reset();
MaybeShowUpNextView();
}
void CalendarView::FocusPreferredDateCellViewOrFirstVisible(bool prefer_today) {
previous_month_->EnableFocus();
current_month_->EnableFocus();
next_month_->EnableFocus();
next_next_month_->EnableFocus();
CalendarDateCellView* to_be_focused_cell =
GetTargetDateCellViewOrFirstFocusable(
prefer_today ? calendar_view_controller_->todays_date_cell_view()
: calendar_view_controller_->selected_date_cell_view());
if (to_be_focused_cell) {
to_be_focused_cell->SetFirstOnFocusedAccessibilityLabel();
GetFocusManager()->SetFocusedView(to_be_focused_cell);
} else {
// If there's no visible row of the current month on the screen, focus on
// the first visible non-grayed-out date of the next month.
GetFocusManager()->SetFocusedView(next_month_->focused_cells().front());
}
AdjustDateCellVoxBounds();
content_view_->SetFocusBehavior(FocusBehavior::NEVER);
}
CalendarDateCellView* CalendarView::GetTargetDateCellViewOrFirstFocusable(
CalendarDateCellView* target_date_cell_view) {
// When focusing on the `content_view_`, we decide which is the to-be-focused
// cell based on the current position.
const int visible_window_y_in_content_view =
scroll_view_->GetVisibleRect().y();
const int row_height = calendar_view_controller_->row_height();
// Check whether at least one row of the current month is visible on the
// screen. The to-be-focused cell should be the first non-grayed date cell
// that is visible, or today's cell if today is in the current month and
// visible.
if (visible_window_y_in_content_view >=
(next_label_->y() - row_height - kMonthVerticalPadding -
kLabelVerticalPadding)) {
return nullptr;
}
const int first_visible_row = CalculateFirstFullyVisibleRow();
if (target_date_cell_view &&
(current_month_ == target_date_cell_view->parent()) &&
(first_visible_row <= target_date_cell_view->row_index())) {
return target_date_cell_view;
}
return current_month_->focused_cells()[first_visible_row];
}
int CalendarView::CalculateFirstFullyVisibleRow() {
const int visible_window_y_in_content_view =
scroll_view_->GetVisibleRect().y();
int row_index = 0;
// Get first visible row index. If `event_list_view_` is showing, account
// for the extra padding added to `scroll_view_`'s visible window.
while (visible_window_y_in_content_view >
(GetPositionOfCurrentMonth() +
row_index * calendar_view_controller_->row_height() +
(event_list_view_ ? GetExpandedCalendarPadding() : 0))) {
++row_index;
if (row_index > kMaxRowsInOneMonth) {
NOTREACHED() << "CalendarMonthView's cannot have more than "
<< kMaxRowsInOneMonth << " rows.";
}
}
return row_index;
}
void CalendarView::SetCalendarSlidingSurfaceBounds(BoundsType type) {
const int x_position = scroll_view_->x() + kEventListViewHorizontalOffset;
const int width = scroll_view_->GetVisibleRect().width() -
kEventListViewHorizontalOffset * 2;
const int event_list_view_height = GetBoundsInScreen().bottom() -
scroll_view_->GetBoundsInScreen().y() -
GetSingleVisibleRowHeight();
switch (type) {
// If the event list view is showing, position the calendar sliding surface
// where the opened event list view will be.
case BoundsType::EVENT_LIST_VIEW_BOUNDS: {
calendar_sliding_surface_->SetBounds(
x_position, scroll_view_->y() + GetSingleVisibleRowHeight(), width,
event_list_view_height);
break;
}
// If the event list view is not showing and the up next view is showing,
// position the calendar sliding surface where the up next view will be.
case BoundsType::UP_NEXT_VIEW_BOUNDS: {
const int up_next_view_preferred_height =
up_next_view_->GetPreferredSize().height();
calendar_sliding_surface_->SetBounds(
x_position, GetLocalBounds().bottom() - up_next_view_preferred_height,
width, event_list_view_height);
break;
}
// If neither event list nor up next are showing, position the calendar
// sliding surface off the bottom of the screen.
case BoundsType::CALENDAR_BOTTOM_BOUNDS: {
calendar_sliding_surface_->SetBounds(x_position,
GetVisibleBounds().bottom(), width,
event_list_view_height);
break;
}
}
}
void CalendarView::MaybeShowUpNextView() {
if (calendar_view_controller_->UpcomingEvents().empty()) {
RemoveUpNextView();
return;
}
if (user_has_scrolled_) {
return;
}
// If the `event_list_view_` is currently showing, then early return.
// `event_list_view_` should be checked before `up_next_view_` since if
// `event_list_view_` is showing, we don't want to refresh or fade in the
// `up_next_view_`.
if (event_list_view_) {
return;
}
if (up_next_view_) {
up_next_view_->RefreshEvents();
// If `up_next_view_` is invisible (i.e. has faded out), fade it in to make
// it visible.
if (!up_next_view_->GetVisible()) {
FadeInUpNextView();
}
return;
}
MaybeCreateUpNextView();
// Sets the visibility to manually trigger the fade in animation.
up_next_view_->SetVisible(false);
FadeInUpNextView();
}
void CalendarView::RemoveUpNextView() {
if (!up_next_view_) {
return;
}
// Sets the `up_next_view_` to be invisible instead of removing it.
up_next_view_->SetVisible(false);
SetCalendarSlidingSurfaceBounds(event_list_view_
? BoundsType::EVENT_LIST_VIEW_BOUNDS
: BoundsType::CALENDAR_BOTTOM_BOUNDS);
ClipScrollViewHeight(event_list_view_ ? ScrollViewState::EVENT_LIST_SHOWING
: ScrollViewState::FULL_HEIGHT);
}
void CalendarView::OpenEventListForTodaysDate() {
calendar_metrics::RecordEventListForTodayActivated();
const auto upcoming_events = calendar_view_controller_->UpcomingEvents();
const base::Time upcoming_event_start_time =
!upcoming_events.empty() ? upcoming_events.back().start_time().date_time()
: base::Time::Now();
if (!current_month_->has_today()) {
ResetToToday();
}
calendar_view_controller_->ShowEventListView(
/*selected_calendar_date_cell_view=*/calendar_view_controller_
->todays_date_cell_view(),
/*selected_date=*/upcoming_event_start_time,
/*row_index=*/calendar_view_controller_->today_row() - 1);
}
void CalendarView::ClipScrollViewHeight(ScrollViewState state_to_change_to) {
switch (state_to_change_to) {
case ScrollViewState::FULL_HEIGHT:
scroll_view_->ClipHeightTo(0, INT_MAX);
break;
case ScrollViewState::UP_NEXT_SHOWING:
scroll_view_->ClipHeightTo(
0, GetBoundsInScreen().bottom() -
scroll_view_->GetBoundsInScreen().y() -
up_next_view_->GetPreferredSize().height() +
calendar_utils::kUpNextOverlapInPx);
break;
case ScrollViewState::EVENT_LIST_SHOWING:
scroll_view_->ClipHeightTo(0, GetSingleVisibleRowHeight());
break;
}
}
void CalendarView::FadeInUpNextView() {
// If `up_next_view_` is already visible, don't perform the animation again.
if (IsUpNextViewVisible()) {
return;
}
MaybeCreateUpNextView();
// Disables scrolling when `up_next_view_` is animating.
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/false);
calendar_view_controller_->set_is_date_cell_clickable(false);
ClipScrollViewHeight(ScrollViewState::UP_NEXT_SHOWING);
// Sets the `calendar_sliding_surface_` bounds to be at the animation end
// position to animate properly.
SetCalendarSlidingSurfaceBounds(BoundsType::UP_NEXT_VIEW_BOUNDS);
// Translate the `up_next_view_` off the screen and animate sliding up.
std::unique_ptr<ui::InterpolatedTranslation> up_next_sliding_up =
std::make_unique<ui::InterpolatedTranslation>(
gfx::PointF(0.f, calendar_sliding_surface_->y()), gfx::PointF());
up_next_view_->SetVisible(true);
auto up_next_view_reporter = calendar_metrics::CreateAnimationReporter(
up_next_view_, kFadeInUpNextViewAnimationHistogram);
is_fade_in_up_next_view_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnAborted(base::BindOnce(&CalendarView::OnFadeInUpNextViewAnimationEnded,
weak_factory_.GetWeakPtr()))
.OnEnded(base::BindOnce(&CalendarView::OnFadeInUpNextViewAnimationEnded,
weak_factory_.GetWeakPtr()))
.Once()
.SetOpacity(up_next_view_, 0.f)
.At(kUpNextAnimationStartDelay)
.SetDuration(kAnimationDurationForClosingEvents)
.SetOpacity(up_next_view_, 1.f)
.SetInterpolatedTransform(calendar_sliding_surface_,
std::move(up_next_sliding_up),
gfx::Tween::FAST_OUT_SLOW_IN_2);
}
void CalendarView::FadeOutUpNextView() {
// Ensure the height of `scroll_view_` is updated before performing the
// animation. There's a corner case that after closing the `event_list_view_`,
// `up_next_view_` is still invisible when this method is called, and will
// skip the animation. So we need to clip the height here to show the
// `scroll_view_` properly.
ClipScrollViewHeight(ScrollViewState::FULL_HEIGHT);
// If `up_next_view_` is already invisible, don't perform the animation again.
if (!IsUpNextViewVisible()) {
return;
}
// Disables scrolling when `up_next_view_` is animating.
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/false);
// Moves `up_next_view_` off the bottom of the `CalendarView`. Sets the
// `calendar_sliding_surface_` bounds to be at the animation end position to
// animate properly.
const int previous_surface_y = calendar_sliding_surface_->y();
SetCalendarSlidingSurfaceBounds(BoundsType::CALENDAR_BOTTOM_BOUNDS);
std::unique_ptr<ui::InterpolatedTranslation> up_next_view_sliding_down =
std::make_unique<ui::InterpolatedTranslation>(
gfx::PointF(0.f, previous_surface_y - calendar_sliding_surface_->y()),
gfx::PointF());
auto up_next_reporter = calendar_metrics::CreateAnimationReporter(
up_next_view_, kFadeOutUpNextViewAnimationHistogram);
is_fade_out_up_next_view_animation_running_ = true;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.OnAborted(
base::BindOnce(&CalendarView::OnFadeOutUpNextViewAnimationEnded,
weak_factory_.GetWeakPtr()))
.OnEnded(base::BindOnce(&CalendarView::OnFadeOutUpNextViewAnimationEnded,
weak_factory_.GetWeakPtr()))
.Once()
.SetOpacity(up_next_view_, 1.f)
.At(base::Milliseconds(0))
.SetDuration(kAnimationDurationForClosingEvents)
.SetOpacity(up_next_view_, 0.f)
.SetInterpolatedTransform(calendar_sliding_surface_,
std::move(up_next_view_sliding_down),
gfx::Tween::FAST_OUT_SLOW_IN);
// Stops the `check_upcoming_events_timer_` when `up_next_view_` starts fading
// out. This is needed when the `up_next_view_` fades out. The timer is
// stopped so that we don't check if we still want the `up_next_view_` to be
// back until the user presses the `reset_to_today_button_` and restarts the
// timer.
StopUpNextTimer();
}
void CalendarView::OnFadeInUpNextViewAnimationEnded() {
is_fade_in_up_next_view_animation_running_ = false;
// `user_has_scrolled_` should always be false since this method is only
// called in `FadeInUpNextView()` which is only called when
// `user_has_scrolled_` is false.
DCHECK(!user_has_scrolled_);
// `todays_date_cell_view()` should not be a nullptr since we'll only fade in
// `up_next_view_` when it's on today's month.
DCHECK(calendar_view_controller_->todays_date_cell_view());
// Resets the scrolling state after the animation completes.
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/true);
}
void CalendarView::OnFadeOutUpNextViewAnimationEnded() {
is_fade_out_up_next_view_animation_running_ = false;
up_next_view_->SetVisible(false);
SetShouldMonthsAnimateAndScrollEnabled(/*enabled=*/true);
}
void CalendarView::StopUpNextTimer() {
if (check_upcoming_events_timer_.IsRunning()) {
check_upcoming_events_timer_.Stop();
}
}
bool CalendarView::IsUpNextViewVisible() const {
return up_next_view_ && up_next_view_->GetVisible();
}
void CalendarView::UpdateAnimationCrashKeys() {
static AnimatingCrashKey event_list_close_animation_key("event_list_close");
UpdateCachedAnimatingState(event_list_close_animation_key,
is_event_list_close_animation_running_);
static AnimatingCrashKey event_list_open_animation_key("event_list_open");
UpdateCachedAnimatingState(event_list_open_animation_key,
is_event_list_open_animation_running_);
static AnimatingCrashKey fade_in_up_next_view_animation_key(
"fade_in_up_next_view");
UpdateCachedAnimatingState(fade_in_up_next_view_animation_key,
is_fade_in_up_next_view_animation_running_);
static AnimatingCrashKey fade_out_up_next_view_animation_key(
"fade_out_up_next_view");
UpdateCachedAnimatingState(fade_out_up_next_view_animation_key,
is_fade_out_up_next_view_animation_running_);
static AnimatingCrashKey header_animation_key("header");
UpdateCachedAnimatingState(header_animation_key,
is_header_animation_running_);
static AnimatingCrashKey reset_to_today_animation_key("reset_to_today");
UpdateCachedAnimatingState(reset_to_today_animation_key,
is_reset_to_today_animation_running_);
static AnimatingCrashKey reset_to_today_fade_in_animation_key(
"reset_to_today_fade_in");
UpdateCachedAnimatingState(reset_to_today_fade_in_animation_key,
is_reset_to_today_fade_in_animation_running_);
}
BEGIN_METADATA(CalendarView)
END_METADATA
} // namespace ash
|