1 // Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be found 3 // in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_VIEWS_WINDOW_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_VIEWS_WINDOW_IMPL_H_ 7 #pragma once 8 9 #include <map> 10 11 #include "include/views/cef_window.h" 12 #include "include/views/cef_window_delegate.h" 13 14 #include "libcef/browser/menu_model_impl.h" 15 #include "libcef/browser/views/panel_impl.h" 16 #include "libcef/browser/views/window_view.h" 17 18 #include "ui/base/accelerators/accelerator.h" 19 #include "ui/views/controls/menu/menu_runner.h" 20 #include "ui/views/widget/widget.h" 21 22 namespace views { 23 class MenuButton; 24 } 25 26 class CefWindowImpl 27 : public CefPanelImpl<CefWindowView, CefWindow, CefWindowDelegate>, 28 public CefWindowView::Delegate, 29 public ui::AcceleratorTarget { 30 public: 31 using ParentClass = CefPanelImpl<CefWindowView, CefWindow, CefWindowDelegate>; 32 33 CefWindowImpl(const CefWindowImpl&) = delete; 34 CefWindowImpl& operator=(const CefWindowImpl&) = delete; 35 36 // Create a new CefWindow instance. |delegate| may be nullptr. 37 static CefRefPtr<CefWindowImpl> Create(CefRefPtr<CefWindowDelegate> delegate); 38 39 // CefWindow methods: 40 void Show() override; 41 void Hide() override; 42 void CenterWindow(const CefSize& size) override; 43 void Close() override; 44 bool IsClosed() override; 45 void Activate() override; 46 void Deactivate() override; 47 bool IsActive() override; 48 void BringToTop() override; 49 void SetAlwaysOnTop(bool on_top) override; 50 bool IsAlwaysOnTop() override; 51 void Maximize() override; 52 void Minimize() override; 53 void Restore() override; 54 void SetFullscreen(bool fullscreen) override; 55 bool IsMaximized() override; 56 bool IsMinimized() override; 57 bool IsFullscreen() override; 58 void SetTitle(const CefString& title) override; 59 CefString GetTitle() override; 60 void SetWindowIcon(CefRefPtr<CefImage> image) override; 61 CefRefPtr<CefImage> GetWindowIcon() override; 62 void SetWindowAppIcon(CefRefPtr<CefImage> image) override; 63 CefRefPtr<CefImage> GetWindowAppIcon() override; 64 CefRefPtr<CefOverlayController> AddOverlayView( 65 CefRefPtr<CefView> view, 66 cef_docking_mode_t docking_mode) override; 67 void ShowMenu(CefRefPtr<CefMenuModel> menu_model, 68 const CefPoint& screen_point, 69 cef_menu_anchor_position_t anchor_position) override; 70 void CancelMenu() override; 71 CefRefPtr<CefDisplay> GetDisplay() override; 72 CefRect GetClientAreaBoundsInScreen() override; 73 void SetDraggableRegions( 74 const std::vector<CefDraggableRegion>& regions) override; 75 CefWindowHandle GetWindowHandle() override; 76 void SendKeyPress(int key_code, uint32 event_flags) override; 77 void SendMouseMove(int screen_x, int screen_y) override; 78 void SendMouseEvents(cef_mouse_button_type_t button, 79 bool mouse_down, 80 bool mouse_up) override; 81 void SetAccelerator(int command_id, 82 int key_code, 83 bool shift_pressed, 84 bool ctrl_pressed, 85 bool alt_pressed) override; 86 void RemoveAccelerator(int command_id) override; 87 void RemoveAllAccelerators() override; 88 89 // CefViewAdapter methods: 90 void Detach() override; 91 92 // CefPanel methods: AsWindow()93 CefRefPtr<CefWindow> AsWindow() override { return this; } 94 95 // CefView methods: 96 void SetBounds(const CefRect& bounds) override; 97 CefRect GetBounds() override; 98 CefRect GetBoundsInScreen() override; 99 void SetSize(const CefSize& bounds) override; 100 void SetPosition(const CefPoint& position) override; 101 void SizeToPreferredSize() override; 102 void SetVisible(bool visible) override; 103 bool IsVisible() override; 104 bool IsDrawn() override; 105 void SetBackgroundColor(cef_color_t color) override; 106 107 // CefWindowView::Delegate methods: 108 bool CanWidgetClose() override; 109 void OnWindowClosing() override; 110 void OnWindowViewDeleted() override; 111 112 // CefViewAdapter methods: GetDebugType()113 std::string GetDebugType() override { return "Window"; } 114 void GetDebugInfo(base::DictionaryValue* info, 115 bool include_children) override; 116 117 // ui::AcceleratorTarget methods: 118 bool AcceleratorPressed(const ui::Accelerator& accelerator) override; 119 bool CanHandleAccelerators() const override; 120 121 // Called for key events that have not been handled by other controls in the 122 // window. Returns true if the event was handled. 123 bool OnKeyEvent(const CefKeyEvent& event); 124 125 void ShowMenu(views::MenuButton* menu_button, 126 CefRefPtr<CefMenuModel> menu_model, 127 const CefPoint& screen_point, 128 cef_menu_anchor_position_t anchor_position); 129 void MenuClosed(); 130 widget()131 views::Widget* widget() const { return widget_; } 132 133 private: 134 // Create a new implementation object. 135 // Always call Initialize() after creation. 136 // |delegate| may be nullptr. 137 explicit CefWindowImpl(CefRefPtr<CefWindowDelegate> delegate); 138 139 // CefViewImpl methods: 140 CefWindowView* CreateRootView() override; 141 void InitializeRootView() override; 142 143 // Initialize the Widget. 144 void CreateWidget(); 145 146 views::Widget* widget_; 147 148 // True if the window has been destroyed. 149 bool destroyed_; 150 151 // The currently active menu model and runner. 152 CefRefPtr<CefMenuModelImpl> menu_model_; 153 std::unique_ptr<views::MenuRunner> menu_runner_; 154 155 // Map of command_id to accelerator. 156 using AcceleratorMap = std::map<int, ui::Accelerator>; 157 AcceleratorMap accelerator_map_; 158 159 #if defined(USE_AURA) 160 // Native widget's handler to receive events after the event target. 161 std::unique_ptr<ui::EventHandler> unhandled_key_event_handler_; 162 #endif 163 164 IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(CefWindowImpl); 165 }; 166 167 #endif // CEF_LIBCEF_BROWSER_VIEWS_WINDOW_IMPL_H_ 168