Skip to content

Commit

Permalink
OverlayBar: Port to GTK4
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt committed Nov 27, 2021
1 parent d01edae commit 48dea41
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 53 deletions.
23 changes: 15 additions & 8 deletions demo/Views/OverlayBarView.vala
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
/*
* Copyright 2017 elementary, Inc. (https://elementary.io)
* Copyright 2017-2021 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

public class OverlayBarView : Gtk.Overlay {
public class OverlayBarView : Gtk.Box {
construct {
var button = new Gtk.ToggleButton.with_label ("Show Spinner");

/* This is necessary to workaround an issue in the stylesheet with buttons packed directly into overlays */
var grid = new Gtk.Grid ();
grid.halign = Gtk.Align.CENTER;
grid.valign = Gtk.Align.CENTER;
grid.add (button);
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6) {
halign = Gtk.Align.CENTER,
valign = Gtk.Align.CENTER
};

var overlaybar = new Granite.Widgets.OverlayBar (this);
box.append (button);

var overlay = new Gtk.Overlay () {
child = box,
hexpand = true
};

var overlaybar = new Granite.Widgets.OverlayBar (overlay);
overlaybar.label = "Hover the OverlayBar to change its position";

add (grid);
append (overlay);

button.toggled.connect (() => {
overlaybar.active = button.active;
Expand Down
78 changes: 37 additions & 41 deletions lib/Widgets/OverlayBar.vala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright 2021 elementary, Inc. (https://elementary.io)
* Copyright 2012 ammonkey <am.monkeyd@gmail.com>
* Copyright 2013 Julián Unrrein <junrrein@gmail.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
Expand Down Expand Up @@ -29,17 +30,17 @@
* public class OverlayBarView : Gtk.Overlay {
* construct {
* var button = new Gtk.ToggleButton.with_label ("Show Spinner");
*
*
* var grid = new Gtk.Grid ();
* grid.halign = Gtk.Align.CENTER;
* grid.valign = Gtk.Align.CENTER;
* grid.add (button);
*
*
* var overlaybar = new Granite.Widgets.OverlayBar (this);
* overlaybar.label = "Hover the OverlayBar to change its position";
*
*
* add (grid);
*
*
* button.toggled.connect (() => {
* overlaybar.active = button.active;
* });
Expand All @@ -50,20 +51,21 @@
* @see Gtk.Overlay
*
*/
public class Granite.Widgets.OverlayBar : Gtk.EventBox {
// TODO: Check events, used to be EventBox
public class Granite.Widgets.OverlayBar : Gtk.Box {

private const string FALLBACK_THEME = """
.overlay-bar {
background-color: alpha (#333, 0.8);
background-color: alpha(#333, 0.8);
border-radius: 3px;
border-width: 0;
box-shadow:
0 1px 3px alpha (#000, 0.12),
0 1px 2px alpha (#000, 0.24);
0 1px 3px alpha(#000, 0.12),
0 1px 2px alpha(#000, 0.24);
color: #fff;
padding: 3px 6px;
margin: 6px;
text-shadow: 0 1px 2px alpha (#000, 0.6);
text-shadow: 0 1px 2px alpha(#000, 0.6);
}
""";

Expand Down Expand Up @@ -108,10 +110,10 @@ public class Granite.Widgets.OverlayBar : Gtk.EventBox {
*/
public bool active {
get {
return spinner.active;
return spinner.spinning;
}
set {
spinner.active = value;
spinner.spinning = value;
revealer.reveal_child = value;
}
}
Expand All @@ -120,7 +122,6 @@ public class Granite.Widgets.OverlayBar : Gtk.EventBox {
*/
public OverlayBar (Gtk.Overlay? overlay = null) {
if (overlay != null) {
overlay.add_events (Gdk.EventMask.ENTER_NOTIFY_MASK);
overlay.add_overlay (this);
}
}
Expand All @@ -132,57 +133,52 @@ public class Granite.Widgets.OverlayBar : Gtk.EventBox {

spinner = new Gtk.Spinner ();

revealer = new Gtk.Revealer ();
revealer.reveal_child = false;
revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT;
revealer.add (spinner);
revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_LEFT,
child = spinner
};

var grid = new Gtk.Grid ();
grid.add (status_label);
grid.add (revealer);
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.append (status_label);
box.append (revealer);

add (grid);
append (box);

set_halign (Gtk.Align.END);
set_valign (Gtk.Align.END);

int priority = Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK;
Granite.Widgets.Utils.set_theming (grid, FALLBACK_THEME, StyleClass.OVERLAY_BAR, priority);
var provider = new Gtk.CssProvider ();
provider.load_from_data ((uint8[])FALLBACK_THEME);

var ctx = grid.get_style_context ();
var state = ctx.get_state ();
int priority = Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK;
var ctx = box.get_style_context ();
ctx.add_class (StyleClass.OVERLAY_BAR);
ctx.add_provider (provider, priority);

var padding = ctx.get_padding (state);
var padding = ctx.get_padding ();
status_label.margin_top = padding.top;
status_label.margin_bottom = padding.bottom;
status_label.margin_start = padding.left;
status_label.margin_end = padding.right;
spinner.margin_end = padding.right;

var margin = ctx.get_margin (state);
grid.margin_top = margin.top;
grid.margin_bottom = margin.bottom;
grid.margin_start = margin.left;
grid.margin_end = margin.right;
}

public override void parent_set (Gtk.Widget? old_parent) {
Gtk.Widget parent = get_parent ();
var margin = ctx.get_margin ();
box.margin_top = margin.top;
box.margin_bottom = margin.bottom;
box.margin_start = margin.left;
box.margin_end = margin.right;

if (old_parent != null)
old_parent.enter_notify_event.disconnect (enter_notify_callback);
if (parent != null)
parent.enter_notify_event.connect (enter_notify_callback);
var focus_controller = new Gtk.EventControllerMotion ();
focus_controller.enter.connect (enter_notify_callback);
add_controller (focus_controller);
}

private bool enter_notify_callback (Gdk.EventCrossing event) {
private void enter_notify_callback () {
if (get_halign () == Gtk.Align.START)
set_halign (Gtk.Align.END);
else
set_halign (Gtk.Align.START);

queue_resize ();

return false;
}
}
9 changes: 5 additions & 4 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project(
'granite',
'vala', 'c',
meson_version: '>= 0.48.2',
version: '6.2.0'
version: '7.0.0'
)

if meson.get_compiler('vala').version().version_compare('<0.48.0')
Expand Down Expand Up @@ -49,7 +49,7 @@ libgranite_deps = [
gio_os_dep,
dependency('glib-2.0', version: '>=' + glib_min_version),
dependency('gobject-2.0', version: '>=' + glib_min_version),
dependency('gtk+-3.0', version: '>=3.22'),
dependency('gtk4', version: '>=4.4'),
]

icons_dir = join_paths(
Expand All @@ -62,9 +62,10 @@ icons_dir = join_paths(
pkgconfig = import('pkgconfig')
i18n = import('i18n')

subdir('lib')
#subdir('lib')
subdir('data')
subdir('demo')
#subdir('demo')
subdir('porting')
subdir('icons')
subdir('po')

Expand Down

0 comments on commit 48dea41

Please sign in to comment.