| 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 } |
| 83 |
| 84 views::View* UI::GetContentsView() { return this; } |
| 85 |
| 86 base::string16 UI::GetWindowTitle() const { |
| 87 // TODO(beng): use resources. |
| 88 return base::ASCIIToUTF16("Login"); |
| 89 } |
| 90 |
| 91 void UI::DeleteDelegate() { |
| 92 delete this; |
| 93 } |
| 94 |
| 95 void UI::Layout() { |
| 96 gfx::Rect button_box = GetLocalBounds(); |
| 97 button_box.Inset(10, 10); |
| 98 |
| 99 gfx::Size ps1 = login_button_1_->GetPreferredSize(); |
| 100 gfx::Size ps2 = login_button_2_->GetPreferredSize(); |
| 101 |
| 102 DCHECK(ps1.height() == ps2.height()); |
| 103 |
| 104 // The 10 is inter-button spacing. |
| 105 button_box.set_x((button_box.width() - ps1.width() - ps2.width() - 10) / 2); |
| 106 button_box.set_y((button_box.height() - ps1.height()) / 2); |
| 107 |
| 108 login_button_1_->SetBounds(button_box.x(), button_box.y(), ps1.width(), |
| 109 ps1.height()); |
| 110 login_button_2_->SetBounds(login_button_1_->bounds().right() + 10, |
| 111 button_box.y(), ps2.width(), ps2.height()); |
| 112 } |
| 113 |
| 114 void UI::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 115 // Login... |
| 116 if (sender == login_button_1_) { |
| 117 user_access_manager_->SetActiveUser(user_id_1_); |
| 118 login_controller_->init()->StartService("mojo:mash_shell", user_id_1_); |
| 119 } else if (sender == login_button_2_) { |
| 120 user_access_manager_->SetActiveUser(user_id_2_); |
| 121 login_controller_->init()->StartService("mojo:mash_shell", user_id_2_); |
| 122 } else { |
| 123 NOTREACHED(); |
| 124 } |
| 125 GetWidget()->Close(); |
| 126 } |
| 127 |
| 128 void UI::StartWindowManager() { |
| 129 window_manager_connection_ = connector_->Connect("mojo:desktop_wm"); |
| 130 window_manager_connection_->SetConnectionLostClosure( |
| 131 base::Bind(&UI::StartWindowManager, base::Unretained(this))); |
| 132 } |
| 133 |
| 134 } // namespace login |
| 135 } // namespace mash |
| OLD | NEW |