Skip to content

Commit

Permalink
Setting initial actions and accelerators for header bar menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
Alecaddd committed Jan 15, 2018
1 parent 2528058 commit d959d60
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
44 changes: 34 additions & 10 deletions src/Widgets/HeaderBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

public class Akira.Widgets.HeaderBar : Gtk.HeaderBar {
public weak Akira.Window window { get; construct; }

private const string TOOLS_DIR = "/com/github/alecaddd/akira/tools/";

public Akira.Partials.HeaderBarButton new_document;
Expand All @@ -41,26 +43,47 @@ public class Akira.Widgets.HeaderBar : Gtk.HeaderBar {
}
}

public HeaderBar () {
Object (toggled: true);
public HeaderBar (Akira.Window main_window) {
Object (
toggled: true,
window: main_window
);
}

construct {
set_title (APP_NAME);
set_show_close_button (true);

var menu_items = new Gtk.Menu ();
menu_items.add (new Gtk.MenuItem.with_label(_("Open")));
menu_items.add (new Gtk.MenuItem.with_label(_("Save")));
menu_items.add (new Gtk.MenuItem.with_label(_("Save As")));

var new_window = new Gtk.MenuItem.with_label (_("New Window"));
new_window.action_name = Akira.Window.ACTION_PREFIX + Akira.Window.ACTION_NEW_WINDOW;
new_window.add_accelerator ("activate", window.accel_group, Gdk.keyval_from_name("N"), Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE);
menu_items.add (new_window);
menu_items.add (new Gtk.SeparatorMenuItem ());
var quit = new Gtk.ImageMenuItem.with_label(_("Quit"));
var image = new Gtk.Image.from_icon_name ("window-close-symbolic", Gtk.IconSize.MENU);
quit.always_show_image = true;
quit.set_image (image);

var open = new Gtk.MenuItem.with_label (_("Open"));
open.action_name = Akira.Window.ACTION_PREFIX + Akira.Window.ACTION_OPEN;
open.add_accelerator ("activate", window.accel_group, Gdk.keyval_from_name("O"), Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE);
menu_items.add (open);

var save = new Gtk.MenuItem.with_label (_("Save"));
save.action_name = Akira.Window.ACTION_PREFIX + Akira.Window.ACTION_SAVE;
save.add_accelerator ("activate", window.accel_group, Gdk.keyval_from_name("S"), Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE);
menu_items.add (save);

var save_as = new Gtk.MenuItem.with_label (_("Save As"));
save_as.action_name = Akira.Window.ACTION_PREFIX + Akira.Window.ACTION_SAVE_AS;
save_as.add_accelerator ("activate", window.accel_group, Gdk.keyval_from_name("S"), Gdk.ModifierType.CONTROL_MASK + Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE);
menu_items.add (save_as);

menu_items.add (new Gtk.SeparatorMenuItem ());

var quit = new Gtk.MenuItem.with_label(_("Quit"));
quit.action_name = Akira.Window.ACTION_PREFIX + Akira.Window.ACTION_QUIT;
quit.accel_path = Akira.Window.ACTION_QUIT;
quit.add_accelerator ("activate", window.accel_group, Gdk.keyval_from_name("Q"), Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE);
menu_items.add (quit);

menu_items.show_all ();

menu = new Akira.Partials.MenuButton ("document-open", _("Menu"), _("Open Menu"));
Expand All @@ -83,6 +106,7 @@ public class Akira.Widgets.HeaderBar : Gtk.HeaderBar {
settings = new Akira.Partials.HeaderBarButton ("preferences-other", _("Preferences"), _("Open Preferences (Ctrl+,)"));

layout = new Akira.Partials.HeaderBarButton ("preferences-system-windows", _("Layout"), _("Toggle Layout (Ctrl+.)"));
layout.action_name = Akira.Window.ACTION_PREFIX + Akira.Window.ACTION_PRESENTATION;
ruler = new Akira.Partials.HeaderBarButton ("applications-accessories", _("Ruler"), _("Toggle Ruler (Ctrl+⇧+R)"));

add (menu);
Expand Down
27 changes: 26 additions & 1 deletion src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public class Akira.Window : Gtk.ApplicationWindow {
public Akira.Utils.Dialogs dialogs;

public SimpleActionGroup actions { get; construct; }
public Gtk.AccelGroup accel_group { get; construct; }

public const string ACTION_PREFIX = "win.";
public const string ACTION_NEW_WINDOW = "action_new_window";
public const string ACTION_OPEN = "action_open";
public const string ACTION_SAVE = "action_save";
public const string ACTION_SAVE_AS = "action_save_as";
public const string ACTION_PRESENTATION = "action_presentation";
public const string ACTION_LABELS = "action_labels";
public const string ACTION_QUIT = "action_quit";
Expand All @@ -37,6 +41,9 @@ public class Akira.Window : Gtk.ApplicationWindow {

private const ActionEntry[] action_entries = {
{ ACTION_NEW_WINDOW, action_new_window },
{ ACTION_OPEN, action_open },
{ ACTION_SAVE, action_save },
{ ACTION_SAVE_AS, action_save_as },
{ ACTION_PRESENTATION, action_presentation },
{ ACTION_LABELS, action_labels },
{ ACTION_QUIT, action_quit }
Expand All @@ -55,6 +62,9 @@ public class Akira.Window : Gtk.ApplicationWindow {

static construct {
action_accelerators.set (ACTION_NEW_WINDOW, "<Control>n");
action_accelerators.set (ACTION_OPEN, "<Control>o");
action_accelerators.set (ACTION_SAVE, "<Control>s");
action_accelerators.set (ACTION_SAVE_AS, "<Control><Shift>s");
action_accelerators.set (ACTION_PRESENTATION, "<Control>period");
action_accelerators.set (ACTION_LABELS, "<Control>l");
action_accelerators.set (ACTION_QUIT, "<Control>q");
Expand All @@ -68,8 +78,11 @@ public class Akira.Window : Gtk.ApplicationWindow {
foreach (var action in action_accelerators.get_keys ()) {
app.set_accels_for_action (ACTION_PREFIX + action, action_accelerators[action].to_array ());
}

accel_group = new Gtk.AccelGroup ();
add_accel_group (accel_group);

headerbar = new Akira.Widgets.HeaderBar ();
headerbar = new Akira.Widgets.HeaderBar (this);
main_window = new Akira.Widgets.MainWindow ();
dialogs = new Akira.Utils.Dialogs (this);

Expand Down Expand Up @@ -152,6 +165,18 @@ public class Akira.Window : Gtk.ApplicationWindow {
app.new_window ();
}

private void action_open () {
warning ("open");
}

private void action_save () {
warning ("save");
}

private void action_save_as () {
warning ("save_as");
}

protected override bool delete_event (Gdk.EventAny event) {
int width, height, x, y;

Expand Down

0 comments on commit d959d60

Please sign in to comment.