OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mash/login/ui.h" | |
6 | |
7 #include "base/guid.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "components/mus/public/cpp/property_type_converters.h" | |
10 #include "mash/login/login.h" | |
11 #include "mash/wm/public/interfaces/container.mojom.h" | |
12 #include "mojo/shell/public/cpp/connector.h" | |
13 #include "ui/views/background.h" | |
14 #include "ui/views/mus/native_widget_mus.h" | |
15 #include "ui/views/mus/window_manager_connection.h" | |
16 | |
17 namespace mash { | |
18 namespace login { | |
19 | |
20 // static | |
21 bool UI::is_showing_ = false; | |
22 | |
23 // static | |
24 void UI::Show(mojo::Connector* connector, LoginController* login_controller) { | |
25 // It's possible multiple clients may have a connection to the Login service, | |
26 // so make sure that only one login UI can be shown at a time. | |
27 if (is_showing_) | |
28 return; | |
29 | |
30 UI* ui = new UI(login_controller, connector); | |
31 | |
32 // TODO(beng): If this is only done once, it should be done in | |
33 // LoginController::Initialize(). However, for as yet unknown reasons it needs | |
34 // to be done the first time after UI(). Figure this out. Also, I'm not | |
35 // certain the window manager is being killed when this UI is closed. | |
36 if (!views::WindowManagerConnection::Exists()) | |
37 views::WindowManagerConnection::Create(connector); | |
38 | |
39 views::Widget* widget = new views::Widget; | |
40 views::Widget::InitParams params( | |
41 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
42 params.delegate = ui; | |
43 | |
44 std::map<std::string, std::vector<uint8_t>> properties; | |
45 properties[mash::wm::mojom::kWindowContainer_Property] = | |
46 mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert( | |
47 static_cast<int32_t>(mash::wm::mojom::Container::LOGIN_WINDOWS)); | |
48 mus::Window* window = | |
49 views::WindowManagerConnection::Get()->NewWindow(properties); | |
50 params.native_widget = new views::NativeWidgetMus( | |
51 widget, connector, window, mus::mojom::SurfaceType::DEFAULT); | |
52 widget->Init(params); | |
53 widget->Show(); | |
54 | |
55 is_showing_ = true; | |
56 } | |
57 | |
58 UI::UI(LoginController* login_controller, mojo::Connector* connector) | |
59 : login_controller_(login_controller), | |
60 connector_(connector), | |
61 user_id_1_("00000000-0000-4000-8000-000000000000"), | |
62 user_id_2_("00000000-0000-4000-8000-000000000001"), | |
63 login_button_1_( | |
64 new views::LabelButton(this, base::ASCIIToUTF16("Timothy"))), | |
65 login_button_2_( | |
66 new views::LabelButton(this, base::ASCIIToUTF16("Jimothy"))) { | |
67 connector_->ConnectToInterface("mojo:mus", &user_access_manager_); | |
68 user_access_manager_->SetActiveUser(login_controller->login_user_id()); | |
69 StartWindowManager(); | |
70 | |
71 set_background(views::Background::CreateSolidBackground(SK_ColorRED)); | |
72 login_button_1_->SetStyle(views::Button::STYLE_BUTTON); | |
73 login_button_2_->SetStyle(views::Button::STYLE_BUTTON); | |
74 AddChildView(login_button_1_); | |
75 AddChildView(login_button_2_); | |
76 } | |
77 | |
78 UI::~UI() { | |
79 // Prevent the window manager from restarting during graceful shutdown. | |
80 window_manager_connection_->SetConnectionLostClosure(base::Closure()); | |
81 is_showing_ = false; | |
82 // TODO(beng): we should be terminating this app at this point. | |
83 } | |
84 | |
85 views::View* UI::GetContentsView() { return this; } | |
86 | |
87 base::string16 UI::GetWindowTitle() const { | |
88 // TODO(beng): use resources. | |
89 return base::ASCIIToUTF16("Login"); | |
90 } | |
91 | |
92 void UI::DeleteDelegate() { | |
93 delete this; | |
94 } | |
95 | |
96 void UI::Layout() { | |
97 gfx::Rect button_box = GetLocalBounds(); | |
98 button_box.Inset(10, 10); | |
99 | |
100 gfx::Size ps1 = login_button_1_->GetPreferredSize(); | |
101 gfx::Size ps2 = login_button_2_->GetPreferredSize(); | |
102 | |
103 DCHECK(ps1.height() == ps2.height()); | |
104 | |
105 // The 10 is inter-button spacing. | |
106 button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); | |
107 button_box.set_y((button_box.height() - ps1.height()) / 2); | |
108 | |
109 login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), | |
110 ps1.height()); | |
111 login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, | |
112 button_box.y(), ps2.width(), ps2.height()); | |
113 } | |
114 | |
115 void UI::ButtonPressed(views::Button* sender, const ui::Event& event) { | |
116 // Login... | |
117 if (sender == login_button_1_) { | |
118 user_access_manager_->SetActiveUser(user_id_1_); | |
119 login_controller_->init()->StartService("mojo:mash_shell", user_id_1_); | |
120 } else if (sender == login_button_2_) { | |
121 user_access_manager_->SetActiveUser(user_id_2_); | |
122 login_controller_->init()->StartService("mojo:mash_shell", user_id_2_); | |
123 } else { | |
124 NOTREACHED(); | |
125 } | |
126 GetWidget()->Close(); | |
127 } | |
128 | |
129 void UI::StartWindowManager() { | |
130 window_manager_connection_ = connector_->Connect("mojo:desktop_wm"); | |
131 window_manager_connection_->SetConnectionLostClosure( | |
132 base::Bind(&UI::StartWindowManager, base::Unretained(this))); | |
133 } | |
134 | |
135 } // namespace login | |
136 } // namespace mash | |
OLD | NEW |