File: app_shim_render_widget_host_view_mac_delegate.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (124 lines) | stat: -rw-r--r-- 3,896 bytes parent folder | download | duplicates (3)
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
// 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.

// 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