blob: b8f67fc307c5d73ecde4b56ecbb07e0bed561782 [file] [log] [blame]
Mitsuru Oshima3e165792017-12-11 22:27:481// Copyright 2015 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
5#ifndef COMPONENTS_EXO_SHELL_SURFACE_BASE_H_
6#define COMPONENTS_EXO_SHELL_SURFACE_BASE_H_
7
8#include <cstdint>
9#include <memory>
10#include <string>
11
12#include "ash/display/window_tree_host_manager.h"
[email protected]93855732019-06-24 19:49:3013#include "base/gtest_prod_util.h"
Mitsuru Oshima3e165792017-12-11 22:27:4814#include "base/macros.h"
15#include "base/optional.h"
16#include "base/strings/string16.h"
17#include "components/exo/surface_observer.h"
18#include "components/exo/surface_tree_host.h"
Dominic Mazzoni336bc0062018-09-23 16:46:4319#include "ui/accessibility/ax_tree_id.h"
Mitsuru Oshimaa9697322018-06-19 07:11:5320#include "ui/aura/client/capture_client_observer.h"
Mitsuru Oshima3e165792017-12-11 22:27:4821#include "ui/aura/window_observer.h"
22#include "ui/base/hit_test.h"
Mitsuru Oshima3e165792017-12-11 22:27:4823#include "ui/display/display_observer.h"
Dominik Laskowski4c2657362018-09-05 00:35:0924#include "ui/display/types/display_constants.h"
Mitsuru Oshima3e165792017-12-11 22:27:4825#include "ui/gfx/geometry/point.h"
26#include "ui/gfx/geometry/rect.h"
27#include "ui/gfx/geometry/vector2d.h"
28#include "ui/views/widget/widget_delegate.h"
Nicholas Hollingum266cddd2019-10-03 06:12:4429#include "ui/views/widget/widget_observer.h"
Mitsuru Oshima3e165792017-12-11 22:27:4830#include "ui/wm/public/activation_change_observer.h"
31
32namespace ash {
Mitsuru Oshima11439772017-12-14 02:06:5933class WindowState;
Nicholas Hollingum605b8372019-03-13 01:41:5734} // namespace ash
Mitsuru Oshima3e165792017-12-11 22:27:4835
36namespace base {
37namespace trace_event {
38class TracedValue;
39}
40} // namespace base
41
Mitsuru Oshima3e165792017-12-11 22:27:4842namespace exo {
43class Surface;
44
45// This class provides functions for treating a surfaces like toplevel,
46// fullscreen or popup widgets, move, resize or maximize them, associate
47// metadata like title and class, etc.
48class ShellSurfaceBase : public SurfaceTreeHost,
49 public SurfaceObserver,
50 public aura::WindowObserver,
Mitsuru Oshimaa9697322018-06-19 07:11:5351 public aura::client::CaptureClientObserver,
Mitsuru Oshima3e165792017-12-11 22:27:4852 public views::WidgetDelegate,
Nicholas Hollingum266cddd2019-10-03 06:12:4453 public views::WidgetObserver,
Mitsuru Oshima3e165792017-12-11 22:27:4854 public views::View,
Mitsuru Oshima3e165792017-12-11 22:27:4855 public wm::ActivationChangeObserver {
56 public:
57 // The |origin| is the initial position in screen coordinates. The position
58 // specified as part of the geometry is relative to the shell surface.
59 ShellSurfaceBase(Surface* surface,
60 const gfx::Point& origin,
61 bool activatable,
62 bool can_minimize,
63 int container);
64 ~ShellSurfaceBase() override;
65
66 // Set the callback to run when the user wants the shell surface to be closed.
67 // The receiver can chose to not close the window on this signal.
68 void set_close_callback(const base::RepeatingClosure& close_callback) {
69 close_callback_ = close_callback;
70 }
71
Nicholas Hollingum3acc1d12019-09-09 02:22:0872 // Set the callback to run when the user has requested to close the surface.
73 // This runs before the normal |close_callback_| and should not be used to
74 // actually close the surface.
75 void set_pre_close_callback(const base::RepeatingClosure& close_callback) {
76 pre_close_callback_ = close_callback;
77 }
78
Mitsuru Oshima3e165792017-12-11 22:27:4879 // Set the callback to run when the surface is destroyed.
80 void set_surface_destroyed_callback(
81 base::OnceClosure surface_destroyed_callback) {
82 surface_destroyed_callback_ = std::move(surface_destroyed_callback);
83 }
84
Mitsuru Oshima3e165792017-12-11 22:27:4885 // Activates the shell surface.
86 void Activate();
87
88 // Set title for the surface.
89 void SetTitle(const base::string16& title);
90
91 // Set icon for the surface.
92 void SetIcon(const gfx::ImageSkia& icon);
93
94 // Sets the system modality.
95 void SetSystemModal(bool system_modal);
96
Mitsuru Oshima3e165792017-12-11 22:27:4897 // Set the application ID for the surface.
David Reveman8bc16412018-04-19 21:59:1198 void SetApplicationId(const char* application_id);
Mitsuru Oshima3e165792017-12-11 22:27:4899
Tim Zheng08df5662018-04-04 05:08:15100 // Set the startup ID for the surface.
101 void SetStartupId(const char* startup_id);
102
Yuki Awanob4514da2018-06-28 05:29:19103 // Set the child ax tree ID for the surface.
Dominic Mazzoni336bc0062018-09-23 16:46:43104 void SetChildAxTreeId(ui::AXTreeID child_ax_tree_id);
Yuki Awanob4514da2018-06-28 05:29:19105
Mitsuru Oshima3e165792017-12-11 22:27:48106 // Set geometry for surface. The geometry represents the "visible bounds"
107 // for the surface from the user's perspective.
108 void SetGeometry(const gfx::Rect& geometry);
109
Dominik Laskowski4c2657362018-09-05 00:35:09110 // If set, geometry is in display rather than window or screen coordinates.
111 void SetDisplay(int64_t display_id);
112
Mitsuru Oshima3e165792017-12-11 22:27:48113 // Set origin in screen coordinate space.
114 void SetOrigin(const gfx::Point& origin);
115
116 // Set activatable state for surface.
117 void SetActivatable(bool activatable);
118
119 // Set container for surface.
120 void SetContainer(int container);
121
122 // Set the maximum size for the surface.
123 void SetMaximumSize(const gfx::Size& size);
124
125 // Set the miniumum size for the surface.
126 void SetMinimumSize(const gfx::Size& size);
127
yoshiki iguchi239252ce2019-01-12 03:22:06128 // Set the aspect ratio for the surface.
129 void SetAspectRatio(const gfx::SizeF& aspect_ratio);
130
131 // Set the flag if the surface can maximize or not.
Mitsuru Oshima3e165792017-12-11 22:27:48132 void SetCanMinimize(bool can_minimize);
133
Mitsuru Oshima62881372018-02-06 01:45:04134 // Prevents shell surface from being moved.
135 void DisableMovement();
136
Mitsuru Oshima3e165792017-12-11 22:27:48137 // Returns a trace value representing the state of the surface.
138 std::unique_ptr<base::trace_event::TracedValue> AsTracedValue() const;
139
Peter Kasting9ffb58bc72020-01-13 17:28:05140 // SurfaceDelegate:
Mitsuru Oshima3e165792017-12-11 22:27:48141 void OnSurfaceCommit() override;
Dominik Laskowski14a163772018-02-09 19:25:18142 bool IsInputEnabled(Surface* surface) const override;
Mitsuru Oshima3e165792017-12-11 22:27:48143 void OnSetFrame(SurfaceFrameType type) override;
David Reveman786d3182017-12-20 22:04:33144 void OnSetFrameColors(SkColor active_color, SkColor inactive_color) override;
Tim Zheng08df5662018-04-04 05:08:15145 void OnSetStartupId(const char* startup_id) override;
David Reveman21e2236d2018-04-12 06:01:10146 void OnSetApplicationId(const char* application_id) override;
Chloe Pelling8a8acdeb2020-07-07 10:45:20147 void SetUseImmersiveForFullscreen(bool value) override;
Nicholas Hollingumbad8a05f62019-12-05 05:56:21148 void OnActivationRequested() override;
Mitsuru Oshima3e165792017-12-11 22:27:48149
Peter Kasting9ffb58bc72020-01-13 17:28:05150 // SurfaceObserver:
Mitsuru Oshima3e165792017-12-11 22:27:48151 void OnSurfaceDestroying(Surface* surface) override;
Prabir Pradhan875083e2020-07-22 20:06:27152 void OnContentSizeChanged(Surface*) override {}
Mitsuru Oshima3e165792017-12-11 22:27:48153
Peter Kasting9ffb58bc72020-01-13 17:28:05154 // CaptureClientObserver:
Mitsuru Oshimaa9697322018-06-19 07:11:53155 void OnCaptureChanged(aura::Window* lost_capture,
156 aura::Window* gained_capture) override;
157
Peter Kasting9ffb58bc72020-01-13 17:28:05158 // views::WidgetDelegate:
Mitsuru Oshima3e165792017-12-11 22:27:48159 bool CanResize() const override;
160 bool CanMaximize() const override;
161 bool CanMinimize() const override;
Dana Fried46c516e2018-12-17 23:00:52162 bool OnCloseRequested(views::Widget::ClosedReason close_reason) override;
Mitsuru Oshima3e165792017-12-11 22:27:48163 void WindowClosing() override;
Allen Bauerd66100e2020-02-10 18:11:43164 views::Widget* GetWidget() override;
165 const views::Widget* GetWidget() const override;
Mitsuru Oshima3e165792017-12-11 22:27:48166 views::View* GetContentsView() override;
Thomas Lukaszewicz814abb352020-06-29 21:35:16167 std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameView(
Mitsuru Oshima3e165792017-12-11 22:27:48168 views::Widget* widget) override;
169 bool WidgetHasHitTestMask() const override;
Raul Tambre038e96d2019-01-07 21:47:44170 void GetWidgetHitTestMask(SkPath* mask) const override;
Mitsuru Oshima3e165792017-12-11 22:27:48171
Peter Kasting9ffb58bc72020-01-13 17:28:05172 // views::WidgetObserver:
Nicholas Hollingum266cddd2019-10-03 06:12:44173 void OnWidgetClosing(views::Widget* widget) override;
174
Peter Kasting9ffb58bc72020-01-13 17:28:05175 // views::View:
Mitsuru Oshima3e165792017-12-11 22:27:48176 gfx::Size CalculatePreferredSize() const override;
177 gfx::Size GetMinimumSize() const override;
178 gfx::Size GetMaximumSize() const override;
Yuki Awanob4514da2018-06-28 05:29:19179 void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
Mitsuru Oshima3e165792017-12-11 22:27:48180
Peter Kasting9ffb58bc72020-01-13 17:28:05181 // aura::WindowObserver:
Mitsuru Oshima3e165792017-12-11 22:27:48182 void OnWindowDestroying(aura::Window* window) override;
Yuichiro Hanada3a6c31d02020-06-03 08:45:12183 void OnWindowPropertyChanged(aura::Window* window,
184 const void* key,
185 intptr_t old_value) override;
Mitsuru Oshima91322d52020-08-03 22:43:33186 void OnWindowAddedToRootWindow(aura::Window* window) override;
Mitsuru Oshima3e165792017-12-11 22:27:48187
Peter Kasting9ffb58bc72020-01-13 17:28:05188 // wm::ActivationChangeObserver:
Mitsuru Oshima3e165792017-12-11 22:27:48189 void OnWindowActivated(ActivationReason reason,
190 aura::Window* gained_active,
191 aura::Window* lost_active) override;
192
Peter Kasting9ffb58bc72020-01-13 17:28:05193 // ui::AcceleratorTarget:
Mitsuru Oshima3e165792017-12-11 22:27:48194 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
195
Mitsuru Oshima4eb127ce2018-07-09 23:53:39196 bool frame_enabled() const {
197 return frame_type_ != SurfaceFrameType::NONE &&
198 frame_type_ != SurfaceFrameType::SHADOW;
199 }
200
Mitsuru Oshima3e165792017-12-11 22:27:48201 Surface* surface_for_testing() { return root_surface(); }
Kazuki Takise709a7fc2020-07-07 10:39:00202 bool get_shadow_bounds_changed_for_testing() {
203 return shadow_bounds_changed_;
204 }
Mitsuru Oshima3e165792017-12-11 22:27:48205
206 protected:
Mitsuru Oshima3e165792017-12-11 22:27:48207 // Creates the |widget_| for |surface_|. |show_state| is the initial state
208 // of the widget (e.g. maximized).
209 void CreateShellSurfaceWidget(ui::WindowShowState show_state);
210
Mitsuru Oshima3e165792017-12-11 22:27:48211 // Returns true if surface is currently being resized.
212 bool IsResizing() const;
213
214 // Updates the bounds of widget to match the current surface bounds.
215 void UpdateWidgetBounds();
216
217 // Called by UpdateWidgetBounds to set widget bounds.
Dominik Laskowski2398e74d2018-08-16 21:28:31218 virtual void SetWidgetBounds(const gfx::Rect& bounds) = 0;
Mitsuru Oshima3e165792017-12-11 22:27:48219
220 // Updates the bounds of surface to match the current widget bounds.
221 void UpdateSurfaceBounds();
222
223 // Creates, deletes and update the shadow bounds based on
224 // |shadow_bounds_|.
225 void UpdateShadow();
226
Kazuki Takise2186bd042020-05-12 03:25:32227 virtual void UpdateFrameType();
228
Mitsuru Oshima3e165792017-12-11 22:27:48229 // Applies |system_modal_| to |widget_|.
230 void UpdateSystemModal();
231
232 // Returns the "visible bounds" for the surface from the user's perspective.
233 gfx::Rect GetVisibleBounds() const;
234
Mitsuru Oshimaa9697322018-06-19 07:11:53235 // Returns the bounds of the client area.nnn
236 gfx::Rect GetClientViewBounds() const;
237
Dominik Laskowski2d4316412017-12-13 19:14:44238 // In the local coordinate system of the window.
239 virtual gfx::Rect GetShadowBounds() const;
240
Mitsuru Oshimaa9697322018-06-19 07:11:53241 // Start the event capture on this surface.
242 void StartCapture();
243
Mitsuru Oshima014ad082018-03-30 22:36:06244 const gfx::Rect& geometry() const { return geometry_; }
245
Mitsuru Oshima37a0eedb2018-09-14 20:42:21246 // Install custom window targeter. Used to restore window targeter.
247 void InstallCustomWindowTargeter();
248
Mitsuru Oshimaaff9c5292019-05-22 07:49:14249 // Creates a NonClientFrameView for shell surface.
Thomas Lukaszewicz814abb352020-06-29 21:35:16250 std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameViewInternal(
Mitsuru Oshimaaff9c5292019-05-22 07:49:14251 views::Widget* widget,
252 bool client_controlled);
253
Kazuki Takise709a7fc2020-07-07 10:39:00254 virtual void OnPostWidgetCommit();
255
Mitsuru Oshima3e165792017-12-11 22:27:48256 views::Widget* widget_ = nullptr;
257 aura::Window* parent_ = nullptr;
258 bool movement_disabled_ = false;
259 gfx::Point origin_;
260
261 // Container Window Id (see ash/public/cpp/shell_window_ids.h)
262 int container_;
Jun Mukai17c449d2018-04-13 18:38:38263 gfx::Rect geometry_;
264 gfx::Rect pending_geometry_;
Dominik Laskowski4c2657362018-09-05 00:35:09265 int64_t display_id_ = display::kInvalidDisplayId;
266 int64_t pending_display_id_ = display::kInvalidDisplayId;
Mitsuru Oshima3e165792017-12-11 22:27:48267 base::Optional<gfx::Rect> shadow_bounds_;
268 bool shadow_bounds_changed_ = false;
Mitsuru Oshima8f000502018-04-07 23:11:50269 SurfaceFrameType frame_type_ = SurfaceFrameType::NONE;
Mitsuru Oshimaa9697322018-06-19 07:11:53270 bool is_popup_ = false;
271 bool has_grab_ = false;
Mitsuru Oshima8f000502018-04-07 23:11:50272
Mitsuru Oshima3e165792017-12-11 22:27:48273 private:
[email protected]93855732019-06-24 19:49:30274 FRIEND_TEST_ALL_PREFIXES(ShellSurfaceTest,
275 HostWindowBoundsUpdatedAfterCommitWidget);
276
Mitsuru Oshima3e165792017-12-11 22:27:48277 // Called on widget creation to initialize its window state.
278 // TODO(reveman): Remove virtual functions below to avoid FBC problem.
James Cook00e65e92019-07-25 03:19:08279 virtual void InitializeWindowState(ash::WindowState* window_state) = 0;
Mitsuru Oshima3e165792017-12-11 22:27:48280
Mitsuru Oshima3e165792017-12-11 22:27:48281 // Returns the scale of the surface tree relative to the shell surface.
282 virtual float GetScale() const;
283
Mitsuru Oshima9f185602018-04-02 21:16:06284 // Return the bounds of the widget/origin of surface taking visible
285 // bounds and current resize direction into account.
Dominik Laskowski090ddbf2018-08-16 21:21:18286 virtual base::Optional<gfx::Rect> GetWidgetBounds() const = 0;
287 virtual gfx::Point GetSurfaceOrigin() const = 0;
Mitsuru Oshima3e165792017-12-11 22:27:48288
Dominik Laskowski4e71dae2018-08-25 04:12:45289 // Commit is deferred if this returns false.
290 virtual bool OnPreWidgetCommit() = 0;
Dominik Laskowski5b1287f2018-08-15 23:18:46291
292 void CommitWidget();
293
Mitsuru Oshima3e165792017-12-11 22:27:48294 bool activatable_ = true;
295 bool can_minimize_ = true;
David Reveman786d3182017-12-20 22:04:33296 bool has_frame_colors_ = false;
297 SkColor active_frame_color_ = SK_ColorBLACK;
298 SkColor inactive_frame_color_ = SK_ColorBLACK;
Mitsuru Oshima3e165792017-12-11 22:27:48299 bool pending_show_widget_ = false;
David Reveman8bc16412018-04-19 21:59:11300 base::Optional<std::string> application_id_;
301 base::Optional<std::string> startup_id_;
Chloe Pelling8a8acdeb2020-07-07 10:45:20302 bool immersive_implied_by_fullscreen_ = true;
Mitsuru Oshima3e165792017-12-11 22:27:48303 base::RepeatingClosure close_callback_;
Nicholas Hollingum3acc1d12019-09-09 02:22:08304 base::RepeatingClosure pre_close_callback_;
Mitsuru Oshima3e165792017-12-11 22:27:48305 base::OnceClosure surface_destroyed_callback_;
Mitsuru Oshima3e165792017-12-11 22:27:48306 bool system_modal_ = false;
307 bool non_system_modal_window_was_active_ = false;
Mitsuru Oshima3e165792017-12-11 22:27:48308 gfx::Size minimum_size_;
309 gfx::Size pending_minimum_size_;
310 gfx::Size maximum_size_;
311 gfx::Size pending_maximum_size_;
yoshiki iguchi239252ce2019-01-12 03:22:06312 gfx::SizeF pending_aspect_ratio_;
Dominic Mazzoni336bc0062018-09-23 16:46:43313 ui::AXTreeID child_ax_tree_id_ = ui::AXTreeIDUnknown();
Mitsuru Oshima3e165792017-12-11 22:27:48314
315 DISALLOW_COPY_AND_ASSIGN(ShellSurfaceBase);
316};
317
318} // namespace exo
319
320#endif // COMPONENTS_EXO_SHELL_SURFACE_BASE_H_