Skip to content

Commit

Permalink
RemoteMacViews: Mojo-ify BridgedNativeWidgetHost
Browse files Browse the repository at this point in the history
Take the existing BridgedNativeWidgetHost and break it into two parts
- a mojo interface views_bridge_mac::mojo::BridgedNativeWidgetHost
- a helper and "stuff we haven't mojo-ified-yet" interface
  BridgedNativeWidgetHostHelper
  - this is not named BridgedNativeWidgetHost to avoid conflicts
  - this will be moved to //ui/views_bridge_mac soon (build is tangled)

Add mojo support for ui::DialogButton, because it's used by this
interface.

For keyboard event handling, use the BridgedNativeWidgetHostHelper
interface because a mojo-compatible interface would be hard for the
callers to use.

Bug: 859152
Change-Id: If6802ab4d6e518184cd3fa57eef5d8a510b80e59
Reviewed-on: https://chromium-review.googlesource.com/1198014
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Commit-Queue: ccameron <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588144}
  • Loading branch information
ccameron-chromium authored and Commit Bot committed Aug 31, 2018
1 parent f8c5470 commit 5ff407e
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 217 deletions.
7 changes: 7 additions & 0 deletions ui/base/mojo/ui_base_types.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

module ui.mojom;

// Dialog button identifiers used to specify which buttons to show the user.
enum DialogButton {
NONE,
OK,
CANCEL,
};

// Specifies the type of modality applied to a window. Different modal
// treatments may be handled differently by the window manager.
enum ModalType {
Expand Down
5 changes: 4 additions & 1 deletion ui/base/mojo/ui_base_types.typemap
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ public_deps = [
"//ui/base",
]
traits_headers = [ "//ui/base/mojo/ui_base_types_struct_traits.h" ]
type_mappings = [ "ui.mojom.ModalType=ui::ModalType" ]
type_mappings = [
"ui.mojom.DialogButton=ui::DialogButton",
"ui.mojom.ModalType=ui::ModalType",
]
35 changes: 35 additions & 0 deletions ui/base/mojo/ui_base_types_struct_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@

namespace mojo {

template <>
struct EnumTraits<ui::mojom::DialogButton, ui::DialogButton> {
static ui::mojom::DialogButton ToMojom(ui::DialogButton modal_type) {
switch (modal_type) {
case ui::DIALOG_BUTTON_NONE:
return ui::mojom::DialogButton::NONE;
case ui::DIALOG_BUTTON_OK:
return ui::mojom::DialogButton::OK;
case ui::DIALOG_BUTTON_CANCEL:
return ui::mojom::DialogButton::CANCEL;
default:
NOTREACHED();
return ui::mojom::DialogButton::NONE;
}
}

static bool FromMojom(ui::mojom::DialogButton modal_type,
ui::DialogButton* out) {
switch (modal_type) {
case ui::mojom::DialogButton::NONE:
*out = ui::DIALOG_BUTTON_NONE;
return true;
case ui::mojom::DialogButton::OK:
*out = ui::DIALOG_BUTTON_OK;
return true;
case ui::mojom::DialogButton::CANCEL:
*out = ui::DIALOG_BUTTON_CANCEL;
return true;
default:
NOTREACHED();
return false;
}
}
};

template <>
struct EnumTraits<ui::mojom::ModalType, ui::ModalType> {
static ui::mojom::ModalType ToMojom(ui::ModalType modal_type) {
Expand Down
66 changes: 29 additions & 37 deletions ui/views/cocoa/bridged_content_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "ui/views/view.h"
#include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/widget.h"
#include "ui/views_bridge_mac/mojo/bridged_native_widget_host.mojom.h"

namespace {

Expand Down Expand Up @@ -423,13 +424,13 @@ - (void)processCapturedMouseEvent:(NSEvent*)theEvent {
[self updateTooltipIfRequiredAt:event_location];

if (isScrollEvent) {
ui::ScrollEvent event(theEvent);
event.set_location(event_location);
bridge_->host()->OnScrollEvent(event);
auto event = std::make_unique<ui::ScrollEvent>(theEvent);
event->set_location(event_location);
bridge_->host()->OnScrollEvent(std::move(event));
} else {
ui::MouseEvent event(theEvent);
event.set_location(event_location);
bridge_->host()->OnMouseEvent(event);
auto event = std::make_unique<ui::MouseEvent>(theEvent);
event->set_location(event_location);
bridge_->host()->OnMouseEvent(std::move(event));
}
}

Expand All @@ -453,11 +454,8 @@ - (void)updateFullKeyboardAccess {
// BridgedContentView private implementation.

- (void)dispatchKeyEvent:(ui::KeyEvent*)event {
bool eventHandled = false;
if (bridge_)
bridge_->host()->DispatchKeyEvent(*event, &eventHandled);
if (eventHandled)
event->SetHandled();
bridge_->host_helper()->DispatchKeyEvent(event);
}

- (BOOL)hasActiveMenuController {
Expand All @@ -468,15 +466,9 @@ - (BOOL)hasActiveMenuController {
}

- (BOOL)dispatchKeyEventToMenuController:(ui::KeyEvent*)event {
bool eventSwallowed = false;
bool eventHandled = false;
if (bridge_) {
bridge_->host()->DispatchKeyEventToMenuController(*event, &eventSwallowed,
&eventHandled);
}
if (eventHandled)
event->SetHandled();
return eventSwallowed;
if (bridge_)
return bridge_->host_helper()->DispatchKeyEventToMenuController(event);
return false;
}

- (void)handleKeyEvent:(ui::KeyEvent*)event {
Expand Down Expand Up @@ -691,13 +683,13 @@ - (void)mouseEvent:(NSEvent*)theEvent {
return;

DCHECK([theEvent type] != NSScrollWheel);
ui::MouseEvent event(theEvent);
[self adjustUiEventLocation:&event fromNativeEvent:theEvent];
auto event = std::make_unique<ui::MouseEvent>(theEvent);
[self adjustUiEventLocation:event.get() fromNativeEvent:theEvent];

// Aura updates tooltips with the help of aura::Window::AddPreTargetHandler().
// Mac hooks in here.
[self updateTooltipIfRequiredAt:event.location()];
bridge_->host()->OnMouseEvent(event);
[self updateTooltipIfRequiredAt:event->location()];
bridge_->host()->OnMouseEvent(std::move(event));
}

- (void)forceTouchEvent:(NSEvent*)theEvent {
Expand Down Expand Up @@ -860,13 +852,13 @@ - (void)scrollWheel:(NSEvent*)theEvent {
if (!bridge_)
return;

ui::ScrollEvent event(theEvent);
[self adjustUiEventLocation:&event fromNativeEvent:theEvent];
auto event = std::make_unique<ui::ScrollEvent>(theEvent);
[self adjustUiEventLocation:event.get() fromNativeEvent:theEvent];

// Aura updates tooltips with the help of aura::Window::AddPreTargetHandler().
// Mac hooks in here.
[self updateTooltipIfRequiredAt:event.location()];
bridge_->host()->OnScrollEvent(event);
[self updateTooltipIfRequiredAt:event->location()];
bridge_->host()->OnScrollEvent(std::move(event));
}

// Called when we get a three-finger swipe, and they're enabled in System
Expand All @@ -890,10 +882,10 @@ - (void)swipeWithEvent:(NSEvent*)event {
// Note this uses the default unique_touch_event_id of 0 (Swipe events do not
// support -[NSEvent eventNumber]). This doesn't seem like a real omission
// because the three-finger swipes are instant and can't be tracked anyway.
ui::GestureEvent gestureEvent(location.x(), location.y(),
ui::EventFlagsFromNative(event),
ui::EventTimeFromNative(event), swipeDetails);
bridge_->host()->OnGestureEvent(gestureEvent);
auto gestureEvent = std::make_unique<ui::GestureEvent>(
location.x(), location.y(), ui::EventFlagsFromNative(event),
ui::EventTimeFromNative(event), swipeDetails);
bridge_->host()->OnGestureEvent(std::move(gestureEvent));
}

- (void)quickLookWithEvent:(NSEvent*)theEvent {
Expand All @@ -906,8 +898,8 @@ - (void)quickLookWithEvent:(NSEvent*)theEvent {
bool foundWord = false;
gfx::DecoratedText decoratedWord;
gfx::Point baselinePoint;
bridge_->host()->GetWordAt(locationInContent, &foundWord, &decoratedWord,
&baselinePoint);
bridge_->host_helper()->GetWordAt(locationInContent, &foundWord,
&decoratedWord, &baselinePoint);
if (!foundWord)
return;

Expand Down Expand Up @@ -1536,21 +1528,21 @@ - (void)draggingSession:(NSDraggingSession*)session

- (id)accessibilityAttributeValue:(NSString*)attribute {
if ([attribute isEqualToString:NSAccessibilityChildrenAttribute]) {
return @[ bridge_->host()->GetNativeViewAccessible() ];
return @[ bridge_->host_helper()->GetNativeViewAccessible() ];
}

return [super accessibilityAttributeValue:attribute];
}

- (id)accessibilityHitTest:(NSPoint)point {
return
[bridge_->host()->GetNativeViewAccessible() accessibilityHitTest:point];
return [bridge_->host_helper()->GetNativeViewAccessible()
accessibilityHitTest:point];
}

- (id)accessibilityFocusedUIElement {
if (!bridge_)
return nil;
return [bridge_->host()->GetNativeViewAccessible()
return [bridge_->host_helper()->GetNativeViewAccessible()
accessibilityFocusedUIElement];
}

Expand Down
2 changes: 1 addition & 1 deletion ui/views/cocoa/bridged_content_view_touch_bar.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "ui/base/cocoa/touch_bar_forward_declarations.h"
#import "ui/views/cocoa/bridged_content_view.h"
#import "ui/views/cocoa/bridged_native_widget.h"
#import "ui/views/cocoa/bridged_native_widget_host.h"
#include "ui/views_bridge_mac/mojo/bridged_native_widget_host.mojom.h"

namespace {

Expand Down
18 changes: 15 additions & 3 deletions ui/views/cocoa/bridged_native_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@
@class NativeWidgetMacNSWindow;
@class ViewsNSWindowDelegate;

namespace views_bridge_mac {
namespace mojom {
class BridgedNativeWidgetHost;
} // namespace mojom
} // namespace views_bridge_mac

namespace views {
namespace test {
class BridgedNativeWidgetTestApi;
}

class BridgedNativeWidgetHost;
class BridgedNativeWidgetHostHelper;
class CocoaMouseCapture;
class CocoaWindowMoveLoop;
class DragDropClientMac;
class NativeWidgetMac;
class View;

using views_bridge_mac::mojom::BridgedNativeWidgetHost;

// A bridge to an NSWindow managed by an instance of NativeWidgetMac or
// DesktopNativeWidgetMac. Serves as a helper class to bridge requests from the
// NativeWidgetMac to the Cocoa window. Behaves a bit like an aura::Window.
Expand All @@ -60,7 +68,9 @@ class VIEWS_EXPORT BridgedNativeWidget
const gfx::Size& size);

// Creates one side of the bridge. |host| and |parent| must not be NULL.
BridgedNativeWidget(BridgedNativeWidgetHost* host, NativeWidgetMac* parent);
BridgedNativeWidget(BridgedNativeWidgetHost* host,
BridgedNativeWidgetHostHelper* host_helper,
NativeWidgetMac* parent);
~BridgedNativeWidget() override;

// Initialize the NSWindow by taking ownership of the specified object.
Expand Down Expand Up @@ -151,6 +161,7 @@ class VIEWS_EXPORT BridgedNativeWidget
NativeWidgetMac* native_widget_mac() { return native_widget_mac_; }
BridgedContentView* ns_view() { return bridged_view_; }
BridgedNativeWidgetHost* host() { return host_; }
BridgedNativeWidgetHostHelper* host_helper() { return host_helper_; }
NSWindow* ns_window();

TooltipManager* tooltip_manager() { return tooltip_manager_.get(); }
Expand Down Expand Up @@ -280,7 +291,8 @@ class VIEWS_EXPORT BridgedNativeWidget
bool IsVisibleParent() const override;
void RemoveChildWindow(BridgedNativeWidget* child) override;

BridgedNativeWidgetHost* const host_; // Weak. Owns this.
BridgedNativeWidgetHost* const host_; // Weak. Owns this.
BridgedNativeWidgetHostHelper* const host_helper_; // Weak, owned by |host_|.
NativeWidgetMac* const native_widget_mac_; // Weak. Owns |host_|.
base::scoped_nsobject<NativeWidgetMacNSWindow> window_;
base::scoped_nsobject<ViewsNSWindowDelegate> window_delegate_;
Expand Down
9 changes: 6 additions & 3 deletions ui/views/cocoa/bridged_native_widget.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#import "ui/views/cocoa/widget_owner_nswindow_adapter.h"
#include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/widget.h"
#include "ui/views_bridge_mac/mojo/bridged_native_widget_host.mojom.h"

using views_bridge_mac::mojom::WindowVisibilityState;

Expand Down Expand Up @@ -236,9 +237,11 @@ NSUInteger CountBridgedWindows(NSArray* child_windows) {
return gfx::Size(NSWidth(frame_rect), NSHeight(frame_rect));
}

BridgedNativeWidget::BridgedNativeWidget(BridgedNativeWidgetHost* host,
NativeWidgetMac* parent)
: host_(host), native_widget_mac_(parent) {
BridgedNativeWidget::BridgedNativeWidget(
BridgedNativeWidgetHost* host,
BridgedNativeWidgetHostHelper* host_helper,
NativeWidgetMac* parent)
: host_(host), host_helper_(host_helper), native_widget_mac_(parent) {
DCHECK(parent);
window_delegate_.reset(
[[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]);
Expand Down
Loading

0 comments on commit 5ff407e

Please sign in to comment.