James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 "mojo/services/native_viewport/platform_viewport.h" |
| 6 | |
| 7 | #include "ui/events/event.h" |
| 8 | #include "ui/events/platform/platform_event_dispatcher.h" |
| 9 | #include "ui/events/platform/platform_event_source.h" |
| 10 | #include "ui/ozone/public/cursor_factory_ozone.h" |
| 11 | #include "ui/ozone/public/ozone_platform.h" |
| 12 | #include "ui/ozone/public/surface_factory_ozone.h" |
| 13 | #include "ui/platform_window/platform_window.h" |
| 14 | #include "ui/platform_window/platform_window_delegate.h" |
| 15 | |
| 16 | namespace mojo { |
| 17 | |
| 18 | // TODO(spang): Deduplicate with PlatformViewportX11.. but there's a hack |
| 19 | // in there that prevents this. |
| 20 | class PlatformViewportOzone : public PlatformViewport, |
| 21 | public ui::PlatformWindowDelegate { |
| 22 | public: |
| 23 | explicit PlatformViewportOzone(Delegate* delegate) : delegate_(delegate) { |
| 24 | ui::OzonePlatform::InitializeForUI(); |
| 25 | } |
| 26 | |
| 27 | virtual ~PlatformViewportOzone() { |
| 28 | // Destroy the platform-window while |this| is still alive. |
| 29 | platform_window_.reset(); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | // Overridden from PlatformViewport: |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 34 | virtual void Init(const gfx::Rect& bounds) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 35 | platform_window_ = |
| 36 | ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); |
| 37 | } |
| 38 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 39 | virtual void Show() override { platform_window_->Show(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 40 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 41 | virtual void Hide() override { platform_window_->Hide(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 42 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 43 | virtual void Close() override { platform_window_->Close(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 44 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 45 | virtual gfx::Size GetSize() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 46 | return platform_window_->GetBounds().size(); |
| 47 | } |
| 48 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 49 | virtual void SetBounds(const gfx::Rect& bounds) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 50 | platform_window_->SetBounds(bounds); |
| 51 | } |
| 52 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 53 | virtual void SetCapture() override { platform_window_->SetCapture(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 54 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 55 | virtual void ReleaseCapture() override { platform_window_->ReleaseCapture(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 56 | |
| 57 | // ui::PlatformWindowDelegate: |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 58 | virtual void OnBoundsChanged(const gfx::Rect& new_bounds) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 59 | delegate_->OnBoundsChanged(new_bounds); |
| 60 | } |
| 61 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 62 | virtual void OnDamageRect(const gfx::Rect& damaged_region) override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 63 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 64 | virtual void DispatchEvent(ui::Event* event) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 65 | delegate_->OnEvent(event); |
| 66 | } |
| 67 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 68 | virtual void OnCloseRequest() override { platform_window_->Close(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 69 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 70 | virtual void OnClosed() override { delegate_->OnDestroyed(); } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 71 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 72 | virtual void OnWindowStateChanged(ui::PlatformWindowState state) override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 73 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 74 | virtual void OnLostCapture() override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 75 | |
| 76 | virtual void OnAcceleratedWidgetAvailable( |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 77 | gfx::AcceleratedWidget widget) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 78 | delegate_->OnAcceleratedWidgetAvailable(widget); |
| 79 | } |
| 80 | |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 81 | virtual void OnActivationChanged(bool active) override {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 82 | |
| 83 | scoped_ptr<ui::PlatformWindow> platform_window_; |
| 84 | Delegate* delegate_; |
| 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(PlatformViewportOzone); |
| 87 | }; |
| 88 | |
| 89 | // static |
| 90 | scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { |
| 91 | return scoped_ptr<PlatformViewport>( |
| 92 | new PlatformViewportOzone(delegate)).Pass(); |
| 93 | } |
| 94 | |
| 95 | } // namespace mojo |