Wireshark 4.7.2
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
ThemeManager Class Reference

#include <theme_manager.h>

Inheritance diagram for ThemeManager:

Public Types

enum class  ThemeMode { System , Dark , Light }
enum class  PreviewScheme { Default , PreferLight , PreferDark }
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 ,
  FilterBusy , FilterBusyText , FilterBookmark , FilterBookmarkCapture ,
  FilterBookmarkMatch , FilterClear , FilterApply , FilterHistory ,
  SyntaxKey , SyntaxString , SyntaxNumber , PaletteWindow ,
  PaletteBase , PaletteText , PaletteWindowText , PaletteAlternateBase ,
  PaletteMid , PaletteMidLight , PaletteHighlightedText , PaletteAccent ,
  HeaderGradientStart , HeaderGradientEnd , SectionHeader , SectionHeaderHover ,
  FieldBorder , Separator , MutedText , TextOnDark ,
  TextOnDarkMuted , UpdateGradientStart , UpdateGradientEnd , UpdateBorder ,
  UpdateText , UpdateTextHighlight , UpdateLink , UpdateLinkHover ,
  UpdateLinkPressed , UpdateButtonBg , UpdateButtonHover , UpdateButtonPressed ,
  UpdateButtonDisabledBg , UpdateButtonDisabledText , UpdateDismissHoverBg , UpdateDismissPressedBg ,
  HighlightColorOrange , HighlightColorGreen , TextOnSuccess , TextOnWarning ,
  TextOnError , TextOnInfo , 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, PreviewScheme scheme) const
bool validateThemeFile (const QString &filePath) 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.

Static Public Member Functions

static ThemeManagerinstance ()
static QString defaultThemeName ()
static QString resolveThemeName (const QString &name)
static void init (const QString &theme=QString())
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

◆ PreviewScheme

enum class ThemeManager::PreviewScheme
strong

Caller-visible appearance preference for previewTheme().

Mirrors the vocabulary of SystemThemeDetector::Scheme but is intentionally a separate type: previewTheme() is the only public surface that needs it, and reusing the detector enum would leak an otherwise-private dependency to the dialog.

Default — "no preference, resolve against the OS now". previewTheme() asks the system detector, which every back-end resolves to Light or Dark via its startup calibration. PreferLight — caller explicitly wants the light side. PreferDark — caller explicitly wants the dark side.

◆ 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.

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.

◆ defaultThemeName()

QString ThemeManager::defaultThemeName ( )
static

Internal name of the theme that ships as the out-of-the-box default for the current application flavor. Returns "stratoshark" for Stratoshark builds and "wireshark" for everything else (Wireshark, future flavors without their own theme directory).

Phrased as "wireshark unless stratoshark" so a custom build that strips one flavor's theme but keeps the other still has a working default — no separate ultimate-fallback knob needed. The flavor lookup goes through application_flavor_is_wireshark() (app/application_flavor.h) so the same shared Qt code picks the right default automatically without per-app #ifdef branches.

Use this anywhere you used to write the literal "default"init(), the recent_common fallback, the preferences picker's "currently selected" comparison, etc.

◆ 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 = QString())
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,
PreviewScheme scheme ) 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").
schemewhich light/dark side to render. Default defers to the OS detector so the preview tracks the live system preference; the Prefer* variants pin the side regardless of the live mode or OS.
Returns
token → resolved QColor for the requested mode.

◆ resolveThemeName()

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

Normalises a saved-theme-name to a current resource name.

Recognises the legacy sentinel "default" (written by older builds that knew only one theme directory) and empty strings as "use whatever this flavor considers default" — both get mapped to defaultThemeName(). Any other input is returned unchanged and assumed to be a real theme directory name; the caller is responsible for handling the case where that directory has been removed from the build.

◆ 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.

◆ validateThemeFile()

bool ThemeManager::validateThemeFile ( const QString & filePath) const

Runs ThemeParser on filePath and returns true if it produces a usable result. Reuses the live section / role caches so the validation criteria match what a real load would apply — required sections, required tokens, schema-typed values.

Intended for callers that have just written a theme file and need to confirm it parses before persisting a reference to it (e.g. the legacy color prefs migration, which avoids stamping recent.gui_theme_name with a name that points at a broken file).

Parameters
filePathAbsolute filesystem path to the candidate JSONC.
Returns
true if the file parses cleanly; false on any parse error.

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