Rename client GtkUtil package to conform to style: Util.Gtk

This commit is contained in:
Michael Gratton 2019-10-29 12:25:59 +11:00 committed by Michael James Gratton
parent de6ef699de
commit 7be9115ebc
7 changed files with 84 additions and 86 deletions

View file

@ -77,7 +77,7 @@ public class CountBadge : Geary.BaseObject {
ctx.close_path();
// Colorize our shape.
GtkUtil.set_source_color_from_string(ctx, UNREAD_BG_COLOR);
Util.Gtk.set_source_color_from_string(ctx, UNREAD_BG_COLOR);
ctx.fill_preserve();
ctx.set_line_width(2.0);
ctx.stroke();

View file

@ -68,10 +68,9 @@ public class ComposerHeaderbar : Gtk.HeaderBar {
detach_start.visible = false;
detach_end.visible = true;
} else {
bool at_end = GtkUtil.close_button_at_end();
bool at_end = Util.Gtk.close_button_at_end();
detach_start.visible = !at_end;
detach_end.visible = at_end;
}
}
}

View file

@ -2022,7 +2022,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
// Step 3.
GtkUtil.menu_foreach(context_menu_model, (label, name, target, section) => {
Util.Gtk.menu_foreach(context_menu_model, (label, name, target, section) => {
if (context_menu.last() != null) {
context_menu.append(new WebKit.ContextMenuItem.separator());
}
@ -2060,7 +2060,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
private inline void append_menu_section(WebKit.ContextMenu context_menu,
Menu section) {
GtkUtil.menu_foreach(section, (label, name, target, section) => {
Util.Gtk.menu_foreach(section, (label, name, target, section) => {
string simple_name = name;
if ("." in simple_name) {
simple_name = simple_name.split(".")[1];

View file

@ -333,7 +333,7 @@ public class FormattedConversationData : Geary.BaseObject {
// Draw separator line.
if (ctx != null && cell_area != null) {
ctx.set_line_width(1.0);
GtkUtil.set_source_color_from_string(ctx, CountBadge.UNREAD_BG_COLOR);
Util.Gtk.set_source_color_from_string(ctx, CountBadge.UNREAD_BG_COLOR);
ctx.move_to(cell_area.x - 1, cell_area.y + cell_area.height);
ctx.line_to(cell_area.x + cell_area.width + 1, cell_area.y + cell_area.height);
ctx.stroke();

View file

@ -798,7 +798,7 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
bool supports_trash = get_action_enabled(ACTION_TRASH_MESSAGE);
bool supports_delete = get_action_enabled(ACTION_DELETE_MESSAGE);
bool show_trash_button = !this.shift_key_down && (supports_trash || !supports_delete);
GtkUtil.menu_foreach(this.email_menu_model, (label, name, target, section) => {
Util.Gtk.menu_foreach(this.email_menu_model, (label, name, target, section) => {
if ((section != this.email_menu_trash || show_trash_button) &&
(section != this.email_menu_delete || !show_trash_button)) {
this.email_menu.append_item(new MenuItem.section(label, section));

View file

@ -826,7 +826,7 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface {
Gtk.ListBoxRow initial_row = get_row_at_index(0);
int loading_height = 0;
if (initial_row is LoadingRow) {
loading_height = GtkUtil.get_border_box_height(initial_row);
loading_height = Util.Gtk.get_border_box_height(initial_row);
remove(initial_row);
}
@ -843,7 +843,7 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface {
// same place.
row.enable_should_scroll();
row.should_scroll.connect(() => {
listbox_adj.value += GtkUtil.get_border_box_height(row);
listbox_adj.value += Util.Gtk.get_border_box_height(row);
});
// Only adjust for the loading row going away once

View file

@ -6,85 +6,84 @@
* (version 2.1 or later). See the COPYING file in this distribution.
*/
namespace GtkUtil {
/**
* Given an HTML-style color spec, parses the color and sets it to the source RGB of the Cairo context.
* (Borrowed from Shotwell.)
*/
void set_source_color_from_string(Cairo.Context ctx, string spec) {
Gdk.RGBA rgba = Gdk.RGBA();
if (!rgba.parse(spec))
error("Can't parse color %s", spec);
ctx.set_source_rgb(rgba.red, rgba.green, rgba.blue);
}
/**
* Returns whether the close button is at the end of the headerbar.
*/
bool close_button_at_end() {
string layout = Gtk.Settings.get_default().gtk_decoration_layout;
bool at_end = false;
// Based on logic of close_button_at_end in gtkheaderbar.c: Close button appears
// at end iff "close" follows a colon in the layout string.
if (layout != null) {
int colon_ind = layout.index_of(":");
at_end = (colon_ind >= 0 && layout.index_of("close", colon_ind) >= 0);
}
return at_end;
}
/**
* Allows iterating over a GMenu, without having to handle MenuItems
* @param menu - The menu to iterate over
* @param foreach_func - The function which will be called on the attributes of menu's children
*/
void menu_foreach(Menu menu, MenuForeachFunc foreach_func) {
for (int i = 0; i < menu.get_n_items(); i++) {
// Get the attributes we're interested in
Variant? label = menu.get_item_attribute_value(i, Menu.ATTRIBUTE_LABEL, VariantType.STRING);
Variant? action_name = menu.get_item_attribute_value(i, Menu.ATTRIBUTE_ACTION, VariantType.STRING);
Variant? action_target = menu.get_item_attribute_value(i, Menu.ATTRIBUTE_TARGET, VariantType.STRING);
// Check if the child is a section
Menu? section = (Menu) menu.get_item_link(i, Menu.LINK_SECTION);
// Callback
foreach_func((label != null) ? label.get_string() : null,
(action_name != null) ? action_name.get_string() : null,
action_target,
section);
}
}
/*
* Used for menu_foreach()
* @param id - The id if one was set
* @param label - The label if one was set
* @param action_name - The action name, if set
* @param action_target - The action target, if set
* @param section - If the item represents a section, this will return that section (or null otherwise)
*/
delegate void MenuForeachFunc(string? label, string? action_name, Variant? target, Menu? section);
/**
* Returns the CSS border box height for a widget.
*
* This adjusts the GTK widget's allocated height to exclude extra
* space added by the CSS margin property, if any.
*/
public inline int get_border_box_height(Gtk.Widget widget) {
Gtk.StyleContext style = widget.get_style_context();
Gtk.StateFlags flags = style.get_state();
Gtk.Border margin = style.get_margin(flags);
return widget.get_allocated_height() - margin.top - margin.bottom;
}
}
namespace Util.Gtk {
/**
* Given an HTML-style color spec, parses the color and sets it to
* the source RGB of the Cairo context. (Borrowed from Shotwell.)
*/
public void set_source_color_from_string(Cairo.Context ctx, string spec) {
Gdk.RGBA rgba = Gdk.RGBA();
if (!rgba.parse(spec))
error("Can't parse color %s", spec);
ctx.set_source_rgb(rgba.red, rgba.green, rgba.blue);
}
/**
* Returns whether the close button is at the end of the headerbar.
*/
bool close_button_at_end() {
string layout = global::Gtk.Settings.get_default().gtk_decoration_layout;
bool at_end = false;
// Based on logic of close_button_at_end in gtkheaderbar.c:
// Close button appears at end iff "close" follows a colon in
// the layout string.
if (layout != null) {
int colon_ind = layout.index_of(":");
at_end = (colon_ind >= 0 && layout.index_of("close", colon_ind) >= 0);
}
return at_end;
}
/**
* Allows iterating over a GMenu, without having to handle MenuItems
* @param menu - The menu to iterate over
* @param foreach_func - The function which will be called on the
* attributes of menu's children
*/
void menu_foreach(Menu menu, MenuForeachFunc foreach_func) {
for (int i = 0; i < menu.get_n_items(); i++) {
// Get the attributes we're interested in
Variant? label = menu.get_item_attribute_value(i, Menu.ATTRIBUTE_LABEL, VariantType.STRING);
Variant? action_name = menu.get_item_attribute_value(i, Menu.ATTRIBUTE_ACTION, VariantType.STRING);
Variant? action_target = menu.get_item_attribute_value(i, Menu.ATTRIBUTE_TARGET, VariantType.STRING);
// Check if the child is a section
Menu? section = (Menu) menu.get_item_link(i, Menu.LINK_SECTION);
// Callback
foreach_func((label != null) ? label.get_string() : null,
(action_name != null) ? action_name.get_string() : null,
action_target,
section);
}
}
/*
* Used for menu_foreach()
* @param id - The id if one was set
* @param label - The label if one was set
* @param action_name - The action name, if set
* @param action_target - The action target, if set
* @param section - If the item represents a section, this will
* return that section (or null otherwise)
*/
delegate void MenuForeachFunc(string? label, string? action_name, Variant? target, Menu? section);
/**
* Returns the CSS border box height for a widget.
*
* This adjusts the GTK widget's allocated height to exclude extra
* space added by the CSS margin property, if any.
*/
public inline int get_border_box_height(global::Gtk.Widget widget) {
global::Gtk.StyleContext style = widget.get_style_context();
global::Gtk.StateFlags flags = style.get_state();
global::Gtk.Border margin = style.get_margin(flags);
return widget.get_allocated_height() - margin.top - margin.bottom;
}
/** Copies a GLib menu, setting targets for the given actions. */
public GLib.Menu copy_menu_with_targets(GLib.Menu template,
string group,