feat: finish frameless window

This commit is contained in:
rankun 2020-10-12 13:50:41 +08:00
parent d33e8a7eff
commit 1e15a305e7
3 changed files with 84 additions and 3 deletions

View file

@ -24,3 +24,7 @@ void WindowFramelessHelper::setTarget(QQuickWindow *target)
emit targetChanged();
}
void WindowFramelessHelper::updateStyle() {
}

View file

@ -14,6 +14,9 @@ public:
QQuickWindow *target() const;
void setTarget(QQuickWindow *target);
protected:
void updateStyle();
signals:
void targetChanged();

View file

@ -2,6 +2,70 @@
#include <Cocoa/Cocoa.h>
#include <QOperatingSystemVersion>
#import <objc/runtime.h>
#include <QDebug>
void SwizzleSelector(Class originalCls, SEL originalSelector, Class swizzledCls, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(originalCls, originalSelector);
Method swizzledMethod = class_getInstanceMethod(swizzledCls, swizzledSelector);
BOOL didAddMethod =
class_addMethod(originalCls,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(originalCls,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@interface NSWindow(FilterWindow)
@end
@implementation NSWindow(FilterWindow)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SwizzleSelector([self class],
@selector(setStyleMask:),
[self class],
@selector(filter_setStyleMask:));
SwizzleSelector([self class],
@selector(setTitlebarAppearsTransparent:),
[self class],
@selector(filter_setTitlebarAppearsTransparent:));
});
}
- (void)filter_setStyleMask:(NSWindowStyleMask)styleMask
{
[self filter_setStyleMask:styleMask];
//设置标题文字和图标为不可见
self.titleVisibility = NSWindowTitleHidden;
//设置标题栏为透明
self.titlebarAppearsTransparent = YES;
//设置不可由标题栏拖动,避免与自定义拖动冲突
self.movable = NO;
self.hasShadow = YES;
//设置view扩展到标题栏
if (!(self.styleMask & NSWindowStyleMaskFullSizeContentView)) {
self.styleMask |= NSWindowStyleMaskFullSizeContentView;
}
}
- (void)filter_setTitlebarAppearsTransparent:(BOOL)titlebarAppearsTransparent
{
[self filter_setTitlebarAppearsTransparent:titlebarAppearsTransparent];
}
@end
WindowFramelessHelper::WindowFramelessHelper(QObject *parent) : QObject(parent)
{
@ -20,12 +84,22 @@ void WindowFramelessHelper::setTarget(QQuickWindow *target)
}
m_target = target;
//updateStyle();
emit targetChanged();
}
/*
void WindowFramelessHelper::updateStyle() {
if (!m_target) {
return;
}
//如果当前osx版本老于10.9,则后续代码不可用。转为使用定制的系统按钮,不支持自由缩放窗口及窗口阴影
if (QOperatingSystemVersion::current() < QOperatingSystemVersion::OSXYosemite) {
return;
}
NSView* view = (NSView*)target->winId();
NSView* view = (NSView*)m_target->winId();
if (nullptr == view) {
return;
}
@ -44,6 +118,6 @@ void WindowFramelessHelper::setTarget(QQuickWindow *target)
window.hasShadow = YES;
//设置view扩展到标题栏
window.styleMask |= NSWindowStyleMaskFullSizeContentView;
emit targetChanged();
}
*/