Gnome::Gtk4::StyleContext
Description
Gnome::Gtk4::StyleContext stores styling information affecting a widget.
In order to construct the final style information, Gnome::Gtk4::StyleContext queries information from all attached GtkStyleProviders. Style providers can be either attached explicitly to the context through .add-provider()
, or to the display through .StyleContext.add-provider-for-display()
. The resulting style is a combination of all providers’ information in priority order.
For GTK widgets, any Gnome::Gtk4::StyleContext returned by .get-style-context() in class Gnome::Gtk4::Widget
will already have a Gnome::Gdk4::Display and RTL/LTR information set. The style context will also be updated automatically if any of these settings change on the widget.
Style Classes
Widgets can add style classes to their context, which can be used to associate different styles by class. The documentation for individual widgets lists which style classes it uses itself, and which style classes may be added by applications to affect their appearance.
Custom styling in UI libraries and applications
If you are developing a library with custom widgets that render differently than standard components, you may need to add a Gnome::Gtk4::R-StyleProvider yourself with the GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
priority, either a Gnome::Gtk4::CssProvider or a custom object implementing the Gnome::Gtk4::R-StyleProvider interface. This way themes may still attempt to style your UI elements in a different way if needed so.
If you are using custom styling on an applications, you probably want then to make your style information prevail to the theme’s, so you must use a Gnome::Gtk4::R-StyleProvider with the GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
priority, keep in mind that the user settings in XDG_CONFIG_HOME/gtk-4.0/gtk.css` will still take precedence over your changes, as it uses the GTK_STYLE_PROVIDER_PRIORITY_USER
priority.
Class initialization
Note: The native version of this class is deprecated in gtk4-lib() since version 4.10
new
:native-object
Create an object using a native object from elsewhere. See also Gnome::N::TopLevelSupportClass.
multi method new ( N-Object :$native-object! )
Methods
add-class
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Adds a style class to $context
, so later uses of the style context will make use of this new class for styling.
In the CSS file format, a Gnome::Gtk4::Entry defining a “search” class, would be matched by:
While any widget defining a “search” class would be matched by:
method add-class ( Str $class-name )
$class-name; class name to use in styling.
add-provider
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Adds a style provider to $context
, to be used in style construction.
Note that a style provider added by this function only affects the style of the widget to which $context
belongs. If you want to affect the style of all widgets, use .StyleContext.add-provider-for-display()
.
Note: If both priorities are the same, a Gnome::Gtk4::R-StyleProvider added through this function takes precedence over another added through .StyleContext.add-provider-for-display()
.
method add-provider ( N-Object() $provider, UInt() $priority )
$provider; a Gnome::Gtk4::R-StyleProvider.
$priority; the priority of the style provider. The lower it is, the earlier it will be used in the style construction. Typically this will be in the range between
GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
andGTK_STYLE_PROVIDER_PRIORITY_USER
.
get-border
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Gets the border for a given state as a Gnome::Gtk4::N-Border.
method get-border ( N-Object $border )
$border; return value for the border settings
get-color
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Gets the foreground color for a given state.
method get-color ( N-Object $color )
$color; return value for the foreground color
get-display
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Returns the Gnome::Gdk4::Display to which $context
is attached.
method get-display (--> N-Object )
Return value; a Gnome::Gdk4::Display..
get-margin
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Gets the margin for a given state as a Gnome::Gtk4::N-Border.
method get-margin ( N-Object $margin )
$margin; return value for the margin settings
get-padding
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Gets the padding for a given state as a Gnome::Gtk4::N-Border.
method get-padding ( N-Object $padding )
$padding; return value for the padding settings
get-scale
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Returns the scale used for assets.
method get-scale (--> Int )
Return value; the scale.
get-state
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Returns the state used for style matching.
This method should only be used to retrieve the bit field GtkStateFlags defined in Gnome::Gtk4::T-enums
to pass to Gnome::Gtk4::StyleContext methods, like .get-padding()
. If you need to retrieve the current state of a Gnome::Gtk4::Widget, use .get-state-flags() in class Gnome::Gtk4::Widget
.
method get-state (--> UInt )
Return value; the state flags.
has-class
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Returns True
if $context
currently has defined the given class name.
method has-class ( Str $class-name --> Bool )
$class-name; a class name.
Return value; True
if $context
has $class-name
defined.
lookup-color
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Looks up and resolves a color name in the $context
color map.
method lookup-color ( Str $color-name, N-Object $color --> Bool )
$color-name; color name to lookup.
$color; Return location for the looked up color
Return value; True
if $color-name
was found and resolved, False
otherwise.
remove-class
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Removes $class-name
from $context
.
method remove-class ( Str $class-name )
$class-name; class name to remove.
remove-provider
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Removes $provider
from the style providers list in $context
.
method remove-provider ( N-Object() $provider )
$provider; a Gnome::Gtk4::R-StyleProvider.
restore
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Restores $context
state to a previous stage.
See .save()
.
method restore ( )
save
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Saves the $context
state.
This allows temporary modifications done through .add-class()
, .remove-class()
, .set-state()
to be quickly reverted in one go through .restore()
.
The matching call to .restore()
must be done before GTK returns to the main loop.
method save ( )
set-display
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Attaches $context
to the given display.
The display is used to add style information from “global” style providers, such as the display's Gnome::Gtk4::Settings instance.
If you are using a Gnome::Gtk4::StyleContext returned from .get-style-context() in class Gnome::Gtk4::Widget
, you do not need to call this yourself.
method set-display ( N-Object() $display )
$display; a Gnome::Gdk4::Display.
set-scale
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Sets the scale to use when getting image assets for the style.
method set-scale ( Int() $scale )
$scale; scale.
set-state
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Sets the state to be used for style matching.
method set-state ( UInt $flags )
$flags; state to represent.
to-string
Note: The native version of this routine is deprecated in gtk4-lib() since version 4.10
Converts the style context into a string representation.
The string representation always includes information about the name, state, id, visibility and style classes of the CSS node that is backing $context
. Depending on the flags, more information may be included.
This function is intended for testing and debugging of the CSS implementation in GTK. There are no guarantees about the format of the returned string, it may change.
method to-string ( UInt $flags --> Str )
$flags; Flags that determine what to print.
Return value; a newly allocated string representing $context
.
Functions
add-provider-for-display
Adds a global style provider to $display
, which will be used in style construction for all GtkStyleContexts under $display
.
GTK uses this to make styling information from Gnome::Gtk4::Settings available.
Note: If both priorities are the same, A Gnome::Gtk4::R-StyleProvider added through .add-provider()
takes precedence over another added through this function.
method add-provider-for-display ( N-Object() $display, N-Object() $provider, UInt() $priority )
$display; a Gnome::Gdk4::Display.
$provider; a Gnome::Gtk4::R-StyleProvider.
$priority; the priority of the style provider. The lower it is, the earlier it will be used in the style construction. Typically this will be in the range between
GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
andGTK_STYLE_PROVIDER_PRIORITY_USER
.
remove-provider-for-display
Removes $provider
from the global style providers list in $display
.
method remove-provider-for-display ( N-Object() $display, N-Object() $provider )
$display; a Gnome::Gdk4::Display.
$provider; a Gnome::Gtk4::R-StyleProvider.