electron_inspectable_web_contents_view.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright (c) 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h"
  5. #include "content/public/browser/render_widget_host_view.h"
  6. #include "shell/browser/api/electron_api_web_contents.h"
  7. #include "shell/browser/ui/cocoa/event_dispatching_window.h"
  8. #include "shell/browser/ui/inspectable_web_contents.h"
  9. #include "shell/browser/ui/inspectable_web_contents_view_delegate.h"
  10. #include "shell/browser/ui/inspectable_web_contents_view_mac.h"
  11. #include "ui/base/cocoa/base_view.h"
  12. #include "ui/gfx/mac/scoped_cocoa_disable_screen_updates.h"
  13. #if !defined(__has_feature) || !__has_feature(objc_arc)
  14. #error "This file requires ARC support."
  15. #endif
  16. @implementation ElectronInspectableWebContentsView
  17. - (instancetype)initWithInspectableWebContentsViewMac:
  18. (InspectableWebContentsViewMac*)view {
  19. self = [super init];
  20. if (!self)
  21. return nil;
  22. inspectableWebContentsView_ = view;
  23. devtools_visible_ = NO;
  24. devtools_docked_ = NO;
  25. devtools_is_first_responder_ = NO;
  26. attached_to_window_ = NO;
  27. if (inspectableWebContentsView_->inspectable_web_contents()->IsGuest()) {
  28. fake_view_ = [[NSView alloc] init];
  29. [fake_view_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
  30. [self addSubview:fake_view_];
  31. } else {
  32. auto* contents = inspectableWebContentsView_->inspectable_web_contents()
  33. ->GetWebContents();
  34. auto* contentsView = contents->GetNativeView().GetNativeNSView();
  35. [contentsView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
  36. [self addSubview:contentsView];
  37. }
  38. // See https://code.google.com/p/chromium/issues/detail?id=348490.
  39. [self setWantsLayer:YES];
  40. return self;
  41. }
  42. - (void)dealloc {
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. }
  45. - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
  46. [self adjustSubviews];
  47. }
  48. - (void)viewDidMoveToWindow {
  49. if (attached_to_window_ && !self.window) {
  50. attached_to_window_ = NO;
  51. [[NSNotificationCenter defaultCenter] removeObserver:self];
  52. } else if (!attached_to_window_ && self.window) {
  53. attached_to_window_ = YES;
  54. [[NSNotificationCenter defaultCenter]
  55. addObserver:self
  56. selector:@selector(viewDidBecomeFirstResponder:)
  57. name:kViewDidBecomeFirstResponder
  58. object:nil];
  59. [[NSNotificationCenter defaultCenter]
  60. addObserver:self
  61. selector:@selector(parentWindowBecameMain:)
  62. name:NSWindowDidBecomeMainNotification
  63. object:nil];
  64. }
  65. }
  66. - (IBAction)showDevTools:(id)sender {
  67. inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools(true);
  68. }
  69. - (void)notifyDevToolsFocused {
  70. if (inspectableWebContentsView_->GetDelegate())
  71. inspectableWebContentsView_->GetDelegate()->DevToolsFocused();
  72. }
  73. - (void)notifyDevToolsResized {
  74. // When devtools is opened, resizing devtools would not trigger
  75. // UpdateDraggableRegions for WebContents, so we have to notify the window
  76. // to do an update of draggable regions.
  77. if (inspectableWebContentsView_->GetDelegate())
  78. inspectableWebContentsView_->GetDelegate()->DevToolsResized();
  79. }
  80. - (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate {
  81. if (visible == devtools_visible_)
  82. return;
  83. auto* inspectable_web_contents =
  84. inspectableWebContentsView_->inspectable_web_contents();
  85. auto* devToolsWebContents =
  86. inspectable_web_contents->GetDevToolsWebContents();
  87. auto* devToolsView = devToolsWebContents->GetNativeView().GetNativeNSView();
  88. devtools_visible_ = visible;
  89. if (devtools_docked_) {
  90. if (visible) {
  91. // Place the devToolsView under contentsView, notice that we didn't set
  92. // sizes for them until the setContentsResizingStrategy message.
  93. [self addSubview:devToolsView positioned:NSWindowBelow relativeTo:nil];
  94. [self adjustSubviews];
  95. // Focus on web view.
  96. devToolsWebContents->RestoreFocus();
  97. } else {
  98. gfx::ScopedCocoaDisableScreenUpdates disabler;
  99. [devToolsView removeFromSuperview];
  100. [self adjustSubviews];
  101. [self notifyDevToolsResized];
  102. }
  103. } else {
  104. if (visible) {
  105. if (activate) {
  106. [devtools_window_ makeKeyAndOrderFront:nil];
  107. } else {
  108. [devtools_window_ orderBack:nil];
  109. }
  110. } else {
  111. [devtools_window_ setDelegate:nil];
  112. [devtools_window_ close];
  113. devtools_window_ = nil;
  114. }
  115. }
  116. }
  117. - (BOOL)isDevToolsVisible {
  118. return devtools_visible_;
  119. }
  120. - (BOOL)isDevToolsFocused {
  121. if (devtools_docked_) {
  122. return [[self window] isKeyWindow] && devtools_is_first_responder_;
  123. } else {
  124. return [devtools_window_ isKeyWindow];
  125. }
  126. }
  127. - (void)setIsDocked:(BOOL)docked activate:(BOOL)activate {
  128. // Revert to no-devtools state.
  129. [self setDevToolsVisible:NO activate:NO];
  130. // Switch to new state.
  131. devtools_docked_ = docked;
  132. auto* inspectable_web_contents =
  133. inspectableWebContentsView_->inspectable_web_contents();
  134. auto* devToolsWebContents =
  135. inspectable_web_contents->GetDevToolsWebContents();
  136. auto devToolsView = devToolsWebContents->GetNativeView().GetNativeNSView();
  137. if (!docked) {
  138. auto styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
  139. NSWindowStyleMaskMiniaturizable |
  140. NSWindowStyleMaskResizable |
  141. NSWindowStyleMaskTexturedBackground |
  142. NSWindowStyleMaskUnifiedTitleAndToolbar;
  143. devtools_window_ = [[EventDispatchingWindow alloc]
  144. initWithContentRect:NSMakeRect(0, 0, 800, 600)
  145. styleMask:styleMask
  146. backing:NSBackingStoreBuffered
  147. defer:YES];
  148. [devtools_window_ setDelegate:self];
  149. [devtools_window_ setFrameAutosaveName:@"electron.devtools"];
  150. [devtools_window_ setTitle:@"Developer Tools"];
  151. [devtools_window_ setReleasedWhenClosed:NO];
  152. [devtools_window_ setAutorecalculatesContentBorderThickness:NO
  153. forEdge:NSMaxYEdge];
  154. [devtools_window_ setContentBorderThickness:24 forEdge:NSMaxYEdge];
  155. NSView* contentView = [devtools_window_ contentView];
  156. devToolsView.frame = contentView.bounds;
  157. devToolsView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  158. [contentView addSubview:devToolsView];
  159. [devToolsView setMouseDownCanMoveWindow:NO];
  160. } else {
  161. [devToolsView setMouseDownCanMoveWindow:YES];
  162. }
  163. [self setDevToolsVisible:YES activate:activate];
  164. }
  165. - (void)setContentsResizingStrategy:
  166. (const DevToolsContentsResizingStrategy&)strategy {
  167. strategy_.CopyFrom(strategy);
  168. [self adjustSubviews];
  169. }
  170. - (void)adjustSubviews {
  171. if (![[self subviews] count])
  172. return;
  173. if (![self isDevToolsVisible] || devtools_window_) {
  174. DCHECK_EQ(1u, [[self subviews] count]);
  175. NSView* contents = [[self subviews] objectAtIndex:0];
  176. [contents setFrame:[self bounds]];
  177. return;
  178. }
  179. NSView* devToolsView = [[self subviews] objectAtIndex:0];
  180. NSView* contentsView = [[self subviews] objectAtIndex:1];
  181. DCHECK_EQ(2u, [[self subviews] count]);
  182. gfx::Rect new_devtools_bounds;
  183. gfx::Rect new_contents_bounds;
  184. ApplyDevToolsContentsResizingStrategy(
  185. strategy_, gfx::Size(NSSizeToCGSize([self bounds].size)),
  186. &new_devtools_bounds, &new_contents_bounds);
  187. [devToolsView setFrame:[self flipRectToNSRect:new_devtools_bounds]];
  188. [contentsView setFrame:[self flipRectToNSRect:new_contents_bounds]];
  189. // Move mask to the devtools area to exclude it from dragging.
  190. NSRect cf = contentsView.frame;
  191. NSRect sb = [self bounds];
  192. NSRect devtools_frame;
  193. if (cf.size.height < sb.size.height) { // bottom docked
  194. devtools_frame.origin.x = 0;
  195. devtools_frame.origin.y = 0;
  196. devtools_frame.size.width = sb.size.width;
  197. devtools_frame.size.height = sb.size.height - cf.size.height;
  198. } else { // left or right docked
  199. if (cf.origin.x > 0) // left docked
  200. devtools_frame.origin.x = 0;
  201. else // right docked.
  202. devtools_frame.origin.x = cf.size.width;
  203. devtools_frame.origin.y = 0;
  204. devtools_frame.size.width = sb.size.width - cf.size.width;
  205. devtools_frame.size.height = sb.size.height;
  206. }
  207. [self notifyDevToolsResized];
  208. }
  209. - (void)setTitle:(NSString*)title {
  210. [devtools_window_ setTitle:title];
  211. }
  212. - (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
  213. auto* inspectable_web_contents =
  214. inspectableWebContentsView_->inspectable_web_contents();
  215. DCHECK(inspectable_web_contents);
  216. auto* webContents = inspectable_web_contents->GetWebContents();
  217. if (!webContents)
  218. return;
  219. auto* webContentsView = webContents->GetNativeView().GetNativeNSView();
  220. NSView* view = [notification object];
  221. if ([[webContentsView subviews] containsObject:view]) {
  222. devtools_is_first_responder_ = NO;
  223. return;
  224. }
  225. auto* devToolsWebContents =
  226. inspectable_web_contents->GetDevToolsWebContents();
  227. if (!devToolsWebContents)
  228. return;
  229. auto devToolsView = devToolsWebContents->GetNativeView().GetNativeNSView();
  230. if ([[devToolsView subviews] containsObject:view]) {
  231. devtools_is_first_responder_ = YES;
  232. [self notifyDevToolsFocused];
  233. }
  234. }
  235. - (void)parentWindowBecameMain:(NSNotification*)notification {
  236. NSWindow* parentWindow = [notification object];
  237. if ([self window] == parentWindow && devtools_docked_ &&
  238. devtools_is_first_responder_)
  239. [self notifyDevToolsFocused];
  240. }
  241. - (void)redispatchContextMenuEvent:(base::apple::OwnedNSEvent)event {
  242. DCHECK(event.Get().type == NSEventTypeRightMouseDown ||
  243. (event.Get().type == NSEventTypeLeftMouseDown &&
  244. (event.Get().modifierFlags & NSEventModifierFlagControl)));
  245. content::WebContents* contents =
  246. inspectableWebContentsView_->inspectable_web_contents()->GetWebContents();
  247. electron::api::WebContents* api_contents =
  248. electron::api::WebContents::From(contents);
  249. if (api_contents) {
  250. // Temporarily pretend that the WebContents is fully non-draggable while we
  251. // re-send the mouse event. This allows the re-dispatched event to "land"
  252. // on the WebContents, instead of "falling through" back to the window.
  253. auto* rwhv = contents->GetRenderWidgetHostView();
  254. if (rwhv) {
  255. api_contents->SetForceNonDraggable(true);
  256. BaseView* contentsView =
  257. (BaseView*)rwhv->GetNativeView().GetNativeNSView();
  258. [contentsView mouseEvent:event.Get()];
  259. api_contents->SetForceNonDraggable(false);
  260. }
  261. }
  262. }
  263. #pragma mark - NSWindowDelegate
  264. - (void)windowWillClose:(NSNotification*)notification {
  265. inspectableWebContentsView_->inspectable_web_contents()->CloseDevTools();
  266. }
  267. - (void)windowDidBecomeMain:(NSNotification*)notification {
  268. content::WebContents* web_contents =
  269. inspectableWebContentsView_->inspectable_web_contents()
  270. ->GetDevToolsWebContents();
  271. if (!web_contents)
  272. return;
  273. web_contents->RestoreFocus();
  274. content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
  275. if (rwhv)
  276. rwhv->SetActive(true);
  277. [self notifyDevToolsFocused];
  278. }
  279. - (void)windowDidResignMain:(NSNotification*)notification {
  280. content::WebContents* web_contents =
  281. inspectableWebContentsView_->inspectable_web_contents()
  282. ->GetDevToolsWebContents();
  283. if (!web_contents)
  284. return;
  285. web_contents->StoreFocus();
  286. content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
  287. if (rwhv)
  288. rwhv->SetActive(false);
  289. }
  290. @end