Skip to content

Commit 78b6e7d

Browse files
authored
Merge pull request #52 from scratchcpp/linux_native_menu_bar
Implement Linux native menu bar
2 parents e833340 + f5f0a80 commit 78b6e7d

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ option(SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS "Build unit tests" ON)
1010
find_package(Qt6 6.6 COMPONENTS Quick QuickControls2 Widgets OpenGLWidgets REQUIRED)
1111
set(QT_LIBS Qt6::Quick Qt6::QuickControls2 Qt6::Widgets Qt6::OpenGLWidgets)
1212

13+
if (LINUX)
14+
find_package(Qt6 6.6 COMPONENTS DBus REQUIRED)
15+
set(QT_LIBS ${QT_LIBS} Qt6::DBus)
16+
endif()
17+
1318
if (SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS)
1419
set(GTEST_DIR thirdparty/googletest)
1520
add_subdirectory(${PROJECT_SOURCE_DIR}/${GTEST_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${GTEST_DIR})

src/ui/internal/uiengine.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <QCoreApplication>
44
#include <QPushButton>
5+
#ifdef Q_OS_LINUX
6+
#include <QtDBus/QDBusConnection>
7+
#include <QtDBus/QDBusConnectionInterface>
8+
#endif
59

610
#include "uiengine.h"
711

@@ -66,3 +70,24 @@ void UiEngine::setActiveFocusItem(QQuickItem *newActiveFocusItem)
6670
m_activeFocusItem = newActiveFocusItem;
6771
emit activeFocusItemChanged();
6872
}
73+
74+
bool UiEngine::useNativeMenuBar() const
75+
{
76+
#if defined(Q_OS_MACOS)
77+
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
78+
return true;
79+
#else
80+
// Since Qt 6.8, Qt Quick Controls menu bar is native
81+
return false;
82+
#endif
83+
#elif defined(Q_OS_LINUX)
84+
const QDBusConnection connection = QDBusConnection::sessionBus();
85+
static const QString registrarService = QStringLiteral("com.canonical.AppMenu.Registrar");
86+
if (const auto iface = connection.interface())
87+
return iface->isServiceRegistered(registrarService);
88+
else
89+
return false;
90+
#else
91+
return false;
92+
#endif
93+
}

src/ui/internal/uiengine.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class UiEngine
1919
{
2020
Q_OBJECT
2121
Q_PROPERTY(QQuickItem *activeFocusItem READ activeFocusItem WRITE setActiveFocusItem NOTIFY activeFocusItemChanged)
22+
Q_PROPERTY(bool useNativeMenuBar READ useNativeMenuBar NOTIFY useNativeMenuBarChanged FINAL)
2223

2324
public:
2425
explicit UiEngine(QObject *parent = nullptr);
@@ -34,8 +35,11 @@ class UiEngine
3435
QQuickItem *activeFocusItem() const;
3536
void setActiveFocusItem(QQuickItem *newActiveFocusItem);
3637

38+
bool useNativeMenuBar() const;
39+
3740
signals:
3841
void activeFocusItemChanged();
42+
void useNativeMenuBarChanged();
3943

4044
private:
4145
static std::shared_ptr<UiEngine> m_instance;

src/uicomponents/CustomMenuBar.qml

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import QtQuick
44
import QtQuick.Controls
5+
import Qt.labs.platform as Platform
56
import ScratchCPP.Ui
67
import ScratchCPP.UiComponents
78

@@ -22,8 +23,7 @@ MenuBar {
2223
}
2324

2425
function getComponentString(typeName) {
25-
//var imports = "import QtQuick; import QtQuick.Controls; import Qt.labs.platform as Platform;"
26-
var imports = "import QtQuick; import QtQuick.Controls;"
26+
var imports = "import QtQuick; import QtQuick.Controls; import Qt.labs.platform as Platform;"
2727
return imports + " " + typeName + " {}";
2828
}
2929

@@ -103,11 +103,11 @@ MenuBar {
103103
}
104104

105105
function reload() {
106-
/*if(nativeMenuBarEnabled)
106+
if(UiEngine.useNativeMenuBar)
107107
{
108108
root.visible = false;
109109
return;
110-
}*/
110+
}
111111

112112
var oldObjects = [];
113113

@@ -132,14 +132,14 @@ MenuBar {
132132

133133
Component.onCompleted: reload();
134134

135-
/*onEnabledChanged: {
135+
onEnabledChanged: {
136136
if(platformMenuBarLoader.active)
137137
platformMenuBarLoader.item.reload();
138138
}
139139

140140
Loader {
141141
id: platformMenuBarLoader
142-
active: // whether the native menu bar is active
142+
active: UiEngine.useNativeMenuBar
143143

144144
sourceComponent: Platform.MenuBar {
145145
id: platformMenuBar
@@ -149,14 +149,14 @@ MenuBar {
149149
createMenuBar(platformMenuBar, "Platform.Menu", "Platform.MenuItem", "Platform.MenuSeparator");
150150
}
151151

152-
Connections {
153-
target: QmlUtils
152+
/*Connections {
153+
target: // TODO: Add a class for the menu bar reload signal
154154
function onMenuBarReloadTriggered() {
155155
platformMenuBar.reload();
156156
}
157-
}
157+
}*/
158158

159159
Component.onCompleted: reload();
160160
}
161-
}*/
161+
}
162162
}

0 commit comments

Comments
 (0)