Skip to content

Commit

Permalink
Support auto-night mode on macOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jul 23, 2020
1 parent 8c4e821 commit 25d6943
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Telegram/SourceFiles/platform/mac/main_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ - (void) activeSpaceDidChange:(NSNotification *)aNotification {

- (void) darkModeChanged:(NSNotification *)aNotification {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Core::App().settings().setSystemDarkMode(Platform::IsDarkMode());
Core::App().domain().notifyUnreadBadgeChanged();
});
}
Expand Down Expand Up @@ -650,7 +651,7 @@ void _placeCounter(QImage &img, int size, int count, style::color bg, style::col
_private->setWindowBadge(string);

if (trayIcon) {
bool dm = objc_darkMode();
bool dm = Platform::IsDarkMenuBar();
auto &bg = (muted ? st::trayCounterBgMute : st::trayCounterBg);
QIcon icon;
QImage img(psTrayIcon(dm)), imgsel(psTrayIcon(true));
Expand Down
4 changes: 1 addition & 3 deletions Telegram/SourceFiles/platform/mac/specific_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ namespace Platform {

void RemoveQuarantine(const QString &path);

inline std::optional<bool> IsDarkMode() {
return std::nullopt;
}
[[nodiscard]] bool IsDarkMenuBar();

inline void FallbackFontConfigCheckBegin() {
}
Expand Down
20 changes: 20 additions & 0 deletions Telegram/SourceFiles/platform/mac/specific_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mainwindow.h"
#include "history/history_location_manager.h"
#include "base/platform/mac/base_utilities_mac.h"
#include "base/platform/base_platform_info.h"

#include <QtGui/QDesktopServices>
#include <QtWidgets/QApplication>
Expand Down Expand Up @@ -137,6 +138,25 @@ void RemoveQuarantine(const QString &path) {
removexattr(local.data(), kQuarantineAttribute, 0);
}

bool IsDarkMenuBar() {
bool result = false;
@autoreleasepool {

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:Q2NSString(strStyleOfInterface())];
BOOL darkModeOn = (style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"]);
result = darkModeOn ? true : false;

}
return result;
}

std::optional<bool> IsDarkMode() {
return IsMac10_14OrGreater()
? std::make_optional(IsDarkMenuBar())
: std::nullopt;
}

void RegisterCustomScheme(bool force) {
#ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
OSStatus result = LSSetDefaultHandlerForURLScheme(CFSTR("tg"), (CFStringRef)[[NSBundle mainBundle] bundleIdentifier]);
Expand Down
2 changes: 0 additions & 2 deletions Telegram/SourceFiles/platform/mac/specific_mac_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ For license and copyright information please follow this link:
// e is NSEvent*
bool objc_handleMediaKeyEvent(void *e);

bool objc_darkMode();

void objc_debugShowAlert(const QString &str);
void objc_outputDebugString(const QString &str);

Expand Down
13 changes: 0 additions & 13 deletions Telegram/SourceFiles/platform/mac/specific_mac_p.mm
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,6 @@ void SetApplicationIcon(const QIcon &icon) {

} // namespace Platform

bool objc_darkMode() {
bool result = false;
@autoreleasepool {

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:Q2NSString(strStyleOfInterface())];
BOOL darkModeOn = (style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"]);
result = darkModeOn ? true : false;

}
return result;
}

bool objc_handleMediaKeyEvent(void *ev) {
auto e = reinterpret_cast<NSEvent*>(ev);

Expand Down

0 comments on commit 25d6943

Please sign in to comment.