Class: Gtk::LevelBar

Inherits:
Widget
  • Object
show all
Extended by:
GLib::Deprecatable
Defined in:
lib/gtk4/level-bar.rb,
lib/gtk4/deprecated.rb

Overview

GtkLevelBar is a widget that can be used as a level indicator.

Typical use cases are displaying the strength of a password, or showing the charge level of a battery.

An example GtkLevelBar

Use [methodGtk.LevelBar.set_value] to set the current value, and [methodGtk.LevelBar.add_offset_value] to set the value offsets at which the bar will be considered in a different state. GTK will add a few offsets by default on the level bar: %GTK_LEVEL_BAR_OFFSET_LOW, %GTK_LEVEL_BAR_OFFSET_HIGH and %GTK_LEVEL_BAR_OFFSET_FULL, with values 0.25, 0.75 and 1.0 respectively.

Note that it is your responsibility to update preexisting offsets when changing the minimum or maximum value. GTK will simply clamp them to the new range.

Adding a custom offset on the bar

static GtkWidget *
create_level_bar (void)
{
  GtkWidget *widget;
  GtkLevelBar *bar;

  widget = gtk_level_bar_new ();
  bar = GTK_LEVEL_BAR (widget);

  // This changes the value of the default low offset

  gtk_level_bar_add_offset_value (bar,
                                  GTK_LEVEL_BAR_OFFSET_LOW,
                                  0.10);

  // This adds a new offset to the bar; the application will
  // be able to change its color CSS like this:
  //
  // levelbar block.my-offset {
  //   background-color: magenta;
  //   border-style: solid;
  //   border-color: black;
  //   border-width: 1px;
  // }

  gtk_level_bar_add_offset_value (bar, "my-offset", 0.60);

  return widget;
}

The default interval of values is between zero and one, but it’s possible to modify the interval using [methodGtk.LevelBar.set_min_value] and [methodGtk.LevelBar.set_max_value]. The value will be always drawn in proportion to the admissible interval, i.e. a value of 15 with a specified interval between 10 and 20 is equivalent to a value of 0.5 with an interval between 0 and 1. When %GTK_LEVEL_BAR_MODE_DISCRETE is used, the bar level is rendered as a finite number of separated blocks instead of a single one. The number of blocks that will be rendered is equal to the number of units specified by the admissible interval.

For instance, to build a bar rendered with five blocks, it’s sufficient to set the minimum value to 0 and the maximum value to 5 after changing the indicator mode to discrete.

GtkLevelBar as GtkBuildable

The GtkLevelBar implementation of the GtkBuildable interface supports a custom <offsets> element, which can contain any number of <offset> elements, each of which must have "name" and "value" attributes.

CSS nodes

levelbar[.discrete]
╰── trough
    ├── block.filled.level-name
    ┊
    ├── block.empty
    ┊

GtkLevelBar has a main CSS node with name levelbar and one of the style classes .discrete or .continuous and a subnode with name trough. Below the trough node are a number of nodes with name block and style class .filled or .empty. In continuous mode, there is exactly one node of each, in discrete mode, the number of filled and unfilled nodes corresponds to blocks that are drawn. The block.filled nodes also get a style class .level-name corresponding to the level for the current value.

In horizontal orientation, the nodes are always arranged from left to right, regardless of text direction.

Accessibility

GtkLevelBar uses the %GTK_ACCESSIBLE_ROLE_METER role.

Instance Method Summary collapse

Methods inherited from Widget

#accessible_role, #accessible_role=, #action_set_enabled, #activate, #activate_action, #activate_action_variant, #activate_default, #activate_signal, #activate_signal=, #activate_signal_from_name=, #add_binding, #add_binding_action, #add_binding_signal, #add_controller, #add_css_class, #add_mnemonic_label, #add_shortcut, #add_tick_callback, #allocate, #allocated_baseline, #allocated_height, #allocated_width, #baseline, #bind_template_callback_full, #bind_template_child, #bind_template_child_full, #can_focus, #can_focus=, #can_focus?, #can_target, #can_target=, #can_target?, #child_focus, #child_visible, #child_visible=, #children, #clipboard, #compute_bounds, #compute_expand, #compute_point, #compute_transform, #contains, #create_pango_context, #create_pango_layout, #css_classes, #css_classes=, #css_name, #css_name=, #cursor, #cursor=, #cursor_from_name=, default_direction, default_direction=, #direction, #direction=, #display, #dispose_template, #drag_check_threshold, #error_bell, #first_child, #focus_child, #focus_child=, #focus_on_click, #focus_on_click=, #focus_on_click?, #focusable, #focusable=, #focusable?, #font_map, #font_map=, #font_options, #font_options=, #frame_clock, #get_allocation, #get_ancestor, #get_color, #get_preferred_size, #get_size, #get_size_request, #get_template_child, #grab_focus, #halign, #halign=, #has_css_class, #has_default, #has_default=, #has_default?, #has_focus, #has_focus=, #has_focus?, #has_tooltip, #has_tooltip=, #has_tooltip?, #has_visible_focus, have_template?, #height, #height_request, #height_request=, #hexpand, #hexpand=, #hexpand?, #hexpand_set, #hexpand_set=, #hexpand_set?, #hide, #in_destruction, #init_template, #insert_action_group, #insert_action_group_raw, #insert_after, #insert_before, #install_action, #install_property_action, #is_ancestor, #is_drawable, #is_focus, #is_sensitive, #is_visible, #keynav_failed, #last_child, #layout_manager, #layout_manager=, #layout_manager_type, #layout_manager_type=, #list_mnemonic_labels, #map, #mapped, #margin_bottom, #margin_bottom=, #margin_end, #margin_end=, #margin_start, #margin_start=, #margin_top, #margin_top=, #measure, #mnemonic_activate, #name, #name=, #native, #next_sibling, #observe_children, #observe_controllers, #opacity, #opacity=, #overflow, #overflow=, #pango_context, #parent, #parent=, #pick, #prev_sibling, #primary_clipboard, #query_action, #queue_allocate, #queue_draw, #queue_resize, #realize, #realized, #receives_default, #receives_default=, #receives_default?, #remove_controller, #remove_css_class, #remove_mnemonic_label, #remove_tick_callback, #request_mode, #root, #root=, #scale_factor, #scale_factor=, #sensitive, #sensitive=, #sensitive?, #set_size_request, #set_size_request_raw, #set_state_flags, #set_template, #set_template_raw, #settings, #should_layout, #show, #size_allocate, #snapshot_child, #state_flags, #style_context, #style_context_raw, #template=, template_children, #template_from_resource=, #template_scope=, #tooltip_markup, #tooltip_markup=, #tooltip_text, #tooltip_text=, #translate_coordinates, #translate_coordinates_raw, #trigger_tooltip_query, #unmap, #unparent, #unrealize, #unset_state_flags, #valign, #valign=, #vexpand, #vexpand=, #vexpand?, #vexpand_set, #vexpand_set=, #vexpand_set?, #visible, #visible=, #visible?, #width, #width_request, #width_request=

Constructor Details

#initialize(min_value, max_value) ⇒ Gtk::Widget

Creates a new GtkLevelBar for the specified interval.

Parameters:

  • min_value (Float)

    a positive value

  • max_value (Float)

    a positive value

Instance Method Details

#add_offset_value(name, value) ⇒ nil

Adds a new offset marker on self at the position specified by value.

When the bar value is in the interval topped by value (or between value and [propertyGtk.LevelBar:max-value] in case the offset is the last one on the bar) a style class named level-name will be applied when rendering the level bar fill.

If another offset marker named name exists, its value will be replaced by value.

Parameters:

  • name (String)

    the name of the new offset

  • value (Float)

    the value for the new offset

Returns:

  • (nil)

#get_offset_value(name) ⇒ Boolean

Fetches the value specified for the offset marker name in self.

Parameters:

  • name (String)

    the name of an offset in the bar

  • value (Float)

    location where to store the value

Returns:

  • (Boolean)

    true if the specified offset is found



22
23
24
25
26
27
28
29
# File 'lib/gtk4/level-bar.rb', line 22

def get_offset_value(name)
  found, offset = get_offset_value_raw(name)
  if found
    offset
  else
    nil
  end
end

#get_offset_value_rawBoolean

Fetches the value specified for the offset marker name in self.

Parameters:

  • name (String)

    the name of an offset in the bar

  • value (Float)

    location where to store the value

Returns:

  • (Boolean)

    true if the specified offset is found



# File 'lib/gtk4/level-bar.rb', line 20

#invertedBoolean

Returns whether the levelbar is inverted.

Returns:

  • (Boolean)

    true if the level bar is inverted

#inverted=(inverted) ⇒ Boolean

Whether the GtkLeveBar is inverted.

Level bars normally grow from top to bottom or left to right. Inverted level bars grow in the opposite direction.

Parameters:

  • inverted (Boolean)

Returns:

  • (Boolean)

    inverted

  • (Boolean)

    inverted

#inverted?Boolean

Whether the GtkLeveBar is inverted.

Level bars normally grow from top to bottom or left to right. Inverted level bars grow in the opposite direction.

Returns:

  • (Boolean)

    inverted

#max_valueFloat

Determines the maximum value of the interval that can be displayed by the bar.

Returns:

  • (Float)

    max-value

#max_value=(max_value) ⇒ Float

Determines the maximum value of the interval that can be displayed by the bar.

Parameters:

  • max_value (Float)

Returns:

  • (Float)

    max-value

  • (Float)

    max-value

#min_valueFloat

Determines the minimum value of the interval that can be displayed by the bar.

Returns:

  • (Float)

    min-value

#min_value=(min_value) ⇒ Float

Determines the minimum value of the interval that can be displayed by the bar.

Parameters:

  • min_value (Float)

Returns:

  • (Float)

    min-value

  • (Float)

    min-value

#modeGtk::LevelBarMode

Determines the way GtkLevelBar interprets the value properties to draw the level fill area.

Specifically, when the value is %GTK_LEVEL_BAR_MODE_CONTINUOUS, GtkLevelBar will draw a single block representing the current value in that area; when the value is %GTK_LEVEL_BAR_MODE_DISCRETE, the widget will draw a succession of separate blocks filling the draw area, with the number of blocks being equal to the units separating the integral roundings of [propertyGtk.LevelBar:min-value] and [propertyGtk.LevelBar:max-value].

Returns:

#mode=(mode) ⇒ Gtk::LevelBarMode

Determines the way GtkLevelBar interprets the value properties to draw the level fill area.

Specifically, when the value is %GTK_LEVEL_BAR_MODE_CONTINUOUS, GtkLevelBar will draw a single block representing the current value in that area; when the value is %GTK_LEVEL_BAR_MODE_DISCRETE, the widget will draw a succession of separate blocks filling the draw area, with the number of blocks being equal to the units separating the integral roundings of [propertyGtk.LevelBar:min-value] and [propertyGtk.LevelBar:max-value].

Parameters:

Returns:

#remove_offset_value(name) ⇒ nil

Removes an offset marker from a GtkLevelBar.

The marker must have been previously added with [methodGtk.LevelBar.add_offset_value].

Parameters:

  • name (String)

    the name of an offset in the bar

Returns:

  • (nil)

#valueFloat

Determines the currently filled value of the level bar.

Returns:

  • (Float)

    value

#value=(value) ⇒ Float

Determines the currently filled value of the level bar.

Parameters:

  • value (Float)

Returns:

  • (Float)

    value

  • (Float)

    value