OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "ash/wm/overview/transparent_activate_window_button.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "ash/shell.h" | |
10 #include "ash/shell_window_ids.h" | |
11 #include "ash/wm/overview/transparent_activate_window_button_delegate.h" | |
12 #include "ui/events/event.h" | |
13 #include "ui/events/gesture_event_details.h" | |
14 #include "ui/gfx/display.h" | |
15 #include "ui/gfx/screen.h" | |
16 #include "ui/views/controls/button/button.h" | |
17 #include "ui/views/controls/button/custom_button.h" | |
18 #include "ui/views/widget/widget.h" | |
19 | |
20 namespace ash { | |
21 | |
22 namespace { | |
23 const char kTransparentButtonName[] = "TransparentButton"; | |
24 | |
25 // Transparent button that handles events to activate or close windows in | |
26 // overview mode. | |
27 class TransparentButton | |
28 : public views::CustomButton, | |
29 public views::ButtonListener { | |
30 public: | |
31 explicit TransparentButton(TransparentActivateWindowButtonDelegate* delegate); | |
32 | |
33 ~TransparentButton() override; | |
34 | |
35 TransparentActivateWindowButtonDelegate* delegate() { return delegate_; } | |
36 | |
37 // views::View: | |
38 void OnGestureEvent(ui::GestureEvent* event) override; | |
39 | |
40 const char* GetClassName() const override { return kTransparentButtonName; } | |
41 | |
42 // views::ButtonListener: | |
43 void ButtonPressed(views::Button* sender, const ui::Event& event) override; | |
44 | |
45 private: | |
46 TransparentActivateWindowButtonDelegate* delegate_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(TransparentButton); | |
49 }; | |
50 | |
51 TransparentButton::TransparentButton( | |
52 TransparentActivateWindowButtonDelegate* delegate) | |
53 : CustomButton(this), | |
54 delegate_(delegate) { | |
55 } | |
56 | |
57 TransparentButton::~TransparentButton() { | |
58 } | |
59 | |
60 void TransparentButton::OnGestureEvent(ui::GestureEvent* event) { | |
61 // TODO(tdanderson): Re-evaluate whether we want to set capture once | |
62 // the a fix has landed to avoid crashing when a window | |
63 // having an active gesture sequence is destroyed as a | |
64 // result of a gesture in a separate window. Consider using | |
65 // a StaticWindowTargeter instead of a transparent overlay | |
66 // widget to re-direct input events. | |
67 if (event->type() == ui::ET_GESTURE_TAP_DOWN) | |
68 GetWidget()->SetCapture(this); | |
69 | |
70 if (event->type() == ui::ET_GESTURE_TAP || | |
71 event->type() == ui::ET_GESTURE_END) { | |
72 GetWidget()->ReleaseCapture(); | |
73 } | |
74 | |
75 CustomButton::OnGestureEvent(event); | |
76 event->StopPropagation(); | |
77 } | |
78 | |
79 void TransparentButton::ButtonPressed(views::Button* sender, | |
80 const ui::Event& event) { | |
81 delegate_->Select(); | |
82 } | |
83 | |
84 // Initializes the event handler transparent window. | |
85 views::Widget* InitEventHandler(aura::Window* root_window) { | |
86 views::Widget* widget = new views::Widget; | |
87 views::Widget::InitParams params; | |
88 params.type = views::Widget::InitParams::TYPE_POPUP; | |
89 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
90 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
91 params.accept_events = true; | |
92 params.parent = Shell::GetContainer(root_window, | |
93 kShellWindowId_OverlayContainer); | |
94 widget->set_focus_on_creation(false); | |
95 widget->Init(params); | |
96 widget->Show(); | |
97 | |
98 aura::Window* handler = widget->GetNativeWindow(); | |
99 handler->parent()->StackChildAtBottom(handler); | |
100 | |
101 return widget; | |
102 } | |
103 | |
104 } // namespace | |
105 | |
106 TransparentActivateWindowButton::TransparentActivateWindowButton( | |
107 aura::Window* root_window, | |
108 TransparentActivateWindowButtonDelegate* delegate) | |
109 : event_handler_widget_(InitEventHandler(root_window)), | |
110 transparent_button_(new TransparentButton(delegate)) { | |
111 event_handler_widget_->SetContentsView(transparent_button_); | |
112 } | |
113 | |
114 TransparentActivateWindowButton::~TransparentActivateWindowButton() { | |
115 } | |
116 | |
117 void TransparentActivateWindowButton::SetAccessibleName( | |
118 const base::string16& name) { | |
119 transparent_button_->SetAccessibleName(name); | |
120 } | |
121 | |
122 void TransparentActivateWindowButton::SetBounds(const gfx::Rect& bounds) { | |
123 aura::Window* window = event_handler_widget_->GetNativeWindow(); | |
124 const gfx::Display& display = gfx::Screen::GetScreenFor( | |
125 window)->GetDisplayNearestWindow(window); | |
126 // Explicitly specify the display so that the window doesn't change root | |
127 // windows and cause the z-order of the transparent overlays to be updated. | |
128 window->SetBoundsInScreen(bounds, display); | |
129 } | |
130 | |
131 void TransparentActivateWindowButton::SendFocusAlert() const { | |
132 event_handler_widget_->GetContentsView()-> | |
133 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); | |
134 } | |
135 | |
136 } // namespace ash | |
OLD | NEW |