summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.cc
blob: d4e9a54342663dc13c80a223d1fe22ca7f3a6336 (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
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
// 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 "chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.h"

#include "ash/shelf/shelf_model.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "chrome/browser/extensions/webstore_install_with_prompt.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_v2app.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
#include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h"
#include "chrome/browser/ui/ash/launcher/launcher_context_menu.h"
#include "chrome/browser/ui/ash/launcher/launcher_item_controller.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/native_app_window.h"
#include "skia/ext/image_operations.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/wm/core/window_animations.h"

using extensions::AppWindow;

namespace {

// Size of the icon in the shelf launcher in display-independent pixels.
const int kAppListIconSize = 24;

// This will return a slightly smaller icon than the app icon to be used in
// the application list menu.
gfx::Image GetAppListIcon(AppWindow* app_window) {
  // TODO(skuhne): We instead might want to use LoadImages in
  // AppWindow::UpdateExtensionAppIcon() to let the extension give us
  // pre-defined icons in the launcher and the launcher list sizes. Since there
  // is no mock yet, doing this now seems a bit premature and we scale for the
  // time being.
  if (app_window->app_icon().IsEmpty())
    return gfx::Image();

  SkBitmap bmp =
      skia::ImageOperations::Resize(*app_window->app_icon().ToSkBitmap(),
                                    skia::ImageOperations::RESIZE_BEST,
                                    kAppListIconSize,
                                    kAppListIconSize);
  return gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(bmp));
}

// Returns true if the app window is visible on screen, i.e. not hidden or
// minimized.
bool IsAppWindowVisible(AppWindow* app_window) {
  return app_window &&
         !app_window->is_hidden() &&
         !app_window->GetBaseWindow()->IsMinimized();
}

// Functor for std::find_if used in AppLauncherItemController.
class AppWindowHasWindow {
 public:
  explicit AppWindowHasWindow(aura::Window* window) : window_(window) {}

  bool operator()(AppWindow* app_window) const {
    return app_window->GetNativeWindow() == window_;
  }

 private:
  const aura::Window* window_;
};

}  // namespace

AppWindowLauncherItemController::AppWindowLauncherItemController(
    Type type,
    const std::string& app_shelf_id,
    const std::string& app_id,
    ChromeLauncherController* controller)
    : LauncherItemController(type, app_id, controller),
      last_active_app_window_(NULL),
      app_shelf_id_(app_shelf_id),
      observed_windows_(this) {}

AppWindowLauncherItemController::~AppWindowLauncherItemController() {}

void AppWindowLauncherItemController::AddAppWindow(
    AppWindow* app_window,
    ash::ShelfItemStatus status) {
  if (app_window->window_type_is_panel() && type() != TYPE_APP_PANEL)
    LOG(ERROR) << "AppWindow of type Panel added to non-panel launcher item";
  app_windows_.push_front(app_window);
  observed_windows_.Add(app_window->GetNativeWindow());
}

void AppWindowLauncherItemController::RemoveAppWindowForWindow(
    aura::Window* window) {
  AppWindowList::iterator iter = std::find_if(
      app_windows_.begin(), app_windows_.end(), AppWindowHasWindow(window));
  if (iter != app_windows_.end()) {
    if (*iter == last_active_app_window_)
      last_active_app_window_ = NULL;
    app_windows_.erase(iter);
  }
  observed_windows_.Remove(window);
}

void AppWindowLauncherItemController::SetActiveWindow(aura::Window* window) {
  AppWindowList::iterator iter = std::find_if(
      app_windows_.begin(), app_windows_.end(), AppWindowHasWindow(window));
  if (iter != app_windows_.end())
    last_active_app_window_ = *iter;
}

bool AppWindowLauncherItemController::IsOpen() const {
  return !app_windows_.empty();
}

bool AppWindowLauncherItemController::IsVisible() const {
  // Return true if any windows are visible.
  for (AppWindowList::const_iterator iter = app_windows_.begin();
       iter != app_windows_.end();
       ++iter) {
    if ((*iter)->GetNativeWindow()->IsVisible())
      return true;
  }
  return false;
}

void AppWindowLauncherItemController::Launch(ash::LaunchSource source,
                                             int event_flags) {
  launcher_controller()->LaunchApp(app_id(), source, ui::EF_NONE);
}

bool AppWindowLauncherItemController::Activate(ash::LaunchSource source) {
  DCHECK(!app_windows_.empty());
  AppWindow* window_to_activate =
      last_active_app_window_ ? last_active_app_window_ : app_windows_.back();
  window_to_activate->GetBaseWindow()->Activate();
  return false;
}

void AppWindowLauncherItemController::Close() {
  // Note: Closing windows may affect the contents of app_windows_.
  AppWindowList windows_to_close = app_windows_;
  for (AppWindowList::iterator iter = windows_to_close.begin();
       iter != windows_to_close.end();
       ++iter) {
    (*iter)->GetBaseWindow()->Close();
  }
}

void AppWindowLauncherItemController::ActivateIndexedApp(size_t index) {
  if (index >= app_windows_.size())
    return;
  AppWindowList::iterator it = app_windows_.begin();
  std::advance(it, index);
  ShowAndActivateOrMinimize(*it);
}

void AppWindowLauncherItemController::InstallApp() {
  // Find a visible window in order to position the install dialog. If there is
  // no visible window, the dialog will be centered on the screen.
  AppWindow* parent_window = NULL;
  if (IsAppWindowVisible(last_active_app_window_)) {
    parent_window = last_active_app_window_;
  } else {
    for (AppWindowList::iterator iter = app_windows_.begin();
         iter != app_windows_.end(); ++iter) {
      if (IsAppWindowVisible(*iter)) {
        parent_window = *iter;
        break;
      }
    }
  }

  scoped_refptr<extensions::WebstoreInstallWithPrompt> installer;
  if (parent_window) {
    installer = new extensions::WebstoreInstallWithPrompt(
        app_id(),
        launcher_controller()->profile(),
        parent_window->GetNativeWindow(),
        extensions::WebstoreInstallWithPrompt::Callback());
  } else {
    installer = new extensions::WebstoreInstallWithPrompt(
        app_id(),
        launcher_controller()->profile(),
        extensions::WebstoreInstallWithPrompt::Callback());
  }
  installer->BeginInstall();
}

ChromeLauncherAppMenuItems AppWindowLauncherItemController::GetApplicationList(
    int event_flags) {
  ChromeLauncherAppMenuItems items;
  items.push_back(new ChromeLauncherAppMenuItem(GetTitle(), NULL, false));
  int index = 0;
  for (AppWindowList::iterator iter = app_windows_.begin();
       iter != app_windows_.end();
       ++iter) {
    AppWindow* app_window = *iter;

    // If the app's web contents provides a favicon, use it. Otherwise, use a
    // scaled down app icon.
    FaviconTabHelper* favicon_tab_helper =
        FaviconTabHelper::FromWebContents(app_window->web_contents());
    gfx::Image result = favicon_tab_helper->GetFavicon();
    if (result.IsEmpty())
      result = GetAppListIcon(app_window);

    items.push_back(new ChromeLauncherAppMenuItemV2App(
        app_window->GetTitle(),
        &result,  // Will be copied
        app_id(),
        launcher_controller(),
        index,
        index == 0 /* has_leading_separator */));
    ++index;
  }
  return items.Pass();
}

bool AppWindowLauncherItemController::ItemSelected(const ui::Event& event) {
  if (app_windows_.empty())
    return false;
  if (type() == TYPE_APP_PANEL) {
    DCHECK(app_windows_.size() == 1);
    AppWindow* panel = app_windows_.front();
    aura::Window* panel_window = panel->GetNativeWindow();
    // If the panel is attached on another display, move it to the current
    // display and activate it.
    if (ash::wm::GetWindowState(panel_window)->panel_attached() &&
        ash::wm::MoveWindowToEventRoot(panel_window, event)) {
      if (!panel->GetBaseWindow()->IsActive())
        ShowAndActivateOrMinimize(panel);
    } else {
      ShowAndActivateOrMinimize(panel);
    }
  } else {
    AppWindow* window_to_show = last_active_app_window_
                                    ? last_active_app_window_
                                    : app_windows_.front();
    // If the event was triggered by a keystroke, we try to advance to the next
    // item if the window we are trying to activate is already active.
    if (app_windows_.size() >= 1 &&
        window_to_show->GetBaseWindow()->IsActive() &&
        event.type() == ui::ET_KEY_RELEASED) {
      ActivateOrAdvanceToNextAppWindow(window_to_show);
    } else {
      ShowAndActivateOrMinimize(window_to_show);
    }
  }
  return false;
}

base::string16 AppWindowLauncherItemController::GetTitle() {
  // For panels return the title of the contents if set.
  // Otherwise return the title of the app.
  if (type() == TYPE_APP_PANEL && !app_windows_.empty()) {
    AppWindow* app_window = app_windows_.front();
    if (app_window->web_contents()) {
      base::string16 title = app_window->web_contents()->GetTitle();
      if (!title.empty())
        return title;
    }
  }
  return GetAppTitle();
}

ui::MenuModel* AppWindowLauncherItemController::CreateContextMenu(
    aura::Window* root_window) {
  ash::ShelfItem item = *(launcher_controller()->model()->ItemByID(shelf_id()));
  return new LauncherContextMenu(launcher_controller(), &item, root_window);
}

ash::ShelfMenuModel* AppWindowLauncherItemController::CreateApplicationMenu(
    int event_flags) {
  return new LauncherApplicationMenuItemModel(GetApplicationList(event_flags));
}

bool AppWindowLauncherItemController::IsDraggable() {
  if (type() == TYPE_APP_PANEL)
    return true;
  return launcher_controller()->CanPin() ? true : false;
}

bool AppWindowLauncherItemController::ShouldShowTooltip() {
  if (type() == TYPE_APP_PANEL && IsVisible())
    return false;
  return true;
}

void AppWindowLauncherItemController::OnWindowPropertyChanged(
    aura::Window* window,
    const void* key,
    intptr_t old) {
  if (key == aura::client::kDrawAttentionKey) {
    ash::ShelfItemStatus status;
    if (ash::wm::IsActiveWindow(window)) {
      status = ash::STATUS_ACTIVE;
    } else if (window->GetProperty(aura::client::kDrawAttentionKey)) {
      status = ash::STATUS_ATTENTION;
    } else {
      status = ash::STATUS_RUNNING;
    }
    launcher_controller()->SetItemStatus(shelf_id(), status);
  }
}

void AppWindowLauncherItemController::ShowAndActivateOrMinimize(
    AppWindow* app_window) {
  // Either show or minimize windows when shown from the launcher.
  launcher_controller()->ActivateWindowOrMinimizeIfActive(
      app_window->GetBaseWindow(), GetApplicationList(0).size() == 2);
}

void AppWindowLauncherItemController::ActivateOrAdvanceToNextAppWindow(
    AppWindow* window_to_show) {
  AppWindowList::iterator i(
      std::find(app_windows_.begin(), app_windows_.end(), window_to_show));
  if (i != app_windows_.end()) {
    if (++i != app_windows_.end())
      window_to_show = *i;
    else
      window_to_show = app_windows_.front();
  }
  if (window_to_show->GetBaseWindow()->IsActive()) {
    // Coming here, only a single window is active. For keyboard activations
    // the window gets animated.
    AnimateWindow(window_to_show->GetNativeWindow(),
                  wm::WINDOW_ANIMATION_TYPE_BOUNCE);
  } else {
    ShowAndActivateOrMinimize(window_to_show);
  }
}