| 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 if (!views::WindowManagerConnection::Get()) |
| 33 views::WindowManagerConnection::Create(connector); |
| 34 |
| 35 views::Widget* widget = new views::Widget; |
| 36 views::Widget::InitParams params( |
| 37 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 38 params.delegate = ui; |
| 39 |
| 40 std::map<std::string, std::vector<uint8_t>> properties; |
| 41 properties[mash::wm::mojom::kWindowContainer_Property] = |
| 42 mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert( |
| 43 static_cast<int32_t>(mash::wm::mojom::Container::LOGIN_WINDOWS)); |
| 44 mus::Window* window = |
| 45 views::WindowManagerConnection::Get()->NewWindow(properties); |
| 46 params.native_widget = new views::NativeWidgetMus( |
| 47 widget, connector, window, mus::mojom::SurfaceType::DEFAULT); |
| 48 widget->Init(params); |
| 49 widget->Show(); |
| 50 |
| 51 is_showing_ = true; |
| 52 } |
| 53 |
| 54 UI::UI(LoginController* login_controller, mojo::Connector* connector) |
| 55 : login_controller_(login_controller), |
| 56 connector_(connector), |
| 57 user_id_1_("00000000-0000-4000-8000-000000000000"), |
| 58 user_id_2_("00000000-0000-4000-8000-000000000001"), |
| 59 login_button_1_( |
| 60 new views::LabelButton(this, base::ASCIIToUTF16("Timothy"))), |
| 61 login_button_2_( |
| 62 new views::LabelButton(this, base::ASCIIToUTF16("Jimothy"))) { |
| 63 connector_->ConnectToInterface("mojo:mus", &user_access_manager_); |
| 64 user_access_manager_->SetActiveUser(login_controller->login_user_id()); |
| 65 StartWindowManager(); |
| 66 |
| 67 set_background(views::Background::CreateSolidBackground(SK_ColorRED)); |
| 68 login_button_1_->SetStyle(views::Button::STYLE_BUTTON); |
| 69 login_button_2_->SetStyle(views::Button::STYLE_BUTTON); |
| 70 AddChildView(login_button_1_); |
| 71 AddChildView(login_button_2_); |
| 72 } |
| 73 |
| 74 UI::~UI() { |
| 75 // Prevent the window manager from restarting during graceful shutdown. |
| 76 window_manager_connection_->SetConnectionLostClosure(base::Closure()); |
| 77 is_showing_ = false; |
| 78 } |
| 79 |
| 80 views::View* UI::GetContentsView() { return this; } |
| 81 |
| 82 base::string16 UI::GetWindowTitle() const { |
| 83 // TODO(beng): use resources. |
| 84 return base::ASCIIToUTF16("Login"); |
| 85 } |
| 86 |
| 87 void UI::DeleteDelegate() { |
| 88 delete this; |
| 89 } |
| 90 |
| 91 void UI::Layout() { |
| 92 gfx::Rect button_box = GetLocalBounds(); |
| 93 button_box.Inset(10, 10); |
| 94 |
| 95 gfx::Size ps1 = login_button_1_->GetPreferredSize(); |
| 96 gfx::Size ps2 = login_button_2_->GetPreferredSize(); |
| 97 |
| 98 DCHECK(ps1.height() == ps2.height()); |
| 99 |
| 100 // The 10 is inter-button spacing. |
| 101 button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); |
| 102 button_box.set_y((button_box.height() - ps1.height()) / 2); |
| 103 |
| 104 login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), |
| 105 ps1.height()); |
| 106 login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, |
| 107 button_box.y(), ps2.width(), ps2.height()); |
| 108 } |
| 109 |
| 110 void UI::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 111 // Login... |
| 112 if (sender == login_button_1_) { |
| 113 user_access_manager_->SetActiveUser(user_id_1_); |
| 114 login_controller_->init()->StartService("mojo:mash_shell", user_id_1_); |
| 115 } else if (sender == login_button_2_) { |
| 116 user_access_manager_->SetActiveUser(user_id_2_); |
| 117 login_controller_->init()->StartService("mojo:mash_shell", user_id_2_); |
| 118 } else { |
| 119 NOTREACHED(); |
| 120 } |
| 121 GetWidget()->Close(); |
| 122 } |
| 123 |
| 124 void UI::StartWindowManager() { |
| 125 window_manager_connection_ = connector_->Connect("mojo:desktop_wm"); |
| 126 window_manager_connection_->SetConnectionLostClosure( |
| 127 base::Bind(&UI::StartWindowManager, base::Unretained(this))); |
| 128 } |
| 129 |
| 130 } // namespace login |
| 131 } // namespace mash |
| OLD | NEW |