From e687d0c4f6e3c4ea4e81976b9ea03172f7afa117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Thu, 31 Dec 2020 18:51:46 +0100 Subject: [PATCH] Render statusbar natively on macOS Instead of trying to mimic native statusbar background rendering in our own code, use NSWindow setContentBorderThickness:forEdge: to extend the border that is drawn by the system. This is the canonical way of doing statusbars ("bottom bar") in AppKit. The text is still drawn generically, but that's a much smaller issue than trying to match the border gradient. --- include/wx/osx/cocoa/private.h | 2 ++ include/wx/osx/core/private.h | 2 ++ include/wx/osx/frame.h | 2 ++ include/wx/osx/statusbr.h | 4 +-- src/osx/carbon/frame.cpp | 6 ++++ src/osx/carbon/statbrma.cpp | 53 +++++----------------------------- src/osx/cocoa/nonownedwnd.mm | 6 ++++ 7 files changed, 27 insertions(+), 48 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 3e6dc20a6e..7c13d5897c 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -329,6 +329,8 @@ public : virtual void SetRepresentedFilename(const wxString& filename) wxOVERRIDE; + virtual void SetBottomBorderThickness(int thickness) wxOVERRIDE; + wxNonOwnedWindow* GetWXPeer() { return m_wxPeer; } CGWindowLevel GetWindowLevel() const wxOVERRIDE { return m_macWindowLevel; } diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index a25b3e104e..bc4c852bce 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -984,6 +984,8 @@ public : virtual void SetRepresentedFilename(const wxString& WXUNUSED(filename)) { } + virtual void SetBottomBorderThickness(int WXUNUSED(thickness)) { } + #if wxOSX_USE_IPHONE virtual CGFloat GetWindowLevel() const { return 0.0; } #else diff --git a/include/wx/osx/frame.h b/include/wx/osx/frame.h index 155e7ccf3a..9fef19f146 100644 --- a/include/wx/osx/frame.h +++ b/include/wx/osx/frame.h @@ -71,6 +71,8 @@ public: long style = wxSTB_DEFAULT_STYLE, wxWindowID id = 0, const wxString& name = wxASCII_STR(wxStatusLineNameStr)) wxOVERRIDE; + + virtual void SetStatusBar(wxStatusBar *statbar) wxOVERRIDE; #endif // wxUSE_STATUSBAR void PositionBars(); diff --git a/include/wx/osx/statusbr.h b/include/wx/osx/statusbr.h index 9b6e84e56a..ea931f998f 100644 --- a/include/wx/osx/statusbr.h +++ b/include/wx/osx/statusbr.h @@ -38,9 +38,7 @@ protected: virtual void InitColours() wxOVERRIDE; private: - wxColour m_textActive, m_textInactive, - m_bgActiveFrom, m_bgActiveTo, - m_borderActive, m_borderInactive; + wxColour m_textActive, m_textInactive; wxDECLARE_DYNAMIC_CLASS(wxStatusBarMac); wxDECLARE_EVENT_TABLE(); diff --git a/src/osx/carbon/frame.cpp b/src/osx/carbon/frame.cpp index 25ee3616a2..0ca60a20b3 100644 --- a/src/osx/carbon/frame.cpp +++ b/src/osx/carbon/frame.cpp @@ -112,6 +112,12 @@ wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id, return statusBar; } +void wxFrame::SetStatusBar(wxStatusBar *statbar) +{ + wxFrameBase::SetStatusBar(statbar); + m_nowpeer->SetBottomBorderThickness(statbar ? WX_MAC_STATUSBAR_HEIGHT : 0); +} + void wxFrame::PositionStatusBar() { if (m_frameStatusBar && m_frameStatusBar->IsShown() ) diff --git a/src/osx/carbon/statbrma.cpp b/src/osx/carbon/statbrma.cpp index 4201551b50..29e7b9fe76 100644 --- a/src/osx/carbon/statbrma.cpp +++ b/src/osx/carbon/statbrma.cpp @@ -78,42 +78,22 @@ void wxStatusBarMac::InitColours() { if ( WX_IS_MACOS_AVAILABLE(10, 14) ) { - // FIXME: None of this is correct and is only very loose - // approximation. 10.14's dark mode uses dynamic colors that - // use desktop tinting. The only correct way to render the - // statusbar is to use windowBackgroundColor in a NSBox. - wxColour bg = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE); - m_textActive = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT); - m_textInactive = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT); - if ( wxSystemSettings::GetAppearance().IsDark() ) { - // dark mode appearance - m_textActive = wxColour(0xB0, 0xB0, 0xB0); - m_bgActiveFrom = wxColour(0x32, 0x32, 0x34); - m_bgActiveTo = wxColour(0x29, 0x29, 0x2A); - m_borderActive = wxColour(0x00, 0x00, 0x00); - m_borderInactive = wxColour(0x00, 0x00, 0x00); + m_textActive = wxColour(0xA9, 0xA9, 0xA9); + m_textInactive = wxColour(0x67, 0x67, 0x67); } else { - m_bgActiveFrom = wxColour(0xE9, 0xE7, 0xEA); - m_bgActiveTo = wxColour(0xCD, 0xCB, 0xCE); - m_borderActive = wxColour(0xBA, 0xB8, 0xBB); - m_borderInactive = wxColour(0xC3, 0xC3, 0xC3); + m_textActive = wxColour(0x4B, 0x4B, 0x4B); + m_textInactive = wxColour(0xB1, 0xB1, 0xB1); } - SetBackgroundColour(bg); // inactive bg } - else + else // 10.10 Yosemite to 10.13: { - // 10.10 Yosemite to 10.13 : + m_textActive = wxColour(0x40, 0x40, 0x40); m_textInactive = wxColour(0x4B, 0x4B, 0x4B); - m_bgActiveFrom = wxColour(0xE9, 0xE7, 0xEA); - m_bgActiveTo = wxColour(0xCD, 0xCB, 0xCE); - m_borderActive = wxColour(0xBA, 0xB8, 0xBB); - m_borderInactive = wxColour(0xC3, 0xC3, 0xC3); - SetBackgroundColour(wxColour(0xF4, 0xF4, 0xF4)); // inactive bg } } @@ -165,10 +145,6 @@ void wxStatusBarMac::DoUpdateStatusText(int number) void wxStatusBarMac::OnPaint(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc(this); - dc.Clear(); - - int w, h; - GetSize( &w, &h ); // Notice that wxOSXGetKeyWindow (aka [NSApp keyWindow] used below is // subtly different from IsActive() (aka [NSWindow iskeyWindow]): the @@ -188,22 +164,9 @@ void wxStatusBarMac::OnPaint(wxPaintEvent& WXUNUSED(event)) break; } - if ( tlw == keyWindow ) - { - dc.GradientFillLinear(dc.GetSize(), m_bgActiveFrom, m_bgActiveTo, wxBOTTOM); + // Don't paint any background, that's handled by the OS. Only draw text: - // Finder statusbar border color - dc.SetPen(wxPen(m_borderActive, 2, wxPENSTYLE_SOLID)); - dc.SetTextForeground(m_textActive); - } - else - { - // Finder statusbar border color - dc.SetPen(wxPen(m_borderInactive, 2, wxPENSTYLE_SOLID)); - dc.SetTextForeground(m_textInactive); - } - - dc.DrawLine(0, 0, w, 0); + dc.SetTextForeground(tlw == keyWindow ? m_textActive : m_textInactive); if ( GetFont().IsOk() ) dc.SetFont(GetFont()); diff --git a/src/osx/cocoa/nonownedwnd.mm b/src/osx/cocoa/nonownedwnd.mm index 2068807142..d9702e6682 100644 --- a/src/osx/cocoa/nonownedwnd.mm +++ b/src/osx/cocoa/nonownedwnd.mm @@ -1288,6 +1288,12 @@ void wxNonOwnedWindowCocoaImpl::SetRepresentedFilename(const wxString& filename) [m_macWindow setRepresentedFilename:wxCFStringRef(filename).AsNSString()]; } +void wxNonOwnedWindowCocoaImpl::SetBottomBorderThickness(int thickness) +{ + [m_macWindow setAutorecalculatesContentBorderThickness:(thickness ? NO : YES) forEdge:NSMinYEdge]; + [m_macWindow setContentBorderThickness:thickness forEdge:NSMinYEdge]; +} + void wxNonOwnedWindowCocoaImpl::RestoreWindowLevel() { if ( [m_macWindow level] != m_macWindowLevel )