todo-d/source/app.d

159 lines
4.0 KiB
D
Raw Permalink Normal View History

2022-04-26 17:33:31 +00:00
import model;
2022-04-28 20:24:47 +00:00
import std.stdio;
2022-04-26 17:33:31 +00:00
import gtk.MainWindow;
import gtk.Main;
2022-04-28 20:24:47 +00:00
import gtk.Builder;
import gtk.Application;
import gtk.ApplicationWindow;
import gtk.Entry;
import gtk.Box;
import gtk.ListBox;
import gtk.ListBoxRow;
import gtk.Label;
import gtk.CheckButton;
import gtk.ToggleButton;
import gtk.Button;
2022-04-29 06:46:52 +00:00
import gio.Resource;
import glib.Bytes;
2022-04-28 20:24:47 +00:00
class ToDoItemWidget : Box {
this(ToDoItem item, ToDoModel todoModel) {
super(GtkOrientation.HORIZONTAL, 5);
CheckButton button = new CheckButton();
button.setActive(item.checked);
button.addOnToggled(delegate(ToggleButton b) {
item.checked = b.getActive();
});
Label label = new Label(item.text);
label.setLineWrap(true);
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);
}
}
Entry taskEntry;
ListBox taskList;
2022-04-29 06:46:52 +00:00
ApplicationWindow window;
2022-04-28 20:24:47 +00:00
ToDoModel todoModel;
2022-04-26 17:33:31 +00:00
void main(string[] args) {
Main.init(args);
2022-04-29 06:46:52 +00:00
auto bytes = new Bytes(cast(ubyte[]) import("resources.gresource"));
Resource.register(new Resource(bytes));
Builder builder = new Builder();
builder.addFromResource("/ui/todo-ui.glade");
2022-04-28 20:24:47 +00:00
builder.addCallbackSymbol("onAddTask", &addTask);
builder.addCallbackSymbol("onWindowDestroy", &onWindowDestroy);
2022-04-29 06:46:52 +00:00
builder.addCallbackSymbol("onNewMenuActivated", &onNewMenuActivated);
builder.addCallbackSymbol("onSaveMenuActivated", &onSaveMenuActivated);
builder.addCallbackSymbol("onSaveAsMenuActivated", &onSaveAsMenuActivated);
builder.addCallbackSymbol("onOpenMenuActivated", &onOpenMenuActivated);
2022-04-28 20:24:47 +00:00
builder.addCallbackSymbol("onQuitMenuActivated", &onWindowDestroy);
builder.connectSignals(null);
taskList = cast(ListBox) builder.getObject("taskList");
taskEntry = cast(Entry) builder.getObject("addTaskEntry");
todoModel = new ToDoModel();
import std.functional : toDelegate;
todoModel.addListener(ModelUpdateListener.of(toDelegate(&itemsUpdated)));
2022-04-29 06:46:52 +00:00
window = cast(ApplicationWindow) builder.getObject("window");
2022-04-26 17:33:31 +00:00
window.showAll();
Main.run();
}
2022-04-28 20:24:47 +00:00
void itemsUpdated(ToDoItem[] items) {
taskList.removeAll();
foreach (item; items) {
auto widget = new ToDoItemWidget(item, todoModel);
auto row = new ListBoxRow();
row.add(widget);
taskList.add(row);
}
taskList.showAll();
}
extern (C) void addTask() {
todoModel.addItem(taskEntry.getText());
taskEntry.setText("");
}
extern (C) void onWindowDestroy() {
Main.quit();
2022-04-29 06:46:52 +00:00
if (todoModel.getOpenFilename() !is null) {
todoModel.saveToJson(todoModel.getOpenFilename());
}
}
extern (C) void onNewMenuActivated() {
todoModel.clear();
}
extern (C) void onSaveMenuActivated() {
if (todoModel.getOpenFilename() is null) {
onSaveAsMenuActivated();
} else {
todoModel.saveToJson(todoModel.getOpenFilename());
}
}
extern (C) void onSaveAsMenuActivated() {
import gtk.FileChooserDialog;
auto dialog = new FileChooserDialog(
"Save As",
window,
FileChooserAction.SAVE
);
if (todoModel.getOpenFilename() !is null) {
dialog.setFilename(todoModel.getOpenFilename());
}
int result = dialog.run();
if (result == -5) {
todoModel.saveToJson(dialog.getFilename());
}
dialog.close();
}
extern (C) void onOpenMenuActivated() {
import gtk.FileChooserDialog;
auto dialog = new FileChooserDialog(
"Open",
window,
FileChooserAction.OPEN
);
int result = dialog.run();
if (result == -5) {
todoModel.openFromJson(dialog.getFilename());
}
dialog.close();
2022-04-28 20:24:47 +00:00
}