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

ash / display / display_move_window_util.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/display/display_move_window_util.h"

#include <stdint.h>
#include <algorithm>
#include <array>

#include "ash/accessibility/accessibility_controller.h"
#include "ash/shell.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/window_util.h"
#include "base/containers/contains.h"
#include "base/metrics/user_metrics.h"
#include "ui/aura/window.h"
#include "ui/display/display.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_constants.h"
#include "ui/display/util/display_util.h"

namespace ash {

namespace display_move_window_util {

namespace {

aura::Window* GetTargetWindow() {
  aura::Window* window = window_util::GetActiveWindow();
  if (!window)
    return nullptr;

  // If |window| is transient window, move its first non-transient
  // transient-parent window instead.
  if (::wm::GetTransientParent(window)) {
    while (::wm::GetTransientParent(window))
      window = ::wm::GetTransientParent(window);
    if (window == window->GetRootWindow())
      return nullptr;
  }
  return window;
}

}  // namespace

bool CanHandleMoveActiveWindowBetweenDisplays() {
  display::DisplayManager* display_manager = Shell::Get()->display_manager();
  // Accelerators to move window between displays on unified desktop mode and
  // mirror mode is disabled.
  if (display_manager->IsInUnifiedMode() || display_manager->IsInMirrorMode())
    return false;

  if (display::Screen::GetScreen()->GetNumDisplays() < 2)
    return false;

  // The movement target window must be in window cycle list.
  return base::Contains(
      Shell::Get()->mru_window_tracker()->BuildWindowForCycleList(kActiveDesk),
      GetTargetWindow());
}

void HandleMoveActiveWindowBetweenDisplays() {
  DCHECK(CanHandleMoveActiveWindowBetweenDisplays());
  aura::Window* window = GetTargetWindow();
  DCHECK(window);

  int64_t origin_display_id =
      display::Screen::GetScreen()->GetDisplayNearestWindow(window).id();
  auto displays = display::Screen::GetScreen()->GetAllDisplays();
  display::DisplayIdList display_id_list =
      display::CreateDisplayIdList(displays);
  // Find target display id in sorted display id list in a cycling way.
  auto itr = std::upper_bound(display_id_list.begin(), display_id_list.end(),
                              origin_display_id, display::CompareDisplayIds);
  int64_t target_display_id =
      itr == display_id_list.end() ? display_id_list[0] : *itr;
  window_util::MoveWindowToDisplay(window, target_display_id);
  Shell::Get()->accessibility_controller()->TriggerAccessibilityAlert(
      AccessibilityAlert::WINDOW_MOVED_TO_ANOTHER_DISPLAY);
  base::RecordAction(
      base::UserMetricsAction("Accel_Move_Active_Window_Between_Displays"));
}

}  // namespace display_move_window_util

}  // namespace ash