summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/window_switcher_button.cc
blob: f51129d6034b2f3a7af13e8dd2397d08c2408d58 (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
// 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/chromeos/status/window_switcher_button.h"

#include "chrome/browser/chromeos/status/status_area_host.h"
#include "chrome/browser/chromeos/wm_ipc.h"
#include "chrome/browser/ui/browser_window.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"

namespace chromeos {

namespace {
int GetNormalBrowserCount() {
  int count = 0;
  BrowserList::const_iterator iter;
  for (iter = BrowserList::begin(); iter != BrowserList::end(); ++iter) {
    if ((*iter)->is_type_tabbed())
      count++;
  }
  return count;
}
}

WindowSwitcherButton::WindowSwitcherButton(StatusAreaHost* host)
    : StatusAreaButton(host, this) {
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WINDOW_SWITCHER));
  SetEnabled(true);

  UpdateStatus();
  BrowserList::AddObserver(this);
}

WindowSwitcherButton::~WindowSwitcherButton() {
  BrowserList::RemoveObserver(this);
}

void WindowSwitcherButton::UpdateStatus() {
  if (GetNormalBrowserCount() < 2) {
    // There are less than two browsers.  This means we can't switch
    // anywhere, so we disappear.
    SetTooltipText(L"");
    SetVisible(false);
    PreferredSizeChanged();
    return;
  }

  SetTooltipText(UTF16ToWide(l10n_util::GetStringUTF16(
      IDS_STATUSBAR_WINDOW_SWITCHER_TOOLTIP)));

  // There are at least two browsers, so we show ourselves.
  SetVisible(true);
  PreferredSizeChanged();
}

////////////////////////////////////////////////////////////////////////////////
// WindowSwitcherButton, views::ViewMenuDelegate implementation:

void WindowSwitcherButton::RunMenu(views::View* source, const gfx::Point& pt) {
  // Don't do anything if there aren't at least two normal browsers to
  // switch between.
  if (GetNormalBrowserCount() < 2)
    return;

  // Send a message to the ChromeOS window manager to switch to the
  // next top level browser window.  Only the window manager knows
  // what order they are in, so we can't use Chrome's browser list
  // to decide.
  WmIpc::Message message(chromeos::WM_IPC_MESSAGE_WM_CYCLE_WINDOWS);
  message.set_param(0, true);
  WmIpc::instance()->SendMessage(message);
}

////////////////////////////////////////////////////////////////////////////////
// BrowserList::Observer implementation:

// Called immediately after a browser is added to the list
void WindowSwitcherButton::OnBrowserAdded(const Browser* browser) {
  UpdateStatus();
}

// Called immediately after a browser is removed from the list
void WindowSwitcherButton::OnBrowserRemoved(const Browser* browser) {
  UpdateStatus();
}

}  // namespace chromeos