Skip to content

Implement Linux native menu bar #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ option(SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS "Build unit tests" ON)
find_package(Qt6 6.6 COMPONENTS Quick QuickControls2 Widgets OpenGLWidgets REQUIRED)
set(QT_LIBS Qt6::Quick Qt6::QuickControls2 Qt6::Widgets Qt6::OpenGLWidgets)

if (LINUX)
find_package(Qt6 6.6 COMPONENTS DBus REQUIRED)
set(QT_LIBS ${QT_LIBS} Qt6::DBus)
endif()

if (SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS)
set(GTEST_DIR thirdparty/googletest)
add_subdirectory(${PROJECT_SOURCE_DIR}/${GTEST_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${GTEST_DIR})
Expand Down
25 changes: 25 additions & 0 deletions src/ui/internal/uiengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <QCoreApplication>
#include <QPushButton>
#ifdef Q_OS_LINUX
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>
#endif

#include "uiengine.h"

Expand Down Expand Up @@ -66,3 +70,24 @@ void UiEngine::setActiveFocusItem(QQuickItem *newActiveFocusItem)
m_activeFocusItem = newActiveFocusItem;
emit activeFocusItemChanged();
}

bool UiEngine::useNativeMenuBar() const
{
#if defined(Q_OS_MACOS)
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
return true;
#else
// Since Qt 6.8, Qt Quick Controls menu bar is native
return false;
#endif
#elif defined(Q_OS_LINUX)
const QDBusConnection connection = QDBusConnection::sessionBus();
static const QString registrarService = QStringLiteral("com.canonical.AppMenu.Registrar");
if (const auto iface = connection.interface())
return iface->isServiceRegistered(registrarService);
else
return false;
#else
return false;
#endif
}
4 changes: 4 additions & 0 deletions src/ui/internal/uiengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class UiEngine
{
Q_OBJECT
Q_PROPERTY(QQuickItem *activeFocusItem READ activeFocusItem WRITE setActiveFocusItem NOTIFY activeFocusItemChanged)
Q_PROPERTY(bool useNativeMenuBar READ useNativeMenuBar NOTIFY useNativeMenuBarChanged FINAL)

public:
explicit UiEngine(QObject *parent = nullptr);
Expand All @@ -34,8 +35,11 @@ class UiEngine
QQuickItem *activeFocusItem() const;
void setActiveFocusItem(QQuickItem *newActiveFocusItem);

bool useNativeMenuBar() const;

signals:
void activeFocusItemChanged();
void useNativeMenuBarChanged();

private:
static std::shared_ptr<UiEngine> m_instance;
Expand Down
20 changes: 10 additions & 10 deletions src/uicomponents/CustomMenuBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import QtQuick
import QtQuick.Controls
import Qt.labs.platform as Platform
import ScratchCPP.Ui
import ScratchCPP.UiComponents

Expand All @@ -22,8 +23,7 @@ MenuBar {
}

function getComponentString(typeName) {
//var imports = "import QtQuick; import QtQuick.Controls; import Qt.labs.platform as Platform;"
var imports = "import QtQuick; import QtQuick.Controls;"
var imports = "import QtQuick; import QtQuick.Controls; import Qt.labs.platform as Platform;"
return imports + " " + typeName + " {}";
}

Expand Down Expand Up @@ -103,11 +103,11 @@ MenuBar {
}

function reload() {
/*if(nativeMenuBarEnabled)
if(UiEngine.useNativeMenuBar)
{
root.visible = false;
return;
}*/
}

var oldObjects = [];

Expand All @@ -132,14 +132,14 @@ MenuBar {

Component.onCompleted: reload();

/*onEnabledChanged: {
onEnabledChanged: {
if(platformMenuBarLoader.active)
platformMenuBarLoader.item.reload();
}

Loader {
id: platformMenuBarLoader
active: // whether the native menu bar is active
active: UiEngine.useNativeMenuBar

sourceComponent: Platform.MenuBar {
id: platformMenuBar
Expand All @@ -149,14 +149,14 @@ MenuBar {
createMenuBar(platformMenuBar, "Platform.Menu", "Platform.MenuItem", "Platform.MenuSeparator");
}

Connections {
target: QmlUtils
/*Connections {
target: // TODO: Add a class for the menu bar reload signal
function onMenuBarReloadTriggered() {
platformMenuBar.reload();
}
}
}*/

Component.onCompleted: reload();
}
}*/
}
}
Loading