1 // Copyright (c) 2012 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 #ifndef ASH_WM_OVERLAY_EVENT_FILTER_H_
6 #define ASH_WM_OVERLAY_EVENT_FILTER_H_
7 
8 #include "ash/ash_export.h"
9 #include "ash/public/cpp/session/session_observer.h"
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "ui/aura/window.h"
13 #include "ui/events/event_handler.h"
14 
15 namespace ash {
16 
17 // EventFilter for the "overlay window", which intercepts events before they are
18 // processed by the usual path (e.g. the partial screenshot UI, the keyboard
19 // overlay).  It does nothing the first time, but works when |Activate()| is
20 // called.  The main task of this event filter is just to stop propagation
21 // of any key events during activation, and also signal cancellation when keys
22 // for canceling are pressed.
23 class ASH_EXPORT OverlayEventFilter : public ui::EventHandler,
24                                       public SessionObserver {
25  public:
26   // Windows that need to receive events from OverlayEventFilter implement this.
27   class ASH_EXPORT Delegate {
28    public:
29     // Invoked when OverlayEventFilter needs to stop handling events.
30     virtual void Cancel() = 0;
31 
32     // Returns true if the overlay should be canceled in response to |event|.
33     virtual bool IsCancelingKeyEvent(ui::KeyEvent* event) = 0;
34 
35     // Returns the window that needs to receive events. NULL if no window needs
36     // to receive key events from OverlayEventFilter.
37     virtual aura::Window* GetWindow() = 0;
38   };
39 
40   OverlayEventFilter();
41   ~OverlayEventFilter() override;
42 
43   // Starts the filtering of events.  It also notifies the specified
44   // |delegate| when a key event means cancel (like Esc).  It holds the
45   // pointer to the specified |delegate| until Deactivate() is called, but
46   // does not take ownership.
47   void Activate(Delegate* delegate);
48 
49   // Ends the filtering of events.
50   void Deactivate(Delegate* delegate);
51 
52   // Cancels the partial screenshot UI.  Do nothing if it's not activated.
53   void Cancel();
54 
55   // Returns true if it's currently active.
56   bool IsActive();
57 
58   // ui::EventHandler overrides:
59   void OnKeyEvent(ui::KeyEvent* event) override;
60 
61   // SessionObserver overrides:
62   void OnLoginStatusChanged(LoginStatus status) override;
63   void OnChromeTerminating() override;
64   void OnLockStateChanged(bool locked) override;
65 
66  private:
67   Delegate* delegate_ = nullptr;
68   ScopedSessionObserver scoped_session_observer_;
69 
70   DISALLOW_COPY_AND_ASSIGN(OverlayEventFilter);
71 };
72 
73 }  // namespace ash
74 
75 #endif  // ASH_WM_OVERLAY_EVENT_FILTER_H_
76