Commit bc6d23dc authored by sadrul@chromium.org's avatar sadrul@chromium.org

ash: Add accelerator for snapping windows left/right.

BUG=none
TEST=none

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=133982

Review URL: https://chromiumcodereview.appspot.com/9956056

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133989 0039d316-1c4b-4281-b951-d872f2087c98
parent 02a5fe29
......@@ -21,8 +21,10 @@
#include "ash/system/brightness/brightness_control_delegate.h"
#include "ash/system/tray/system_tray.h"
#include "ash/volume_control_delegate.h"
#include "ash/wm/property_util.h"
#include "ash/wm/window_cycle_controller.h"
#include "ash/wm/window_util.h"
#include "ash/wm/workspace/snap_sizer.h"
#include "base/command_line.h"
#include "ui/aura/event.h"
#include "ui/aura/root_window.h"
......@@ -452,6 +454,41 @@ bool AcceleratorController::AcceleratorPressed(
case SELECT_LAST_WIN:
SwitchToWindow(-1);
break;
case WINDOW_SNAP_LEFT:
case WINDOW_SNAP_RIGHT: {
aura::Window* window = wm::GetActiveWindow();
if (!window)
break;
internal::SnapSizer sizer(window,
gfx::Point(),
action == WINDOW_SNAP_LEFT ? internal::SnapSizer::LEFT_EDGE :
internal::SnapSizer::RIGHT_EDGE,
shell->GetGridSize());
window->SetBounds(sizer.GetSnapBounds(window->bounds()));
break;
}
case WINDOW_MINIMIZE: {
aura::Window* window = wm::GetActiveWindow();
if (window)
wm::MinimizeWindow(window);
break;
}
case WINDOW_MAXIMIZE_RESTORE: {
aura::Window* window = wm::GetActiveWindow();
if (window) {
if (wm::IsWindowMaximized(window))
wm::RestoreWindow(window);
else
wm::MaximizeWindow(window);
}
break;
}
case WINDOW_POSITION_CENTER: {
aura::Window* window = wm::GetActiveWindow();
if (window)
wm::CenterWindow(window);
break;
}
case ROTATE_WINDOWS:
return HandleRotateWindows();
#if !defined(NDEBUG)
......
......@@ -85,6 +85,13 @@ const AcceleratorData kAcceleratorData[] = {
{ true, ui::VKEY_8, true, false, true, SELECT_WIN_7 },
{ true, ui::VKEY_9, true, false, true, SELECT_LAST_WIN },
// Window management shortcuts.
{ true, ui::VKEY_OEM_4, false, false, true, WINDOW_SNAP_LEFT },
{ true, ui::VKEY_OEM_6, false, false, true, WINDOW_SNAP_RIGHT },
{ true, ui::VKEY_OEM_MINUS, false, false, true, WINDOW_MINIMIZE },
{ true, ui::VKEY_OEM_PLUS, false, false, true, WINDOW_MAXIMIZE_RESTORE },
{ true, ui::VKEY_OEM_PLUS, true, false, true, WINDOW_POSITION_CENTER },
{ true, ui::VKEY_F3, true, true, true, ROTATE_WINDOWS },
#if !defined(NDEBUG)
{ true, ui::VKEY_HOME, false, true, false, ROTATE_SCREEN },
......
......@@ -46,6 +46,11 @@ enum AcceleratorAction {
VOLUME_DOWN,
VOLUME_MUTE,
VOLUME_UP,
WINDOW_MAXIMIZE_RESTORE,
WINDOW_MINIMIZE,
WINDOW_POSITION_CENTER,
WINDOW_SNAP_LEFT,
WINDOW_SNAP_RIGHT,
#if defined(OS_CHROMEOS)
LOCK_SCREEN,
OPEN_CROSH,
......
......@@ -12,6 +12,7 @@
#include "ui/aura/window.h"
#include "ui/aura/window_property.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/monitor.h"
#include "ui/gfx/screen.h"
DECLARE_WINDOW_PROPERTY_TYPE(bool);
......@@ -96,5 +97,11 @@ void RestoreWindow(aura::Window* window) {
window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
}
void CenterWindow(aura::Window* window) {
const gfx::Monitor monitor = gfx::Screen::GetMonitorNearestWindow(window);
gfx::Rect center = monitor.work_area().Center(window->bounds().size());
window->SetBounds(center);
}
} // namespace wm
} // namespace ash
......@@ -50,6 +50,9 @@ ASH_EXPORT void MinimizeWindow(aura::Window* window);
// Restores |window|, which must not be NULL.
ASH_EXPORT void RestoreWindow(aura::Window* window);
// Moves the window to the center of the monitor.
ASH_EXPORT void CenterWindow(aura::Window* window);
} // namespace wm
} // namespace ash
......
......@@ -66,6 +66,18 @@ void SnapSizer::Update(const gfx::Point& location) {
time_last_update_ = base::TimeTicks::Now();
}
gfx::Rect SnapSizer::GetSnapBounds(const gfx::Rect& bounds) {
size_t current;
for (current = 0; current < arraysize(kPercents); ++current) {
gfx::Rect target = GetTargetBoundsForPercent(current);
if (target == bounds) {
++current;
break;
}
}
return GetTargetBoundsForPercent(current % arraysize(kPercents));
}
int SnapSizer::CalculateIncrement(int x, int reference_x) const {
if (AlongEdge(x))
return 1;
......@@ -94,12 +106,16 @@ void SnapSizer::ChangeBounds(int x, int delta) {
}
gfx::Rect SnapSizer::GetTargetBounds() const {
return GetTargetBoundsForPercent(percent_index_);
}
gfx::Rect SnapSizer::GetTargetBoundsForPercent(int percent_index) const {
gfx::Rect work_area(ScreenAsh::GetUnmaximizedWorkAreaBounds(window_));
int y = WindowResizer::AlignToGridRoundUp(work_area.y(), grid_size_);
// We don't align to the bottom of the grid as the launcher may not
// necessarily align to the grid (happens when auto-hidden).
int max_y = work_area.bottom();
int width = static_cast<float>(work_area.width()) * kPercents[percent_index_];
int width = static_cast<float>(work_area.width()) * kPercents[percent_index];
if (edge_ == LEFT_EDGE) {
int x = WindowResizer::AlignToGridRoundUp(work_area.x(), grid_size_);
int mid_x = WindowResizer::AlignToGridRoundUp(
......
......@@ -38,6 +38,10 @@ class ASH_EXPORT SnapSizer {
// Bounds to position the window at.
const gfx::Rect& target_bounds() const { return target_bounds_; }
// Returns the appropriate snap bounds (e.g. if a window is already snapped,
// then it returns the next snap-bounds).
gfx::Rect GetSnapBounds(const gfx::Rect& bounds);
private:
// Calculates the amount to increment by. This returns one of -1, 0 or 1 and
// is intended to by applied to |percent_index_|. |x| is the current
......@@ -53,6 +57,8 @@ class ASH_EXPORT SnapSizer {
// Returns the target bounds based on the edge and |percent_index_|.
gfx::Rect GetTargetBounds() const;
gfx::Rect GetTargetBoundsForPercent(int percent_index) const;
// Returns true if the specified point is along the edge of the screen.
bool AlongEdge(int x) const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment