Skip to content

Commit 1b46e13

Browse files
committed
Add macOS code to open default browser, move the OpenURL function into a separate class.
1 parent 243b9f3 commit 1b46e13

File tree

5 files changed

+54
-38
lines changed

5 files changed

+54
-38
lines changed

src/gui/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ add_library(ProjectMSDL-GUI STATIC
3131
ProjectMGUI.h
3232
ToastMessage.cpp
3333
ToastMessage.h
34-
HelpWindow.cpp HelpWindow.h SettingsWindow.cpp SettingsWindow.h)
34+
HelpWindow.cpp HelpWindow.h SettingsWindow.cpp SettingsWindow.h
35+
SystemBrowser.cpp
36+
SystemBrowser.h)
3537

3638
target_include_directories(ProjectMSDL-GUI
3739
PUBLIC

src/gui/MainMenu.cpp

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#include "MainMenu.h"
1+
#include "gui/MainMenu.h"
22

3-
#include "ProjectMGUI.h"
43
#include "ProjectMWrapper.h"
54
#include "AudioCapture.h"
65

6+
#include "gui/ProjectMGUI.h"
7+
#include "gui/SystemBrowser.h"
8+
79
#include "notifications/PlaybackControlNotification.h"
810
#include "notifications/QuitNotification.h"
911
#include "notifications/UpdateWindowTitleNotification.h"
@@ -14,16 +16,6 @@
1416

1517
#include <Poco/Util/Application.h>
1618

17-
#ifdef __MSC_VER__
18-
#include <shellapi.h>
19-
#include <windows.h>
20-
#endif
21-
#ifdef __linux__
22-
#include <Poco/Process.h>
23-
#endif
24-
#ifdef __APPLE__
25-
#endif
26-
2719
MainMenu::MainMenu(ProjectMGUI& gui)
2820
: _notificationCenter(Poco::NotificationCenter::defaultCenter())
2921
, _gui(gui)
@@ -159,28 +151,15 @@ void MainMenu::Draw()
159151

160152
if (ImGui::MenuItem("Visit the projectM Wiki on GitHub"))
161153
{
162-
OpenURL("https://github.com/projectM-visualizer/projectm/wiki");
154+
SystemBrowser::OpenURL("https://github.com/projectM-visualizer/projectm/wiki");
163155
}
164156
if (ImGui::MenuItem("Report a Bug or Request a Feature"))
165157
{
166-
OpenURL("https://github.com/projectM-visualizer/projectm/issues/new/choose");
158+
SystemBrowser::OpenURL("https://github.com/projectM-visualizer/projectm/issues/new/choose");
167159
}
168160
ImGui::EndMenu();
169161
}
170162

171163
ImGui::EndMainMenuBar();
172164
}
173165
}
174-
175-
void MainMenu::OpenURL(const std::string& url)
176-
{
177-
#ifdef __MSC_VER__
178-
ShellExecute(0, 0, url.c_str(), 0, 0, SW_SHOW);
179-
#endif
180-
#ifdef __linux__
181-
auto handle = Poco::Process::launch("xdg-open", {url});
182-
handle.wait();
183-
#endif
184-
#ifdef __APPLE__
185-
#endif
186-
}

src/gui/MainMenu.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ class ProjectMGUI;
66
class ProjectMWrapper;
77
class AudioCapture;
88

9-
namespace Poco
10-
{
9+
namespace Poco {
1110
class NotificationCenter;
1211
}
1312

@@ -24,14 +23,6 @@ class MainMenu
2423
void Draw();
2524

2625
private:
27-
/**
28-
* @brief Opens the given URL in the default browser, if the platform is supported.
29-
*
30-
* Only Windows (ShellExecute), macOS (LSOpenCFURLRef) and Linux (xdg-open) are currently supported.
31-
* @param url The URL to open.
32-
*/
33-
void OpenURL(const std::string& url);
34-
3526
Poco::NotificationCenter& _notificationCenter; //!< Notification center instance.
3627
ProjectMGUI& _gui; //!< Reference to the GUI subsystem.
3728
ProjectMWrapper& _projectMWrapper; //!< Reference to the projectM wrapper subsystem.

src/gui/SystemBrowser.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "SystemBrowser.h"
2+
3+
#ifdef __MSC_VER__
4+
#include <shellapi.h>
5+
#include <windows.h>
6+
#endif
7+
#ifdef __linux__
8+
#include <Poco/Process.h>
9+
#endif
10+
#ifdef __APPLE__
11+
#include <ApplicationServices/ApplicationServices.h>
12+
#include <CoreFoundation/CFBundle.h>
13+
#endif
14+
15+
void SystemBrowser::OpenURL(const std::string& url)
16+
{
17+
#ifdef __MSC_VER__
18+
ShellExecute(0, 0, url.c_str(), 0, 0, SW_SHOW);
19+
#endif
20+
#ifdef __linux__
21+
auto handle = Poco::Process::launch("xdg-open", {url});
22+
handle.wait();
23+
#endif
24+
#ifdef __APPLE__
25+
CFURLRef urlRef = CFURLCreateWithBytes(nullptr, reinterpret_cast<UInt8*>(url.c_str()), url.length(), kCFStringEncodingASCII, nullptr);
26+
LSOpenCFURLRef(urlRef, 0);
27+
CFRelease(urlRef);
28+
#endif
29+
}

src/gui/SystemBrowser.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
class SystemBrowser
6+
{
7+
public:
8+
/**
9+
* @brief Opens the given URL in the default browser, if the platform is supported.
10+
*
11+
* Only Windows (ShellExecute), macOS (LSOpenCFURLRef) and Linux (xdg-open) are currently supported.
12+
* @param url The URL to open.
13+
*/
14+
static void OpenURL(const std::string& url);
15+
};

0 commit comments

Comments
 (0)