File: folder_selection_dialog_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 4,907 bytes parent folder | download | duplicates (6)
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
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2021 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/capture_mode/folder_selection_dialog_controller.h"

#include <memory>

#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "base/files/file_path.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window_observer.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/event.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/transient_window_manager.h"

namespace ash {

namespace {

// Returns true if |event| is targeting a window in the subtree rooted at
// |window|.
bool IsEventTargetingWindowInSubtree(const ui::Event* event,
                                     const aura::Window* window) {
  DCHECK(window);

  auto* target = static_cast<aura::Window*>(event->target());
  return window->Contains(target);
}

}  // namespace

FolderSelectionDialogController::FolderSelectionDialogController(
    Delegate* delegate,
    aura::Window* root)
    : delegate_(delegate),
      // The SettingBubbleContainer was chosen since it's below the virtual
      // keyboard container, and therefore users can use the VK to interact with
      // this dialog e.g. to rename a folder.
      dialog_background_dimmer_(
          root->GetChildById(kShellWindowId_SettingBubbleContainer)),
      select_folder_dialog_(ui::SelectFileDialog::Create(
          /*listener=*/this,
          /*policy=*/nullptr)) {
  DCHECK(delegate_);
  DCHECK(root);
  DCHECK(root->IsRootWindow());

  auto* owner = dialog_background_dimmer_.window();
  owner->SetId(kShellWindowId_CaptureModeFolderSelectionDialogOwner);
  window_observation_.Observe(wm::TransientWindowManager::GetOrCreate(owner));

  dialog_background_dimmer_.SetDimColor(kColorAshShieldAndBase40);
  owner->Show();

  select_folder_dialog_->SelectFile(
      ui::SelectFileDialog::SELECT_FOLDER,
      l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_SAVE_TO_DIALOG_TITLE),
      /*default_path=*/base::FilePath(),
      /*file_types=*/nullptr,
      /*file_type_index=*/0,
      /*default_extension=*/base::FilePath::StringType(),
      /*owning_window=*/owner);
}

FolderSelectionDialogController::~FolderSelectionDialogController() {
  if (select_folder_dialog_)
    select_folder_dialog_->ListenerDestroyed();
}

bool FolderSelectionDialogController::ShouldConsumeEvent(
    const ui::Event* event) const {
  if (!dialog_window_)
    return true;

  if (IsEventTargetingWindowInSubtree(event, dialog_window_))
    return false;

  // The event maybe targeting a virtual keyboard window that is being used to
  // interact with the dialog. In this case the event should not be consumed.
  auto* keyboard_ui_controller = keyboard::KeyboardUIController::Get();
  DCHECK(keyboard_ui_controller);

  if (!keyboard_ui_controller->IsKeyboardVisible())
    return true;

  auto* keyboard_window = keyboard_ui_controller->GetKeyboardWindow();
  if (!keyboard_window ||
      keyboard_window->GetRootWindow() != dialog_window_->GetRootWindow()) {
    return true;
  }

  return !IsEventTargetingWindowInSubtree(event, keyboard_window);
}

void FolderSelectionDialogController::FileSelected(
    const ui::SelectedFileInfo& file,
    int index) {
  did_user_select_a_folder_ = true;
  delegate_->OnFolderSelected(file.path());
}

void FolderSelectionDialogController::OnTransientChildAdded(
    aura::Window* window,
    aura::Window* transient) {
  DCHECK_EQ(window, dialog_background_dimmer_.window());
  DCHECK(!dialog_window_);

  dialog_window_ = transient;

  // The dialog should never resize, minimize or maximize.
  auto* widget = views::Widget::GetWidgetForNativeWindow(dialog_window_);
  DCHECK(widget);
  views::WidgetDelegate* widget_delegate = widget->widget_delegate();
  DCHECK(widget_delegate);
  widget_delegate->SetCanResize(false);
  widget_delegate->SetCanMinimize(false);
  widget_delegate->SetCanMaximize(false);
  widget_delegate->SetCanFullscreen(false);

  delegate_->OnSelectionWindowAdded();

  if (on_dialog_window_added_callback_for_test_)
    std::move(on_dialog_window_added_callback_for_test_).Run();
}

void FolderSelectionDialogController::OnTransientChildRemoved(
    aura::Window* window,
    aura::Window* transient) {
  DCHECK_EQ(window, dialog_background_dimmer_.window());
  DCHECK(dialog_window_);
  DCHECK_EQ(transient, dialog_window_);

  dialog_window_ = nullptr;
  delegate_->OnSelectionWindowClosed();
  // |this| will be deleted after the above call.
}

}  // namespace ash