macStatsGraphViewController.mm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file macStatsGraphViewController.mm
  10. * @author rdb
  11. * @date 2023-08-28
  12. */
  13. #include "macStatsGraphViewController.h"
  14. #include "macStatsGraph.h"
  15. #include "macStatsMonitor.h"
  16. #include "cocoa_compat.h"
  17. @implementation MacStatsGraphViewController
  18. - (id)initWithGraph:(MacStatsGraph *)graph {
  19. if (self = [super init]) {
  20. _graph = graph;
  21. }
  22. return self;
  23. }
  24. - (void)windowWillClose:(NSNotification *)notification {
  25. MacStatsGraph *graph = _graph;
  26. if (graph != nullptr) {
  27. MacStatsMonitor *monitor = graph->get_monitor();
  28. if (monitor != nullptr) {
  29. _graph = nullptr;
  30. monitor->remove_graph(graph);
  31. }
  32. }
  33. }
  34. - (void)loadView {
  35. NSView *graph_view = [[MacStatsGraphView alloc] initWithGraph:_graph];
  36. NSView *background;
  37. if (@available(macOS 10.14, *)) {
  38. NSVisualEffectView *effect_view = [[NSVisualEffectView alloc] init];
  39. effect_view.material = (NSVisualEffectMaterial)18;//NSVisualEffectMaterialContentBackground;
  40. background = effect_view;
  41. } else {
  42. background = [[NSView alloc] init];
  43. background.wantsLayer = YES;
  44. background.layer.backgroundColor = [NSColor controlBackgroundColor].CGColor;
  45. }
  46. [background addSubview:graph_view];
  47. self.view = background;
  48. [graph_view.widthAnchor constraintEqualToAnchor:background.widthAnchor].active = YES;
  49. [graph_view.heightAnchor constraintEqualToAnchor:background.heightAnchor].active = YES;
  50. [graph_view release];
  51. [background release];
  52. }
  53. - (MacStatsGraphView *)graphView {
  54. return (MacStatsGraphView *)self.view.subviews[0];
  55. }
  56. - (void)handleSplitViewResize:(NSNotification *)notification {
  57. NSSplitView *split_view = (NSSplitView *)notification.object;
  58. NSWindow *window = split_view.window;
  59. NSToolbar *toolbar = window.toolbar;
  60. if ([split_view isSubviewCollapsed:split_view.arrangedSubviews[0]]) {
  61. if (toolbar.items[0].itemIdentifier != NSToolbarToggleSidebarItemIdentifier) {
  62. [toolbar insertItemWithItemIdentifier:NSToolbarToggleSidebarItemIdentifier atIndex:0];
  63. }
  64. } else {
  65. if (toolbar.items[0].itemIdentifier == NSToolbarToggleSidebarItemIdentifier) {
  66. [toolbar removeItemAtIndex:0];
  67. }
  68. }
  69. }
  70. - (BOOL)backToolbarItemVisible {
  71. NSToolbar *toolbar = self.view.window.toolbar;
  72. if ([toolbar.items[0].itemIdentifier isEqual:@"back"] ||
  73. [toolbar.items[1].itemIdentifier isEqual:@"back"]) {
  74. return YES;
  75. } else {
  76. return NO;
  77. }
  78. }
  79. - (void)setBackToolbarItemVisible:(BOOL)show {
  80. NSToolbar *toolbar = self.view.window.toolbar;
  81. if ([toolbar.items[1].itemIdentifier isEqual:@"back"]) {
  82. if (!show) {
  83. [toolbar removeItemAtIndex:1];
  84. }
  85. }
  86. else if ([toolbar.items[0].itemIdentifier isEqual:@"back"]) {
  87. if (!show) {
  88. [toolbar removeItemAtIndex:0];
  89. }
  90. }
  91. else if (show) {
  92. // Insert it after the sidebar toggle, if we have one.
  93. int index = ([toolbar.items[0].itemIdentifier isEqual:NSToolbarToggleSidebarItemIdentifier]);
  94. [toolbar insertItemWithItemIdentifier:@"back" atIndex:index];
  95. }
  96. }
  97. - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
  98. return @[];
  99. }
  100. - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
  101. return @[];
  102. }
  103. - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  104. itemForItemIdentifier:(NSString *)ident
  105. willBeInsertedIntoToolbar:(BOOL)flag {
  106. if (@available(macOS 11.0, *)) {
  107. if ([ident isEqual:@"sep"]) {
  108. return [NSTrackingSeparatorToolbarItem trackingSeparatorToolbarItemWithIdentifier:@"sep" splitView:_graph->get_split_view() dividerIndex:0];
  109. }
  110. if ([ident isEqual:@"back"]) {
  111. NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:ident] autorelease];
  112. item.label = @"Back";
  113. item.image = [NSImage imageWithSystemSymbolName:@"chevron.left" accessibilityDescription:@""];
  114. item.target = self;
  115. item.action = @selector(handleBack:);
  116. [item setNavigational:YES];
  117. [item setBordered:YES];
  118. return item;
  119. }
  120. }
  121. return nil;
  122. }
  123. - (void)handleSetAsFocus:(NSMenuItem *)item {
  124. _graph->on_click_label(item.tag);
  125. }
  126. - (void)handleChangeColor:(NSMenuItem *)item {
  127. _graph->get_monitor()->choose_collector_color(item.tag);
  128. }
  129. - (void)handleResetColor:(NSMenuItem *)item {
  130. _graph->get_monitor()->reset_collector_color(item.tag);
  131. }
  132. - (void)handleBack:(id)sender {
  133. _graph->handle_back();
  134. }
  135. @end
  136. @implementation MacStatsScrollableGraphViewController
  137. - (void)loadView {
  138. NSView *graph_view = [[MacStatsGraphView alloc] initWithGraph:_graph];
  139. NSScrollView *scroll = [[NSScrollView alloc] init];
  140. scroll.hasHorizontalScroller = NO;
  141. scroll.hasVerticalScroller = YES;
  142. scroll.horizontalScrollElasticity = NSScrollElasticityNone;
  143. scroll.usesPredominantAxisScrolling = NO;
  144. scroll.drawsBackground = YES;
  145. scroll.scrollerStyle = NSScrollerStyleOverlay;
  146. scroll.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  147. //scroll.translatesAutoresizingMaskIntoConstraints = NO;
  148. scroll.automaticallyAdjustsContentInsets = YES;
  149. scroll.documentView = graph_view;
  150. self.view = scroll;
  151. [graph_view.widthAnchor constraintEqualToAnchor:scroll.widthAnchor].active = YES;
  152. if (@available(macOS 11.0, *)) {
  153. [graph_view.heightAnchor constraintGreaterThanOrEqualToAnchor:((NSLayoutGuide *)[scroll safeAreaLayoutGuide]).heightAnchor].active = YES;
  154. } else {
  155. [graph_view.heightAnchor constraintGreaterThanOrEqualToAnchor:scroll.heightAnchor].active = YES;
  156. }
  157. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  158. [center addObserver:self
  159. selector:@selector(handleScroll:)
  160. name:NSScrollViewDidLiveScrollNotification
  161. object:scroll];
  162. [scroll release];
  163. [graph_view release];
  164. }
  165. - (MacStatsGraphView *)graphView {
  166. return (MacStatsGraphView *)((NSScrollView *)self.view).documentView;
  167. }
  168. - (NSClipView *)clipView {
  169. return ((NSScrollView *)self.view).contentView;
  170. }
  171. - (void)viewDidLayout {
  172. if (_graph != nullptr) {
  173. _graph->handle_scroll();
  174. }
  175. }
  176. - (void)handleScroll:(NSNotification *)notification {
  177. if (_graph != nullptr) {
  178. _graph->handle_scroll();
  179. }
  180. }
  181. - (void)handleSideScroll:(NSNotification *)notification {
  182. // Graph view is flipped, side bar isn't, so we need to convert coordinates
  183. NSScrollView *side_sv = ((NSScrollView *)notification.object);
  184. NSScrollView *graph_sv = (NSScrollView *)self.view;
  185. NSPoint point;
  186. point.x = 0;
  187. point.y = self.graphView.frame.size.height - (side_sv.documentVisibleRect.size.height + side_sv.documentVisibleRect.origin.y) - graph_sv.contentInsets.top;
  188. [graph_sv.contentView scrollToPoint:point];
  189. [graph_sv reflectScrolledClipView:graph_sv.contentView];
  190. }
  191. @end