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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018-2021 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------
#ifdef __APPLE__
#import <Cocoa/Cocoa.h>
#import <CoreServices/CoreServices.h>
#include "Open3DViewer.h"
#include "open3d/utility/FileSystem.h"
#include "open3d/visualization/gui/Application.h"
#include "open3d/visualization/gui/Button.h"
#include "open3d/visualization/gui/Dialog.h"
#include "open3d/visualization/gui/Label.h"
#include "open3d/visualization/gui/Layout.h"
#include "open3d/visualization/gui/Native.h"
#include "open3d/visualization/gui/Theme.h"
#include "open3d/visualization/visualizer/GuiVisualizer.h"
// ----------------------------------------------------------------------------
using namespace open3d::visualization::gui;
class Open3DVisualizer : public open3d::visualization::GuiVisualizer {
using Super = GuiVisualizer;
public:
Open3DVisualizer()
: open3d::visualization::GuiVisualizer("Open3D", WIDTH, HEIGHT) {
AddItemsToAppMenu({{"Make Default 3D Viewer", MAC_MAKE_DEFAULT_APP}});
}
protected:
static constexpr Menu::ItemId MAC_MAKE_DEFAULT_APP = 100;
void OnMenuItemSelected(Menu::ItemId item_id) override {
if (item_id == MAC_MAKE_DEFAULT_APP) {
auto em = GetTheme().font_size;
auto dlg = std::make_shared<Dialog>("Make Open3D default");
auto cancel = std::make_shared<Button>("Cancel");
cancel->SetOnClicked([this]() { this->CloseDialog(); });
auto ok = std::make_shared<Button>("Make Default");
ok->SetOnClicked([this]() {
// This will set the users personal default to use Open3D for
// the file types below. THIS SHOULD ONLY BE CALLED
// AFTER THE USER EXPLICITLY CONFIRMS THAT THEY WANT TO DO THIS!
CFStringRef open3dBundleId = (__bridge CFStringRef)@"com.isl-org.open3d.Open3D";
// The UTIs should match what we declare in Info.plist
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.gl-transmission-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.gl-binary-transmission-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.geometry-definition-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.object-file-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.point-cloud-library-file",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.polygon-file-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.3d-points-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.standard-tesselated-geometry-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.xyz-points-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.xyzn-points-format",
kLSRolesAll, open3dBundleId);
LSSetDefaultRoleHandlerForContentType(
(__bridge CFStringRef)@"public.xyzrgb-points-format",
kLSRolesAll, open3dBundleId);
this->CloseDialog();
});
auto vert = std::make_shared<Vert>(0, Margins(em));
vert->AddChild(std::make_shared<Label>(
"This will make Open3D the default application for the "
"following file types:"));
vert->AddFixed(em);
auto table = std::make_shared<VGrid>(2, 0, Margins(em, 0, 0, 0));
table->AddChild(std::make_shared<Label>("Mesh:"));
table->AddChild(std::make_shared<Label>(".gltf, .glb, .obj, .off, .ply, .stl"));
table->AddChild(std::make_shared<Label>("Point clouds:"));
table->AddChild(std::make_shared<Label>(".pcd, .ply, .pts, .xyz, .xyzn, .xyzrgb"));
vert->AddChild(table);
vert->AddFixed(em);
auto buttons = std::make_shared<Horiz>(0.5 * em);
buttons->AddStretch();
buttons->AddChild(cancel);
buttons->AddChild(ok);
vert->AddChild(buttons);
dlg->AddChild(vert);
ShowDialog(dlg);
} else {
Super::OnMenuItemSelected(item_id);
}
}
};
constexpr Menu::ItemId Open3DVisualizer::MAC_MAKE_DEFAULT_APP; // for Xcode
// ----------------------------------------------------------------------------
static void LoadAndCreateWindow(const char *path) {
auto vis = std::make_shared<Open3DVisualizer>();
bool is_path_valid = (path && path[0] != '\0');
if (is_path_valid) {
vis->LoadGeometry(path);
}
Application::GetInstance().AddWindow(vis);
}
// ----------------------------------------------------------------------------
@interface AppDelegate : NSObject <NSApplicationDelegate>
@end
@interface AppDelegate ()
{
bool open_empty_window_;
}
@property (retain) NSTimer *timer;
@end
@implementation AppDelegate
- (id)init {
if ([super init]) {
open_empty_window_ = true;
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
// -application:openFile: runs before applicationDidFinishLaunching: so we
// need to check if we loaded a file or we need to display an empty window.
if (open_empty_window_) {
LoadAndCreateWindow("");
}
}
// Called by [NSApp run] if the user passes command line arguments (which may
// be multiple times if multiple files are given), or the app is launched by
// double-clicking a file in the Finder, or by dropping files onto the app
// either in the Finder or (more likely) onto the Dock icon. It is also called
// after launching if the user double-clicks a file in the Finder or drops
// a file onto the app icon and the application is already launched.
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
open_empty_window_ = false; // LoadAndCreateWindow() always opens a window
LoadAndCreateWindow(filename.UTF8String);
return YES;
}
- (void)applicationWillTerminate:(NSNotification *)notification {
// The app will terminate after this function exits. This will result
// in the Application object in main() getting destructed, but it still
// thinks it is running. So tell Application to quit, which will post
// the required events to the event loop to properly clean up.
Application::GetInstance().OnTerminate();
}
@end
// ----------------------------------------------------------------------------
int main(int argc, const char *argv[]) {
// If we double-clicked the app, the CWD gets set to /, so change that
// to the user's home directory so that file dialogs act reasonably.
if (open3d::utility::filesystem::GetWorkingDirectory() == "/") {
std::string homedir = NSHomeDirectory().UTF8String;
open3d::utility::filesystem::ChangeWorkingDirectory(homedir);
}
Application::GetInstance().Initialize(argc, argv);
// Note: if NSApp is created (which happens in +sharedApplication)
// then GLFW will use our NSApp with our delegate instead of its
// own delegate that doesn't have the openFile and terminate
// selectors.
// Ideally we could do the following:
//@autoreleasepool {
// AppDelegate *delegate = [[AppDelegate alloc] init];
// NSApplication *application = [NSApplication sharedApplication];
// [application setDelegate:delegate];
// [NSApp run];
//}
// But somewhere along the line GLFW seems to clean up the autorelease
// pool, which then causes a crash when [NSApp run] finishes and the
// autorelease pool cleans up at the '}'. To avoid that, we will not
// autorelease things. That creates a memory leak, but we're exiting
// after that so it does not matter.
AppDelegate *delegate = [[AppDelegate alloc] init];
NSApplication *application = [NSApplication sharedApplication];
[application setDelegate:delegate];
// ---- [NSApp run] equivalent ----
// https://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html
[NSApp finishLaunching];
Application::GetInstance().Run();
// ----
}
#endif // __APPLE__
|