summaryrefslogtreecommitdiffstats
path: root/ash/display/display_error_dialog.cc
blob: 604d86acf6d9d9d4c9017f8cae2d216b45ade1e0 (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
// Copyright (c) 2012 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 "ash/display/display_error_dialog.h"

#include "ash/screen_ash.h"
#include "ash/shell.h"
#include "grit/ash_strings.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace internal {
namespace {

// The width of the area to show the error message.
const int kDialogMessageWidthPixel = 300;

// The margin width from the error message to the edge of the dialog.
const int kDialogMessageMarginWidthPixel = 5;

DisplayErrorDialog* g_instance = NULL;

}  // namespace

// static
void  DisplayErrorDialog::ShowDialog() {
  if (g_instance) {
    DCHECK(g_instance->GetWidget());
    g_instance->GetWidget()->StackAtTop();
    g_instance->GetWidget()->Activate();
    return;
  }

  gfx::Screen* screen = Shell::GetScreen();
  const gfx::Display& target_display =
      (screen->GetNumDisplays() > 1) ?
      ScreenAsh::GetSecondaryDisplay() : screen->GetPrimaryDisplay();

  g_instance = new DisplayErrorDialog();
  views::Widget* widget = new views::Widget;
  views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
  params.delegate = g_instance;
  // Makes |widget| belong to the target display.  Size and location are
  // fixed by CenterWindow() below.
  params.bounds = target_display.bounds();
  DisplayController* display_controller =
      Shell::GetInstance()->display_controller();
  params.context =
      display_controller->GetRootWindowForDisplayId(target_display.id());
  params.keep_on_top = true;
  widget->Init(params);

  widget->GetNativeView()->SetName("DisplayErrorDialog");
  widget->CenterWindow(widget->GetRootView()->GetPreferredSize());
  widget->Show();
}

DisplayErrorDialog::DisplayErrorDialog() {
  Shell::GetInstance()->display_controller()->AddObserver(this);
  label_ = new views::Label(
      l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_FAILURE_ON_MIRRORING));
  AddChildView(label_);

  label_->SetMultiLine(true);
  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label_->set_border(views::Border::CreateEmptyBorder(
      kDialogMessageMarginWidthPixel,
      kDialogMessageMarginWidthPixel,
      kDialogMessageMarginWidthPixel,
      kDialogMessageMarginWidthPixel));
  label_->SizeToFit(kDialogMessageWidthPixel);
}

DisplayErrorDialog::~DisplayErrorDialog() {
  Shell::GetInstance()->display_controller()->RemoveObserver(this);
  g_instance = NULL;
}

int DisplayErrorDialog::GetDialogButtons() const {
  return ui::DIALOG_BUTTON_OK;
}

ui::ModalType DisplayErrorDialog::GetModalType() const {
  return ui::MODAL_TYPE_NONE;
}

gfx::Size DisplayErrorDialog::GetPreferredSize() {
  return label_->GetPreferredSize();
}

void DisplayErrorDialog::OnDisplayConfigurationChanging() {
  GetWidget()->Close();
}

// static
DisplayErrorDialog* DisplayErrorDialog::GetInstanceForTest() {
  return g_instance;
}

}  // namespace internal
}  // namespace ash