Added keybinds, and other stuff I forgot.

This commit is contained in:
Andrew Lalis 2022-05-01 21:33:59 +02:00
parent 93f6fd7d18
commit aded0b6400
3 changed files with 68 additions and 24 deletions

View File

@ -133,7 +133,7 @@
<object class="GtkListBox" id="taskList"> <object class="GtkListBox" id="taskList">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="selection_mode">none</property> <property name="activate_on_single_click">False</property>
</object> </object>
</child> </child>
</object> </object>
@ -153,6 +153,7 @@
<object class="GtkEntry" id="addTaskEntry"> <object class="GtkEntry" id="addTaskEntry">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<signal name="activate" handler="onAddTask" swapped="no"/>
</object> </object>
<packing> <packing>
<property name="expand">True</property> <property name="expand">True</property>
@ -169,7 +170,6 @@
<property name="use_stock">True</property> <property name="use_stock">True</property>
<property name="always_show_image">True</property> <property name="always_show_image">True</property>
<signal name="clicked" handler="onAddTask" swapped="no"/> <signal name="clicked" handler="onAddTask" swapped="no"/>
<accelerator key="Return" signal="clicked"/>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>

View File

@ -1,5 +1,7 @@
import model; import model;
import std.stdio; import std.stdio;
import std.functional : toDelegate;
import gtk.MainWindow; import gtk.MainWindow;
import gtk.Main; import gtk.Main;
@ -14,6 +16,10 @@ import gtk.Label;
import gtk.CheckButton; import gtk.CheckButton;
import gtk.ToggleButton; import gtk.ToggleButton;
import gtk.Button; import gtk.Button;
import gtk.Widget;
import gdk.Keymap;
import gdk.Keysyms : GdkKeysyms;
import gio.Resource; import gio.Resource;
import glib.Bytes; import glib.Bytes;
@ -30,26 +36,6 @@ class ToDoItemWidget : Box {
label.setHalign(GtkAlign.START); label.setHalign(GtkAlign.START);
label.setValign(GtkAlign.CENTER); label.setValign(GtkAlign.CENTER);
this.packStart(button, false, false, 0); 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); this.packEnd(label, true, true, 0);
} }
} }
@ -80,10 +66,11 @@ void main(string[] args) {
builder.connectSignals(null); builder.connectSignals(null);
taskList = cast(ListBox) builder.getObject("taskList"); taskList = cast(ListBox) builder.getObject("taskList");
Widget listWidget = cast(Widget) taskList;
listWidget.addOnKeyPress(toDelegate(&taskListKeyPressed));
taskEntry = cast(Entry) builder.getObject("addTaskEntry"); taskEntry = cast(Entry) builder.getObject("addTaskEntry");
todoModel = new ToDoModel(); todoModel = new ToDoModel();
import std.functional : toDelegate;
todoModel.addListener(ModelUpdateListener.of(toDelegate(&itemsUpdated))); todoModel.addListener(ModelUpdateListener.of(toDelegate(&itemsUpdated)));
window = cast(ApplicationWindow) builder.getObject("window"); window = cast(ApplicationWindow) builder.getObject("window");
@ -96,12 +83,60 @@ void itemsUpdated(ToDoItem[] items) {
foreach (item; items) { foreach (item; items) {
auto widget = new ToDoItemWidget(item, todoModel); auto widget = new ToDoItemWidget(item, todoModel);
auto row = new ListBoxRow(); auto row = new ListBoxRow();
row.setSelectable(true);
row.setActivatable(false);
row.add(widget); row.add(widget);
taskList.add(row); taskList.add(row);
} }
taskList.showAll(); 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() { extern (C) void addTask() {
todoModel.addItem(taskEntry.getText()); todoModel.addItem(taskEntry.getText());
taskEntry.setText(""); taskEntry.setText("");

View File

@ -36,6 +36,15 @@ class ToDoModel {
return openFilename; 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) { void addItem(string text) {
addItem(new ToDoItem(text, 1_000_000, false)); 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); foreach (l; listeners) l.itemsUpdated(this.items);
} }
} }