<commit_before>// Copyright (c) 2012 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 "ash/wm/system_gesture_event_filter.h"

#include "ash/accelerators/accelerator_controller.h"
#include "ash/shell.h"
#include "ash/system/brightness/brightness_control_delegate.h"
#include "ash/volume_control_delegate.h"
#include "ash/wm/property_util.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_util.h"
#include "ash/wm/workspace/phantom_window_controller.h"
#include "ash/wm/workspace/snap_sizer.h"
#include "ui/aura/event.h"
#include "ui/aura/root_window.h"
#include "ui/gfx/screen.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"

namespace {

const int kSystemPinchPoints = 4;

enum SystemGestureStatus {
  SYSTEM_GESTURE_PROCESSED,  // The system gesture has been processed.
  SYSTEM_GESTURE_IGNORED,    // The system gesture was ignored.
  SYSTEM_GESTURE_END,        // Marks the end of the sytem gesture.
};

aura::Window* GetTargetForSystemGestureEvent(aura::Window* target) {
  aura::Window* system_target = target;
  if (!system_target || system_target == ash::Shell::GetPrimaryRootWindow())
    system_target = ash::wm::GetActiveWindow();
  if (system_target)
    system_target = system_target->GetToplevelWindow();
  return system_target;
}

}

namespace ash {
namespace internal {

class SystemPinchHandler {
 public:
  explicit SystemPinchHandler(aura::Window* target)
      : target_(target),
        phantom_(target),
        pinch_factor_(1.) {
    widget_ = views::Widget::GetWidgetForNativeWindow(target_);
  }

  ~SystemPinchHandler() {
  }

  SystemGestureStatus ProcessGestureEvent(const aura::GestureEvent& event) {
    // The target has changed, somehow. Let's bale.
    if (!widget_ || !widget_->widget_delegate()->CanResize())
      return SYSTEM_GESTURE_END;

    switch (event.type()) {
      case ui::ET_GESTURE_TAP_UP: {
        if (event.delta_x() > kSystemPinchPoints)
          break;

        gfx::Rect bounds = phantom_.IsShowing() ?  phantom_.bounds() :
                                                   target_->bounds();
        int grid = Shell::GetInstance()->GetGridSize();
        bounds.set_x(WindowResizer::AlignToGridRoundUp(bounds.x(), grid));
        bounds.set_y(WindowResizer::AlignToGridRoundUp(bounds.y(), grid));
        if (wm::IsWindowFullscreen(target_) || wm::IsWindowMaximized(target_)) {
          SetRestoreBounds(target_, bounds);
          wm::RestoreWindow(target_);
        } else {
          target_->SetBounds(bounds);
        }
        return SYSTEM_GESTURE_END;
      }

      case ui::ET_GESTURE_SCROLL_UPDATE: {
        if (wm::IsWindowFullscreen(target_) || wm::IsWindowMaximized(target_)) {
          if (!phantom_.IsShowing())
            break;
        } else {
          gfx::Rect bounds = target_->bounds();
          bounds.set_x(static_cast<int>(bounds.x() + event.delta_x()));
          bounds.set_y(static_cast<int>(bounds.y() + event.delta_y()));
          target_->SetBounds(bounds);
        }

        if (phantom_.IsShowing()) {
          gfx::Rect bounds = phantom_.bounds();
          bounds.set_x(static_cast<int>(bounds.x() + event.delta_x()));
          bounds.set_y(static_cast<int>(bounds.y() + event.delta_y()));
          phantom_.SetBounds(bounds);
        }
        break;
      }

      case ui::ET_GESTURE_PINCH_UPDATE: {
        // The PINCH_UPDATE events contain incremental scaling updates.
        pinch_factor_ *= event.delta_x();
        gfx::Rect bounds = target_->bounds();

        if (wm::IsWindowFullscreen(target_) || wm::IsWindowMaximized(target_)) {
          // For a fullscreen/maximized window, if you start pinching in, and
          // you pinch enough, then it shows the phantom window with the
          // restore-bounds. The subsequent pinch updates then work on the
          // restore bounds instead of the fullscreen/maximized bounds.
          const gfx::Rect* restore = NULL;
          if (phantom_.IsShowing()) {
            restore = GetRestoreBounds(target_);
          } else if (pinch_factor_ < 0.8) {
            restore = GetRestoreBounds(target_);
            // Reset the pinch factor.
            pinch_factor_ = 1.0;
          }

          if (restore)
            bounds = *restore;
          else
            break;
        }

        gfx::Rect new_bounds = bounds.Scale(pinch_factor_);
        new_bounds.set_x(bounds.x() + (event.x() - event.x() * pinch_factor_));
        new_bounds.set_y(bounds.y() + (event.y() - event.y() * pinch_factor_));
        phantom_.Show(new_bounds);
        break;
      }

      default:
        break;
    }

    return SYSTEM_GESTURE_PROCESSED;
  }
<commit_msg>ash: Add support for fling to snap left/right.<commit_after>// Copyright (c) 2012 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 "ash/wm/system_gesture_event_filter.h"

#include "ash/accelerators/accelerator_controller.h"
#include "ash/shell.h"
#include "ash/system/brightness/brightness_control_delegate.h"
#include "ash/volume_control_delegate.h"
#include "ash/wm/property_util.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_util.h"
#include "ash/wm/workspace/phantom_window_controller.h"
#include "ash/wm/workspace/snap_sizer.h"
#include "ui/aura/event.h"
#include "ui/aura/root_window.h"
#include "ui/gfx/screen.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"

namespace {

const int kSystemPinchPoints = 4;

enum SystemGestureStatus {
  SYSTEM_GESTURE_PROCESSED,  // The system gesture has been processed.
  SYSTEM_GESTURE_IGNORED,    // The system gesture was ignored.
  SYSTEM_GESTURE_END,        // Marks the end of the sytem gesture.
};

aura::Window* GetTargetForSystemGestureEvent(aura::Window* target) {
  aura::Window* system_target = target;
  if (!system_target || system_target == ash::Shell::GetPrimaryRootWindow())
    system_target = ash::wm::GetActiveWindow();
  if (system_target)
    system_target = system_target->GetToplevelWindow();
  return system_target;
}

}

namespace ash {
namespace internal {

class SystemPinchHandler {
 public:
  explicit SystemPinchHandler(aura::Window* target)
      : target_(target),
        phantom_(target),
        pinch_factor_(1.) {
    widget_ = views::Widget::GetWidgetForNativeWindow(target_);
  }

  ~SystemPinchHandler() {
  }

  SystemGestureStatus ProcessGestureEvent(const aura::GestureEvent& event) {
    // The target has changed, somehow. Let's bale.
    if (!widget_ || !widget_->widget_delegate()->CanResize())
      return SYSTEM_GESTURE_END;

    switch (event.type()) {
      case ui::ET_GESTURE_TAP_UP: {
        if (event.delta_x() > kSystemPinchPoints)
          break;

        gfx::Rect bounds = phantom_.IsShowing() ?  phantom_.bounds() :
                                                   target_->bounds();
        int grid = Shell::GetInstance()->GetGridSize();
        bounds.set_x(WindowResizer::AlignToGridRoundUp(bounds.x(), grid));
        bounds.set_y(WindowResizer::AlignToGridRoundUp(bounds.y(), grid));
        if (wm::IsWindowFullscreen(target_) || wm::IsWindowMaximized(target_)) {
          SetRestoreBounds(target_, bounds);
          wm::RestoreWindow(target_);
        } else {
          target_->SetBounds(bounds);
        }
        return SYSTEM_GESTURE_END;
      }

      case ui::ET_GESTURE_SCROLL_UPDATE: {
        if (wm::IsWindowFullscreen(target_) || wm::IsWindowMaximized(target_)) {
          if (!phantom_.IsShowing())
            break;
        } else {
          gfx::Rect bounds = target_->bounds();
          bounds.set_x(static_cast<int>(bounds.x() + event.delta_x()));
          bounds.set_y(static_cast<int>(bounds.y() + event.delta_y()));
          target_->SetBounds(bounds);
        }

        if (phantom_.IsShowing()) {
          gfx::Rect bounds = phantom_.bounds();
          bounds.set_x(static_cast<int>(bounds.x() + event.delta_x()));
          bounds.set_y(static_cast<int>(bounds.y() + event.delta_y()));
          phantom_.SetBounds(bounds);
        }
        break;
      }

      case ui::ET_GESTURE_PINCH_UPDATE: {
        // The PINCH_UPDATE events contain incremental scaling updates.
        pinch_factor_ *= event.delta_x();
        gfx::Rect bounds = target_->bounds();

        if (wm::IsWindowFullscreen(target_) || wm::IsWindowMaximized(target_)) {
          // For a fullscreen/maximized window, if you start pinching in, and
          // you pinch enough, then it shows the phantom window with the
          // restore-bounds. The subsequent pinch updates then work on the
          // restore bounds instead of the fullscreen/maximized bounds.
          const gfx::Rect* restore = NULL;
          if (phantom_.IsShowing()) {
            restore = GetRestoreBounds(target_);
          } else if (pinch_factor_ < 0.8) {
            restore = GetRestoreBounds(target_);
            // Reset the pinch factor.
            pinch_factor_ = 1.0;
          }

          if (restore)
            bounds = *restore;
          else
            break;
        }

        gfx::Rect new_bounds = bounds.Scale(pinch_factor_);
        new_bounds.set_x(bounds.x() + (event.x() - event.x() * pinch_factor_));
        new_bounds.set_y(bounds.y() + (event.y() - event.y() * pinch_factor_));
        phantom_.Show(new_bounds);
        break;
      }

      case ui::ET_GESTURE_MULTIFINGER_SWIPE: {
        // Snap for left/right swipes.
        if (event.delta_x()) {
          SnapSizer sizer(target_,
              gfx::Point(),
              event.delta_x() < 0 ? internal::SnapSizer::LEFT_EDGE :
                                    internal::SnapSizer::RIGHT_EDGE,
              Shell::GetInstance()->GetGridSize());
          target_->SetBounds(sizer.GetSnapBounds(target_->bounds()));
          phantom_.Hide();
          pinch_factor_ = 1.0;
        }
        break;
      }

      default:
        break;
    }

    return SYSTEM_GESTURE_PROCESSED;
  }

{'fraction_non_alphanumeric': 0.12449402705104157, 'fraction_numerical': 0.0022707078684963966, 'mean_word_length': 2.7147048038137145, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4136037', 'n_tokens_mistral': 3227, 'n_tokens_neox': 3081, 'n_words': 612}