Skip to content

Commit

Permalink
Merge pull request #600 from Sjoerd1993/Set-headerbar-CSS-on-style-ch…
Browse files Browse the repository at this point in the history
…ange

Set headerbar css on style change
  • Loading branch information
sstendahl authored Nov 20, 2023
2 parents 9647f0e + cae0e26 commit d2aff15
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ def _on_style_change(self, override: bool = False) -> None:
figure_settings.bind_property(prop, canvas, prop, 1 | 2)
data.bind_property("items", canvas, "items", 2)
window = self.props.application.get_window()
headerbar = window.get_content_headerbar()
headerbar.provider = Gtk.CssProvider()

# Set headerbar color and contrast
bg_color = self._selected_style_params["figure.facecolor"]
contrast = utilities.get_luminance(bg_color)
color = "@dark_5" if contrast > 150 else "@light_1"
css = f"headerbar {{ background-color: {bg_color}; color: {color}; }}"
context = headerbar.get_style_context()
headerbar.provider.load_from_data(css.encode())
context.add_provider(headerbar.provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

window.set_canvas(canvas)
window.get_cut_button().bind_property(
"sensitive", canvas, "highlight_enabled", 2,
Expand Down
8 changes: 8 additions & 0 deletions src/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def hex_to_rgba(hex_str):
return rgba


def get_luminance(hex_color):
color = hex_color[1:]
hex_red = int(color[0:2], base=16)
hex_green = int(color[2:4], base=16)
hex_blue = int(color[4:6], base=16)
return hex_red * 0.2126 + hex_green * 0.7152 + hex_blue * 0.0722


def sig_fig_round(number, digits):
"""Round a number to the specified number of significant digits."""
try:
Expand Down
3 changes: 3 additions & 0 deletions src/window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ namespace Graphs {
[GtkChild]
private unowned Adw.ToastOverlay toast_overlay { get; }

[GtkChild]
public unowned Adw.HeaderBar content_headerbar { get; }

public int mode {
set {
this.pan_button.set_active (value == 0);
Expand Down

0 comments on commit d2aff15

Please sign in to comment.