Necessary updates applied to make it work on mac

This commit is contained in:
Sale Djenic 2022-07-21 11:04:07 +02:00 committed by osmaczko
parent 9b89e08294
commit 132cfda244
4 changed files with 72 additions and 61 deletions

View File

@ -7,27 +7,32 @@ find_package(
file(GLOB_RECURSE QML_FILES "../*.qml") file(GLOB_RECURSE QML_FILES "../*.qml")
file(GLOB_RECURSE JS_FILES "../*.js") file(GLOB_RECURSE JS_FILES "../*.js")
file(GLOB_RECURSE HEADERS *.h)
if(APPLE)
file(GLOB_RECURSE SOURCES *.cpp *.mm)
else()
file(GLOB_RECURSE SOURCES *.cpp)
endif()
qt5_add_big_resources(QRC_FILES qml.qrc ../src/statusq.qrc ../src/assets.qrc) qt5_add_big_resources(QRC_FILES qml.qrc ../src/statusq.qrc ../src/assets.qrc)
add_executable( add_executable(
${PROJECT_NAME} ${PROJECT_NAME}
main.cpp ${HEADERS}
handler.cpp ${SOURCES}
sandboxapp.cpp
spellchecker.cpp
${QRC_FILES} ${QRC_FILES}
${QML_FILES} ${QML_FILES}
${JS_FILES}) ${JS_FILES})
if(APPLE)
target_sources(${PROJECT_NAME} PRIVATE statuswindow_mac.mm)
else()
target_sources(${PROJECT_NAME} PRIVATE statuswindow.cpp)
endif()
target_compile_definitions(${PROJECT_NAME} target_compile_definitions(${PROJECT_NAME}
PRIVATE SRC_DIR="${CMAKE_CURRENT_LIST_DIR}") PRIVATE SRC_DIR="${CMAKE_CURRENT_LIST_DIR}")
target_link_libraries( target_link_libraries(
${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick Qt5::QuickControls2 ${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick Qt5::QuickControls2
SortFilterProxyModel) SortFilterProxyModel)
if (APPLE)
find_library(AppKit AppKit)
find_library(Foundation Foundation)
target_link_libraries(${PROJECT_NAME} PRIVATE ${AppKit} ${Foundation})
endif()

View File

@ -1,5 +1,33 @@
#include "statuswindow.h" #include "statuswindow.h"
StatusWindow::StatusWindow(QQuickWindow *parent)
: QQuickWindow(parent)
, m_isFullScreen(false)
{
removeTitleBar();
connect(this, &QQuickWindow::windowStateChanged, [&](Qt::WindowState windowState) {
if (windowState == Qt::WindowNoState) {
removeTitleBar();
m_isFullScreen = false;
emit isFullScreenChanged();
} else if (windowState == Qt::WindowFullScreen) {
m_isFullScreen = true;
emit isFullScreenChanged();
showTitleBar();
}
});
}
void StatusWindow::updatePosition()
{
auto point = QPoint(screen()->geometry().center().x() - geometry().width() / 2,
screen()->geometry().center().y() - geometry().height() / 2);
if (point != this->position()) {
this->setPosition(point);
}
}
void StatusWindow::toggleFullScreen() void StatusWindow::toggleFullScreen()
{ {
if (m_isFullScreen) { if (m_isFullScreen) {
@ -16,10 +44,26 @@ bool StatusWindow::isFullScreen() const
void StatusWindow::removeTitleBar() void StatusWindow::removeTitleBar()
{ {
#ifdef Q_OS_WIN
removeTitleBarWin();
#elif defined Q_OS_MACOS
removeTitleBarMac();
#endif
} }
void StatusWindow::showTitleBar() void StatusWindow::showTitleBar()
{ {
#ifdef Q_OS_WIN
showTitleBarWin();
#elif defined Q_OS_MACOS
showTitleBarMac();
#endif
} }
#ifdef Q_OS_WIN
void StatusWindow::removeTitleBarWin()
{}
void StatusWindow::showTitleBarWin()
{}
#endif

View File

@ -11,40 +11,11 @@ class StatusWindow: public QQuickWindow
Q_PROPERTY(bool isFullScreen READ isFullScreen NOTIFY isFullScreenChanged) Q_PROPERTY(bool isFullScreen READ isFullScreen NOTIFY isFullScreenChanged)
public: public:
struct EventCallbacks { explicit StatusWindow(QQuickWindow *parent = nullptr);
std::function<void()> onResize;
std::function<void()> willExitFullScreen;
std::function<void()> didExitFullScreen;
};
explicit StatusWindow(QWindow *parent = nullptr)
: QQuickWindow(parent),
m_isFullScreen(false)
{
removeTitleBar();
connect(this, &QQuickWindow::windowStateChanged, [&](Qt::WindowState windowState) {
if (windowState == Qt::WindowNoState) {
removeTitleBar();
m_isFullScreen = false;
emit isFullScreenChanged();
} else if (windowState == Qt::WindowFullScreen) {
m_isFullScreen = true;
emit isFullScreenChanged();
showTitleBar();
}
});
}
Q_INVOKABLE void toggleFullScreen(); Q_INVOKABLE void toggleFullScreen();
Q_INVOKABLE void updatePosition() { Q_INVOKABLE void updatePosition();
auto point = QPoint(screen()->geometry().center().x() - geometry().width() / 2, screen()->geometry().center().y() - geometry().height() / 2);
if (point != this->position()) {
this->setPosition(point);
}
}
bool isFullScreen() const; bool isFullScreen() const;
@ -54,10 +25,15 @@ signals:
private: private:
void removeTitleBar(); void removeTitleBar();
void showTitleBar(); void showTitleBar();
void initCallbacks(); #ifdef Q_OS_WIN
void removeTitleBarWin();
void showTitleBarWin();
#elif defined Q_OS_MACOS
void removeTitleBarMac();
void showTitleBarMac();
#endif
private: private:
EventCallbacks m_callbacks;
bool m_isFullScreen; bool m_isFullScreen;
}; };

View File

@ -12,21 +12,7 @@
#include <AppKit/NSButton.h> #include <AppKit/NSButton.h>
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
void StatusWindow::toggleFullScreen() void StatusWindow::removeTitleBarMac()
{
if (m_isFullScreen) {
showNormal();
} else {
showFullScreen();
}
}
bool StatusWindow::isFullScreen() const
{
return m_isFullScreen;
}
void StatusWindow::removeTitleBar()
{ {
NSView *nsView = reinterpret_cast<NSView*>(this->winId()); NSView *nsView = reinterpret_cast<NSView*>(this->winId());
NSWindow *window = [nsView window]; NSWindow *window = [nsView window];
@ -39,7 +25,7 @@ void StatusWindow::removeTitleBar()
[titleBarContainerView setHidden:YES]; [titleBarContainerView setHidden:YES];
} }
void StatusWindow::showTitleBar() void StatusWindow::showTitleBarMac()
{ {
NSView *nsView = reinterpret_cast<NSView*>(this->winId()); NSView *nsView = reinterpret_cast<NSView*>(this->winId());
NSWindow *window = [nsView window]; NSWindow *window = [nsView window];