From 32ecb21028f634051bd39c8fc4f02781918ae3dd Mon Sep 17 00:00:00 2001
From: "dslomov@chromium.org"
 <dslomov@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Thu, 23 Feb 2012 21:56:49 +0000
Subject: Minimize for panels in Aura compact mode

Review URL: http://codereview.chromium.org/9427024

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123340 0039d316-1c4b-4281-b951-d872f2087c98
---
 ash/wm/panel_frame_view.cc          | 14 ----------
 ash/wm/panel_layout_manager.cc      | 34 ++++++++++++++++++++++
 ash/wm/panel_layout_manager.h       |  2 ++
 ash/wm/panel_window_event_filter.cc | 56 ++++++++++++++++++++++++++-----------
 ash/wm/panel_window_event_filter.h  | 11 ++++++--
 5 files changed, 83 insertions(+), 34 deletions(-)

(limited to 'ash/wm')

diff --git a/ash/wm/panel_frame_view.cc b/ash/wm/panel_frame_view.cc
index 0196a54..abe7d51 100644
--- a/ash/wm/panel_frame_view.cc
+++ b/ash/wm/panel_frame_view.cc
@@ -64,12 +64,7 @@ class PanelCaption : public views::View,
     close_button_ =
         new PanelControlButton(this,
                                *rb.GetBitmapNamed(IDR_AURA_WINDOW_CLOSE_ICON));
-    minimize_button_ =
-        // TODO(dslomov): minimize: whole caption dclick/different icon
-        new PanelControlButton(this,
-                               *rb.GetBitmapNamed(IDR_AURA_WINDOW_ZOOM_ICON));
     AddChildView(close_button_);
-    AddChildView(minimize_button_);
   }
   virtual ~PanelCaption() {}
 
@@ -82,8 +77,6 @@ class PanelCaption : public views::View,
     View::ConvertPointToView(parent(), this, &translated_point);
     if (close_button_->GetMirroredBounds().Contains(translated_point))
       return HTCLOSE;
-    else if (minimize_button_->GetMirroredBounds().Contains(translated_point))
-      return HTMINBUTTON;
     return HTCAPTION;
   }
 
@@ -109,10 +102,6 @@ class PanelCaption : public views::View,
     close_button_->SetBoundsRect(
         gfx::Rect(width() - close_ps.width(), 0,
                   close_ps.width(), close_ps.height()));
-    gfx::Size minimize_ps = minimize_button_->GetPreferredSize();
-    minimize_button_->SetBoundsRect(
-        gfx::Rect(width() - close_ps.width() - minimize_ps.width(), 0,
-                  minimize_ps.width(), minimize_ps.height()));
   }
 
   // Overriden from views::ButtonListener:
@@ -120,12 +109,9 @@ class PanelCaption : public views::View,
                              const views::Event& event) OVERRIDE {
     if (sender == close_button_)
       GetWidget()->Close();
-    else if (sender == minimize_button_)
-      GetWidget()->Minimize();
   }
 
   views::Button* close_button_;
-  views::Button* minimize_button_;
 
   DISALLOW_COPY_AND_ASSIGN(PanelCaption);
 };
diff --git a/ash/wm/panel_layout_manager.cc b/ash/wm/panel_layout_manager.cc
index 2cc2870..d793428 100644
--- a/ash/wm/panel_layout_manager.cc
+++ b/ash/wm/panel_layout_manager.cc
@@ -8,15 +8,21 @@
 
 #include "ash/launcher/launcher.h"
 #include "ash/shell.h"
+#include "ash/wm/property_util.h"
 #include "base/auto_reset.h"
+#include "ui/aura/client/aura_constants.h"
 #include "ui/aura/root_window.h"
 #include "ui/aura/window.h"
 #include "ui/gfx/rect.h"
+#include "ui/gfx/size.h"
 #include "ui/views/widget/widget.h"
 
 namespace {
 const int kPanelMarginEdge = 4;
 const int kPanelMarginMiddle = 8;
+
+const int kMinimizedHeight = 24;
+
 const float kMaxHeightFactor = .80f;
 const float kMaxWidthFactor = .50f;
 }
@@ -49,6 +55,34 @@ void PanelLayoutManager::FinishDragging() {
   Relayout();
 }
 
+void PanelLayoutManager::ToggleMinimize(aura::Window* panel) {
+  DCHECK(panel->parent() == panel_container_);
+  if (panel->GetProperty(aura::client::kShowStateKey) ==
+      ui::SHOW_STATE_MINIMIZED) {
+    const gfx::Rect& old_bounds = panel->bounds();
+    panel->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
+
+    gfx::Rect new_bounds(old_bounds);
+    const gfx::Rect* restore_bounds = GetRestoreBounds(panel);
+    if (restore_bounds != NULL) {
+      new_bounds.set_height(restore_bounds->height());
+      new_bounds.set_y(old_bounds.bottom() - restore_bounds->height());
+      SetChildBounds(panel, new_bounds);
+      ClearRestoreBounds(panel);
+    }
+  } else {
+    const gfx::Rect& old_bounds = panel->bounds();
+    panel->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
+    SetRestoreBounds(panel, old_bounds);
+    SetChildBounds(panel,
+                   gfx::Rect(old_bounds.x(),
+                             old_bounds.bottom() - kMinimizedHeight,
+                             old_bounds.width(),
+                             kMinimizedHeight));
+  }
+  Relayout();
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // PanelLayoutManager, aura::LayoutManager implementation:
 
diff --git a/ash/wm/panel_layout_manager.h b/ash/wm/panel_layout_manager.h
index 90497ea..6951c29 100644
--- a/ash/wm/panel_layout_manager.h
+++ b/ash/wm/panel_layout_manager.h
@@ -41,6 +41,8 @@ class ASH_EXPORT PanelLayoutManager : public aura::LayoutManager {
   void StartDragging(aura::Window* panel);
   void FinishDragging();
 
+  void ToggleMinimize(aura::Window* panel);
+
   // Overridden from aura::LayoutManager:
   virtual void OnWindowResized() OVERRIDE;
   virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE;
diff --git a/ash/wm/panel_window_event_filter.cc b/ash/wm/panel_window_event_filter.cc
index 50c5e962..8eca9d4 100644
--- a/ash/wm/panel_window_event_filter.cc
+++ b/ash/wm/panel_window_event_filter.cc
@@ -26,7 +26,8 @@ PanelWindowEventFilter::PanelWindowEventFilter(
     : aura::EventFilter(),
       panel_container_(panel_container),
       layout_manager_(layout_manager),
-      dragged_panel_(NULL) {
+      dragged_panel_(NULL),
+      drag_state_(DRAG_NONE) {
 }
 
 PanelWindowEventFilter::~PanelWindowEventFilter() {
@@ -44,19 +45,48 @@ bool PanelWindowEventFilter::PreHandleMouseEvent(aura::Window* target,
       int hitResult = target->delegate()->
           GetNonClientComponent(event->location());
       if (hitResult == HTCAPTION) {
-        StartDrag(target, event);
-        return true;
+        if (drag_state_ == DRAG_NONE) {
+          dragged_panel_ = target;
+          drag_location_in_dragged_window_ = event->location();
+          drag_state_ = DRAG_CLICKED;
+          return true;
+        } else {
+          return false;
+        }
       } else {
         return false;
       }
     }
+
     case ui::ET_MOUSE_DRAGGED:
-      if (dragged_panel_ != NULL)
+      if (drag_state_ == DRAG_CLICKED) {
+        drag_state_ = DRAG_STARTED;
+        layout_manager_->StartDragging(dragged_panel_);
+      }
+      if (drag_state_ == DRAG_STARTED)
         return HandleDrag(target, event);
+      else
+        return false;
+
+    case ui::ET_MOUSE_CAPTURE_CHANGED:
+      if (drag_state_ == DRAG_STARTED) {
+        FinishDrag();
+        return true;
+      } else if (drag_state_ == DRAG_CLICKED) {
+        drag_state_ = DRAG_NONE;
+        dragged_panel_ = NULL;
+        return true;
+      }
       return false;
+
     case ui::ET_MOUSE_RELEASED:
-      if (dragged_panel_ != NULL) {
-        CompleteDrag(target, event);
+      if (drag_state_ == DRAG_STARTED) {
+        FinishDrag();
+        return true;
+      } else if (dragged_panel_ != NULL) {
+        drag_state_ = DRAG_NONE;
+        layout_manager_->ToggleMinimize(dragged_panel_);
+        dragged_panel_ = NULL;
         return true;
       }
       return false;
@@ -77,13 +107,6 @@ ui::GestureStatus PanelWindowEventFilter::PreHandleGestureEvent(
 }
 
 
-void PanelWindowEventFilter::StartDrag(aura::Window* target,
-                                       aura::LocatedEvent* event) {
-  dragged_panel_ = target;
-  drag_location_in_dragged_window_ = event->location();
-  layout_manager_->StartDragging(target);
-}
-
 bool PanelWindowEventFilter::HandleDrag(aura::Window* target,
                                         aura::LocatedEvent* event) {
   gfx::Rect target_bounds = dragged_panel_->bounds();
@@ -95,13 +118,12 @@ bool PanelWindowEventFilter::HandleDrag(aura::Window* target,
       event_location_in_parent.x() - drag_location_in_dragged_window_.x());
   dragged_panel_->SetBounds(target_bounds);
   return true;
-
 }
 
-void PanelWindowEventFilter::CompleteDrag(aura::Window* target,
-                                          aura::LocatedEvent* event) {
-  dragged_panel_ = NULL;
+void PanelWindowEventFilter::FinishDrag() {
   layout_manager_->FinishDragging();
+  drag_state_ = DRAG_NONE;
+  dragged_panel_ = NULL;
 }
 
 }
diff --git a/ash/wm/panel_window_event_filter.h b/ash/wm/panel_window_event_filter.h
index bcbfed2..8b9a04e 100644
--- a/ash/wm/panel_window_event_filter.h
+++ b/ash/wm/panel_window_event_filter.h
@@ -38,16 +38,21 @@ class PanelWindowEventFilter : public aura::EventFilter {
       aura::GestureEvent* event) OVERRIDE;
 
  private:
-  void StartDrag(aura::Window* target, aura::LocatedEvent* event);
+  enum DragState {
+    DRAG_NONE,
+    DRAG_CLICKED,
+    DRAG_STARTED
+  };
+
   bool HandleDrag(aura::Window* target, aura::LocatedEvent* event);
-  void CompleteDrag(aura::Window* target, aura::LocatedEvent* event);
+  void FinishDrag();
 
   aura::Window* panel_container_;
   PanelLayoutManager* layout_manager_;
   gfx::Point drag_origin_;
   gfx::Point drag_location_in_dragged_window_;
-  bool started_dragging_;
   aura::Window* dragged_panel_;
+  DragState drag_state_;
 
   DISALLOW_COPY_AND_ASSIGN(PanelWindowEventFilter);
 };
-- 
cgit v1.1