blob: dcac5ad06a0bc78512f52cf0b6ad3814e24ad6ce [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
16namespace mojo {
17
18// TODO(spang): Deduplicate with PlatformViewportX11.. but there's a hack
19// in there that prevents this.
20class 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 Robinsonbaf71d32014-10-08 13:00:20 -070034 virtual void Init(const gfx::Rect& bounds) override {
James Robinson646469d2014-10-03 15:33:28 -070035 platform_window_ =
36 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds);
37 }
38
James Robinsonbaf71d32014-10-08 13:00:20 -070039 virtual void Show() override { platform_window_->Show(); }
James Robinson646469d2014-10-03 15:33:28 -070040
James Robinsonbaf71d32014-10-08 13:00:20 -070041 virtual void Hide() override { platform_window_->Hide(); }
James Robinson646469d2014-10-03 15:33:28 -070042
James Robinsonbaf71d32014-10-08 13:00:20 -070043 virtual void Close() override { platform_window_->Close(); }
James Robinson646469d2014-10-03 15:33:28 -070044
James Robinsonbaf71d32014-10-08 13:00:20 -070045 virtual gfx::Size GetSize() override {
James Robinson646469d2014-10-03 15:33:28 -070046 return platform_window_->GetBounds().size();
47 }
48
James Robinsonbaf71d32014-10-08 13:00:20 -070049 virtual void SetBounds(const gfx::Rect& bounds) override {
James Robinson646469d2014-10-03 15:33:28 -070050 platform_window_->SetBounds(bounds);
51 }
52
James Robinsonbaf71d32014-10-08 13:00:20 -070053 virtual void SetCapture() override { platform_window_->SetCapture(); }
James Robinson646469d2014-10-03 15:33:28 -070054
James Robinsonbaf71d32014-10-08 13:00:20 -070055 virtual void ReleaseCapture() override { platform_window_->ReleaseCapture(); }
James Robinson646469d2014-10-03 15:33:28 -070056
57 // ui::PlatformWindowDelegate:
James Robinsonbaf71d32014-10-08 13:00:20 -070058 virtual void OnBoundsChanged(const gfx::Rect& new_bounds) override {
James Robinson646469d2014-10-03 15:33:28 -070059 delegate_->OnBoundsChanged(new_bounds);
60 }
61
James Robinsonbaf71d32014-10-08 13:00:20 -070062 virtual void OnDamageRect(const gfx::Rect& damaged_region) override {}
James Robinson646469d2014-10-03 15:33:28 -070063
James Robinsonbaf71d32014-10-08 13:00:20 -070064 virtual void DispatchEvent(ui::Event* event) override {
James Robinson646469d2014-10-03 15:33:28 -070065 delegate_->OnEvent(event);
66 }
67
James Robinsonbaf71d32014-10-08 13:00:20 -070068 virtual void OnCloseRequest() override { platform_window_->Close(); }
James Robinson646469d2014-10-03 15:33:28 -070069
James Robinsonbaf71d32014-10-08 13:00:20 -070070 virtual void OnClosed() override { delegate_->OnDestroyed(); }
James Robinson646469d2014-10-03 15:33:28 -070071
James Robinsonbaf71d32014-10-08 13:00:20 -070072 virtual void OnWindowStateChanged(ui::PlatformWindowState state) override {}
James Robinson646469d2014-10-03 15:33:28 -070073
James Robinsonbaf71d32014-10-08 13:00:20 -070074 virtual void OnLostCapture() override {}
James Robinson646469d2014-10-03 15:33:28 -070075
76 virtual void OnAcceleratedWidgetAvailable(
James Robinsonbaf71d32014-10-08 13:00:20 -070077 gfx::AcceleratedWidget widget) override {
James Robinson646469d2014-10-03 15:33:28 -070078 delegate_->OnAcceleratedWidgetAvailable(widget);
79 }
80
James Robinsonbaf71d32014-10-08 13:00:20 -070081 virtual void OnActivationChanged(bool active) override {}
James Robinson646469d2014-10-03 15:33:28 -070082
83 scoped_ptr<ui::PlatformWindow> platform_window_;
84 Delegate* delegate_;
85
86 DISALLOW_COPY_AND_ASSIGN(PlatformViewportOzone);
87};
88
89// static
90scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) {
91 return scoped_ptr<PlatformViewport>(
92 new PlatformViewportOzone(delegate)).Pass();
93}
94
95} // namespace mojo