folder_selection_dialog_controller.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_CAPTURE_MODE_FOLDER_SELECTION_DIALOG_CONTROLLER_H_
  5. #define ASH_CAPTURE_MODE_FOLDER_SELECTION_DIALOG_CONTROLLER_H_
  6. #include "ash/wm/window_dimmer.h"
  7. #include "base/callback_forward.h"
  8. #include "base/files/file_path.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/scoped_observation.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/shell_dialogs/select_file_dialog.h"
  13. #include "ui/wm/core/transient_window_manager.h"
  14. #include "ui/wm/core/transient_window_observer.h"
  15. namespace ui {
  16. class Event;
  17. } // namespace ui
  18. namespace ash {
  19. // Creates, shows and controls capture mode's folder selection dialog.
  20. class FolderSelectionDialogController : public ui::SelectFileDialog::Listener,
  21. public wm::TransientWindowObserver {
  22. public:
  23. class Delegate {
  24. public:
  25. // Called to inform the delegate that the user selected the given |path|.
  26. virtual void OnFolderSelected(const base::FilePath& path) = 0;
  27. // Called to inform the delegate that the dialog window has been added.
  28. virtual void OnSelectionWindowAdded() = 0;
  29. // Called to inform the delegate that the dialog window has been closed.
  30. // This will be called for both when the user selects and accepts a folder
  31. // or cancels or closes the dialog without making a selection.
  32. virtual void OnSelectionWindowClosed() = 0;
  33. protected:
  34. virtual ~Delegate() = default;
  35. };
  36. // Constructs and shows a folder selection dialog under the given |root|
  37. // window.
  38. FolderSelectionDialogController(Delegate* delegate, aura::Window* root);
  39. FolderSelectionDialogController(const FolderSelectionDialogController&) =
  40. delete;
  41. FolderSelectionDialogController& operator=(
  42. const FolderSelectionDialogController&) = delete;
  43. ~FolderSelectionDialogController() override;
  44. aura::Window* dialog_window() { return dialog_window_; }
  45. bool did_user_select_a_folder() const { return did_user_select_a_folder_; }
  46. // Returns false while the dialog window is shown, and the |event| is
  47. // targeting a window in the dialog subtree (in this case, the
  48. // |CaptureModeSession| should not consume this event and let it go through to
  49. // the dialog). Returns true otherwise so the session can consume this event.
  50. bool ShouldConsumeEvent(const ui::Event* event) const;
  51. // ui::SelectFileDialog::Listener:
  52. void FileSelected(const base::FilePath& path,
  53. int index,
  54. void* params) override;
  55. // wm::TransientWindowObserver:
  56. void OnTransientChildAdded(aura::Window* window,
  57. aura::Window* transient) override;
  58. void OnTransientChildRemoved(aura::Window* window,
  59. aura::Window* transient) override;
  60. private:
  61. friend class CaptureModeTestApi;
  62. Delegate* const delegate_;
  63. // Dims everything behind the dialog (including the capture bar, the settings
  64. // menu, and any capture-related UIs). The dimming window is the transient
  65. // parent of the dialog window.
  66. WindowDimmer dialog_background_dimmer_;
  67. // Provides us with the APIs needed to construct a folder selection dialog.
  68. scoped_refptr<ui::SelectFileDialog> select_folder_dialog_;
  69. // This is the window of the dialog that gets created by
  70. // |select_folder_dialog_| as a transient child of the dimming window.
  71. aura::Window* dialog_window_ = nullptr;
  72. // It will be set to true when user selects a folder from the dialog.
  73. bool did_user_select_a_folder_ = false;
  74. // An optional callback that will be invoked when |dialog_window_| gets added.
  75. base::OnceClosure on_dialog_window_added_callback_for_test_;
  76. // We observe the transient window manager of the dimming window to know when
  77. // the dialog window is added or removed.
  78. base::ScopedObservation<wm::TransientWindowManager,
  79. wm::TransientWindowObserver>
  80. window_observation_{this};
  81. };
  82. } // namespace ash
  83. #endif // ASH_CAPTURE_MODE_FOLDER_SELECTION_DIALOG_CONTROLLER_H_