Use GdStackSwitcher to handle stack elements

This commit is contained in:
Ignacio Casal Quinteiro 2013-02-25 21:57:52 +01:00
parent 89a415912e
commit 0e2bd426c3
9 changed files with 39 additions and 161 deletions

View file

@ -22,76 +22,21 @@ namespace Gitg
public class UIElements<T>
{
private class ActiveUIElement
{
public GitgExt.UIElement element;
public Gtk.RadioButton? navigation_button;
public ActiveUIElement(GitgExt.UIElement e)
{
element = e;
}
}
private Peas.ExtensionSet d_extensions;
private HashTable<string, ActiveUIElement> d_available_elements;
private HashTable<string, GitgExt.UIElement> d_available_elements;
private HashTable<string, GitgExt.UIElement> d_elements;
private List<ActiveUIElement> d_available_sorted;
private Gtk.Box? d_box;
private ActiveUIElement? d_current;
private GitgExt.UIElement? d_current;
private Gd.Stack d_stack;
public signal void activated(GitgExt.UIElement element);
private Gtk.RadioButton? create_header_button(GitgExt.UIElement e)
{
if (d_box == null)
{
return null;
}
Icon? icon = e.icon;
if (icon == null)
{
return null;
}
var img = new Gtk.Image.from_gicon(icon, Gtk.IconSize.MENU);
img.show();
Gtk.RadioButton button;
if (d_box.get_children().length() != 0)
{
var ic = d_box.get_children();
button = new Gtk.RadioButton.from_widget(ic.data as Gtk.RadioButton);
}
else
{
button = new Gtk.RadioButton(null);
}
e.bind_property("enabled", button, "sensitive", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
button.set_mode(false);
button.set_image(img);
var context = button.get_style_context();
context.add_class("image-button");
button.show();
return button;
}
public T? current
{
get
{
if (d_current != null)
{
return (T)d_current.element;
return (T)d_current;
}
else
{
@ -125,15 +70,15 @@ public class UIElements<T>
// Note that this will also set elem to current if needed
add_available(elem);
}
else if (wasavail != null && wasavail.navigation_button != null)
else if (wasavail != null)
{
if (!wasavail.element.enabled && d_current == wasavail)
if (!wasavail.enabled && d_current == wasavail)
{
d_current = null;
}
else if (wasavail.element.enabled && d_current == null)
else if (wasavail.enabled && d_current == null)
{
set_current_impl(wasavail.element);
set_current_impl(wasavail);
}
}
});
@ -148,88 +93,48 @@ public class UIElements<T>
{
if (!element.available ||
!element.enabled ||
(d_current != null && d_current.element == element))
(d_current != null && d_current == element))
{
return;
}
ActiveUIElement? el = d_available_elements.lookup(element.id);
GitgExt.UIElement? el = d_available_elements.lookup(element.id);
if (el != null)
{
if (d_current != null)
{
if (d_current.navigation_button != null)
{
d_current.navigation_button.active = false;
}
}
d_current = el;
if (el.navigation_button != null)
{
el.navigation_button.active = true;
}
if (d_stack != null)
{
d_stack.set_visible_child(el.element.widget);
d_stack.set_visible_child(el.widget);
}
activated(el.element);
activated(el);
}
}
private void remove_available(GitgExt.UIElement e)
{
ActiveUIElement ae;
GitgExt.UIElement ae;
if (d_available_elements.lookup_extended(e.id, null, out ae))
{
if (ae.navigation_button != null)
{
d_available_sorted.remove(ae);
ae.navigation_button.destroy();
}
if (ae == d_current)
{
d_current = null;
}
d_stack.remove(ae.element.widget);
d_stack.remove(ae.widget);
d_available_elements.remove(e.id);
}
}
private void add_available(GitgExt.UIElement e)
{
Gtk.RadioButton? button = create_header_button(e);
ActiveUIElement ae = new ActiveUIElement(e);
ae.navigation_button = button;
if (button != null)
{
d_available_sorted.insert_sorted(ae, (a, b) => {
return a.element.negotiate_order(b.element);
});
d_box.pack_start(button);
d_box.reorder_child(button, d_available_sorted.index(ae));
update_visibility();
button.toggled.connect((b) => {
if (b.active)
{
set_current_impl(ae.element);
}
});
}
d_stack.add(ae.element.widget);
d_available_elements.insert(e.id, ae);
d_stack.add_with_properties(e.widget,
"title", e.display_name,
"symbolic-icon-name", e.icon);
d_available_elements.insert(e.id, e);
}
private void available_changed(Object o, ParamSpec spec)
@ -272,17 +177,6 @@ public class UIElements<T>
remove_ui_element(obj as GitgExt.UIElement);
}
private void on_box_add_remove(Gtk.Widget box,
Gtk.Widget item)
{
update_visibility();
}
private void update_visibility()
{
d_box.visible = (d_box.get_children().length() > 1);
}
public delegate bool ForeachUIElementFunc(GitgExt.UIElement element);
public void foreach(ForeachUIElementFunc func)
@ -291,7 +185,7 @@ public class UIElements<T>
foreach (var val in vals)
{
if (!func(val.element))
if (!func(val))
{
break;
}
@ -299,37 +193,18 @@ public class UIElements<T>
}
public UIElements(Peas.ExtensionSet extensions,
Gd.Stack? stack = null,
Gtk.Box? box = null)
Gd.Stack? stack = null)
{
d_extensions = extensions;
d_box = box;
d_stack = stack;
d_available_elements = new HashTable<string, ActiveUIElement>(str_hash, str_equal);
d_available_elements = new HashTable<string, GitgExt.UIElement>(str_hash, str_equal);
d_elements = new HashTable<string, GitgExt.UIElement>(str_hash, str_equal);
if (d_box != null)
{
var context = d_box.get_style_context();
context.add_class("linked");
context.add_class("raised");
d_box.add.connect(on_box_add_remove);
d_box.remove.connect(on_box_add_remove);
update_visibility();
}
// Add all the extensions
d_extensions.foreach(extension_added);
d_extensions.extension_added.connect(extension_added);
d_extensions.extension_removed.connect(extension_removed);
if (d_current == null && d_available_sorted != null)
{
set_current_impl(d_available_sorted.data.element);
}
}
}

View file

@ -35,7 +35,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable, Gtk.
private Gd.HeaderBar d_header_bar;
private Gtk.MenuButton d_config;
private Gtk.Box d_header_box;
private Gd.StackSwitcher d_commit_view_switcher;
private Gtk.Paned d_paned_views;
private Gtk.Paned d_paned_panels;
@ -80,13 +80,14 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable, Gtk.
{
// Extract widgets from the builder
d_header_bar = builder.get_object("header-bar") as Gd.HeaderBar;
d_header_box = builder.get_object("header-bar-box") as Gtk.Box;
d_paned_views = builder.get_object("paned_views") as Gtk.Paned;
d_paned_panels = builder.get_object("paned_panels") as Gtk.Paned;
d_stack_view = builder.get_object("stack_view") as Gd.Stack;
d_stack_panel = builder.get_object("stack_panel") as Gd.Stack;
d_commit_view_switcher = builder.get_object("commit-view-switcher") as Gd.StackSwitcher;
d_commit_view_switcher.stack = d_stack_panel;
d_navigation = builder.get_object("tree_view_navigation") as GitgExt.NavigationTreeView;
d_config = builder.get_object("button_config") as Gtk.MenuButton;
@ -189,8 +190,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable, Gtk.
typeof(GitgExt.Panel),
"application",
this),
d_stack_panel,
d_header_box);
d_stack_panel);
d_panels.activated.connect(on_panel_activated);

View file

@ -17,6 +17,7 @@
<object class="GdHeaderBar" id="header-bar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="vexpand">False</property>
<child>
<object class="GdHeaderToggleButton" id="search-button">
<property name="visible">True</property>
@ -28,7 +29,9 @@
</packing>
</child>
<child>
<object class="GtkBox" id="header-bar-box"/>
<object class="GdStackSwitcher" id="commit-view-switcher">
<property name="visible">True</property>
</object>
<packing>
<property name="pack_type">end</property>
</packing>

2
libgd

@ -1 +1 @@
Subproject commit 34da80eaebfe28ee4913840a45d799577de28b97
Subproject commit 6bcecf53ab1abfef4239ca7d26ea6e5fd1943706

View file

@ -70,7 +70,7 @@ public interface UIElement : Object
* If provided, the icon will be used in navigation toolbars
* so that users can switch to the ui element.
*/
public abstract Icon? icon { owned get; }
public abstract string? icon { owned get; }
/**
* The ui element widget.

View file

@ -69,11 +69,11 @@ namespace GitgDash
owned get { return _("Dashboard"); }
}
public Icon? icon
public string? icon
{
owned get
{
return new ThemedIcon("document-open-recent-symbolic");
return "document-open-recent-symbolic";
}
}

View file

@ -68,15 +68,15 @@ namespace GitgDiff
owned get { return _("Diff"); }
}
public Icon? icon
public string? icon
{
owned get { return new ThemedIcon("diff-symbolic"); }
owned get { return "diff-symbolic"; }
}
private void on_selection_changed(GitgExt.ObjectSelection selection)
{
selection.foreach_selected((commit) => {
var c = commit as Ggit.Commit;
var c = commit as Gitg.Commit;
if (c != null)
{

View file

@ -75,9 +75,9 @@ namespace GitgFiles
owned get { return _("Files"); }
}
public Icon? icon
public string? icon
{
owned get { return new ThemedIcon("system-file-manager-symbolic"); }
owned get { return "system-file-manager-symbolic"; }
}
private void on_selection_changed(GitgExt.ObjectSelection selection)

View file

@ -135,9 +135,9 @@ namespace GitgHistory
owned get { return _("History"); }
}
public Icon? icon
public string? icon
{
owned get { return new ThemedIcon("view-list-symbolic"); }
owned get { return "view-list-symbolic"; }
}
public Gtk.Widget? widget