This source file includes following definitions.
- is_default_view
- last_percent_
- SetBrightnessPercent
- OnMaximizeModeStarted
- OnMaximizeModeEnded
- OnBoundsChanged
- SliderValueChanged
- SliderDragStarted
- SliderDragEnded
- got_current_percent_
- GetInitialBrightness
- HandleInitialBrightness
- CreateTrayView
- CreateDefaultView
- CreateDetailedView
- DestroyTrayView
- DestroyDefaultView
- DestroyDetailedView
- UpdateAfterLoginStatusChange
- ShouldHideArrow
- ShouldShowShelf
- BrightnessChanged
- HandleBrightnessChanged
#include "ash/system/chromeos/brightness/tray_brightness.h"
#include "ash/accelerators/accelerator_controller.h"
#include "ash/ash_constants.h"
#include "ash/display/display_manager.h"
#include "ash/metrics/user_metrics_recorder.h"
#include "ash/shell.h"
#include "ash/shell_observer.h"
#include "ash/system/brightness_control_delegate.h"
#include "ash/system/tray/fixed_sized_image_view.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/system/tray/tray_constants.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/power_manager_client.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/slider.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
namespace ash {
namespace tray {
namespace {
const double kMinBrightnessPercent = 5.0;
}
class BrightnessView : public ShellObserver,
public views::View,
public views::SliderListener {
public:
BrightnessView(bool default_view, double initial_percent);
virtual ~BrightnessView();
bool is_default_view() const {
return is_default_view_;
}
void SetBrightnessPercent(double percent);
virtual void OnMaximizeModeStarted() OVERRIDE;
virtual void OnMaximizeModeEnded() OVERRIDE;
private:
virtual void OnBoundsChanged(const gfx::Rect& old_bounds) OVERRIDE;
virtual void SliderValueChanged(views::Slider* sender,
float value,
float old_value,
views::SliderChangeReason reason) OVERRIDE;
virtual void SliderDragStarted(views::Slider* slider) OVERRIDE;
virtual void SliderDragEnded(views::Slider* slider) OVERRIDE;
views::Slider* slider_;
bool dragging_;
bool is_default_view_;
double last_percent_;
DISALLOW_COPY_AND_ASSIGN(BrightnessView);
};
BrightnessView::BrightnessView(bool default_view, double initial_percent)
: dragging_(false),
is_default_view_(default_view),
last_percent_(initial_percent) {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems));
views::ImageView* icon = new FixedSizedImageView(0, kTrayPopupItemHeight);
gfx::Image image = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_AURA_UBER_TRAY_BRIGHTNESS);
icon->SetImage(image.ToImageSkia());
AddChildView(icon);
slider_ = new views::Slider(this, views::Slider::HORIZONTAL);
slider_->set_focus_border_color(kFocusBorderColor);
slider_->SetValue(static_cast<float>(initial_percent / 100.0));
slider_->SetAccessibleName(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_ASH_STATUS_TRAY_BRIGHTNESS));
AddChildView(slider_);
if (is_default_view_) {
Shell::GetInstance()->AddShellObserver(this);
SetVisible(Shell::GetInstance()->IsMaximizeModeWindowManagerEnabled());
}
}
BrightnessView::~BrightnessView() {
if (is_default_view_)
Shell::GetInstance()->RemoveShellObserver(this);
}
void BrightnessView::SetBrightnessPercent(double percent) {
last_percent_ = percent;
if (!dragging_)
slider_->SetValue(static_cast<float>(percent / 100.0));
}
void BrightnessView::OnMaximizeModeStarted() {
SetVisible(true);
}
void BrightnessView::OnMaximizeModeEnded() {
SetVisible(false);
}
void BrightnessView::OnBoundsChanged(const gfx::Rect& old_bounds) {
int w = width() - slider_->x();
slider_->SetSize(gfx::Size(w, slider_->height()));
}
void BrightnessView::SliderValueChanged(views::Slider* sender,
float value,
float old_value,
views::SliderChangeReason reason) {
DCHECK_EQ(sender, slider_);
if (reason != views::VALUE_CHANGED_BY_USER)
return;
AcceleratorController* ac = Shell::GetInstance()->accelerator_controller();
if (ac->brightness_control_delegate()) {
double percent = std::max(value * 100.0, kMinBrightnessPercent);
ac->brightness_control_delegate()->SetBrightnessPercent(percent, true);
}
}
void BrightnessView::SliderDragStarted(views::Slider* slider) {
DCHECK_EQ(slider, slider_);
dragging_ = true;
}
void BrightnessView::SliderDragEnded(views::Slider* slider) {
DCHECK_EQ(slider, slider_);
dragging_ = false;
slider_->SetValue(static_cast<float>(last_percent_ / 100.0));
}
}
TrayBrightness::TrayBrightness(SystemTray* system_tray)
: SystemTrayItem(system_tray),
weak_ptr_factory_(this),
brightness_view_(NULL),
current_percent_(100.0),
got_current_percent_(false) {
base::MessageLoopForUI::current()->PostTask(
FROM_HERE,
base::Bind(&TrayBrightness::GetInitialBrightness,
weak_ptr_factory_.GetWeakPtr()));
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
AddObserver(this);
}
TrayBrightness::~TrayBrightness() {
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
RemoveObserver(this);
}
void TrayBrightness::GetInitialBrightness() {
BrightnessControlDelegate* delegate =
Shell::GetInstance()->accelerator_controller()->
brightness_control_delegate();
if (!delegate)
return;
delegate->GetBrightnessPercent(
base::Bind(&TrayBrightness::HandleInitialBrightness,
weak_ptr_factory_.GetWeakPtr()));
}
void TrayBrightness::HandleInitialBrightness(double percent) {
if (!got_current_percent_)
HandleBrightnessChanged(percent, false);
}
views::View* TrayBrightness::CreateTrayView(user::LoginStatus status) {
return NULL;
}
views::View* TrayBrightness::CreateDefaultView(user::LoginStatus status) {
CHECK(brightness_view_ == NULL);
brightness_view_ = new tray::BrightnessView(true, current_percent_);
return brightness_view_;
}
views::View* TrayBrightness::CreateDetailedView(user::LoginStatus status) {
CHECK(brightness_view_ == NULL);
Shell::GetInstance()->metrics()->RecordUserMetricsAction(
ash::UMA_STATUS_AREA_DETAILED_BRIGHTNESS_VIEW);
brightness_view_ = new tray::BrightnessView(false, current_percent_);
return brightness_view_;
}
void TrayBrightness::DestroyTrayView() {
}
void TrayBrightness::DestroyDefaultView() {
if (brightness_view_ && brightness_view_->is_default_view())
brightness_view_ = NULL;
}
void TrayBrightness::DestroyDetailedView() {
if (brightness_view_ && !brightness_view_->is_default_view())
brightness_view_ = NULL;
}
void TrayBrightness::UpdateAfterLoginStatusChange(user::LoginStatus status) {
}
bool TrayBrightness::ShouldHideArrow() const {
return true;
}
bool TrayBrightness::ShouldShowShelf() const {
return false;
}
void TrayBrightness::BrightnessChanged(int level, bool user_initiated) {
Shell::GetInstance()->metrics()->RecordUserMetricsAction(
ash::UMA_STATUS_AREA_BRIGHTNESS_CHANGED);
double percent = static_cast<double>(level);
HandleBrightnessChanged(percent, user_initiated);
}
void TrayBrightness::HandleBrightnessChanged(double percent,
bool user_initiated) {
current_percent_ = percent;
got_current_percent_ = true;
if (brightness_view_)
brightness_view_->SetBrightnessPercent(percent);
if (!user_initiated)
return;
if (!Shell::GetInstance()->display_manager()->HasInternalDisplay())
return;
if (brightness_view_)
SetDetailedViewCloseDelay(kTrayPopupAutoCloseDelayInSeconds);
else
PopupDetailedView(kTrayPopupAutoCloseDelayInSeconds, false);
}
}