summaryrefslogtreecommitdiff
path: root/athena/home/home_card_impl.cc
blob: 949d5512d4e158be816cb91bb5bd3cff28e85757 (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
154
155
156
157
// Copyright 2014 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 "athena/home/public/home_card.h"

#include "athena/home/app_list_view_delegate.h"
#include "athena/input/public/accelerator_manager.h"
#include "athena/input/public/input_manager.h"
#include "athena/screen/public/screen_manager.h"
#include "ui/app_list/pagination_model.h"
#include "ui/app_list/views/app_list_view.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"
#include "ui/wm/core/visibility_controller.h"
#include "ui/wm/core/window_animations.h"

namespace athena {
namespace {

HomeCard* instance = NULL;

class HomeCardLayoutManager : public aura::LayoutManager {
 public:
  explicit HomeCardLayoutManager(aura::Window* container)
      : container_(container) {}
  virtual ~HomeCardLayoutManager() {}

 private:
  // aura::LayoutManager:
  virtual void OnWindowResized() OVERRIDE { Layout(); }
  virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { Layout(); }
  virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
  virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {
    Layout();
  }
  virtual void OnChildWindowVisibilityChanged(aura::Window* child,
                                              bool visible) OVERRIDE {
    Layout();
  }
  virtual void SetChildBounds(aura::Window* child,
                              const gfx::Rect& requested_bounds) OVERRIDE {
    SetChildBoundsDirect(child, gfx::Rect(requested_bounds.size()));
  }

  void Layout() {
    const int kHomeCardHeight = 150;
    const int kHomeCardHorizontalMargin = 50;
    // Currently the home card is provided as a bubble and the bounds has to be
    // increased to cancel the shadow.
    // TODO(mukai): stops using the bubble and remove this.
    const int kHomeCardShadowWidth = 30;
    if (container_->children().size() < 1)
      return;
    aura::Window* home_card = container_->children()[0];
    if (!home_card->IsVisible())
      return;
    gfx::Rect screen_bounds = home_card->GetRootWindow()->bounds();
    gfx::Rect card_bounds = screen_bounds;
    card_bounds.Inset(kHomeCardHorizontalMargin,
                      screen_bounds.height() - kHomeCardHeight,
                      kHomeCardHorizontalMargin,
                      0);
    card_bounds.Inset(-kHomeCardShadowWidth, -kHomeCardShadowWidth);
    SetChildBoundsDirect(home_card, card_bounds);
  }

  aura::Window* container_;

  DISALLOW_COPY_AND_ASSIGN(HomeCardLayoutManager);
};

class HomeCardImpl : public HomeCard, public AcceleratorHandler {
 public:
  HomeCardImpl();
  virtual ~HomeCardImpl();

  void Init();

 private:
  enum Command {
    COMMAND_SHOW_HOME_CARD,
  };
  void InstallAccelerators();

  // AcceleratorHandler:
  virtual bool IsCommandEnabled(int command_id) const OVERRIDE { return true; }
  virtual bool OnAcceleratorFired(int command_id,
                                  const ui::Accelerator& accelerator) OVERRIDE {
    DCHECK_EQ(COMMAND_SHOW_HOME_CARD, command_id);
    home_card_widget_->Show();
    return true;
  }

  views::Widget* home_card_widget_;

  DISALLOW_COPY_AND_ASSIGN(HomeCardImpl);
};

HomeCardImpl::HomeCardImpl()
  : home_card_widget_(NULL) {
  DCHECK(!instance);
  instance = this;
}

HomeCardImpl::~HomeCardImpl() {
  DCHECK(instance);
  home_card_widget_->CloseNow();
  instance = NULL;
}

void HomeCardImpl::Init() {
  InstallAccelerators();

  aura::Window* container =
      ScreenManager::Get()->CreateContainer("HomeCardContainer");
  container->SetLayoutManager(new HomeCardLayoutManager(container));
  wm::SetChildWindowVisibilityChangesAnimated(container);

  app_list::AppListView* view = new app_list::AppListView(
      new AppListViewDelegate);
  view->InitAsBubbleAtFixedLocation(
      container,
      0 /* initial_apps_page */,
      gfx::Point(),
      views::BubbleBorder::FLOAT,
      true /* border_accepts_events */);
  home_card_widget_ = view->GetWidget();
  view->ShowWhenReady();
}

void HomeCardImpl::InstallAccelerators() {
  const AcceleratorData accelerator_data[] = {
      {TRIGGER_ON_PRESS, ui::VKEY_L, ui::EF_CONTROL_DOWN,
       COMMAND_SHOW_HOME_CARD, AF_NONE},
  };
  InputManager::Get()->GetAcceleratorManager()->RegisterAccelerators(
      accelerator_data, arraysize(accelerator_data), this);
}

}  // namespace

// static
HomeCard* HomeCard::Create() {
  (new HomeCardImpl())->Init();
  DCHECK(instance);
  return instance;
}

// static
void HomeCard::Shutdown() {
  DCHECK(instance);
  delete instance;
  instance = NULL;
}

}  // namespace athena