File: glic_window_animator.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 3,960 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
// 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/widget/glic_window_animator.h"

#include "chrome/browser/glic/widget/glic_view.h"
#include "chrome/browser/glic/widget/glic_widget.h"
#include "chrome/browser/glic/widget/glic_window_controller.h"
#include "chrome/browser/glic/widget/glic_window_resize_animation.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/controls/webview/webview.h"

namespace glic {

GlicWindowAnimator::GlicWindowAnimator(GlicWindowController* window_controller)
    : window_controller_(window_controller) {}

GlicWindowAnimator::~GlicWindowAnimator() = default;

void GlicWindowAnimator::AnimateBounds(const gfx::Rect& target_bounds,
                                       base::TimeDelta duration,
                                       base::OnceClosure callback) {
  CHECK(window_controller_->GetGlicWidget());

  if (duration < base::Milliseconds(0)) {
    duration = base::Milliseconds(0);
  }

  if (duration > base::Seconds(60)) {
    duration = base::Seconds(60);
  }

  auto* glic_widget = window_controller_->GetGlicWidget();
  gfx::Rect widget_target_bounds =
      glic_widget->VisibleToWidgetBounds(target_bounds);

  if (window_resize_animation_) {
    // Update the ongoing animation with the new bounds and new duration.
    window_resize_animation_->UpdateTargetBounds(widget_target_bounds,
                                                 std::move(callback));
    window_resize_animation_->SetDuration(
        std::max(window_resize_animation_->duration_left(), duration));
  } else {
    window_resize_animation_ = std::make_unique<GlicWindowResizeAnimation>(
        window_controller_, this, widget_target_bounds, duration,
        std::move(callback));
  }
}

void GlicWindowAnimator::AnimateSize(const gfx::Size& target_size,
                                     base::TimeDelta duration,
                                     base::OnceClosure callback) {
  last_target_size_ = target_size;
  gfx::Rect target_bounds = GetCurrentTargetBounds();
  target_bounds.set_size(target_size);
  AnimateBounds(target_bounds, duration, std::move(callback));
}

void GlicWindowAnimator::AnimatePosition(const gfx::Point& target_position,
                                         base::TimeDelta duration,
                                         base::OnceClosure callback) {
  // Maintain the size whether there's an ongoing animation or not.
  gfx::Rect new_bounds = GetCurrentTargetBounds();
  new_bounds.set_origin(target_position);
  AnimateBounds(new_bounds, duration, std::move(callback));
}

gfx::Rect GlicWindowAnimator::GetCurrentTargetBounds() {
  auto* glic_widget = window_controller_->GetGlicWidget();
  if (window_resize_animation_) {
    // Get the ongoing animation's target bounds if they exist.
    return glic_widget->WidgetToVisibleBounds(
        window_resize_animation_->target_bounds());
  } else {
    return glic_widget->WidgetToVisibleBounds(
        glic_widget->GetWindowBoundsInScreen());
  }
}

bool GlicWindowAnimator::IsAnimating() const {
  return window_resize_animation_ != nullptr;
}

void GlicWindowAnimator::ResetLastTargetSize() {
  last_target_size_ = gfx::Size();
}

void GlicWindowAnimator::MaybeAnimateToTargetSize() {
  if (!last_target_size_.IsEmpty() &&
      last_target_size_ != window_controller_->GetGlicWidget()
                               ->GetWindowBoundsInScreen()
                               .size()) {
    AnimateSize(last_target_size_, base::Milliseconds(300), base::DoNothing());
  }
  ResetLastTargetSize();
}

void GlicWindowAnimator::ResizeFinished() {
  // Destroy window_resize_animation_.
  window_resize_animation_.reset();
  window_controller_->MaybeSetWidgetCanResize();
}

}  // namespace glic