summaryrefslogtreecommitdiffstats
path: root/ash/wm/partial_screenshot_view.cc
blob: 37729a4c29f9c3c3432f1179047dd7f3bc64e7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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/partial_screenshot_view.h"

#include "ash/screenshot_delegate.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/partial_screenshot_event_filter.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/events.h"
#include "ui/base/cursor/cursor.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/rect.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {

PartialScreenshotView::PartialScreenshotView(
    ScreenshotDelegate* screenshot_delegate)
    : is_dragging_(false),
      screenshot_delegate_(screenshot_delegate),
      window_(NULL) {
}

PartialScreenshotView::~PartialScreenshotView() {
  screenshot_delegate_ = NULL;
  // Do not delete the |window_| here because |window_| has the
  // ownership to this object. In case that finishing browser happens
  // while |window_| != NULL, |window_| is still removed correctly by
  // its parent container.
  window_ = NULL;
}

// static
void PartialScreenshotView::StartPartialScreenshot(
    ScreenshotDelegate* screenshot_delegate) {
  views::Widget* widget = new views::Widget;
  PartialScreenshotView* view = new PartialScreenshotView(
      screenshot_delegate);
  aura::RootWindow* root_window = Shell::GetActiveRootWindow();
  views::Widget::InitParams params(
      views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
  params.transparent = true;
  params.delegate = view;
  // The partial screenshot rectangle has to be at the real top of
  // the screen.
  params.parent = Shell::GetContainer(
      root_window,
      internal::kShellWindowId_OverlayContainer);

  widget->Init(params);
  widget->SetContentsView(view);
  widget->SetBounds(root_window->bounds());
  widget->GetNativeView()->SetName("PartialScreenshotView");
  widget->StackAtTop();
  widget->Show();
  // Captures mouse events in case that context menu already captures the
  // events.  This will close the context menu.
  widget->GetNativeView()->SetCapture();

  view->set_window(widget->GetNativeWindow());
  Shell::GetInstance()->partial_screenshot_filter()->Activate(view);
}

void PartialScreenshotView::Cancel() {
  DCHECK(window_);
  window_->Hide();
  Shell::GetInstance()->partial_screenshot_filter()->Deactivate();
  MessageLoop::current()->DeleteSoon(FROM_HERE, window_);
}

gfx::NativeCursor PartialScreenshotView::GetCursor(
    const views::MouseEvent& event) {
  // Always use "crosshair" cursor.
  return ui::kCursorCross;
}

void PartialScreenshotView::OnPaint(gfx::Canvas* canvas) {
  if (is_dragging_) {
    // Screenshot area representation: black rectangle with white
    // rectangle inside.  To avoid capturing these rectangles when mouse
    // release, they should be outside of the actual capturing area.
    gfx::Rect screenshot_rect = GetScreenshotRect();
    screenshot_rect.Inset(-1, -1, -1, -1);
    canvas->DrawRect(screenshot_rect, SK_ColorWHITE);
    screenshot_rect.Inset(-1, -1, -1, -1);
    canvas->DrawRect(screenshot_rect, SK_ColorBLACK);
  }
}

void PartialScreenshotView::OnMouseCaptureLost() {
  Cancel();
}

bool PartialScreenshotView::OnMousePressed(const views::MouseEvent& event) {
  start_position_ = event.location();
  return true;
}

bool PartialScreenshotView::OnMouseDragged(const views::MouseEvent& event) {
  current_position_ = event.location();
  SchedulePaint();
  is_dragging_ = true;
  return true;
}

bool PartialScreenshotView::OnMouseWheel(const views::MouseWheelEvent& event) {
  // Do nothing but do not propagate events futhermore.
  return true;
}

void PartialScreenshotView::OnMouseReleased(const views::MouseEvent& event) {
  Cancel();
  if (!is_dragging_)
    return;

  is_dragging_ = false;
  if (screenshot_delegate_) {
    aura::RootWindow *root_window = Shell::GetPrimaryRootWindow();
    screenshot_delegate_->HandleTakePartialScreenshot(
        root_window, root_window->bounds().Intersect(GetScreenshotRect()));
  }
}

gfx::Rect PartialScreenshotView::GetScreenshotRect() const {
  int left = std::min(start_position_.x(), current_position_.x());
  int top = std::min(start_position_.y(), current_position_.y());
  int width = ::abs(start_position_.x() - current_position_.x());
  int height = ::abs(start_position_.y() - current_position_.y());
  return gfx::Rect(left, top, width, height);
}

}  // namespace ash