File: glic_tab_underline_view.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1014 lines) | stat: -rw-r--r-- 38,401 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/glic/browser_ui/glic_tab_underline_view.h"

#include <math.h>

#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/glic/browser_ui/theme_util.h"
#include "chrome/browser/glic/host/context/glic_tab_data.h"
#include "chrome/browser/glic/public/context/glic_sharing_manager.h"
#include "chrome/browser/glic/public/glic_keyed_service.h"
#include "chrome/browser/glic/public/glic_keyed_service_factory.h"
#include "chrome/browser/glic/resources/grit/glic_browser_resources.h"
#include "chrome/browser/glic/widget/glic_window_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/alert/tab_alert.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_features.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/context_factory.h"
#include "content/public/browser/gpu_data_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/color_parser.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/view_class_properties.h"

namespace glic {
namespace {

// The amount of time for the opacity to go from 0 to 1.
constexpr static base::TimeDelta kOpacityRampUpDuration =
    base::Milliseconds(500);
// The amount of time for the opacity to go from 0 to 1 in a fast ramp up.
constexpr static base::TimeDelta kFastOpacityRampUpDuration =
    base::Milliseconds(200);
// The amount of time for the opacity to go from 1 to 0.
constexpr static base::TimeDelta kOpacityRampDownDuration =
    base::Milliseconds(200);
// The amount of time for the underline emphasis to go from 0 the max.
constexpr static base::TimeDelta kEmphasisRampUpDuration =
    base::Milliseconds(500);
// The amount of time for the underline emphasis to go from max to 0.
constexpr static base::TimeDelta kEmphasisRampDownDuration =
    base::Milliseconds(1000);
// The amount of time for the underline to stay emphasized.
constexpr static base::TimeDelta kEmphasisDuration = base::Milliseconds(1500);
// Time since creation will roll over after this time to prevent growing
// indefinitely.
constexpr static base::TimeDelta kMaxTime = base::Hours(1);

float ClampAndInterpolate(gfx::Tween::Type type,
                          float t,
                          float low,
                          float high) {
  float clamp_lo = std::min(low, high);
  float clamp_hi = std::max(low, high);
  float clamped = std::clamp(t, clamp_lo, clamp_hi);
  // Interpolate `clamped` within [low, high], using the function `type`.
  double calculated = gfx::Tween::CalculateValue(type, clamped);
  // Linear project `calculated` onto [low, high].
  return gfx::Tween::FloatValueBetween(calculated, low, high);
}

int64_t TimeTicksToMicroseconds(base::TimeTicks tick) {
  return (tick - base::TimeTicks()).InMicroseconds();
}

std::vector<SkColor> GetParameterizedColors() {
  std::vector<SkColor> colors;
  if (base::FeatureList::IsEnabled(features::kGlicParameterizedShader)) {
    std::vector<std::string> unparsed_colors =
        base::SplitString(::features::kGlicParameterizedShaderColors.Get(), "#",
                          base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
    for (const auto& unparsed : unparsed_colors) {
      SkColor result;
      if (!content::ParseHexColorString("#" + unparsed, &result)) {
        return std::vector<SkColor>();
      }
      colors.push_back(result);
    }
  }
  return colors;
}

std::vector<float> GetParameterizedFloats() {
  std::vector<float> floats;
  if (base::FeatureList::IsEnabled(features::kGlicParameterizedShader)) {
    std::vector<std::string> unparsed_floats =
        base::SplitString(::features::kGlicParameterizedShaderFloats.Get(), "#",
                          base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
    for (const auto& unparsed : unparsed_floats) {
      double result;
      if (!base::StringToDouble(unparsed, &result)) {
        return std::vector<float>();
      }
      floats.push_back(static_cast<float>(result));
    }
  }
  return floats;
}

}  // namespace

GlicTabUnderlineView::Factory* GlicTabUnderlineView::Factory::factory_ =
    nullptr;

std::unique_ptr<GlicTabUnderlineView> GlicTabUnderlineView::Factory::Create(
    Browser* browser,
    Tab* tab) {
  if (factory_) [[unlikely]] {
    return factory_->CreateUnderlineView(browser, tab);
  }
  return base::WrapUnique(
      new GlicTabUnderlineView(browser, tab, /*tester=*/nullptr));
}

// The following logic makes many references to "pinned" tabs. All of these
// refer to tabs that are selected to be shared with Gemini under the glic
// multitab feature. This is different from the older existing notion of
// "pinned" tabs in the tabstrip, which is the UI treatment that fixes a Tab
// view to one side with a reduced visual. Separate terminology should be used
// for the glic multitab concept in order to disambiguate, but landed code
// already adopts the "pinning" term and so that continues to be used here.
// TODO(crbug.com/433131600): update glic multitab sharing code to use less
// conflicting terminology.
class GlicTabUnderlineView::UnderlineViewUpdater
    : public GlicWindowController::StateObserver {
 public:
  UnderlineViewUpdater(Browser* browser, GlicTabUnderlineView* underline_view)
      : underline_view_(underline_view), browser_(browser) {
    auto* glic_service = GetGlicKeyedService();
    GlicSharingManager& sharing_manager = glic_service->sharing_manager();

    // Subscribe to changes in the focused tab.
    focus_change_subscription_ =
        sharing_manager.AddFocusedTabChangedCallback(base::BindRepeating(
            &GlicTabUnderlineView::UnderlineViewUpdater::OnFocusedTabChanged,
            base::Unretained(this)));

    // Subscribe to changes in the context access indicator status.
    indicator_change_subscription_ =
        glic_service->AddContextAccessIndicatorStatusChangedCallback(
            base::BindRepeating(&GlicTabUnderlineView::UnderlineViewUpdater::
                                    OnIndicatorStatusChanged,
                                base::Unretained(this)));

    // Subscribe to changes in the set of pinned tabs.
    pinned_tabs_change_subscription_ =
        sharing_manager.AddPinnedTabsChangedCallback(base::BindRepeating(
            &GlicTabUnderlineView::UnderlineViewUpdater::OnPinnedTabsChanged,
            base::Unretained(this)));

    // Observe changes in the floaty state.
    glic_service->window_controller().AddStateObserver(this);

    // Subscribe to when new requests are made by glic.
    user_input_submitted_subscription_ =
        glic_service->AddUserInputSubmittedCallback(base::BindRepeating(
            &GlicTabUnderlineView::UnderlineViewUpdater::OnUserInputSubmitted,
            base::Unretained(this)));
  }
  UnderlineViewUpdater(const UnderlineViewUpdater&) = delete;
  UnderlineViewUpdater& operator=(const UnderlineViewUpdater&) = delete;
  ~UnderlineViewUpdater() override {
    GetGlicKeyedService()->window_controller().RemoveStateObserver(this);
  }

  // Called when the focused tab changes with the focused tab data object.
  // This code interprets the tab data to determine how underline_view_'s tab
  // was involved.
  void OnFocusedTabChanged(const FocusedTabData& focused_tab_data) {
    tabs::TabInterface* tab = focused_tab_data.focus();
    auto* previous_focus = glic_current_focused_contents_.get();

    if (tab) {
      glic_current_focused_contents_ = tab->GetContents()->GetWeakPtr();
    } else {
      glic_current_focused_contents_.reset();
    }
    auto* current_focus = glic_current_focused_contents_.get();

    base::WeakPtr<content::WebContents> underline_contents;
    if (auto tab_interface = GetTabInterface()) {
      underline_contents = tab_interface->GetContents()->GetWeakPtr();
    } else {
      return;
    }

    bool focus_changed = previous_focus != current_focus;
    bool tab_switch =
        previous_focus && glic_current_focused_contents_ && focus_changed;
    bool this_tab_gained_focus =
        (underline_contents.get() == current_focus) && focus_changed;
    bool this_tab_lost_focus =
        (underline_contents.get() == previous_focus) && focus_changed;

    bool window_gained_focus =
        !previous_focus && glic_current_focused_contents_;
    bool window_lost_focus = previous_focus && !glic_current_focused_contents_;

    if (tab_switch) {
      if (this_tab_gained_focus) {
        UpdateUnderlineView(
            UpdateUnderlineReason::kFocusedTabChanged_TabGainedFocus);
      } else if (this_tab_lost_focus) {
        UpdateUnderlineView(
            UpdateUnderlineReason::kFocusedTabChanged_TabLostFocus);
      } else {
        UpdateUnderlineView(
            UpdateUnderlineReason::kFocusedTabChanged_NoFocusChange);
      }
    } else {
      if (window_gained_focus) {
        UpdateUnderlineView(
            UpdateUnderlineReason::kFocusedTabChanged_ChromeGainedFocus);
      } else if (window_lost_focus) {
        UpdateUnderlineView(
            UpdateUnderlineReason::kFocusedTabChanged_ChromeLostFocus);
      }
    }
  }

  // Called when the client changes the context access indicator status. This
  // happens when the sharing control in the floaty is toggled, and implicitly
  // when floaty is [back/fore]grounded while sharing is on.
  void OnIndicatorStatusChanged(bool enabled) {
    if (context_access_indicator_enabled_ == enabled) {
      return;
    }
    context_access_indicator_enabled_ = enabled;
    UpdateUnderlineView(
        context_access_indicator_enabled_
            ? UpdateUnderlineReason::kContextAccessIndicatorOn
            : UpdateUnderlineReason::kContextAccessIndicatorOff);
  }

  // Called when the glic set of pinned tabs changes.
  void OnPinnedTabsChanged(
      const std::vector<content::WebContents*>& pinned_contents) {
    if (!GetTabInterface()) {
      // If the TabInterface is invalid at this point, there is no relevant UI
      // to handle.
      return;
    }

    // Triggering is handled based on whether the tab is in the pinned set.
    if (IsUnderlineTabPinned()) {
      UpdateUnderlineView(
          UpdateUnderlineReason::kPinnedTabsChanged_TabInPinnedSet);
      return;
    }
    UpdateUnderlineView(
        UpdateUnderlineReason::kPinnedTabsChanged_TabNotInPinnedSet);
  }

  // The glic panel state must be separately observed because underlines of
  // pinned tabs uniquely respond to showing/hiding of the glic panel.
  void PanelStateChanged(const glic::mojom::PanelState& panel_state,
                         Browser*) override {
    UpdateUnderlineView(
        panel_state.kind == mojom::PanelState::Kind::kHidden
            ? UpdateUnderlineReason::kPanelStateChanged_PanelHidden
            : UpdateUnderlineReason::kPanelStateChanged_PanelShowing);
  }

  void OnUserInputSubmitted() {
    UpdateUnderlineView(UpdateUnderlineReason::kUserInputSubmitted);
  }

 private:
  // Types of updates to the tab underline UI effect given changes in relevant
  // triggering signals, including tab focus, glic sharing controls, pinned tabs
  // and the floaty panel.
  enum class UpdateUnderlineReason {
    kContextAccessIndicatorOn = 0,
    kContextAccessIndicatorOff,

    // Tab focus change not involving this underline.
    kFocusedTabChanged_NoFocusChange,
    // This underline's tab gained focus.
    kFocusedTabChanged_TabGainedFocus,
    // This underline's tab lost focus.
    kFocusedTabChanged_TabLostFocus,

    kFocusedTabChanged_ChromeGainedFocus,
    kFocusedTabChanged_ChromeLostFocus,

    // Chanes were made to the set of pinned of tabs.
    kPinnedTabsChanged_TabInPinnedSet,
    kPinnedTabsChanged_TabNotInPinnedSet,

    // Events related to the glic panel's state.
    kPanelStateChanged_PanelShowing,
    kPanelStateChanged_PanelHidden,

    kUserInputSubmitted,
  };

  GlicKeyedService* GetGlicKeyedService() {
    return GlicKeyedServiceFactory::GetGlicKeyedService(browser_->GetProfile());
  }

  // Returns the TabInterface corresponding to `underline_view_`, if it is
  // valid.
  base::WeakPtr<tabs::TabInterface> GetTabInterface() {
    if (underline_view_ && underline_view_->tab_) {
      if (auto tab_interface = underline_view_->tab_->data().tab_interface) {
        return tab_interface;
      }
    }
    return nullptr;
  }

  bool IsUnderlineTabPinned() {
    if (auto tab_interface = GetTabInterface()) {
      if (auto* glic_service = GetGlicKeyedService()) {
        return glic_service->sharing_manager().IsTabPinned(
            tab_interface->GetHandle());
      }
    }
    return false;
  }

  bool IsUnderlineTabSharedThroughActiveFollow() {
    if (auto tab_interface = GetTabInterface()) {
      if (auto* glic_service = GetGlicKeyedService()) {
        return (glic_service->sharing_manager().GetFocusedTabData().focus() ==
                tab_interface.get()) &&
               context_access_indicator_enabled_;
      }
    }
    return false;
  }

  // Trigger the necessary UI effect, primarily based on the given
  // `UpdateUnderlineReason` and whether or not `underline_view_`'s tab is
  // being shared via pinning or active following.
  void UpdateUnderlineView(UpdateUnderlineReason reason) {
    AddReasonForDebugging(reason);
    auto reasons_string = UpdateReasonsToString();
    SCOPED_CRASH_KEY_STRING1024("crbug-398319435", "update_reasons",
                                reasons_string);
    SCOPED_CRASH_KEY_BOOL("crbug-398319435", "access_indicator",
                          context_access_indicator_enabled_);
    SCOPED_CRASH_KEY_BOOL("crbug-398319435", "glic_focused_contents",
                          !!glic_current_focused_contents_);
    SCOPED_CRASH_KEY_BOOL("crbug-398319435", "is_glic_window_showing",
                          IsGlicWindowShowing());

    switch (reason) {
      case UpdateUnderlineReason::kContextAccessIndicatorOn: {
        // Active follow tab underline should be newly shown, pinned tabs should
        // re-animate or be newly shown if not already visible.
        if (IsUnderlineTabSharedThroughActiveFollow()) {
          ShowAndAnimateUnderline();
        }
        ShowOrAnimatePinnedUnderline();
        break;
      }
      case UpdateUnderlineReason::kContextAccessIndicatorOff: {
        // Underline should be hidden, with exception to pinned tabs while the
        // glic panel remains open.
        if (IsUnderlineTabPinned() && IsGlicWindowShowing()) {
          break;
        }
        HideUnderline();
        break;
      }
      case UpdateUnderlineReason::kFocusedTabChanged_NoFocusChange: {
        // Pinned tab underlines should re-animate if active follow sharing is
        // on.
        if (context_access_indicator_enabled_ && IsUnderlineTabPinned()) {
          AnimateUnderline();
        }
        break;
      }
      case UpdateUnderlineReason::kFocusedTabChanged_TabGainedFocus: {
        // Underline visibility corresponds to the focused tab during active
        // follow. Pinned tabs should not react as the set of shared tabs has
        // not changed.
        if (IsUnderlineTabSharedThroughActiveFollow()) {
          ShowAndAnimateUnderline();
        }
        break;
      }
      case UpdateUnderlineReason::kFocusedTabChanged_TabLostFocus: {
        // Underline visibility corresponds to the focused tab during active
        // follow. Pinned tabs should re-animate if the set of shared tabs has
        // changed
        if (IsUnderlineTabPinned() && context_access_indicator_enabled_) {
          AnimateUnderline();
        } else if (!IsUnderlineTabPinned()) {
          HideUnderline();
        }
        break;
      }
      case UpdateUnderlineReason::kFocusedTabChanged_ChromeGainedFocus:
        // Active follow tab underline should be newly shown, pinned tabs should
        // re-animate or be newly shown if not already visible.
        if (IsUnderlineTabSharedThroughActiveFollow()) {
          ShowAndAnimateUnderline();
        }
        ShowOrAnimatePinnedUnderline();
        break;
      case UpdateUnderlineReason::kFocusedTabChanged_ChromeLostFocus:
        // Underline should be hidden, with exception to pinned tabs.
        if (!IsUnderlineTabPinned()) {
          HideUnderline();
        }
        break;
      case UpdateUnderlineReason::kPinnedTabsChanged_TabInPinnedSet:
        // If `underline_view_` is not visible, then this tab was just added to
        // the set of pinned tabs.
        if (!underline_view_->IsShowing()) {
          // Pinned tab underlines should only be visible while the glic panel
          // is open.
          if (IsGlicWindowShowing()) {
            ShowAndAnimateUnderline();
          }
        } else {
          // This tab was already pinned - re-animate to reflect the change in
          // the set of pinned tabs.
          AnimateUnderline();
        }
        break;
      case UpdateUnderlineReason::kPinnedTabsChanged_TabNotInPinnedSet:
        // Re-animate to reflect the change in the set of pinned tabs.
        if (IsUnderlineTabSharedThroughActiveFollow()) {
          AnimateUnderline();
          return;
        }
        // This tab may have just been removed from the pinned set.
        HideUnderline();
        break;
      case UpdateUnderlineReason::kPanelStateChanged_PanelShowing:
        // Visibility of underlines of pinned tabs should follow visibility of
        // the glic panel.
        if (IsUnderlineTabPinned()) {
          ShowAndAnimateUnderline();
        }
        break;
      case UpdateUnderlineReason::kPanelStateChanged_PanelHidden:
        // Visibility of underlines of pinned tabs should follow visibility of
        // the glic panel.
        if (IsUnderlineTabPinned()) {
          HideUnderline();
        }
        break;
      case UpdateUnderlineReason::kUserInputSubmitted:
        if (underline_view_->IsShowing()) {
          AnimateUnderline();
        }
        break;
    }
  }

  // Off to On. Throw away everything we have and start the animation from
  // the beginning.
  void ShowAndAnimateUnderline() {
    underline_view_->StopShowing();
    underline_view_->Show();
  }

  void HideUnderline() {
    if (underline_view_->IsShowing()) {
      underline_view_->StartRampingDown();
    }
  }

  // Replay the animation without hiding and re-showing the view.
  void AnimateUnderline() { underline_view_->ResetEmphasisAndReplay(); }

  void ShowOrAnimatePinnedUnderline() {
    // Pinned underlines should never be visible if the glic window is closed.
    if (!IsUnderlineTabPinned() || !IsGlicWindowShowing()) {
      return;
    }
    if (underline_view_->IsShowing()) {
      AnimateUnderline();
    } else {
      ShowAndAnimateUnderline();
    }
  }

  bool IsGlicWindowShowing() const {
    return underline_view_->GetGlicService()->window_controller().IsShowing();
  }

  bool IsTabInCurrentWindow(const content::WebContents* tab) const {
    auto* model = browser_->GetTabStripModel();
    CHECK(model);
    int index = model->GetIndexOfWebContents(tab);
    return index != TabStripModel::kNoTab;
  }

  std::string UpdateReasonToString(UpdateUnderlineReason reason) {
    switch (reason) {
      case UpdateUnderlineReason::kContextAccessIndicatorOn:
        return "IndicatorOn";
      case UpdateUnderlineReason::kContextAccessIndicatorOff:
        return "IndicatorOff";
      case UpdateUnderlineReason::kFocusedTabChanged_NoFocusChange:
        return "TabFocusChange";
      case UpdateUnderlineReason::kFocusedTabChanged_TabGainedFocus:
        return "TabGainedFocus";
      case UpdateUnderlineReason::kFocusedTabChanged_TabLostFocus:
        return "TabLostFocus";
      case UpdateUnderlineReason::kFocusedTabChanged_ChromeGainedFocus:
        return "ChromeGainedFocus";
      case UpdateUnderlineReason::kFocusedTabChanged_ChromeLostFocus:
        return "ChromeLostFocus";
      case UpdateUnderlineReason::kPinnedTabsChanged_TabInPinnedSet:
        return "TabInPinnedSet";
      case UpdateUnderlineReason::kPinnedTabsChanged_TabNotInPinnedSet:
        return "TabNotInPinnedSet";
      case UpdateUnderlineReason::kPanelStateChanged_PanelShowing:
        return "PanelShowing";
      case UpdateUnderlineReason::kPanelStateChanged_PanelHidden:
        return "PanelHidden";
      case UpdateUnderlineReason::kUserInputSubmitted:
        return "UserInputSubmitted";
    }
  }

  void AddReasonForDebugging(UpdateUnderlineReason reason) {
    underline_update_reasons_.push_back(UpdateReasonToString(reason));
    if (underline_update_reasons_.size() > kNumReasonsToKeep) {
      underline_update_reasons_.pop_front();
    }
  }

  std::string UpdateReasonsToString() const {
    std::ostringstream oss;
    for (const auto& r : underline_update_reasons_) {
      oss << r << ",";
    }
    return oss.str();
  }

  // Back pointer to the owner. Guaranteed to outlive `this`.
  const raw_ptr<GlicTabUnderlineView> underline_view_;

  // Owned by `BrowserView`. Outlives all the children of the `BrowserView`.
  const raw_ptr<BrowserWindowInterface> browser_;

  // Tracked states and their subscriptions.
  base::WeakPtr<content::WebContents> glic_current_focused_contents_;
  base::CallbackListSubscription focus_change_subscription_;
  bool context_access_indicator_enabled_ = false;
  base::CallbackListSubscription indicator_change_subscription_;
  base::CallbackListSubscription pinned_tabs_change_subscription_;
  base::CallbackListSubscription user_input_submitted_subscription_;

  static constexpr size_t kNumReasonsToKeep = 10u;
  std::list<std::string> underline_update_reasons_;
};

GlicTabUnderlineView::GlicTabUnderlineView(Browser* browser,
                                           Tab* tab,
                                           std::unique_ptr<Tester> tester)
    : updater_(std::make_unique<UnderlineViewUpdater>(browser, this)),
      creation_time_(base::TimeTicks::Now()),
      tester_(std::move(tester)),
      colors_(GetParameterizedColors()),
      floats_(GetParameterizedFloats()),
      theme_service_(ThemeServiceFactory::GetForProfile(browser->GetProfile())),
      tab_(tab),
      browser_(browser) {
  auto* gpu_data_manager = content::GpuDataManager::GetInstance();
  has_hardware_acceleration_ =
      gpu_data_manager->IsGpuRasterizationForUIEnabled();

  // Upon GPU crashing, the hardware acceleration status might change. This
  // will observe GPU changes to keep hardware acceleration status updated.
  gpu_data_manager_observer_.Observe(gpu_data_manager);

  UpdateShader();
  CHECK(!shader_.empty()) << "Shader not initialized.";

  auto* glic_service =
      GlicKeyedServiceFactory::GetGlicKeyedService(browser->GetProfile());
  // Post-initialization updates. Don't do the update in the updater's ctor
  // because at that time GlicTabUnderlineView isn't fully initialized, which
  // can lead to undefined behavior.
  //
  // Fetch the latest context access indicator status from service. We can't
  // assume the WebApp always updates the status on the service (thus the new
  // subscribers not getting the latest value).
  updater_->OnIndicatorStatusChanged(
      glic_service->is_context_access_indicator_enabled());
}

GlicTabUnderlineView::~GlicTabUnderlineView() = default;

void GlicTabUnderlineView::OnPaint(gfx::Canvas* canvas) {
  if (!compositor_) {
    return;
  }
  auto bounds = GetLocalBounds();
  const auto u_resolution = GetLocalBounds();
  // Insets aren't relevant to the tab underline effect, but are defined in the
  // uniforms of the GlicBorderView shader.
  gfx::Insets uniform_insets = gfx::Insets();

  float corner_radius = 0.0f;
#if BUILDFLAG(IS_MAC)
  if (!browser_->window()->IsFullscreen()) {
    corner_radius = 12.0f;
  }
#endif
  // TODO(crbug.com/433136181): shader logic is borrowed from GlicBorderView,
  // but emphasis can be fixed to 0 for the underline and related handling can
  // be removed entirely.
  std::vector<cc::PaintShader::FloatUniform> float_uniforms = {
      {.name = SkString("u_time"), .value = GetEffectTime()},
      {.name = SkString("u_emphasis"), .value = emphasis_},
      {.name = SkString("u_corner_radius"), .value = corner_radius},
      {.name = SkString("u_insets"),
       .value = static_cast<float>(uniform_insets.left())},
      {.name = SkString("u_progress"), .value = progress_}};
  std::vector<cc::PaintShader::Float2Uniform> float2_uniforms = {
      // TODO(https://crbug.com/406026829): Ideally `u_resolution` should be a
      // vec4(x, y, w, h) and does not assume the origin is (0, 0). This way we
      // can eliminate `u_insets` and void the shader-internal origin-padding.
      {.name = SkString("u_resolution"),
       .value = SkV2{static_cast<float>(u_resolution.width()),
                     static_cast<float>(u_resolution.height())}}};
  std::vector<cc::PaintShader::IntUniform> int_uniforms = {
      {.name = SkString("u_dark"),
       .value = UseDarkMode(theme_service_) ? 1 : 0}};

  std::vector<cc::PaintShader::Float4Uniform> float4_uniforms;
  if (base::FeatureList::IsEnabled(features::kGlicParameterizedShader)) {
    for (int i = 0; i < static_cast<int>(colors_.size()); ++i) {
      float4_uniforms.push_back(
          {.name = SkString(absl::StrFormat("u_color%d", i + 1)),
           .value =
               SkV4{static_cast<float>(SkColorGetR(colors_[i]) / 255.0),
                    static_cast<float>(SkColorGetG(colors_[i]) / 255.0),
                    static_cast<float>(SkColorGetB(colors_[i]) / 255.0), 1.f}});
    }
    for (int i = 0; i < static_cast<int>(floats_.size()); ++i) {
      float_uniforms.push_back(
          {.name = SkString(absl::StrFormat("u_float%d", i + 1)),
           .value = floats_[i]});
    }
  }

  views::View::OnPaint(canvas);

  cc::PaintFlags flags;
  auto shader = cc::PaintShader::MakeSkSLCommand(
      shader_, std::move(float_uniforms), std::move(float2_uniforms),
      std::move(float4_uniforms), std::move(int_uniforms),
      cached_paint_shader_);

  flags.setShader(shader);

  if (base::FeatureList::IsEnabled(features::kGlicUseShaderCache)) {
    cached_paint_shader_ = shader;
  }

  constexpr static int kMaxEffectWidth = 2;
  gfx::Point origin =
      bounds.origin() +
      gfx::Vector2d(0, bounds.size().height() - kMaxEffectWidth);
  gfx::Size size(bounds.size().width(), kMaxEffectWidth);
  gfx::Rect effect_bounds(origin, size);
  canvas->DrawRect(gfx::RectF(effect_bounds), flags);
}

void GlicTabUnderlineView::OnAnimationStep(base::TimeTicks timestamp) {
  if (tester_) [[unlikely]] {
    timestamp = tester_->GetTestTimestamp();
  }
  last_animation_step_time_ = timestamp;
  if (first_frame_time_.is_null()) {
    first_frame_time_ = timestamp;
  }
  if (first_emphasis_frame_.is_null()) {
    first_emphasis_frame_ = timestamp;

    // The time gaps when the underline is in steady state cause discontinuous
    // underline states when switching tabs. By keeping track of the total
    // steady time, we can have a continuous effect time. Each steady time
    // interval is added to the total at the very beginning of an upcoming
    // emphasis animation. Note: the opacity ramp up / down is not part of the
    // shader animation.
    if (!last_emphasis_frame_.is_null()) {
      total_steady_time_ += timestamp - last_emphasis_frame_;
      last_emphasis_frame_ = base::TimeTicks{};
    }
  }
  if (record_first_ramp_down_frame_) {
    record_first_ramp_down_frame_ = false;
    first_ramp_down_frame_ = timestamp;
  }

  base::TimeDelta emphasis_since_first_frame =
      timestamp - first_emphasis_frame_;
  emphasis_ = GetEmphasis(emphasis_since_first_frame);
  base::TimeDelta opacity_since_first_frame = timestamp - first_frame_time_;
  opacity_ = GetOpacity(timestamp);
  progress_ = GetEffectProgress(timestamp);

  // TODO(liuwilliam): Ideally this should be done in paint-related methods.
  // Consider moving it to LayerDelegate::OnPaintLayer().
  CHECK(layer());
  layer()->SetOpacity(opacity_);

  // Don't animate if the animations have exhausted and we haven't started
  // ramping down. We shouldn't be an observer for more than 60 seconds
  // (CompositorAnimationObserver::NotifyFailure()).
  bool emphasis_done =
      emphasis_ == 0.f && !emphasis_since_first_frame.is_zero();
  bool opacity_ramp_up_done =
      opacity_ == 1.f && !opacity_since_first_frame.is_zero();
  bool show_steady_state =
      emphasis_done && opacity_ramp_up_done && first_ramp_down_frame_.is_null();

  if (show_steady_state) {
    // If skipping the animation the class does not need to be an animation
    // observer.
    compositor_->RemoveAnimationObserver(this);
    if (last_emphasis_frame_.is_null()) {
      last_emphasis_frame_ = timestamp;
    }
    return;
  }

  bool opacity_ramp_down_done =
      opacity_ == 0.f && !first_ramp_down_frame_.is_null();
  if (opacity_ramp_down_done) {
    StopShowing();
    return;
  }

  SchedulePaint();
}

void GlicTabUnderlineView::OnCompositingShuttingDown(
    ui::Compositor* compositor) {
  StopShowing();
}

void GlicTabUnderlineView::OnGpuInfoUpdate() {
  auto* gpu_data_manager = content::GpuDataManager::GetInstance();
  bool has_hardware_acceleration =
      gpu_data_manager->IsGpuRasterizationForUIEnabled();

  if (has_hardware_acceleration_ != has_hardware_acceleration) {
    has_hardware_acceleration_ = has_hardware_acceleration;
    UpdateShader();

    if (IsShowing()) {
      SchedulePaint();
    }
  }
}

bool GlicTabUnderlineView::IsShowing() const {
  // `compositor_` is set when the underline starts to show and unset when the
  // underline stops to show.
  return !!compositor_;
}

float GlicTabUnderlineView::GetEffectTimeForTesting() const {
  return GetEffectTime();
}

void GlicTabUnderlineView::Show() {
  if (compositor_) {
    // The user can click on the glic icon after the window is shown. The
    // animation is already playing at that time.
    return;
  }

  if (!parent()) {
    base::debug::DumpWithoutCrashing();
    return;
  }

  SetPaintToLayer();
  layer()->SetFillsBoundsOpaquely(false);
  SetVisible(true);

  skip_emphasis_animation_ =
      gfx::Animation::PrefersReducedMotion() || ForceSimplifiedShader();

  ui::Compositor* compositor = layer()->GetCompositor();
  if (!compositor) {
    base::debug::DumpWithoutCrashing();
    return;
  }

  compositor_ = compositor;
  compositor_animation_observation_.Observe(compositor_.get());
  compositor_observation_.Observe(compositor_.get());

  if (tester_) [[unlikely]] {
    tester_->AnimationStarted();
  }
}

void GlicTabUnderlineView::StopShowing() {
  if (!compositor_) {
    return;
  }

  compositor_observation_.Reset();
  compositor_animation_observation_.Reset();
  compositor_ = nullptr;
  first_frame_time_ = base::TimeTicks{};
  first_emphasis_frame_ = base::TimeTicks{};
  last_emphasis_frame_ = base::TimeTicks{};
  first_ramp_down_frame_ = base::TimeTicks{};
  record_first_ramp_down_frame_ = false;
  total_steady_time_ = base::Milliseconds(0);
  opacity_ = 0.f;
  emphasis_ = 0.f;

  // `DestroyLayer()` schedules another paint to repaint the affected area by
  // the destroyed layer.
  DestroyLayer();
  SetVisible(false);
}

float GlicTabUnderlineView::GetEmphasis(base::TimeDelta delta) const {
  if (skip_emphasis_animation_) {
    return 0.f;
  }
  static constexpr base::TimeDelta kRampUpAndSteady =
      kEmphasisRampUpDuration + kEmphasisDuration;
  if (delta < kRampUpAndSteady) {
    auto target = static_cast<float>(delta / kEmphasisRampUpDuration);
    return ClampAndInterpolate(gfx::Tween::Type::EASE_OUT, target, 0, 1);
  }
  auto target = static_cast<float>((delta - kRampUpAndSteady) /
                                   kEmphasisRampDownDuration);
  return ClampAndInterpolate(gfx::Tween::Type::EASE_IN_OUT_2, target, 1, 0);
}

void GlicTabUnderlineView::ResetEmphasisAndReplay() {
  // TOOD(crbug.com/398319435): Remove once we know why this is called before
  // `Show()`.
  if (!compositor_) {
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "opacity", opacity_);
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "emphasis", emphasis_);
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "creation",
                            TimeTicksToMicroseconds(creation_time_));
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "first_frame",
                            TimeTicksToMicroseconds(first_frame_time_));
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "first_emphasis",
                            TimeTicksToMicroseconds(first_emphasis_frame_));
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "last_step",
                            TimeTicksToMicroseconds(last_animation_step_time_));
    SCOPED_CRASH_KEY_NUMBER("crbug-398319435", "first_rampdown",
                            TimeTicksToMicroseconds(first_ramp_down_frame_));
    base::debug::DumpWithoutCrashing();

    // Gracefully handling the crash case in crbug.com/398319435 by
    // closing(minimizing) the glic window.
    // TODO(crbug.com/413442838): Add tests to reproduce the dump without crash
    // and validate the solution.
    GetGlicService()->window_controller().Close();
    return;
  }
  CHECK(compositor_->HasObserver(this));
  if (!compositor_->HasAnimationObserver(this)) {
    compositor_->AddAnimationObserver(this);
  }
  first_emphasis_frame_ = base::TimeTicks{};
  SchedulePaint();

  if (tester_) [[unlikely]] {
    tester_->EmphasisRestarted();
  }
}

float GlicTabUnderlineView::GetOpacity(base::TimeTicks timestamp) {
  auto ramp_up_duration = skip_emphasis_animation_ ? kFastOpacityRampUpDuration
                                                   : kOpacityRampUpDuration;
  if (!first_ramp_down_frame_.is_null()) {
    // The ramp up opacity could be any value between 0-1 during the ramp up
    // time. Thus, the ramping down opacity must be deducted from the value of
    // ramp up opacity at the time of `first_ramp_down_frame_`.
    base::TimeDelta delta = first_ramp_down_frame_ - first_frame_time_;
    float ramp_up_opacity =
        std::clamp(static_cast<float>(delta.InMillisecondsF() /
                                      ramp_up_duration.InMillisecondsF()),
                   0.0f, 1.0f);

    base::TimeDelta time_since_first_ramp_down_frame =
        timestamp - first_ramp_down_frame_;
    float ramp_down_opacity =
        static_cast<float>(time_since_first_ramp_down_frame.InMillisecondsF() /
                           kOpacityRampDownDuration.InMillisecondsF());
    ramp_down_opacity_ =
        std::clamp(ramp_up_opacity - ramp_down_opacity, 0.0f, 1.0f);
    return ramp_down_opacity_;
  } else {
    base::TimeDelta time_since_first_frame = timestamp - first_frame_time_;
    return std::clamp(
        static_cast<float>(ramp_down_opacity_ +
                           (time_since_first_frame.InMillisecondsF() /
                            ramp_up_duration.InMillisecondsF())),
        0.0f, 1.0f);
  }
}

void GlicTabUnderlineView::StartRampingDown() {
  CHECK(compositor_);

  // From now on the opacity will be decreased until it reaches 0.
  record_first_ramp_down_frame_ = true;

  if (!compositor_->HasAnimationObserver(this)) {
    compositor_->AddAnimationObserver(this);
  }

  if (tester_) [[unlikely]] {
    tester_->RampDownStarted();
  }
}

float GlicTabUnderlineView::GetEffectTime() const {
  if (last_animation_step_time_.is_null()) {
    return 0;
  }

  // Returns a constant duration so the underline states don't jump around when
  // switching tabs.
  if (skip_emphasis_animation_) {
    auto time_since_creation =
        (first_frame_time_ - GetCreationTime()) % kMaxTime;
    return time_since_creation.InSecondsF();
  }

  auto time_since_creation =
      ((last_animation_step_time_ - GetCreationTime()) - total_steady_time_) %
      kMaxTime;
  return time_since_creation.InSecondsF();
}

float GlicTabUnderlineView::GetEffectProgress(base::TimeTicks timestamp) const {
  if (skip_emphasis_animation_) {
    return 0.0;
  }
  base::TimeDelta time_since_first_frame = timestamp - first_emphasis_frame_;
  base::TimeDelta total_duration =
      kEmphasisRampUpDuration + kEmphasisRampDownDuration + kEmphasisDuration;
  return std::clamp(
      static_cast<float>(time_since_first_frame.InMillisecondsF() /
                         total_duration.InMillisecondsF()),
      0.0f, 1.0f);
}

base::TimeTicks GlicTabUnderlineView::GetCreationTime() const {
  if (tester_ && !tester_->GetTestCreationTime().is_null()) [[unlikely]] {
    return tester_->GetTestCreationTime();
  }
  return creation_time_;
}

bool GlicTabUnderlineView::ForceSimplifiedShader() const {
  return base::FeatureList::IsEnabled(features::kGlicForceSimplifiedBorder) ||
         !has_hardware_acceleration_;
}

GlicKeyedService* GlicTabUnderlineView::GetGlicService() const {
  auto* service =
      GlicKeyedServiceFactory::GetGlicKeyedService(browser_->GetProfile());
  CHECK(service);
  return service;
}

void GlicTabUnderlineView::UpdateShader() {
  if (base::FeatureList::IsEnabled(features::kGlicParameterizedShader) &&
      !colors_.empty() && !floats_.empty()) {
    shader_ =
        ForceSimplifiedShader()
            ? ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
                  IDR_GLIC_SIMPLIFIED_PARAMETERIZED_BORDER_SHADER)
            : ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
                  IDR_GLIC_PARAMETERIZED_BORDER_SHADER);
  } else {
    shader_ =
        ForceSimplifiedShader()
            ? ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
                  IDR_GLIC_SIMPLIFIED_BORDER_SHADER)
            : ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
                  IDR_GLIC_BORDER_SHADER);
  }
}

BEGIN_METADATA(GlicTabUnderlineView)
END_METADATA

}  // namespace glic