summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/slide_animator_gtk.cc
blob: cea09b6965a1111a2cb946b5c38b07c56ad08dbc (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
// Copyright (c) 2009 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/gtk/slide_animator_gtk.h"

#include "app/animation.h"
#include "app/slide_animation.h"
#include "base/logging.h"

namespace {

void OnFixedSizeAllocate(GtkWidget* fixed,
                         GtkAllocation* allocation,
                         GtkWidget* child) {
  if (allocation->width != child->allocation.width) {
    // The size of the GtkFixed has changed. We want |child_| to match widths,
    // but the height should not change.
    GtkAllocation new_allocation = child->allocation;
    new_allocation.width = allocation->width;
    gtk_widget_size_allocate(child, &new_allocation);
  }
}

}  // namespace

SlideAnimatorGtk::SlideAnimatorGtk(GtkWidget* child,
                                   Direction direction,
                                   int duration,
                                   bool linear,
                                   Delegate* delegate)
    : child_(child),
      direction_(direction),
      delegate_(delegate),
      fixed_needs_resize_(false) {
  widget_.Own(gtk_fixed_new());
  gtk_fixed_put(GTK_FIXED(widget_.get()), child, 0, 0);
  gtk_widget_set_size_request(widget_.get(), -1, 0);
  // We have to manually set the size request for |child_| every time the
  // GtkFixed changes sizes.
  g_signal_connect(widget_.get(), "size-allocate",
                   G_CALLBACK(OnFixedSizeAllocate), child_);

  // The size of the GtkFixed widget is set during animation. When we open
  // without showing the animation, we have to call AnimationProgressed
  // ourselves to properly set the size of the GtkFixed. We can't do this until
  // after the child has been allocated, hence we connect to "size-allocate" on
  // the child.
  g_signal_connect(child, "size-allocate",
                   G_CALLBACK(OnChildSizeAllocate), this);

  child_needs_move_ = (direction == DOWN);

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

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

void SlideAnimatorGtk::Open() {
  gtk_widget_show(widget_.get());
  animation_->Show();
}

void SlideAnimatorGtk::OpenWithoutAnimation() {
  animation_->Reset(1.0);
  Open();

  // This checks to see if |child_| has been allocated yet. If it has been
  // allocated already, we can go ahead and reposition everything by calling
  // AnimationProgressed(). If it has not been allocated, we have to delay
  // this call until it has been allocated (see OnChildSizeAllocate).
  if (child_->allocation.x != -1) {
    AnimationProgressed(animation_.get());
  } else {
    fixed_needs_resize_ = true;
  }
}

void SlideAnimatorGtk::Close() {
  animation_->Hide();
}

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();
}

void SlideAnimatorGtk::AnimationProgressed(const Animation* animation) {
  int showing_height = child_->allocation.height *
                       animation_->GetCurrentValue();
  if (direction_ == DOWN) {
    gtk_fixed_move(GTK_FIXED(widget_.get()), child_, 0,
                   showing_height - child_->allocation.height);
  }
  gtk_widget_set_size_request(widget_.get(), -1, showing_height);
}

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

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

  if (slider->fixed_needs_resize_) {
    slider->AnimationProgressed(slider->animation_.get());
    slider->fixed_needs_resize_ = false;
  }
}