From aded0b6400b8a40e632da2ef777dd93e1bfd9358 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 1 May 2022 21:33:59 +0200 Subject: [PATCH] Added keybinds, and other stuff I forgot. --- resources/todo-ui.glade | 4 +-- source/app.d | 77 ++++++++++++++++++++++++++++++----------- source/model.d | 11 +++++- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/resources/todo-ui.glade b/resources/todo-ui.glade index 2b8cad7..3dc8f12 100644 --- a/resources/todo-ui.glade +++ b/resources/todo-ui.glade @@ -133,7 +133,7 @@ True False - none + False @@ -153,6 +153,7 @@ True True + True @@ -169,7 +170,6 @@ True True - False diff --git a/source/app.d b/source/app.d index 231bd2c..a6baf3a 100644 --- a/source/app.d +++ b/source/app.d @@ -1,5 +1,7 @@ import model; + import std.stdio; +import std.functional : toDelegate; import gtk.MainWindow; import gtk.Main; @@ -14,6 +16,10 @@ import gtk.Label; import gtk.CheckButton; import gtk.ToggleButton; import gtk.Button; +import gtk.Widget; + +import gdk.Keymap; +import gdk.Keysyms : GdkKeysyms; import gio.Resource; import glib.Bytes; @@ -30,26 +36,6 @@ class ToDoItemWidget : Box { label.setHalign(GtkAlign.START); label.setValign(GtkAlign.CENTER); this.packStart(button, false, false, 0); - - Button removeButton = new Button(StockID.REMOVE); - removeButton.addOnClicked(delegate(Button b) { - todoModel.removeItem(item.priority); - }); - this.packEnd(removeButton, false, false, 0); - - if (todoModel.canIncrement(item)) { - Button upButton = new Button(StockID.GO_UP, delegate(Button b) { - todoModel.incrementPriority(item); - }); - this.packEnd(upButton, false, false, 0); - } - if (todoModel.canDecrement(item)) { - Button downButton = new Button(StockID.GO_DOWN, delegate(Button b) { - todoModel.decrementPriority(item); - }); - this.packEnd(downButton, false, false, 0); - } - this.packEnd(label, true, true, 0); } } @@ -80,10 +66,11 @@ void main(string[] args) { builder.connectSignals(null); taskList = cast(ListBox) builder.getObject("taskList"); + Widget listWidget = cast(Widget) taskList; + listWidget.addOnKeyPress(toDelegate(&taskListKeyPressed)); taskEntry = cast(Entry) builder.getObject("addTaskEntry"); todoModel = new ToDoModel(); - import std.functional : toDelegate; todoModel.addListener(ModelUpdateListener.of(toDelegate(&itemsUpdated))); window = cast(ApplicationWindow) builder.getObject("window"); @@ -96,12 +83,60 @@ void itemsUpdated(ToDoItem[] items) { foreach (item; items) { auto widget = new ToDoItemWidget(item, todoModel); auto row = new ListBoxRow(); + row.setSelectable(true); + row.setActivatable(false); row.add(widget); taskList.add(row); } taskList.showAll(); } +bool taskListKeyPressed(GdkEventKey* event, Widget w) { + int idx = taskList.getSelectedRow().getIndex(); + int selectedPrio = idx + 1; + ToDoItem selectedItem = todoModel.getItemAt(selectedPrio); + if (selectedItem is null) return true; + bool ctrlDown = (event.state & ModifierType.CONTROL_MASK) > 0; + if (ctrlDown && event.keyval == GdkKeysyms.GDK_Up && todoModel.canIncrement(selectedItem)) { + todoModel.incrementPriority(selectedItem); + auto row = taskList.getRowAtIndex(idx - 1); + taskList.selectRow(row); + Widget rowWidget = cast(Widget) row; + rowWidget.grabFocus(); + } + if (ctrlDown && event.keyval == GdkKeysyms.GDK_Down && todoModel.canDecrement(selectedItem)) { + todoModel.decrementPriority(selectedItem); + auto row = taskList.getRowAtIndex(idx + 1); + taskList.selectRow(row); + Widget rowWidget = cast(Widget) row; + rowWidget.grabFocus(); + } + if (!ctrlDown && event.keyval == GdkKeysyms.GDK_Up && idx > 0) { + auto row = taskList.getRowAtIndex(idx - 1); + taskList.selectRow(row); + Widget rowWidget = cast(Widget) row; + rowWidget.grabFocus(); + } + if (!ctrlDown && event.keyval == GdkKeysyms.GDK_Down && idx + 1 < todoModel.itemCount) { + auto row = taskList.getRowAtIndex(idx + 1); + taskList.selectRow(row); + Widget rowWidget = cast(Widget) row; + rowWidget.grabFocus(); + } + if (event.keyval == GdkKeysyms.GDK_Delete) { + todoModel.removeItem(selectedPrio); + } + if (event.keyval == GdkKeysyms.GDK_Return) { + selectedItem.checked = !selectedItem.checked; + todoModel.notifyListeners(); + auto row = taskList.getRowAtIndex(idx); + taskList.selectRow(row); + Widget rowWidget = cast(Widget) row; + rowWidget.grabFocus(); + } + return true; +} + extern (C) void addTask() { todoModel.addItem(taskEntry.getText()); taskEntry.setText(""); diff --git a/source/model.d b/source/model.d index b34147c..d6b8534 100644 --- a/source/model.d +++ b/source/model.d @@ -36,6 +36,15 @@ class ToDoModel { return openFilename; } + ToDoItem getItemAt(int prio) { + if (prio < 1 || prio > items.length) return null; + return items[prio - 1]; + } + + ulong itemCount() { + return items.length; + } + void addItem(string text) { addItem(new ToDoItem(text, 1_000_000, false)); } @@ -135,7 +144,7 @@ class ToDoModel { } } - private void notifyListeners() { + public void notifyListeners() { foreach (l; listeners) l.itemsUpdated(this.items); } } \ No newline at end of file