summaryrefslogtreecommitdiffstats
path: root/athena/input/accelerator_manager_impl.cc
blob: ad0302308825974ae9c37587443d046e2183e95c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2014 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 "athena/input/accelerator_manager_impl.h"

#include "athena/input/public/input_manager.h"
#include "athena/util/switches.h"
#include "base/logging.h"
#include "ui/aura/window.h"
#include "ui/base/accelerators/accelerator_manager.h"
#include "ui/events/event.h"
#include "ui/events/event_target.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/focus/focus_manager_delegate.h"
#include "ui/views/focus/focus_manager_factory.h"
#include "ui/wm/core/accelerator_delegate.h"
#include "ui/wm/core/accelerator_filter.h"
#include "ui/wm/core/nested_accelerator_controller.h"
#include "ui/wm/core/nested_accelerator_delegate.h"
#include "ui/wm/public/dispatcher_client.h"

namespace athena {

// This wrapper interface provides a common interface that handles global
// accelerators as well as local accelerators.
class AcceleratorManagerImpl::AcceleratorWrapper {
 public:
  virtual ~AcceleratorWrapper() {}
  virtual void Register(const ui::Accelerator& accelerator,
                        ui::AcceleratorTarget* target) = 0;
  virtual void Unregister(const ui::Accelerator& accelerator,
                          ui::AcceleratorTarget* target) = 0;
  virtual bool Process(const ui::Accelerator& accelerator) = 0;
  virtual ui::AcceleratorTarget* GetCurrentTarget(
      const ui::Accelerator& accelerator) const = 0;
};

namespace {

// Accelerators inside nested message loop are handled by
// wm::NestedAcceleratorController while accelerators in normal case are
// handled by wm::AcceleratorFilter. These delegates act bridges in these
// two different environment so that AcceleratorManagerImpl can handle
// accelerators in an uniform way.

class NestedAcceleratorDelegate : public wm::NestedAcceleratorDelegate {
 public:
  explicit NestedAcceleratorDelegate(
      AcceleratorManagerImpl* accelerator_manager)
      : accelerator_manager_(accelerator_manager) {}
  ~NestedAcceleratorDelegate() override {}

 private:
  // wm::NestedAcceleratorDelegate:
  Result ProcessAccelerator(const ui::Accelerator& accelerator) override {
    return accelerator_manager_->Process(accelerator) ? RESULT_PROCESSED
                                                      : RESULT_NOT_PROCESSED;
  }

  AcceleratorManagerImpl* accelerator_manager_;

  DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorDelegate);
};

class AcceleratorDelegate : public wm::AcceleratorDelegate {
 public:
  explicit AcceleratorDelegate(AcceleratorManagerImpl* accelerator_manager)
      : accelerator_manager_(accelerator_manager) {}
  ~AcceleratorDelegate() override {}

 private:
  // wm::AcceleratorDelegate:
  bool ProcessAccelerator(const ui::KeyEvent& event,
                          const ui::Accelerator& accelerator,
                          KeyType key_type) override {
    aura::Window* target = static_cast<aura::Window*>(event.target());
    if (!target->IsRootWindow() &&
        !accelerator_manager_->IsRegistered(accelerator, AF_RESERVED)) {
      // TODO(oshima): do the same when the active window is in fullscreen.
      return false;
    }
    return accelerator_manager_->Process(accelerator);
  }

  AcceleratorManagerImpl* accelerator_manager_;

  DISALLOW_COPY_AND_ASSIGN(AcceleratorDelegate);
};

class FocusManagerDelegate : public views::FocusManagerDelegate {
 public:
  explicit FocusManagerDelegate(AcceleratorManagerImpl* accelerator_manager)
      : accelerator_manager_(accelerator_manager) {}
  ~FocusManagerDelegate() override {}

  bool ProcessAccelerator(const ui::Accelerator& accelerator) override {
    return accelerator_manager_->Process(accelerator);
  }

  ui::AcceleratorTarget* GetCurrentTargetForAccelerator(
      const ui::Accelerator& accelerator) const override {
    return accelerator_manager_->IsRegistered(accelerator, AF_NONE)
               ? accelerator_manager_
               : nullptr;
  }

 private:
  AcceleratorManagerImpl* accelerator_manager_;

  DISALLOW_COPY_AND_ASSIGN(FocusManagerDelegate);
};

// Key strokes must be sent to web contents to give them a chance to
// consume them unless they are reserved, and unhandled key events are
// sent back to focus manager asynchronously. This installs the athena's
// focus manager that handles athena shell's accelerators.
class FocusManagerFactory : public views::FocusManagerFactory {
 public:
  explicit FocusManagerFactory(AcceleratorManagerImpl* accelerator_manager)
      : accelerator_manager_(accelerator_manager) {}
  ~FocusManagerFactory() override {}

  views::FocusManager* CreateFocusManager(views::Widget* widget,
                                          bool desktop_widget) override {
    return new views::FocusManager(
        widget,
        desktop_widget ? nullptr
                       : new FocusManagerDelegate(accelerator_manager_));
  }

 private:
  AcceleratorManagerImpl* accelerator_manager_;

  DISALLOW_COPY_AND_ASSIGN(FocusManagerFactory);
};

class UIAcceleratorManagerWrapper
    : public AcceleratorManagerImpl::AcceleratorWrapper {
 public:
  UIAcceleratorManagerWrapper()
      : ui_accelerator_manager_(new ui::AcceleratorManager) {}
  ~UIAcceleratorManagerWrapper() override {}

  virtual void Register(const ui::Accelerator& accelerator,
                        ui::AcceleratorTarget* target) override {
    ui_accelerator_manager_->Register(
        accelerator, ui::AcceleratorManager::kNormalPriority, target);
  }

  virtual void Unregister(const ui::Accelerator& accelerator,
                          ui::AcceleratorTarget* target) override {
    ui_accelerator_manager_->Unregister(accelerator, target);
  }

  virtual bool Process(const ui::Accelerator& accelerator) override {
    return ui_accelerator_manager_->Process(accelerator);
  }

  virtual ui::AcceleratorTarget* GetCurrentTarget(
      const ui::Accelerator& accelerator) const override {
    return ui_accelerator_manager_->GetCurrentTarget(accelerator);
  }

 private:
  scoped_ptr<ui::AcceleratorManager> ui_accelerator_manager_;

  DISALLOW_COPY_AND_ASSIGN(UIAcceleratorManagerWrapper);
};

class FocusManagerWrapper : public AcceleratorManagerImpl::AcceleratorWrapper {
 public:
  explicit FocusManagerWrapper(views::FocusManager* focus_manager)
      : focus_manager_(focus_manager) {}
  ~FocusManagerWrapper() override {}

  virtual void Register(const ui::Accelerator& accelerator,
                        ui::AcceleratorTarget* target) override {
    return focus_manager_->RegisterAccelerator(
        accelerator, ui::AcceleratorManager::kNormalPriority, target);
  }

  virtual void Unregister(const ui::Accelerator& accelerator,
                          ui::AcceleratorTarget* target) override {
    focus_manager_->UnregisterAccelerator(accelerator, target);
  }

  virtual bool Process(const ui::Accelerator& accelerator) override {
    NOTREACHED();
    return true;
  }

  virtual ui::AcceleratorTarget* GetCurrentTarget(
      const ui::Accelerator& accelerator) const override {
    return focus_manager_->GetCurrentTargetForAccelerator(accelerator);
  }

 private:
  views::FocusManager* focus_manager_;

  DISALLOW_COPY_AND_ASSIGN(FocusManagerWrapper);
};

}  // namespace

class AcceleratorManagerImpl::InternalData {
 public:
  InternalData(int command_id, AcceleratorHandler* handler, int flags)
      : command_id_(command_id), handler_(handler), flags_(flags) {}

  bool IsNonAutoRepeatable() const { return flags_ & AF_NON_AUTO_REPEATABLE; }
  bool IsDebug() const { return flags_ & AF_DEBUG; }
  int flags() const { return flags_; }

  bool IsCommandEnabled() const {
    return handler_->IsCommandEnabled(command_id_);
  }

  bool OnAcceleratorFired(const ui::Accelerator& accelerator) {
    return handler_->OnAcceleratorFired(command_id_, accelerator);
  }

 private:
  int command_id_;
  AcceleratorHandler* handler_;
  int flags_;

  // This class is copyable by design.
};

// static
AcceleratorManagerImpl*
AcceleratorManagerImpl::CreateGlobalAcceleratorManager() {
  return new AcceleratorManagerImpl(new UIAcceleratorManagerWrapper(), true);
}

scoped_ptr<AcceleratorManager> AcceleratorManagerImpl::CreateForFocusManager(
    views::FocusManager* focus_manager) {
  return scoped_ptr<AcceleratorManager>(
             new AcceleratorManagerImpl(new FocusManagerWrapper(focus_manager),
                                        false)).Pass();
}

AcceleratorManagerImpl::~AcceleratorManagerImpl() {
  nested_accelerator_controller_.reset();
  accelerator_filter_.reset();
  // Reset to use the default focus manager because the athena's
  // FocusManager has the reference to this object.
  if (global_)
    views::FocusManagerFactory::Install(nullptr);
}

void AcceleratorManagerImpl::Init() {
  if (global_)
    views::FocusManagerFactory::Install(new FocusManagerFactory(this));

  ui::EventTarget* toplevel = InputManager::Get()->GetTopmostEventTarget();
  nested_accelerator_controller_.reset(
      new wm::NestedAcceleratorController(new NestedAcceleratorDelegate(this)));

  scoped_ptr<wm::AcceleratorDelegate> accelerator_delegate(
      new AcceleratorDelegate(this));

  accelerator_filter_.reset(
      new wm::AcceleratorFilter(accelerator_delegate.Pass()));
  toplevel->AddPreTargetHandler(accelerator_filter_.get());
}

void AcceleratorManagerImpl::OnRootWindowCreated(aura::Window* root_window) {
  aura::client::SetDispatcherClient(root_window,
                                    nested_accelerator_controller_.get());
}

bool AcceleratorManagerImpl::Process(const ui::Accelerator& accelerator) {
  return accelerator_wrapper_->Process(accelerator);
}

bool AcceleratorManagerImpl::IsRegistered(const ui::Accelerator& accelerator,
                                          int flags) const {
  std::map<ui::Accelerator, InternalData>::const_iterator iter =
      accelerators_.find(accelerator);
  if (iter == accelerators_.end())
    return false;
  DCHECK(accelerator_wrapper_->GetCurrentTarget(accelerator));
  return flags == AF_NONE || iter->second.flags() & flags;
}

AcceleratorManagerImpl::AcceleratorManagerImpl(
    AcceleratorWrapper* accelerator_wrapper,
    bool global)
    : accelerator_wrapper_(accelerator_wrapper),
      debug_accelerators_enabled_(switches::IsDebugAcceleratorsEnabled()),
      global_(global) {
}

void AcceleratorManagerImpl::RegisterAccelerators(
    const AcceleratorData accelerators[],
    size_t num_accelerators,
    AcceleratorHandler* handler) {
  for (size_t i = 0; i < num_accelerators; ++i)
    RegisterAccelerator(accelerators[i], handler);
}

void AcceleratorManagerImpl::SetDebugAcceleratorsEnabled(bool enabled) {
  debug_accelerators_enabled_ = enabled;
}

bool AcceleratorManagerImpl::AcceleratorPressed(
    const ui::Accelerator& accelerator) {
  std::map<ui::Accelerator, InternalData>::iterator iter =
      accelerators_.find(accelerator);
  DCHECK(iter != accelerators_.end());
  if (iter == accelerators_.end())
    return false;
  InternalData& data = iter->second;
  if (data.IsDebug() && !debug_accelerators_enabled_)
    return false;
  if (accelerator.IsRepeat() && data.IsNonAutoRepeatable())
    return false;
  return data.IsCommandEnabled() ? data.OnAcceleratorFired(accelerator) : false;
}

bool AcceleratorManagerImpl::CanHandleAccelerators() const {
  return true;
}

void AcceleratorManagerImpl::RegisterAccelerator(
    const AcceleratorData& accelerator_data,
    AcceleratorHandler* handler) {
  ui::Accelerator accelerator(accelerator_data.keycode,
                              accelerator_data.keyevent_flags);
  accelerator.set_type(accelerator_data.trigger_event == TRIGGER_ON_PRESS
                           ? ui::ET_KEY_PRESSED
                           : ui::ET_KEY_RELEASED);
  accelerator_wrapper_->Register(accelerator, this);
  accelerators_.insert(
      std::make_pair(accelerator,
                     InternalData(accelerator_data.command_id,
                                  handler,
                                  accelerator_data.accelerator_flags)));
}

void AcceleratorManagerImpl::UnregisterAccelerator(
    const AcceleratorData& accelerator_data,
    AcceleratorHandler* handler) {
  ui::Accelerator accelerator(accelerator_data.keycode,
                              accelerator_data.keyevent_flags);
  accelerator.set_type(accelerator_data.trigger_event == TRIGGER_ON_PRESS
                           ? ui::ET_KEY_PRESSED
                           : ui::ET_KEY_RELEASED);
  accelerator_wrapper_->Unregister(accelerator, this);
  accelerators_.erase(accelerator);
}

// static
AcceleratorManager* AcceleratorManager::Get() {
  return InputManager::Get()->GetAcceleratorManager();
}

// static
scoped_ptr<AcceleratorManager> AcceleratorManager::CreateForFocusManager(
    views::FocusManager* focus_manager) {
  return AcceleratorManagerImpl::CreateForFocusManager(focus_manager).Pass();
}

}  // namespace athena