Skip to content

Instantly share code, notes, and snippets.

@kenchris
Created December 11, 2013 01:15
Show Gist options
  • Save kenchris/7903497 to your computer and use it in GitHub Desktop.
Save kenchris/7903497 to your computer and use it in GitHub Desktop.
commit 895c566d341e4a3d3ecca7fea5b3cea87fb29738
Author: Kenneth Rohde Christiansen <kenneth.r.christiansen@intel.com>
Date: Tue Dec 10 14:43:20 2013 +0100
WIP: Getting there
diff --git a/runtime/browser/ui/native_app_window_tizen.cc b/runtime/browser/ui/native_app_window_tizen.cc
index 7c995e6..1d6fe27 100644
--- a/runtime/browser/ui/native_app_window_tizen.cc
+++ b/runtime/browser/ui/native_app_window_tizen.cc
@@ -7,6 +7,8 @@
#include "base/memory/scoped_ptr.h"
#include "ui/aura/root_window.h"
#include "ui/gfx/transform.h"
+#include "ui/gfx/rect.h"
+#include "ui/gfx/screen.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "xwalk/runtime/browser/ui/top_view_layout_views.h"
@@ -16,8 +18,20 @@ namespace xwalk {
NativeAppWindowTizen::NativeAppWindowTizen(
const NativeAppWindow::CreateParams& create_params)
: NativeAppWindowViews(create_params),
- indicator_(new TizenSystemIndicator()),
- orientation_(PORTRAIT) {
+ indicator_(new TizenSystemIndicator()) {
+}
+
+void NativeAppWindowTizen::Initialize() {
+ NativeAppWindowViews::Initialize();
+
+ // Get display info such as device_scale_factor, and current
+ // rotation (orientation).
+ display_ = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
+
+ aura::Window* root_window = GetNativeWindow()->GetRootWindow();
+ DCHECK(root_window);
+ root_window->AddObserver(this);
+
if (SensorProvider::GetInstance())
SensorProvider::GetInstance()->AddObserver(this);
}
@@ -25,6 +39,9 @@ NativeAppWindowTizen::NativeAppWindowTizen(
NativeAppWindowTizen::~NativeAppWindowTizen() {
if (SensorProvider::GetInstance())
SensorProvider::GetInstance()->RemoveObserver(this);
+ aura::Window* root_window = GetNativeWindow()->GetRootWindow();
+ DCHECK(root_window);
+ root_window->RemoveObserver(this);
}
void NativeAppWindowTizen::ViewHierarchyChanged(
@@ -40,51 +57,88 @@ void NativeAppWindowTizen::ViewHierarchyChanged(
}
}
-namespace {
+void NativeAppWindowTizen::OnWindowBoundsChanged(
+ aura::Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
+ aura::Window* root_window = GetNativeWindow()->GetRootWindow();
+ DCHECK_EQ(root_window, window);
+
+ printf("display: %s,orientation=%d\n", display_.ToString().c_str(), display_.rotation());
+ printf("window(root): %p bounds=%d %d %d %d\n",
+ root_window,
+ root_window->bounds().x(),
+ root_window->bounds().y(),
+ root_window->bounds().width(),
+ root_window->bounds().height());
+
+ // We are working with DIPs here. size() returns in DIPs.
+ GetWidget()->GetRootView()->SetSize(new_bounds.size());
+}
+
+gfx::Display::Rotation NativeAppWindowTizen::GetPreferredAllowedRotation() {
+ // FIXME: Given the current orientation, return the preferred rotation
+ // from the set of allowed orientations for this window.
+ return display_.rotation();
+}
+
+void NativeAppWindowTizen::OnWindowVisibilityChanging(aura::Window* window,
+ bool visible) {
+ if (!visible)
+ return;
+
+ gfx::Display::Rotation rotation = GetPreferredAllowedRotation();
+ if (rotation == display_.rotation())
+ return;
+
+ SetDisplayRotation(rotation);
+}
-gfx::Transform GetRotationAroundCenter(const gfx::Size& size,
- const gfx::Display::Rotation& rotation) {
- gfx::Transform transform;
- switch (rotation) {
+gfx::Transform NativeAppWindowTizen::GetRotationTransform() const {
+ // This method assumed a fixed portrait device.
+ // As everything is calculated from the fixed position
+ // we do not update the bounds after rotation change.
+ gfx::Transform rotate;
+ float one_pixel = 1.0f / display_.device_scale_factor();
+ switch (display_.rotation()) {
case gfx::Display::ROTATE_0:
break;
case gfx::Display::ROTATE_90:
- transform.Translate(size.width() - 1, 0);
- transform.Rotate(90);
- break;
- case gfx::Display::ROTATE_180:
- transform.Translate(size.width() - 1, size.height() - 1);
- transform.Rotate(180);
+ rotate.Translate(display_.bounds().width() - one_pixel, 0);
+ rotate.Rotate(90);
break;
case gfx::Display::ROTATE_270:
- transform.Translate(0, size.height() - 1);
- transform.Rotate(270);
+ rotate.Translate(0, display_.bounds().height() - one_pixel);
+ rotate.Rotate(270);
+ break;
+ case gfx::Display::ROTATE_180:
+ rotate.Translate(display_.bounds().width() - one_pixel,
+ display_.bounds().height() - one_pixel);
+ rotate.Rotate(180);
break;
- default:
- NOTREACHED();
}
- return transform;
+
+ return rotate;
}
-TizenSystemIndicator::Orientation ConvertToIndicatorOrientation(
- NativeAppWindowTizen::Orientation orientation) {
- switch (orientation) {
- case NativeAppWindowTizen::PORTRAIT:
- return TizenSystemIndicator::PORTRAIT;
- case NativeAppWindowTizen::LANDSCAPE:
- return TizenSystemIndicator::LANDSCAPE;
- }
+bool IsPortrait(gfx::Display::Rotation rotation) {
+ return rotation == gfx::Display::ROTATE_0
+ || rotation == gfx::Display::ROTATE_180;
}
-} // namespace
+void NativeAppWindowTizen::SetDisplayRotation(gfx::Display::Rotation rotation) {
+ aura::Window* root_window = GetNativeWindow()->GetRootWindow();
-void NativeAppWindowTizen::SetOrientation(Orientation orientation) {
- if (orientation_ == orientation)
- return;
- orientation_ = orientation;
- if (indicator_) {
- indicator_->SetOrientation(ConvertToIndicatorOrientation(orientation_));
- }
+ display_.set_rotation(rotation);
+ root_window->SetTransform(GetRotationTransform());
+
+ if (!indicator_)
+ return;
+
+ // FIXME: refactor to use the rotation instead.
+ TizenSystemIndicator::Orientation orientation = IsPortrait(rotation)
+ ? TizenSystemIndicator::PORTRAIT
+ : TizenSystemIndicator::LANDSCAPE;
+
+ indicator_->SetOrientation(orientation);
}
void NativeAppWindowTizen::OnRotationChanged(gfx::Display::Rotation rotation) {
@@ -92,26 +146,7 @@ void NativeAppWindowTizen::OnRotationChanged(gfx::Display::Rotation rotation) {
if (!root)
return;
- // Set rotation transform for root window. The size of the root window
- // will be changed automaticlly while the transform is set.
- gfx::Rect bounds = GetBounds();
- root->SetTransform(GetRotationAroundCenter(bounds.size(), rotation));
-
- // Adjust the size of sub-windows
- // FIXME(zliang7): It should follow the change while the root is resized.
- if (rotation == gfx::Display::ROTATE_90 ||
- rotation == gfx::Display::ROTATE_270) {
- int width = bounds.width();
- bounds.set_width(bounds.height());
- bounds.set_height(width);
- SetOrientation(LANDSCAPE);
- } else {
- SetOrientation(PORTRAIT);
- }
- GetNativeWindow()->parent()->SetBounds(bounds);
- GetNativeWindow()->SetBounds(bounds);
- // FIXME(zliang7): Why resizing the widget doesn't work?
- GetWidget()->GetRootView()->SetSize(bounds.size());
+ SetDisplayRotation(rotation);
}
} // namespace xwalk
diff --git a/runtime/browser/ui/native_app_window_tizen.h b/runtime/browser/ui/native_app_window_tizen.h
index e15922c..8008b68 100644
--- a/runtime/browser/ui/native_app_window_tizen.h
+++ b/runtime/browser/ui/native_app_window_tizen.h
@@ -9,31 +9,43 @@
#include "xwalk/runtime/browser/tizen/sensor_provider.h"
#include "xwalk/tizen/mobile/ui/tizen_system_indicator.h"
+#include "ui/aura/window_observer.h"
namespace xwalk {
// Tizen uses the Views native window but adds its own features like orientation
// handling and integration with system indicator bar.
-class NativeAppWindowTizen : public NativeAppWindowViews,
+class NativeAppWindowTizen : public aura::WindowObserver,
+ public NativeAppWindowViews,
public SensorProvider::Observer {
public:
explicit NativeAppWindowTizen(const NativeAppWindow::CreateParams& params);
virtual ~NativeAppWindowTizen();
- enum Orientation { LANDSCAPE, PORTRAIT };
-
private:
- // views::View implementation.
+ // NativeAppWindowViews overrides:
+ virtual void Initialize() OVERRIDE;
+
+ // WindowObserver overrides:
+ virtual void OnWindowVisibilityChanging(aura::Window* window,
+ bool visible) OVERRIDE;
+ virtual void OnWindowBoundsChanged(aura::Window* window,
+ const gfx::Rect& old_bounds,
+ const gfx::Rect& new_bounds) OVERRIDE;
+
+ // views::View overrides:
virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE;
- void SetOrientation(Orientation orientation);
+ gfx::Display::Rotation GetPreferredAllowedRotation();
+ gfx::Transform GetRotationTransform() const;
+ void SetDisplayRotation(gfx::Display::Rotation);
- // SensorProvider::Observer implementation.
+ // SensorProvider::Observer overrides:
virtual void OnRotationChanged(gfx::Display::Rotation rotation) OVERRIDE;
scoped_ptr<TizenSystemIndicator> indicator_;
- Orientation orientation_;
+ gfx::Display display_;
DISALLOW_COPY_AND_ASSIGN(NativeAppWindowTizen);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment