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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/apps/chrome_native_app_window_views_aura.h"
#include <utility>
#include "apps/ui/views/app_window_frame_view.h"
#include "build/build_config.h"
#include "chrome/browser/ui/views/apps/app_window_easy_resize_window_targeter.h"
#include "chrome/browser/ui/views/apps/shaped_app_window_targeter.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/widget/widget.h"
#if BUILDFLAG(IS_LINUX)
#include "chrome/browser/shell_integration_linux.h"
#endif
using extensions::AppWindow;
ui::mojom::WindowShowState ChromeNativeAppWindowViewsAura::GetRestorableState(
const ui::mojom::WindowShowState restore_state) const {
// Allowlist states to return so that invalid and transient states
// are not saved and used to restore windows when they are recreated.
switch (restore_state) {
case ui::mojom::WindowShowState::kNormal:
case ui::mojom::WindowShowState::kMaximized:
case ui::mojom::WindowShowState::kFullscreen:
return restore_state;
case ui::mojom::WindowShowState::kDefault:
case ui::mojom::WindowShowState::kMinimized:
case ui::mojom::WindowShowState::kInactive:
case ui::mojom::WindowShowState::kEnd:
return ui::mojom::WindowShowState::kNormal;
}
return ui::mojom::WindowShowState::kNormal;
}
void ChromeNativeAppWindowViewsAura::OnBeforeWidgetInit(
const AppWindow::CreateParams& create_params,
views::Widget::InitParams* init_params,
views::Widget* widget) {
#if BUILDFLAG(IS_LINUX)
std::string app_name =
web_app::GenerateApplicationNameFromAppId(app_window()->extension_id());
// Set up a custom WM_CLASS for app windows. This allows task switchers in
// X11 environments to distinguish them from main browser windows.
init_params->wm_class_name =
shell_integration_linux::GetWMClassFromAppName(app_name);
init_params->wm_class_class = shell_integration_linux::GetProgramClassClass();
const char kX11WindowRoleApp[] = "app";
init_params->wm_role_name = std::string(kX11WindowRoleApp);
#endif // BUILDFLAG(IS_LINUX)
ChromeNativeAppWindowViews::OnBeforeWidgetInit(create_params, init_params,
widget);
}
std::unique_ptr<views::NonClientFrameView>
ChromeNativeAppWindowViewsAura::CreateNonStandardAppFrame() {
auto frame = std::make_unique<apps::AppWindowFrameView>(
widget(), this, HasFrameColor(), ActiveFrameColor(),
InactiveFrameColor());
frame->Init();
// Install an easy resize window targeter, which ensures that the root window
// (not the app) receives mouse events on the edges.
aura::Window* window = widget()->GetNativeWindow();
// Add the AppWindowEasyResizeWindowTargeter on the window, not its root
// window. The root window does not have a delegate, which is needed to
// handle the event in Linux.
window->SetEventTargeter(std::make_unique<AppWindowEasyResizeWindowTargeter>(
gfx::Insets(frame->resize_inside_bounds_size()), this));
return frame;
}
ui::mojom::WindowShowState ChromeNativeAppWindowViewsAura::GetRestoredState()
const {
// First normal states are checked.
if (IsMaximized()) {
return ui::mojom::WindowShowState::kMaximized;
}
if (IsFullscreen()) {
return ui::mojom::WindowShowState::kFullscreen;
}
// Use kRestoreShowStateKey to get the window restore show state in case a
// window is minimized/hidden.
ui::mojom::WindowShowState restore_state =
widget()->GetNativeWindow()->GetProperty(
aura::client::kRestoreShowStateKey);
return GetRestorableState(restore_state);
}
ui::ZOrderLevel ChromeNativeAppWindowViewsAura::GetZOrderLevel() const {
return widget()->GetZOrderLevel();
}
void ChromeNativeAppWindowViewsAura::UpdateShape(
std::unique_ptr<ShapeRects> rects) {
bool had_shape = !!shape();
ChromeNativeAppWindowViews::UpdateShape(std::move(rects));
aura::Window* native_window = widget()->GetNativeWindow();
if (shape() && !had_shape) {
native_window->SetEventTargeter(
std::make_unique<ShapedAppWindowTargeter>(this));
} else if (!shape() && had_shape) {
native_window->SetEventTargeter(nullptr);
}
}
|