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 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/app_shim/app_shim_render_widget_host_view_mac_delegate.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/renderer_host/chrome_render_widget_host_view_mac_history_swiper.h"
#include "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
#include "components/remote_cocoa/app_shim/ns_view_ids.h"
#include "components/remote_cocoa/common/native_widget_ns_window_host.mojom.h"
#include "ui/gfx/native_widget_types.h"
@interface AppShimRenderWidgetHostViewMacDelegate () <HistorySwiperDelegate>
@end
@implementation AppShimRenderWidgetHostViewMacDelegate {
uint64_t _nsviewIDThatWantsHistoryOverlay;
// Responsible for 2-finger swipes history navigation.
HistorySwiper* __strong _historySwiper;
}
- (instancetype)initWithRenderWidgetHostNSViewID:
(uint64_t)renderWidgetHostNSViewID {
if (self = [super init]) {
_nsviewIDThatWantsHistoryOverlay = renderWidgetHostNSViewID;
_historySwiper = [[HistorySwiper alloc] initWithDelegate:self];
}
return self;
}
- (void)dealloc {
_historySwiper.delegate = nil;
}
// Handle an event. All incoming key and mouse events flow through this
// delegate method if implemented. Return YES if the event is fully handled, or
// NO if normal processing should take place.
- (BOOL)handleEvent:(NSEvent*)event {
return [_historySwiper handleEvent:event];
}
// NSWindow events.
- (void)beginGestureWithEvent:(NSEvent*)event {
[_historySwiper beginGestureWithEvent:event];
}
- (void)endGestureWithEvent:(NSEvent*)event {
[_historySwiper endGestureWithEvent:event];
}
// This is a low level API which provides touches associated with an event.
// It is used in conjunction with gestures to determine finger placement
// on the trackpad.
- (void)touchesMovedWithEvent:(NSEvent*)event {
[_historySwiper touchesMovedWithEvent:event];
}
- (void)touchesBeganWithEvent:(NSEvent*)event {
[_historySwiper touchesBeganWithEvent:event];
}
- (void)touchesCancelledWithEvent:(NSEvent*)event {
[_historySwiper touchesCancelledWithEvent:event];
}
- (void)touchesEndedWithEvent:(NSEvent*)event {
[_historySwiper touchesEndedWithEvent:event];
}
- (void)rendererHandledGestureScrollEvent:(const blink::WebGestureEvent&)event
consumed:(BOOL)consumed {
[_historySwiper rendererHandledGestureScrollEvent:event consumed:consumed];
}
- (void)rendererHandledOverscrollEvent:(const ui::DidOverscrollParams&)params {
[_historySwiper onOverscrolled:params];
}
// HistorySwiperDelegate methods.
- (BOOL)shouldAllowHistorySwiping {
return YES;
}
- (NSView*)viewThatWantsHistoryOverlay {
return remote_cocoa::GetNSViewFromId(_nsviewIDThatWantsHistoryOverlay);
}
- (BOOL)canNavigateInDirection:(history_swiper::NavigationDirection)direction
onWindow:(NSWindow*)window {
auto* bridge =
remote_cocoa::NativeWidgetNSWindowBridge::GetFromNSWindow(window);
if (!bridge) {
return NO;
}
if (direction == history_swiper::kForwards) {
return bridge->CanGoForward();
} else {
return bridge->CanGoBack();
}
}
- (void)navigateInDirection:(history_swiper::NavigationDirection)direction
onWindow:(NSWindow*)window {
auto* bridge =
remote_cocoa::NativeWidgetNSWindowBridge::GetFromNSWindow(window);
if (!bridge) {
return;
}
bool was_executed = false;
if (direction == history_swiper::kForwards) {
bridge->host()->ExecuteCommand(
IDC_FORWARD, WindowOpenDisposition::CURRENT_TAB, false, &was_executed);
} else {
bridge->host()->ExecuteCommand(IDC_BACK, WindowOpenDisposition::CURRENT_TAB,
false, &was_executed);
}
DCHECK(was_executed);
}
- (void)backwardsSwipeNavigationLikely {
// TODO(mcnee): It's unclear whether preloading predictions would be useful in
// this context. For now we don't do any prediction. See
// https://crbug.com/1422266 for context.
}
@end
|