<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/desktop/desktop_window_view.h"

#include "ui/gfx/canvas.h"
#include "ui/gfx/transform.h"
#include "views/desktop/desktop_background.h"
#include "views/desktop/desktop_window_root_view.h"
#include "views/layer_property_setter.h"
#include "views/widget/native_widget_view.h"
#include "views/widget/native_widget_views.h"
#include "views/widget/widget.h"
#include "views/window/native_frame_view.h"

#if defined(OS_WIN)
#include "views/widget/native_widget_win.h"
#elif defined(TOOLKIT_USES_GTK)
#include "views/widget/native_widget_gtk.h"
#endif

namespace views {
namespace desktop {

// The Widget that hosts the DesktopWindowView. Subclasses Widget to override
// CreateRootView() so that the DesktopWindowRootView can be supplied instead
// for custom event filtering.
class DesktopWindow : public Widget {
 public:
  explicit DesktopWindow(DesktopWindowView* desktop_window_view)
      : desktop_window_view_(desktop_window_view) {}
  virtual ~DesktopWindow() {}

 private:
  // Overridden from Widget:
  virtual internal::RootView* CreateRootView() OVERRIDE {
    return new DesktopWindowRootView(desktop_window_view_, this);
  }

  virtual bool OnKeyEvent(const KeyEvent& event) OVERRIDE {
    NativeWidgetViews* native_widget =
        desktop_window_view_->active_native_widget();
    return native_widget ? native_widget->OnKeyEvent(event) : false;
  }

  DesktopWindowView* desktop_window_view_;

  DISALLOW_COPY_AND_ASSIGN(DesktopWindow);
};

class TestWindowContentView : public WidgetDelegateView {
 public:
  TestWindowContentView(const std::wstring& title, SkColor color)
      : title_(title),
        color_(color) {
  }
  virtual ~TestWindowContentView() {}

 private:
  // Overridden from View:
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
    canvas->FillRectInt(color_, 0, 0, width(), height());
  }

  // Overridden from WindowDelegate:
  virtual std::wstring GetWindowTitle() const OVERRIDE {
    return title_;
  }
  virtual View* GetContentsView() {
    return this;
  }
  virtual bool CanMaximize() const OVERRIDE {
    return true;
  }
  virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE {
    Widget* widget = View::GetWidget();
    if (widget->IsMinimized())
      widget->Restore();
    else
      widget->Minimize();
    return true;
  }

  std::wstring title_;
  SkColor color_;

  DISALLOW_COPY_AND_ASSIGN(TestWindowContentView);
};

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowView, public:

// static
DesktopWindowView* DesktopWindowView::desktop_window_view = NULL;

DesktopWindowView::DesktopWindowView(DesktopType type)
    : active_native_widget_(NULL),
      type_(type) {
  switch (type_) {
    case DESKTOP_DEFAULT:
    case DESKTOP_NETBOOK:
      set_background(new DesktopBackground);
      break;
    case DESKTOP_OTHER:
      set_background(Background::CreateStandardPanelBackground());
      break;
  }
}

DesktopWindowView::~DesktopWindowView() {
}

// static
void DesktopWindowView::CreateDesktopWindow(DesktopType type) {
  DCHECK(!desktop_window_view);
  desktop_window_view = new DesktopWindowView(type);
  views::Widget* window = new DesktopWindow(desktop_window_view);
  desktop_window_view->widget_ = window;

  views::Widget::InitParams params;
  params.delegate = desktop_window_view;
  // In this environment, CreateChromeWindow will default to creating a views-
  // window, so we need to construct a NativeWidgetWin by hand.
  // TODO(beng): Replace this with NativeWindow::CreateNativeRootWindow().
#if defined(OS_WIN)
  params.native_widget = new views::NativeWidgetWin(window);
  params.bounds = gfx::Rect(20, 20, 1920, 1200);
#elif defined(TOOLKIT_USES_GTK)
  params.native_widget = new views::NativeWidgetGtk(window);
#endif
  window->Init(params);
  window->Show();
  window->Maximize();
}

void DesktopWindowView::ActivateWidget(Widget* widget) {
  if (widget && widget->IsActive())
    return;

  if (widget) {
    if (!widget->HasObserver(this))
      widget->AddObserver(this);
    widget->Activate();
  }
}


void DesktopWindowView::CreateTestWindow(const std::wstring& title,
                                         SkColor color,
                                         gfx::Rect initial_bounds,
                                         bool rotate) {
  views::Widget* window = views::Widget::CreateWindowWithBounds(
      new TestWindowContentView(title, color),
      initial_bounds);
  window->Show();

  if (rotate) {
    ui::Transform transform;
    transform.SetRotate(90.0f);
    transform.SetTranslateX(window->GetWindowScreenBounds().width());
    static_cast<NativeWidgetViews*>(window->native_widget())->GetView()->
        SetTransform(transform);
  }
  static_cast<NativeWidgetViews*>(window->native_widget())->GetView()->
      SetLayerPropertySetter(LayerPropertySetter::CreateAnimatingSetter());
}

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowView, View overrides:

void DesktopWindowView::Layout() {
}

void DesktopWindowView::ViewHierarchyChanged(
    bool is_add, View* parent, View* child) {
  if (!is_add &&
      active_native_widget_ &&
      active_native_widget_->GetView() == child)
    active_native_widget_ = NULL;
}

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowView, WidgetDelegate implementation:

Widget* DesktopWindowView::GetWidget() {
  return widget_;
}

const Widget* DesktopWindowView::GetWidget() const {
  return widget_;
}

bool DesktopWindowView::CanResize() const {
  return true;
}

bool DesktopWindowView::CanMaximize() const {
  return CanResize();<commit_msg>views-desktop: Add the desktop-window as an observer for a widget when it gets added to the desktop.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/desktop/desktop_window_view.h"

#include "ui/gfx/canvas.h"
#include "ui/gfx/transform.h"
#include "views/desktop/desktop_background.h"
#include "views/desktop/desktop_window_root_view.h"
#include "views/layer_property_setter.h"
#include "views/widget/native_widget_view.h"
#include "views/widget/native_widget_views.h"
#include "views/widget/widget.h"
#include "views/window/native_frame_view.h"

#if defined(OS_WIN)
#include "views/widget/native_widget_win.h"
#elif defined(TOOLKIT_USES_GTK)
#include "views/widget/native_widget_gtk.h"
#endif

namespace views {
namespace desktop {

// The Widget that hosts the DesktopWindowView. Subclasses Widget to override
// CreateRootView() so that the DesktopWindowRootView can be supplied instead
// for custom event filtering.
class DesktopWindow : public Widget {
 public:
  explicit DesktopWindow(DesktopWindowView* desktop_window_view)
      : desktop_window_view_(desktop_window_view) {}
  virtual ~DesktopWindow() {}

 private:
  // Overridden from Widget:
  virtual internal::RootView* CreateRootView() OVERRIDE {
    return new DesktopWindowRootView(desktop_window_view_, this);
  }

  virtual bool OnKeyEvent(const KeyEvent& event) OVERRIDE {
    NativeWidgetViews* native_widget =
        desktop_window_view_->active_native_widget();
    return native_widget ? native_widget->OnKeyEvent(event) : false;
  }

  DesktopWindowView* desktop_window_view_;

  DISALLOW_COPY_AND_ASSIGN(DesktopWindow);
};

class TestWindowContentView : public WidgetDelegateView {
 public:
  TestWindowContentView(const std::wstring& title, SkColor color)
      : title_(title),
        color_(color) {
  }
  virtual ~TestWindowContentView() {}

 private:
  // Overridden from View:
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
    canvas->FillRectInt(color_, 0, 0, width(), height());
  }

  // Overridden from WindowDelegate:
  virtual std::wstring GetWindowTitle() const OVERRIDE {
    return title_;
  }
  virtual View* GetContentsView() {
    return this;
  }
  virtual bool CanMaximize() const OVERRIDE {
    return true;
  }
  virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE {
    Widget* widget = View::GetWidget();
    if (widget->IsMinimized())
      widget->Restore();
    else
      widget->Minimize();
    return true;
  }

  std::wstring title_;
  SkColor color_;

  DISALLOW_COPY_AND_ASSIGN(TestWindowContentView);
};

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowView, public:

// static
DesktopWindowView* DesktopWindowView::desktop_window_view = NULL;

DesktopWindowView::DesktopWindowView(DesktopType type)
    : active_native_widget_(NULL),
      type_(type) {
  switch (type_) {
    case DESKTOP_DEFAULT:
    case DESKTOP_NETBOOK:
      set_background(new DesktopBackground);
      break;
    case DESKTOP_OTHER:
      set_background(Background::CreateStandardPanelBackground());
      break;
  }
}

DesktopWindowView::~DesktopWindowView() {
}

// static
void DesktopWindowView::CreateDesktopWindow(DesktopType type) {
  DCHECK(!desktop_window_view);
  desktop_window_view = new DesktopWindowView(type);
  views::Widget* window = new DesktopWindow(desktop_window_view);
  desktop_window_view->widget_ = window;

  views::Widget::InitParams params;
  params.delegate = desktop_window_view;
  // In this environment, CreateChromeWindow will default to creating a views-
  // window, so we need to construct a NativeWidgetWin by hand.
  // TODO(beng): Replace this with NativeWindow::CreateNativeRootWindow().
#if defined(OS_WIN)
  params.native_widget = new views::NativeWidgetWin(window);
  params.bounds = gfx::Rect(20, 20, 1920, 1200);
#elif defined(TOOLKIT_USES_GTK)
  params.native_widget = new views::NativeWidgetGtk(window);
#endif
  window->Init(params);
  window->Show();
  window->Maximize();
}

void DesktopWindowView::ActivateWidget(Widget* widget) {
  if (widget && widget->IsActive())
    return;

  if (widget) {
    if (!widget->HasObserver(this))
      widget->AddObserver(this);
    widget->Activate();
  }
}


void DesktopWindowView::CreateTestWindow(const std::wstring& title,
                                         SkColor color,
                                         gfx::Rect initial_bounds,
                                         bool rotate) {
  views::Widget* window = views::Widget::CreateWindowWithBounds(
      new TestWindowContentView(title, color),
      initial_bounds);
  window->Show();

  if (rotate) {
    ui::Transform transform;
    transform.SetRotate(90.0f);
    transform.SetTranslateX(window->GetWindowScreenBounds().width());
    static_cast<NativeWidgetViews*>(window->native_widget())->GetView()->
        SetTransform(transform);
  }
  static_cast<NativeWidgetViews*>(window->native_widget())->GetView()->
      SetLayerPropertySetter(LayerPropertySetter::CreateAnimatingSetter());
}

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowView, View overrides:

void DesktopWindowView::Layout() {
}

void DesktopWindowView::ViewHierarchyChanged(
    bool is_add, View* parent, View* child) {
  if (!is_add &&
      active_native_widget_ &&
      active_native_widget_->GetView() == child) {
    active_native_widget_ = NULL;
  } else if (child->GetClassName() ==
      internal::NativeWidgetView::kViewClassName) {
    internal::NativeWidgetView* native_widget_view =
        static_cast<internal::NativeWidgetView*>(child);
    native_widget_view->GetAssociatedWidget()->AddObserver(this);
  }
}

////////////////////////////////////////////////////////////////////////////////
// DesktopWindowView, WidgetDelegate implementation:

Widget* DesktopWindowView::GetWidget() {
  return widget_;
}

const Widget* DesktopWindowView::GetWidget() const {
  return widget_;
}

bool DesktopWindowView::CanResize() const {
  return true;
}

bool DesktopWindowView::CanMaximize() const {
  return CanResize();
{'fraction_non_alphanumeric': 0.1456005297574704, 'fraction_numerical': 0.003476533399553017, 'mean_word_length': 3.5678638941398866, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4609521', 'n_tokens_mistral': 3576, 'n_tokens_neox': 3410, 'n_words': 753}