Support dark mode in AuiBar buttons for wx 3.0

This commit is contained in:
Jon Evans 2021-03-01 16:53:20 -05:00
parent 7760570d76
commit 93578517e4
4 changed files with 173 additions and 1 deletions

View File

@ -210,6 +210,7 @@ set( COMMON_DLG_SRCS
set( COMMON_WIDGET_SRCS set( COMMON_WIDGET_SRCS
widgets/app_progress_dialog.cpp widgets/app_progress_dialog.cpp
widgets/aui_art_provider.cpp
widgets/bitmap_button.cpp widgets/bitmap_button.cpp
widgets/bitmap_toggle.cpp widgets/bitmap_toggle.cpp
widgets/button_row_panel.cpp widgets/button_row_panel.cpp

View File

@ -35,6 +35,7 @@
#include <tool/tool_event.h> #include <tool/tool_event.h>
#include <tool/tool_interactive.h> #include <tool/tool_interactive.h>
#include <tool/tool_manager.h> #include <tool/tool_manager.h>
#include <widgets/aui_art_provider.h>
#include <widgets/bitmap_button.h> #include <widgets/bitmap_button.h>
#include <wx/popupwin.h> #include <wx/popupwin.h>
#include <wx/renderer.h> #include <wx/renderer.h>
@ -169,6 +170,12 @@ ACTION_TOOLBAR::ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id, const wxP
{ {
m_paletteTimer = new wxTimer( this ); m_paletteTimer = new wxTimer( this );
#if !wxCHECK_VERSION( 3, 1, 0 )
// Custom art provider makes dark mode work on wx < 3.1
AUI_ART_PROVIDER* newArt = new AUI_ART_PROVIDER();
SetArtProvider( newArt );
#endif
Connect( wxEVT_COMMAND_TOOL_CLICKED, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolEvent ), Connect( wxEVT_COMMAND_TOOL_CLICKED, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolEvent ),
NULL, this ); NULL, this );
Connect( wxEVT_AUITOOLBAR_RIGHT_CLICK, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolRightClick ), Connect( wxEVT_AUITOOLBAR_RIGHT_CLICK, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolRightClick ),

View File

@ -0,0 +1,123 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <wx/aui/aui.h>
#include <wx/bitmap.h>
#include <wx/dc.h>
#include <wx/settings.h>
#include <kiplatform/ui.h>
#include <widgets/aui_art_provider.h>
/**
* wxAuiDefaultToolBarArt::DrawButton except with dark-mode awareness based on BITMAP_BUTTON
* Unfortunately, wx 3.0 does not provide any hooks that would make it possible to do this in a way
* other than just copy/pasting the upstream implementation and modifying it...
*/
void AUI_ART_PROVIDER::DrawButton( wxDC& aDc, wxWindow* aWindow, const wxAuiToolBarItem& aItem,
const wxRect& aRect )
{
bool darkMode = KIPLATFORM::UI::IsDarkTheme();
int textWidth = 0;
int textHeight = 0;
if( m_flags & wxAUI_TB_TEXT )
{
aDc.SetFont( m_font );
int tx, ty;
aDc.GetTextExtent( wxT( "ABCDHgj" ), &tx, &textHeight );
textWidth = 0;
aDc.GetTextExtent( aItem.GetLabel(), &textWidth, &ty );
}
int bmpX = 0, bmpY = 0;
int textX = 0, textY = 0;
if( m_textOrientation == wxAUI_TBTOOL_TEXT_BOTTOM )
{
bmpX = aRect.x + ( aRect.width / 2 ) - ( aItem.GetBitmap().GetWidth() / 2 );
bmpY = aRect.y + ( ( aRect.height - textHeight ) / 2 ) - ( aItem.GetBitmap().GetHeight() / 2 );
textX = aRect.x + ( aRect.width / 2 ) - ( textWidth / 2 ) + 1;
textY = aRect.y + aRect.height - textHeight - 1;
}
else if( m_textOrientation == wxAUI_TBTOOL_TEXT_RIGHT )
{
bmpX = aRect.x + 3;
bmpY = aRect.y + ( aRect.height / 2 ) - ( aItem.GetBitmap().GetHeight() / 2 );
textX = bmpX + 3 + aItem.GetBitmap().GetWidth();
textY = aRect.y + ( aRect.height / 2 ) - ( textHeight / 2 );
}
if( !( aItem.GetState() & wxAUI_BUTTON_STATE_DISABLED ) )
{
if( aItem.GetState() & wxAUI_BUTTON_STATE_PRESSED )
{
aDc.SetPen( wxPen( m_highlightColour ) );
aDc.SetBrush( wxBrush( m_highlightColour.ChangeLightness( darkMode ? 20 : 150 ) ) );
aDc.DrawRectangle( aRect );
}
else if( ( aItem.GetState() & wxAUI_BUTTON_STATE_HOVER ) || aItem.IsSticky() )
{
aDc.SetPen( wxPen( m_highlightColour ) );
aDc.SetBrush( wxBrush( m_highlightColour.ChangeLightness( darkMode ? 40 : 170 ) ) );
// draw an even lighter background for checked aItem hovers (since
// the hover background is the same color as the check background)
if( aItem.GetState() & wxAUI_BUTTON_STATE_CHECKED )
aDc.SetBrush( wxBrush( m_highlightColour.ChangeLightness( darkMode ? 50 : 180 ) ) );
aDc.DrawRectangle( aRect );
}
else if( aItem.GetState() & wxAUI_BUTTON_STATE_CHECKED )
{
// it's important to put this code in an else statement after the
// hover, otherwise hovers won't draw properly for checked aItems
aDc.SetPen( wxPen( m_highlightColour ) );
aDc.SetBrush( wxBrush( m_highlightColour.ChangeLightness( darkMode ? 40 : 170 ) ) );
aDc.DrawRectangle( aRect );
}
}
wxBitmap bmp;
if( aItem.GetState() & wxAUI_BUTTON_STATE_DISABLED )
bmp = aItem.GetDisabledBitmap();
else
bmp = aItem.GetBitmap();
if( bmp.IsOk() )
aDc.DrawBitmap( bmp, bmpX, bmpY, true );
// set the aItem's text color based on if it is disabled
aDc.SetTextForeground( *wxBLACK );
if( aItem.GetState() & wxAUI_BUTTON_STATE_DISABLED )
aDc.SetTextForeground( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
if( ( m_flags & wxAUI_TB_TEXT ) && !aItem.GetLabel().empty() )
{
aDc.DrawText( aItem.GetLabel(), textX, textY );
}
}

View File

@ -0,0 +1,41 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KICAD_AUI_ART_PROVIDER_H
#define KICAD_AUI_ART_PROVIDER_H
#include <wx/aui/auibar.h>
/**
* wxWidgets 3.1 has support for dark mode detection, but 3.0 doesn't.
* The main purpose of this replacement art provider is to backport that functionality
* so that it is available in Linux systems that will likely be stuck on 3.0 for a while.
*/
class AUI_ART_PROVIDER : public wxAuiDefaultToolBarArt
{
public:
AUI_ART_PROVIDER() : wxAuiDefaultToolBarArt() {}
virtual ~AUI_ART_PROVIDER() = default;
void DrawButton( wxDC& aDc, wxWindow* aWindow, const wxAuiToolBarItem& aItem,
const wxRect& aRect ) override;
};
#endif // KICAD_AUI_ART_PROVIDER_H