fix: calling of X11 functions when running under Wayland (#33355)

* fix: don't call X11 functions in file dialog and message box

* refactor: remove unused GtkUiPlatform declaration

* fix: set gtk darktheme only when running under X11

* fix: replace X11 window state watcher with implementation using ozone

* fix: make sure global menu barr is used only when supported

* fix: don't call X11 function in native window views under wayland

* style: fix lint issues

* fix: use GtkUiPlatform::ShowGtkWindow instead of gtk_window_present directly

* refactor: extract CreateGlobalMenuBar into separate function

* refactor: move checking for WaylandWindowDecorations inside class

* fix: check if we run under X11 only in ozone build

* refactor: drop including unused ui/base/ui_base_features.h header

* fix: modify ui_gtk_public_header.patch to also export gtk_ui.h

* fix: refactor guarding of X11 calls

- Introduce patch exposing new electron_can_call_x11 property
- Replace defined(USE_OZONE) with BUILDFLAG(OZONE_PLATFORM_X11) flags

* fix: remove the last remaining usage of USE_X11

* fix: usage of BUILDFLAG(OZONE_PLATFORM_X11) not building on non ozone

* fix: call UpdateWindowState from OnBoundsChanged only under X11
This commit is contained in:
Marek Rusinowski 2022-03-28 18:48:50 +02:00 committed by GitHub
parent 1153a5ce5a
commit cdf2b3f4e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 305 additions and 309 deletions

View file

@ -1,82 +0,0 @@
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/x/window_state_watcher.h"
#include <vector>
#include "ui/base/x/x11_util.h"
#include "ui/gfx/x/x11_atom_cache.h"
#include "ui/gfx/x/xproto_util.h"
namespace electron {
WindowStateWatcher::WindowStateWatcher(NativeWindowViews* window)
: window_(window),
widget_(window->GetAcceleratedWidget()),
net_wm_state_atom_(x11::GetAtom("_NET_WM_STATE")),
net_wm_state_hidden_atom_(x11::GetAtom("_NET_WM_STATE_HIDDEN")),
net_wm_state_maximized_vert_atom_(
x11::GetAtom("_NET_WM_STATE_MAXIMIZED_VERT")),
net_wm_state_maximized_horz_atom_(
x11::GetAtom("_NET_WM_STATE_MAXIMIZED_HORZ")),
net_wm_state_fullscreen_atom_(x11::GetAtom("_NET_WM_STATE_FULLSCREEN")),
was_minimized_(window_->IsMinimized()),
was_maximized_(window_->IsMaximized()) {
ui::X11EventSource::GetInstance()->connection()->AddEventObserver(this);
}
WindowStateWatcher::~WindowStateWatcher() {
ui::X11EventSource::GetInstance()->connection()->RemoveEventObserver(this);
}
void WindowStateWatcher::OnEvent(const x11::Event& x11_event) {
if (IsWindowStateEvent(x11_event)) {
std::vector<x11::Atom> wm_states;
if (GetArrayProperty(
static_cast<x11::Window>(window_->GetAcceleratedWidget()),
net_wm_state_atom_, &wm_states)) {
const auto props =
base::flat_set<x11::Atom>(std::begin(wm_states), std::end(wm_states));
const bool is_minimized = props.contains(net_wm_state_hidden_atom_);
const bool is_maximized =
props.contains(net_wm_state_maximized_vert_atom_) &&
props.contains(net_wm_state_maximized_horz_atom_);
const bool is_fullscreen = props.contains(net_wm_state_fullscreen_atom_);
if (is_minimized != was_minimized_) {
if (is_minimized)
window_->NotifyWindowMinimize();
else
window_->NotifyWindowRestore();
} else if (is_maximized != was_maximized_) {
if (is_maximized)
window_->NotifyWindowMaximize();
else
window_->NotifyWindowUnmaximize();
} else {
// If this is neither a "maximize" or "minimize" event, then we think it
// is a "fullscreen" event.
// The "IsFullscreen()" becomes true immediately before "OnEvent"
// is called, so we can not handle this like "maximize" and "minimize"
// by watching whether they have changed.
if (is_fullscreen)
window_->NotifyWindowEnterFullScreen();
else
window_->NotifyWindowLeaveFullScreen();
}
was_minimized_ = is_minimized;
was_maximized_ = is_maximized;
}
}
}
bool WindowStateWatcher::IsWindowStateEvent(const x11::Event& x11_event) const {
auto* property = x11_event.As<x11::PropertyNotifyEvent>();
return (property && property->atom == net_wm_state_atom_ &&
static_cast<uint32_t>(property->window) == widget_);
}
} // namespace electron

View file

@ -1,45 +0,0 @@
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_UI_X_WINDOW_STATE_WATCHER_H_
#define ELECTRON_SHELL_BROWSER_UI_X_WINDOW_STATE_WATCHER_H_
#include "ui/events/platform/x11/x11_event_source.h"
#include "ui/gfx/x/event.h"
#include "shell/browser/native_window_views.h"
namespace electron {
class WindowStateWatcher : public x11::EventObserver {
public:
explicit WindowStateWatcher(NativeWindowViews* window);
~WindowStateWatcher() override;
// disable copy
WindowStateWatcher(const WindowStateWatcher&) = delete;
WindowStateWatcher& operator=(const WindowStateWatcher&) = delete;
protected:
// x11::EventObserver:
void OnEvent(const x11::Event& x11_event) override;
private:
bool IsWindowStateEvent(const x11::Event& x11_event) const;
NativeWindowViews* window_;
gfx::AcceleratedWidget widget_;
const x11::Atom net_wm_state_atom_;
const x11::Atom net_wm_state_hidden_atom_;
const x11::Atom net_wm_state_maximized_vert_atom_;
const x11::Atom net_wm_state_maximized_horz_atom_;
const x11::Atom net_wm_state_fullscreen_atom_;
bool was_minimized_ = false;
bool was_maximized_ = false;
};
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_UI_X_WINDOW_STATE_WATCHER_H_