Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
Public Types | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
ThemeManager Class Reference

#include <theme_manager.h>

Inheritance diagram for ThemeManager:

Public Types

enum class  ThemeMode { System , Dark , Light }
 
enum  ThemeToken {
  BrandPrimary , BrandDeep , AccentSuccess , AccentWarning ,
  AccentError , AccentInfo , ExpertComment , ExpertChat ,
  ExpertNote , ExpertWarn , ExpertError , ExpertForeground ,
  PacketsSelection , PacketsSelectionText , PacketsInactive , PacketsInactiveText ,
  PacketsMarked , PacketsMarkedText , PacketsIgnored , PacketsIgnoredText ,
  PacketsHidden , ConversationClient , ConversationClientText , ConversationServer ,
  ConversationServerText , FilterValid , FilterInvalid , FilterDeprecated ,
  SyntaxKey , SyntaxString , SyntaxNumber , PaletteWindow ,
  PaletteBase , PaletteText , PaletteWindowText , PaletteAlternateBase ,
  PaletteMid , PaletteMidLight , PaletteAccent , HeaderGradientStart ,
  HeaderGradientEnd , SectionHeader , SectionHeaderHover , TextOnDark ,
  TextOnDarkMuted , UpdateGradientStart , UpdateGradientEnd , UpdateBorder ,
  UpdateText , UpdateTextHighlight , UpdateLink , UpdateLinkHover ,
  UpdateLinkPressed , UpdateButtonBg , UpdateButtonHover , UpdateButtonPressed ,
  UpdateButtonDisabledBg , UpdateButtonDisabledText , UpdateDismissHoverBg , UpdateDismissPressedBg ,
  HighlightColorOrange , HighlightColorGreen , NoRole
}
 

Signals

void themeChanged ()
 

Public Member Functions

void cleanup ()
 
ThemeInfo info () const
 
QColor color (ThemeToken role) const
 
bool colorIsAvailable (ThemeToken role) const
 
QHash< ThemeToken, QColor > previewTheme (const QString &internalName, bool wantDark) const
 
QString loadStyleSheet (const QString &name) const
 
bool isDarkMode () const
 
ThemeMode mode () const
 
void setMode (ThemeMode mode)
 
QColor graphColor (int idx) const
 Returns the graph color for the given index, cycling through available graph colors if necessary.
 
QColor graphDefaultColor () const
 
qsizetype graphColorCount () const
 Returns the number of graph colors defined in the theme.
 
QFont regularFont () const
 
QFont monospaceFont () const
 

Static Public Member Functions

static ThemeManagerinstance ()
 
static void init (const QString &theme=QStringLiteral("default"))
 
static QList< ThemeInfoavailableThemes ()
 
static QString styleSheet (const QString &name)
 
static void setValidationState (QWidget *w, const QString &state)
 
static bool isDark ()
 

Protected Member Functions

 ThemeManager (QObject *parent=nullptr)
 

Detailed Description

Gateway for Wireshark's theme system.

Owns the process-wide theme state (current theme, mode, token map, font choices, the platform scheme detector) and fronts the public API. Non-trivial work is delegated to helpers under ui/qt/utils/themes/, split by responsibility:

themes/theme_parser.{h,cpp} — reads theme.jsonc themes/theme_token_handler.{h,cpp} — derives Header* / Update* / … from the brand + accent inputs themes/theme_palette_builder.{h,cpp} — constructs the QPalette and pushes it via setPalette() themes/theme_stylesheet_loader.{h,cpp} — reads :/stylesheets/ and resolves wstheme(...) tokens themes/color_math.{h,cpp} — pure color-math statics (mix/darken/lighten/contrast/…) themes/system_theme_detector*.{h,cpp,mm} — platform-native OS light/dark detection.

A theme file (resources/themes/<name>/theme.jsonc) declares six mandatory colors — brand.primary, brand.deep, and four accent.* values. Every other token the UI consumes (Header*, Section*, Update*, Packets*, Conversation*, Expert*, TextOnDark*, …) is derived from those inputs by ThemeTokenHandler at load time.

Stylesheets reference tokens via wstheme(TokenName). The loader rewrites each occurrence to the resolved #rrggbb hex value at the widget's setStyleSheet() call site.

Member Enumeration Documentation

◆ ThemeMode

enum class ThemeManager::ThemeMode
strong

Selects how the app decides between the light and dark sides of the current theme.

System — follow the OS preference (default). Flips live when the user changes the OS appearance. Dark — always return the dark side, regardless of the OS. Light — always return the light side, regardless of the OS.

Constructor & Destructor Documentation

◆ ThemeManager()

ThemeManager::ThemeManager ( QObject *  parent = nullptr)
explicitprotected

CONVENIENCE PRE-CACHING

These pre-caches make it cheap to answer "which theme.jsonc section does this token belong to?" and "which QPalette role does this palette-prefixed token map to?" at runtime. The parser and the palette builder both consult them by string name during a load.

Adding a new token only requires:

  • Append the new ThemeToken to the enum in the header.
  • If a new top-level section is introduced, list it in sections_ below with its required/optional flag.

Everything else (section-to-token grouping, palette-role lookup) falls out of the Q_ENUM metaobject scanning in the loops below.

At this point we have a valid monospace font from the system, BUT it was NOT read from the user preferences, therefore we are going to store it in the prefs just in case, but ONLY if it was not. This WILL store the font name everytime the theme changes as well, but that is acceptable for now. The reason why is, that this way we can change the default font of the application via the default theme. This needs to be done AFTER the app is initialized.

Member Function Documentation

◆ availableThemes()

QList< ThemeInfo > ThemeManager::availableThemes ( )
static

Enumerates every theme bundled under :/themes/. For each one the JSONC file is parsed to extract its metadata (display name, author, description, internal name). Intended for populating a theme picker in the preferences dialog.

Themes whose JSONC fails to parse are skipped silently (a warning is logged by ThemeParser). The returned list is not sorted — callers that want alphabetical order should sort by display name themselves.

◆ color()

QColor ThemeManager::color ( ThemeToken  role) const

Returns the resolved color value for a given ThemeToken in the current light/dark mode.

Parameters
roleThe ThemeToken enum value, e.g. ThemeManager::HeaderGradientStart
Returns
The color defined for the role in the current mode, or an invalid QColor if the role is not defined.

◆ graphColor()

QColor ThemeManager::graphColor ( int  idx) const

Returns the graph color for the given index, cycling through available graph colors if necessary.

NOTE: if no graph colors are defined, this will return an invalid QColor. NOTE: the index will be modulo'd by the number of available graph colors, so callers can safely pass any index and it will cycle through the defined colors.

Parameters
idxthe index for the graph color.
Returns
QColor

◆ graphColorCount()

qsizetype ThemeManager::graphColorCount ( ) const

Returns the number of graph colors defined in the theme.

Returns
int > 0 if graph colors are defined, 0 if not.

◆ info()

ThemeInfo ThemeManager::info ( ) const

Returns metadata about the currently loaded theme.

◆ init()

void ThemeManager::init ( const QString &  theme = QStringLiteral("default"))
static

Initializes the ThemeManager by loading the default theme from the built-in resources. Call once during application startup after QApplication is constructed.

◆ isDark()

bool ThemeManager::isDark ( )
static

Convenience shortcut for ThemeManager::instance()->isDarkMode().

The color-manipulation helpers (mix, darken, contrastingText, …) previously declared here have moved to ColorMath in ui/qt/utils/themes/color_math.h.

◆ isDarkMode()

bool ThemeManager::isDarkMode ( ) const

Returns true if the current appearance is dark.

Resolves in this order:

  • ThemeMode::Dark → true
  • ThemeMode::Light → false
  • ThemeMode::System → value reported by the platform-native SystemThemeDetector. If the detector cannot determine the OS preference, falls back to Qt's own QStyleHints::colorScheme() (Qt ≥ 6.5), and finally to a QPalette window/text luminance comparison.

This is the single authority for the "is the app dark?" question. The static isDark() convenience delegates here.

Named *Mode because the class also has a static isDark(QColor) luminance helper; keeping the instance method's name distinct avoids overload confusion at call sites.

◆ loadStyleSheet()

QString ThemeManager::loadStyleSheet ( const QString &  name) const

Loads a QSS stylesheet from the built-in :/stylesheets/ resource tree and replaces all wstheme(TokenName) references with their resolved color values. Thin delegator; see ThemeStyleSheetLoader::load() for the full semantics.

The stylesheet is always looked up under :/stylesheets/, with a ".qss" extension automatically appended. The name is sanitized to prevent path traversal: it must consist of alphanumerics, underscores, hyphens, dots and forward slashes only, must not start with '/' or '.', and must not contain "..". Invalid or missing stylesheets fail silently and return an empty string.

Parameters
nameLogical stylesheet name relative to :/stylesheets/, without extension, e.g. "widgets/learn-card".
Returns
The processed stylesheet string, or an empty string if the name is invalid or the file could not be read.

◆ mode()

ThemeManager::ThemeMode ThemeManager::mode ( ) const

Returns the currently active mode. Reflects whichever of gui.color_scheme the user picked at app start plus any live setMode() calls (setMode is currently only invoked by the preferences re-apply path).

◆ previewTheme()

QHash< ThemeManager::ThemeToken, QColor > ThemeManager::previewTheme ( const QString &  internalName,
bool  wantDark 
) const

Resolves the color table for a named theme in the requested light/dark mode without altering live application state.

Runs the same parser + derive pipeline as loadTheme(), but on a stack-local copy of the parsed result — the active theme, application palette, and stylesheet are untouched. Useful for offering a "preview" of a theme inside the preferences dialog: the dropdown can drive a swatch / mockup widget with no risk of leaking partial state into the running app.

Returns an empty hash if the theme cannot be found or fails to parse — callers should treat that as "fall back to the live ThemeManager's colors".

Parameters
internalNameinternal theme name (e.g. "default").
wantDarktrue for the dark side, false for light.
Returns
token → resolved QColor for the requested mode.

◆ setMode()

void ThemeManager::setMode ( ThemeMode  mode)

Switches the mode. If the new mode differs from the current one, re-runs the theme's derive/apply path and emits themeChanged so cached stylesheets reload. Does NOT persist to preferences; that is the caller's responsibility.

◆ setValidationState()

void ThemeManager::setValidationState ( QWidget *  w,
const QString &  state 
)
static

Marks a widget as being in a named validation state for QSS rule matching. Sets the dynamic property wsValidation on w and re-polishes the widget so selectors in ui/stylesheets/application.qss (e.g. QLineEdit[wsValidation="invalid"]) take effect immediately.

Pass an empty QString to clear the state and fall back to the default palette. Conventional values are "valid", "invalid", and "deprecated" — see application.qss.

◆ styleSheet()

QString ThemeManager::styleSheet ( const QString &  name)
static

Convenience static accessor equivalent to ThemeManager::instance()->loadStyleSheet(name)

Allows callers to load a themed stylesheet without first resolving the singleton, e.g. widget->setStyleSheet(ThemeManager::styleSheet("widgets/learn-card"));

Parameters
nameLogical stylesheet name relative to :/stylesheets/, without extension, e.g. "widgets/learn-card".
Returns
The processed stylesheet string, or an empty string if the name is invalid or the file could not be read.

The documentation for this class was generated from the following files: