mirror of
https://github.com/YACReader/yacreader
synced 2025-07-18 21:14:33 -04:00
Implement native toolbars on macos on Qt6 to have a modern looking unified toolbars
This commit is contained in:
@ -99,6 +99,7 @@ class YACReaderMacOSXSearchLineEdit : public YACReaderSearchLineEdit
|
||||
|
||||
class YACReaderMacOSXToolbar : public YACReaderMainToolBar
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit YACReaderMacOSXToolbar(QWidget *parent = 0);
|
||||
QSize sizeHint() const override;
|
||||
@ -109,20 +110,41 @@ public:
|
||||
void updateViewSelectorIcon(const QIcon &icon);
|
||||
void attachToWindow(QMainWindow *window);
|
||||
|
||||
void *getSearchEditDelegate() { return searchEditDelegate; };
|
||||
|
||||
void emitFilterChange(const QString &filter) { emit filterChanged(filter); };
|
||||
|
||||
QAction *actionFromIdentifier(const QString &identifier);
|
||||
signals:
|
||||
void filterChanged(QString);
|
||||
|
||||
private:
|
||||
void paintEvent(QPaintEvent *) override;
|
||||
|
||||
void *searchEditDelegate;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
class YACReaderMacOSXToolbar : public QToolBar
|
||||
class YACReaderMacOSXToolbar : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit YACReaderMacOSXToolbar(QWidget *parent = 0);
|
||||
void attachToWindow(QMainWindow *window);
|
||||
void addStretch();
|
||||
|
||||
void setMovable(bool movable) {};
|
||||
void addSeparator() {};
|
||||
|
||||
void setIconSize(const QSize &size) {};
|
||||
|
||||
public slots:
|
||||
void setHidden(bool hidden);
|
||||
void show();
|
||||
void hide();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -398,8 +398,294 @@ void MacToolBarItemWrapper::updateIcon(bool enabled)
|
||||
}
|
||||
#else
|
||||
|
||||
#import <AppKit/AppKit.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
NSImage *QIconToNSImage(const QIcon &icon, const QSize &size, const QColor &color = QColor())
|
||||
{
|
||||
QPixmap pixmap = icon.pixmap(size);
|
||||
QImage qImage = pixmap.toImage().convertToFormat(QImage::Format_RGBA8888);
|
||||
|
||||
if (color.isValid()) {
|
||||
QPainter p;
|
||||
|
||||
QImage mask(qImage);
|
||||
|
||||
p.begin(&mask);
|
||||
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
||||
QBrush brush(color);
|
||||
p.fillRect(QRect(0, 0, size.width(), size.height()), brush);
|
||||
p.end();
|
||||
|
||||
p.begin(&qImage);
|
||||
p.setCompositionMode(QPainter::CompositionMode_Overlay);
|
||||
p.drawImage(0, 0, mask);
|
||||
p.end();
|
||||
}
|
||||
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
CGContextRef context = CGBitmapContextCreate(
|
||||
(void *)qImage.bits(),
|
||||
qImage.width(),
|
||||
qImage.height(),
|
||||
8,
|
||||
qImage.bytesPerLine(),
|
||||
colorSpace,
|
||||
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
||||
|
||||
CGImageRef cgImage = CGBitmapContextCreateImage(context);
|
||||
NSImage *nsImage = [[NSImage alloc] initWithCGImage:cgImage size:NSMakeSize(qImage.width(), qImage.height())];
|
||||
|
||||
// Clean up
|
||||
CGImageRelease(cgImage);
|
||||
CGContextRelease(context);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
|
||||
return nsImage;
|
||||
}
|
||||
|
||||
void bindActionToNSToolbarItem(QAction *action, NSToolbarItem *toolbarItem, const QColor &iconColor = QColor())
|
||||
{
|
||||
if (action == nullptr || toolbarItem == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto update = [=] {
|
||||
toolbarItem.enabled = action->isEnabled();
|
||||
|
||||
QString text = action->text();
|
||||
QString tooltip = action->toolTip();
|
||||
|
||||
toolbarItem.label = text.isEmpty() ? @"" : [NSString stringWithUTF8String:text.toUtf8().constData()];
|
||||
toolbarItem.paletteLabel = toolbarItem.label;
|
||||
toolbarItem.toolTip = tooltip.isEmpty() ? @"" : [NSString stringWithUTF8String:tooltip.toUtf8().constData()];
|
||||
|
||||
QIcon icon = action->icon();
|
||||
|
||||
__auto_type image = QIconToNSImage(icon, { 24, 24 }, iconColor);
|
||||
|
||||
if (action->isChecked()) {
|
||||
NSSize size = image.size;
|
||||
NSImage *decoratedImage = [[NSImage alloc] initWithSize:size];
|
||||
|
||||
[decoratedImage lockFocus];
|
||||
|
||||
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
|
||||
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:8 yRadius:8];
|
||||
[[NSColor colorWithCalibratedRed:0.8 green:0.8 blue:0.8 alpha:0.9] setFill];
|
||||
[path fill];
|
||||
|
||||
NSRect imageRect = NSMakeRect(4, 4, size.width - 8, size.height - 8);
|
||||
[image drawInRect:imageRect
|
||||
fromRect:NSZeroRect
|
||||
operation:NSCompositingOperationSourceOver
|
||||
fraction:1.0];
|
||||
|
||||
[decoratedImage unlockFocus];
|
||||
|
||||
toolbarItem.image = decoratedImage;
|
||||
} else {
|
||||
NSSize size = image.size;
|
||||
NSImage *decoratedImage = [[NSImage alloc] initWithSize:size];
|
||||
|
||||
[decoratedImage lockFocus];
|
||||
|
||||
NSRect imageRect = NSMakeRect(4, 4, size.width - 8, size.height - 8);
|
||||
[image drawInRect:imageRect
|
||||
fromRect:NSZeroRect
|
||||
operation:NSCompositingOperationSourceOver
|
||||
fraction:1.0];
|
||||
|
||||
[decoratedImage unlockFocus];
|
||||
|
||||
toolbarItem.image = decoratedImage;
|
||||
}
|
||||
|
||||
[image release];
|
||||
};
|
||||
|
||||
if (action->isCheckable()) {
|
||||
QObject::connect(
|
||||
action, &QAction::triggered,
|
||||
[=](bool checked) {
|
||||
update();
|
||||
});
|
||||
}
|
||||
|
||||
QObject::connect(
|
||||
action, &QAction::enabledChanged,
|
||||
[=](bool enabled) {
|
||||
toolbarItem.enabled = enabled;
|
||||
});
|
||||
|
||||
QObject::connect(
|
||||
action, &QAction::changed,
|
||||
[=]() {
|
||||
update();
|
||||
});
|
||||
|
||||
toolbarItem.bordered = YES;
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
#ifdef YACREADER_LIBRARY
|
||||
|
||||
@interface YACReaderLibraryToolbarDelegate : NSObject <NSToolbarDelegate> {
|
||||
@public
|
||||
YACReaderMacOSXToolbar *mytoolbar;
|
||||
}
|
||||
|
||||
- (IBAction)itemClicked:(id)sender;
|
||||
|
||||
@end
|
||||
|
||||
@implementation YACReaderLibraryToolbarDelegate
|
||||
|
||||
- (NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
|
||||
return @[
|
||||
@"Back",
|
||||
@"Forward",
|
||||
@"Settings",
|
||||
@"Server",
|
||||
@"Help",
|
||||
NSToolbarSpaceItemIdentifier,
|
||||
@"ToggleView",
|
||||
NSToolbarSpaceItemIdentifier,
|
||||
@"Search",
|
||||
];
|
||||
}
|
||||
|
||||
- (NSArray<NSToolbarItemIdentifier> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
|
||||
return @[
|
||||
@"Back",
|
||||
@"Forward",
|
||||
@"Settings",
|
||||
@"Server",
|
||||
@"Help",
|
||||
@"ToggleView",
|
||||
@"Search",
|
||||
NSToolbarSpaceItemIdentifier,
|
||||
];
|
||||
}
|
||||
|
||||
/*
|
||||
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
|
||||
QList<QMacToolBarItem *> items = mytoolbar->items();
|
||||
foreach (const QMacToolBarItem * item, items) {
|
||||
[array addObject : item->nativeToolBarItem().itemIdentifier];
|
||||
}
|
||||
return array;
|
||||
//NSMutableArray *array = toolbarPrivate->getItemIdentifiers(toolbarPrivate->items, true);
|
||||
//[array addObjectsFromArray:toolbarPrivate->getItemIdentifiers(toolbarPrivate->allowedItems, true)];
|
||||
//return array;
|
||||
}*/
|
||||
|
||||
- (IBAction)itemClicked:(id)sender
|
||||
{
|
||||
NSToolbarItem *item = reinterpret_cast<NSToolbarItem *>(sender);
|
||||
QString identifier = QString::fromNSString([item itemIdentifier]);
|
||||
|
||||
QAction *action = mytoolbar->actionFromIdentifier(identifier);
|
||||
;
|
||||
|
||||
if (action != nullptr) {
|
||||
action->trigger();
|
||||
}
|
||||
}
|
||||
|
||||
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)willBeInserted
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
Q_UNUSED(willBeInserted);
|
||||
|
||||
QString identifier = QString::fromNSString(itemIdentifier);
|
||||
|
||||
if (identifier == "Search") {
|
||||
NSSearchToolbarItem *searchItem = [[NSSearchToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
|
||||
|
||||
searchItem.resignsFirstResponderWithCancel = true;
|
||||
searchItem.searchField.delegate = id<NSSearchFieldDelegate>(mytoolbar->getSearchEditDelegate());
|
||||
searchItem.toolTip = @"Search";
|
||||
|
||||
return searchItem;
|
||||
}
|
||||
|
||||
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
|
||||
|
||||
toolbarItem.target = self;
|
||||
toolbarItem.action = @selector(itemClicked:);
|
||||
|
||||
QAction *action = mytoolbar->actionFromIdentifier(identifier);
|
||||
|
||||
if (identifier == "Back") {
|
||||
toolbarItem.navigational = YES;
|
||||
} else if (identifier == "Forward") {
|
||||
toolbarItem.navigational = YES;
|
||||
}
|
||||
|
||||
bindActionToNSToolbarItem(action, toolbarItem);
|
||||
|
||||
return toolbarItem;
|
||||
}
|
||||
|
||||
- (BOOL)validateToolbarItem:(NSToolbarItem *)item
|
||||
{
|
||||
|
||||
QString identifier = QString::fromNSString([item itemIdentifier]);
|
||||
|
||||
if (identifier == "Search") {
|
||||
return YES;
|
||||
}
|
||||
|
||||
QAction *action = mytoolbar->actionFromIdentifier(identifier);
|
||||
|
||||
if (action == nullptr) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return action->isEnabled();
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface YACReaderLibrarySearchDelegate : NSObject <NSSearchFieldDelegate> {
|
||||
@public
|
||||
YACReaderMacOSXToolbar *mytoolbar;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation YACReaderLibrarySearchDelegate
|
||||
|
||||
- (void)searchFieldDidStartSearching:(NSSearchField *)sender
|
||||
{
|
||||
}
|
||||
- (void)searchFieldDidEndSearching:(NSSearchField *)sender
|
||||
{
|
||||
[sender resignFirstResponder];
|
||||
}
|
||||
- (void)controlTextDidChange:(NSNotification *)notification
|
||||
{
|
||||
NSSearchField *searchField = notification.object;
|
||||
NSLog(@"Search text changed: %@", searchField.stringValue);
|
||||
|
||||
mytoolbar->emitFilterChange(QString::fromNSString(searchField.stringValue));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
YACReaderMacOSXToolbar::YACReaderMacOSXToolbar(QWidget *parent)
|
||||
: YACReaderMainToolBar(parent)
|
||||
{
|
||||
@ -467,22 +753,159 @@ void YACReaderMacOSXToolbar::updateViewSelectorIcon(const QIcon &icon)
|
||||
|
||||
void YACReaderMacOSXToolbar::attachToWindow(QMainWindow *window)
|
||||
{
|
||||
auto toolbar = new QToolBar();
|
||||
NSView *nsview = (NSView *)window->winId();
|
||||
NSWindow *nswindow = [nsview window];
|
||||
|
||||
toolbar->addWidget(this);
|
||||
toolbar->setMovable(false);
|
||||
YACReaderLibrarySearchDelegate *searchDelegate = [[YACReaderLibrarySearchDelegate alloc] init];
|
||||
this->searchEditDelegate = searchDelegate;
|
||||
searchDelegate->mytoolbar = this;
|
||||
|
||||
window->addToolBar(toolbar);
|
||||
// Create the NSToolbar
|
||||
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"mainToolbar"];
|
||||
[toolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
|
||||
[toolbar setShowsBaselineSeparator:false];
|
||||
|
||||
__auto_type delegate = [[YACReaderLibraryToolbarDelegate alloc] init];
|
||||
delegate->mytoolbar = this;
|
||||
[toolbar setDelegate:delegate];
|
||||
|
||||
[nswindow setToolbar:toolbar];
|
||||
}
|
||||
|
||||
void YACReaderMacOSXToolbar::paintEvent(QPaintEvent *)
|
||||
{
|
||||
}
|
||||
|
||||
QAction *YACReaderMacOSXToolbar::actionFromIdentifier(const QString &identifier)
|
||||
{
|
||||
if (identifier == "Back") {
|
||||
return backButton->defaultAction();
|
||||
} else if (identifier == "Forward") {
|
||||
return forwardButton->defaultAction();
|
||||
} else if (identifier == "Settings") {
|
||||
return settingsButton->defaultAction();
|
||||
} else if (identifier == "Server") {
|
||||
return serverButton->defaultAction();
|
||||
} else if (identifier == "Help") {
|
||||
return helpButton->defaultAction();
|
||||
} else if (identifier == "ToggleView") {
|
||||
return toggleComicsViewButton->defaultAction();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
@interface YACReaderToolbarDelegate : NSObject <NSToolbarDelegate> {
|
||||
@public
|
||||
YACReaderMacOSXToolbar *mytoolbar;
|
||||
}
|
||||
|
||||
- (IBAction)itemClicked:(id)sender;
|
||||
|
||||
@end
|
||||
|
||||
@implementation YACReaderToolbarDelegate
|
||||
|
||||
- (NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
|
||||
auto actions = mytoolbar->actions();
|
||||
NSMutableArray<NSToolbarItemIdentifier> *identifiers = [NSMutableArray arrayWithCapacity:actions.size()];
|
||||
|
||||
for (QAction *action : actions) {
|
||||
[identifiers addObject:[NSString stringWithFormat:@"action_%p", action]];
|
||||
}
|
||||
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
- (NSArray<NSToolbarItemIdentifier> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
|
||||
auto actions = mytoolbar->actions();
|
||||
NSMutableArray<NSToolbarItemIdentifier> *identifiers = [NSMutableArray arrayWithCapacity:actions.size()];
|
||||
|
||||
for (QAction *action : actions) {
|
||||
[identifiers addObject:[NSString stringWithFormat:@"action_%p", action]];
|
||||
}
|
||||
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
// - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
|
||||
// {
|
||||
// Q_UNUSED(toolbar);
|
||||
|
||||
// auto actions = mytoolbar->actions();
|
||||
// NSMutableArray<NSToolbarItemIdentifier> *identifiers = [NSMutableArray arrayWithCapacity:actions.size()];
|
||||
|
||||
// for (QAction *action : actions) {
|
||||
// if (action->isCheckable()) {
|
||||
// [identifiers addObject:[NSString stringWithFormat:@"action_%p", action]];
|
||||
// }
|
||||
// }
|
||||
|
||||
// return identifiers;
|
||||
// }
|
||||
|
||||
- (IBAction)itemClicked:(id)sender
|
||||
{
|
||||
NSToolbarItem *item = reinterpret_cast<NSToolbarItem *>(sender);
|
||||
NSString *itemIdentifier = [item itemIdentifier];
|
||||
|
||||
auto actions = mytoolbar->actions();
|
||||
|
||||
for (QAction *action : actions) {
|
||||
if ([itemIdentifier isEqualTo:[NSString stringWithFormat:@"action_%p", action]]) {
|
||||
action->trigger();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)willBeInserted
|
||||
{
|
||||
Q_UNUSED(toolbar);
|
||||
Q_UNUSED(willBeInserted);
|
||||
|
||||
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
|
||||
|
||||
toolbarItem.target = self;
|
||||
toolbarItem.action = @selector(itemClicked:);
|
||||
|
||||
auto actions = mytoolbar->actions();
|
||||
|
||||
for (QAction *action : actions) {
|
||||
if ([itemIdentifier isEqualTo:[NSString stringWithFormat:@"action_%p", action]]) {
|
||||
bindActionToNSToolbarItem(action, toolbarItem, QColor(200, 200, 200));
|
||||
}
|
||||
}
|
||||
|
||||
return toolbarItem;
|
||||
}
|
||||
|
||||
- (BOOL)validateToolbarItem:(NSToolbarItem *)item
|
||||
{
|
||||
NSString *itemIdentifier = [item itemIdentifier];
|
||||
|
||||
auto actions = mytoolbar->actions();
|
||||
|
||||
for (QAction *action : actions) {
|
||||
if ([itemIdentifier isEqualTo:[NSString stringWithFormat:@"action_%p", action]]) {
|
||||
return action->isEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
YACReaderMacOSXToolbar::YACReaderMacOSXToolbar(QWidget *parent)
|
||||
: QToolBar(parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setMovable(false);
|
||||
setIconSize(QSize(24, 24));
|
||||
@ -490,14 +913,42 @@ YACReaderMacOSXToolbar::YACReaderMacOSXToolbar(QWidget *parent)
|
||||
|
||||
void YACReaderMacOSXToolbar::attachToWindow(QMainWindow *window)
|
||||
{
|
||||
window->setUnifiedTitleAndToolBarOnMac(true);
|
||||
window->addToolBar(this);
|
||||
NSView *nsview = (NSView *)window->winId();
|
||||
NSWindow *nswindow = [nsview window];
|
||||
|
||||
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"mainToolbar"];
|
||||
[toolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
|
||||
[toolbar setShowsBaselineSeparator:false];
|
||||
|
||||
__auto_type delegate = [[YACReaderToolbarDelegate alloc] init];
|
||||
delegate->mytoolbar = this;
|
||||
[toolbar setDelegate:delegate];
|
||||
|
||||
[nswindow setToolbar:toolbar];
|
||||
}
|
||||
|
||||
void YACReaderMacOSXToolbar::addStretch()
|
||||
{
|
||||
}
|
||||
|
||||
void YACReaderMacOSXToolbar::setHidden(bool hidden)
|
||||
{
|
||||
NSView *nsView = reinterpret_cast<NSView *>(this->winId());
|
||||
NSWindow *window = [nsView window];
|
||||
if (window && window.toolbar) {
|
||||
window.toolbar.visible = !hidden;
|
||||
}
|
||||
}
|
||||
void YACReaderMacOSXToolbar::show()
|
||||
{
|
||||
setHidden(false);
|
||||
}
|
||||
|
||||
void YACReaderMacOSXToolbar::hide()
|
||||
{
|
||||
setHidden(true);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user