summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/slide_animator_gtk.cc
blob: 3c1b9746fe66b8b598badc710877a53124e60e2d (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/gtk/slide_animator_gtk.h"

#include "ui/base/animation/animation.h"
#include "ui/base/animation/slide_animation.h"
#include "ui/base/gtk/gtk_expanded_container.h"

namespace {

void OnChildSizeRequest(GtkWidget* expanded,
                        GtkWidget* child,
                        GtkRequisition* requisition,
                        gpointer control_child_size) {
  // If |control_child_size| is true, then we want |child_| to match the width
  // of the |widget_|, but the height of |child_| should not change.
  if (!GPOINTER_TO_INT(control_child_size)) {
    requisition->width = -1;
  }
  requisition->height = -1;
}

}  // namespace

bool SlideAnimatorGtk::animations_enabled_ = true;

SlideAnimatorGtk::SlideAnimatorGtk(GtkWidget* child,
                                   Direction direction,
                                   int duration,
                                   bool linear,
                                   bool control_child_size,
                                   Delegate* delegate)
    : child_(child),
      direction_(direction),
      delegate_(delegate) {
  widget_.Own(gtk_expanded_container_new());
  gtk_container_add(GTK_CONTAINER(widget_.get()), child);
  gtk_widget_set_size_request(widget_.get(), -1, 0);

  // If the child requests it, we will manually set the size request for
  // |child_| every time the |widget_| changes sizes. This is mainly useful
  // for bars, where we want the child to expand to fill all available space.
  g_signal_connect(widget_.get(), "child-size-request",
                   G_CALLBACK(OnChildSizeRequest),
                   GINT_TO_POINTER(control_child_size));

  // We connect to this signal to set an initial position for our child widget.
  // The reason we connect to this signal rather than setting the initial
  // position here is that the widget is currently unallocated and may not
  // even have a size request.
  g_signal_connect(child, "size-allocate",
                   G_CALLBACK(OnChildSizeAllocate), this);

  child_needs_move_ = (direction == DOWN);

  animation_.reset(new ui::SlideAnimation(this));
  // Default tween type is EASE_OUT.
  if (linear)
    animation_->SetTweenType(ui::Tween::LINEAR);
  if (duration != 0)
    animation_->SetSlideDuration(duration);
}

SlideAnimatorGtk::~SlideAnimatorGtk() {
  widget_.Destroy();
}

void SlideAnimatorGtk::Open() {
  if (!animations_enabled_)
    return OpenWithoutAnimation();

  gtk_widget_show(widget_.get());
  animation_->Show();
}

void SlideAnimatorGtk::OpenWithoutAnimation() {
  gtk_widget_show(widget_.get());
  animation_->Reset(1.0);
  animation_->Show();
  AnimationProgressed(animation_.get());
}

void SlideAnimatorGtk::Close() {
  if (!animations_enabled_)
    return CloseWithoutAnimation();

  animation_->Hide();
}

void SlideAnimatorGtk::End() {
  animation_->End();
}

void SlideAnimatorGtk::CloseWithoutAnimation() {
  animation_->Reset(0.0);
  animation_->Hide();
  AnimationProgressed(animation_.get());
  gtk_widget_hide(widget_.get());
}

bool SlideAnimatorGtk::IsShowing() {
  return animation_->IsShowing();
}

bool SlideAnimatorGtk::IsClosing() {
  return animation_->IsClosing();
}

bool SlideAnimatorGtk::IsAnimating() {
  return animation_->is_animating();
}

void SlideAnimatorGtk::AnimationProgressed(const ui::Animation* animation) {
  GtkRequisition req;
  gtk_widget_size_request(child_, &req);

  int showing_height = static_cast<int>(req.height *
                                        animation_->GetCurrentValue());
  if (direction_ == DOWN) {
    if (widget_.get()->parent) {
      gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(widget_.get()),
                                  child_, 0, showing_height - req.height);
    }
    child_needs_move_ = false;
  }
  gtk_widget_set_size_request(widget_.get(), -1, showing_height);
}

void SlideAnimatorGtk::AnimationEnded(const ui::Animation* animation) {
  if (!animation_->IsShowing()) {
    gtk_widget_hide(widget_.get());
    if (delegate_)
      delegate_->Closed();
  }
}

// static
void SlideAnimatorGtk::SetAnimationsForTesting(bool enable) {
  animations_enabled_ = enable;
}

// static
void SlideAnimatorGtk::OnChildSizeAllocate(GtkWidget* child,
                                           GtkAllocation* allocation,
                                           SlideAnimatorGtk* slider) {
  if (slider->child_needs_move_) {
    gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(slider->widget()),
                                child, 0, -allocation->height);
    slider->child_needs_move_ = false;
  }
}