Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement 'app-command' events for browser history navigation keys on Linux #15441

Merged
merged 7 commits into from Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_top_level_window.cc
Expand Up @@ -267,7 +267,7 @@ void TopLevelWindow::OnWindowAlwaysOnTopChanged() {
Emit("always-on-top-changed", IsAlwaysOnTop());
}

void TopLevelWindow::OnExecuteWindowsCommand(const std::string& command_name) {
void TopLevelWindow::OnExecuteAppCommand(const std::string& command_name) {
Emit("app-command", command_name);
}

Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_top_level_window.h
Expand Up @@ -78,7 +78,7 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
void OnWindowEnterHtmlFullScreen() override;
void OnWindowLeaveHtmlFullScreen() override;
void OnWindowAlwaysOnTopChanged() override;
void OnExecuteWindowsCommand(const std::string& command_name) override;
void OnExecuteAppCommand(const std::string& command_name) override;
void OnTouchBarItemResult(const std::string& item_id,
const base::DictionaryValue& details) override;
void OnNewWindowForTab() override;
Expand Down
5 changes: 2 additions & 3 deletions atom/browser/native_window.cc
Expand Up @@ -540,10 +540,9 @@ void NativeWindow::NotifyWindowAlwaysOnTopChanged() {
observer.OnWindowAlwaysOnTopChanged();
}

void NativeWindow::NotifyWindowExecuteWindowsCommand(
const std::string& command) {
void NativeWindow::NotifyWindowExecuteAppCommand(const std::string& command) {
for (NativeWindowObserver& observer : observers_)
observer.OnExecuteWindowsCommand(command);
observer.OnExecuteAppCommand(command);
}

void NativeWindow::NotifyTouchBarItemInteraction(
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/native_window.h
Expand Up @@ -257,7 +257,7 @@ class NativeWindow : public base::SupportsUserData,
void NotifyWindowEnterHtmlFullScreen();
void NotifyWindowLeaveHtmlFullScreen();
void NotifyWindowAlwaysOnTopChanged();
void NotifyWindowExecuteWindowsCommand(const std::string& command);
void NotifyWindowExecuteAppCommand(const std::string& command);
void NotifyTouchBarItemInteraction(const std::string& item_id,
const base::DictionaryValue& details);
void NotifyNewWindowForTab();
Expand Down
3 changes: 2 additions & 1 deletion atom/browser/native_window_observer.h
Expand Up @@ -95,7 +95,8 @@ class NativeWindowObserver : public base::CheckedObserver {
#endif

// Called on Windows when App Commands arrive (WM_APPCOMMAND)
virtual void OnExecuteWindowsCommand(const std::string& command_name) {}
// Some commands are implemented on on other platforms as well
virtual void OnExecuteAppCommand(const std::string& command_name) {}
};

} // namespace atom
Expand Down
33 changes: 33 additions & 0 deletions atom/browser/native_window_views.cc
Expand Up @@ -22,6 +22,7 @@
#include "atom/browser/web_contents_preferences.h"
#include "atom/browser/web_view_manager.h"
#include "atom/browser/window_list.h"
#include "atom/common/atom_constants.h"
#include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/options_switches.h"
Expand Down Expand Up @@ -287,6 +288,13 @@ NativeWindowViews::NativeWindowViews(const mate::Dictionary& options,
last_window_state_ = ui::SHOW_STATE_NORMAL;
last_normal_bounds_ = GetBounds();
#endif

#if defined(OS_LINUX)
// Listen to move events.
aura::Window* window = GetNativeWindow();
if (window)
window->AddPreTargetHandler(this);
#endif
}

NativeWindowViews::~NativeWindowViews() {
Expand All @@ -296,6 +304,12 @@ NativeWindowViews::~NativeWindowViews() {
// Disable mouse forwarding to relinquish resources, should any be held.
SetForwardMouseMessages(false);
#endif

#if defined(OS_LINUX)
aura::Window* window = GetNativeWindow();
if (window)
window->RemovePreTargetHandler(this);
#endif
}

void NativeWindowViews::SetContentView(views::View* view) {
Expand Down Expand Up @@ -1274,11 +1288,30 @@ void NativeWindowViews::OnWidgetMove() {
void NativeWindowViews::HandleKeyboardEvent(
content::WebContents*,
const content::NativeWebKeyboardEvent& event) {
#if defined(OS_LINUX)
if (event.windows_key_code == ui::VKEY_BROWSER_BACK)
NotifyWindowExecuteAppCommand(kBrowserBackward);
else if (event.windows_key_code == ui::VKEY_BROWSER_FORWARD)
NotifyWindowExecuteAppCommand(kBrowserForward);
#endif

keyboard_event_handler_->HandleKeyboardEvent(event,
root_view_->GetFocusManager());
root_view_->HandleKeyEvent(event);
}

#if defined(OS_LINUX)
void NativeWindowViews::OnMouseEvent(ui::MouseEvent* event) {
if (event->type() != ui::ET_MOUSE_PRESSED)
return;

if (event->changed_button_flags() == ui::EF_BACK_MOUSE_BUTTON)
NotifyWindowExecuteAppCommand(kBrowserBackward);
else if (event->changed_button_flags() == ui::EF_FORWARD_MOUSE_BUTTON)
NotifyWindowExecuteAppCommand(kBrowserForward);
}
#endif

ui::WindowShowState NativeWindowViews::GetRestoredState() {
if (IsMaximized())
return ui::SHOW_STATE_MAXIMIZED;
Expand Down
8 changes: 7 additions & 1 deletion atom/browser/native_window_views.h
Expand Up @@ -40,7 +40,8 @@ class NativeWindowViews : public NativeWindow,
#if defined(OS_WIN)
public MessageHandlerDelegate,
#endif
public views::WidgetObserver {
public views::WidgetObserver,
public ui::EventHandler {
public:
NativeWindowViews(const mate::Dictionary& options, NativeWindow* parent);
~NativeWindowViews() override;
Expand Down Expand Up @@ -204,6 +205,11 @@ class NativeWindowViews : public NativeWindow,
content::WebContents*,
const content::NativeWebKeyboardEvent& event) override;

#if defined(OS_LINUX)
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override;
#endif

// Returns the restore state for the window.
ui::WindowShowState GetRestoredState();

Expand Down
7 changes: 4 additions & 3 deletions atom/browser/native_window_views_win.cc
Expand Up @@ -4,6 +4,7 @@

#include "atom/browser/browser.h"
#include "atom/browser/native_window_views.h"
#include "atom/common/atom_constants.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "ui/base/win/accessibility_misc_utils.h"

Expand All @@ -18,9 +19,9 @@ namespace {
const char* AppCommandToString(int command_id) {
switch (command_id) {
case APPCOMMAND_BROWSER_BACKWARD:
return "browser-backward";
return kBrowserBackward;
case APPCOMMAND_BROWSER_FORWARD:
return "browser-forward";
return kBrowserForward;
case APPCOMMAND_BROWSER_REFRESH:
return "browser-refresh";
case APPCOMMAND_BROWSER_STOP:
Expand Down Expand Up @@ -141,7 +142,7 @@ HHOOK NativeWindowViews::mouse_hook_ = NULL;

bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
std::string command = AppCommandToString(command_id);
NotifyWindowExecuteWindowsCommand(command);
NotifyWindowExecuteAppCommand(command);

return false;
}
Expand Down
3 changes: 3 additions & 0 deletions atom/common/atom_constants.cc
Expand Up @@ -6,6 +6,9 @@

namespace atom {

const char kBrowserForward[] = "browser-forward";
const char kBrowserBackward[] = "browser-backward";

const char kCORSHeader[] = "Access-Control-Allow-Origin: *";

const char kSHA1Certificate[] = "SHA-1 Certificate";
Expand Down
4 changes: 4 additions & 0 deletions atom/common/atom_constants.h
Expand Up @@ -9,6 +9,10 @@

namespace atom {

// The app-command in NativeWindow.
extern const char kBrowserForward[];
extern const char kBrowserBackward[];

// Header to ignore CORS.
extern const char kCORSHeader[];

Expand Down
7 changes: 6 additions & 1 deletion docs/api/browser-window.md
Expand Up @@ -554,7 +554,7 @@ Returns:

Emitted when the window is set or unset to show always on top of other windows.

#### Event: 'app-command' _Windows_
#### Event: 'app-command' _Windows_ _Linux_

Returns:

Expand All @@ -580,6 +580,11 @@ win.on('app-command', (e, cmd) => {
})
```

The following app commands are explictly supported on Linux:

* `browser-backward`
* `browser-forward`

#### Event: 'scroll-touch-begin' _macOS_

Emitted when scroll wheel event phase has begun.
Expand Down