Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ src/*/*.gresource
src/*/*.gresource.xml
src/*/jsconfig.json
src/*/rustc-ice-*.txt
src/*/*.sqlite*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SHELL:=/bin/bash -O globstar

setup:
flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak install --or-update --user --noninteractive flathub org.freedesktop.Sdk.Extension.rust-stable//24.08 org.freedesktop.Sdk.Extension.vala//24.08 org.freedesktop.Sdk.Extension.node20//24.08 #org.freedesktop.Sdk.Extension.typescript//24.08
flatpak install --or-update --user --noninteractive flathub org.freedesktop.Sdk.Extension.rust-stable//24.08 org.freedesktop.Sdk.Extension.vala//24.08 org.freedesktop.Sdk.Extension.node20//24.08 org.freedesktop.Sdk.Extension.typescript//24.08
# flatpak remote-add --user --if-not-exists flathub-beta https://flathub.org/beta-repo/flathub-beta.flatpakrepo
flatpak remote-add --user --if-not-exists gnome-nightly https://nightly.gnome.org/gnome-nightly.flatpakrepo
flatpak install --or-update --user --noninteractive gnome-nightly re.sonny.Workbench.Devel
Expand Down
98 changes: 98 additions & 0 deletions src/Database/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Gtk 4.0;
using Adw 1;

Adw.StatusPage {
title: _("Database");
description: _("Search, load and store data using SQLite");

Box {
orientation: vertical;
halign: center;
spacing: 18;

Box {
styles [
"linked"
]

Entry text_entry {
placeholder-text: _("Enter Text");
hexpand: true;
}

Button insert_button {
label: _("Insert");
}
}

Box {
orientation: horizontal;
spacing: 18;

Box {
orientation: vertical;
spacing: 12;
hexpand: true;

Label {
label: _("Search by Text");
}

SearchEntry search_entry {
search-delay: 100;
}
}

Box {
spacing: 12;
orientation: vertical;

Label {
label: _("Find by ID");
}

SearchEntry id_entry {
search-delay: 100;
}
}
}

ScrolledWindow {
height-request: 340;
has-frame: true;

ColumnView column_view {
show-column-separators: true;

ColumnViewColumn column_text {
title: _("Text");
expand: true;

factory: SignalListItemFactory {};
}

ColumnViewColumn column_id {
title: _("ID");
expand: true;

factory: SignalListItemFactory {};
}
}
}

Box {
orientation: horizontal;
halign: center;

LinkButton {
label: _("API Reference");
uri: "https://gjs-docs.gnome.org/gom10~1.0/";
}

LinkButton {
label: _("SQLite");
uri: "https://www.sqlite.org/";
}
}
}
}
140 changes: 140 additions & 0 deletions src/Database/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import GObject from "gi://GObject";
import Gom from "gi://Gom";
import Gtk from "gi://Gtk";
import GLib from "gi://GLib";
import Gio from "gi://Gio";

Gio._promisify(Gom.Adapter.prototype, "open_async", "open_finish");

Gio._promisify(
Gom.Repository.prototype,
"automatic_migrate_async",
"automatic_migrate_finish",
);
Gio._promisify(Gom.Repository.prototype, "find_async", "find_finish");

Gio._promisify(Gom.Resource.prototype, "save_async", "save_finish");
Gio._promisify(Gom.ResourceGroup.prototype, "fetch_async", "fetch_finish");

const text_entry = workbench.builder.get_object("text_entry");
const insert_button = workbench.builder.get_object("insert_button");
const search_entry = workbench.builder.get_object("search_entry");
const column_view = workbench.builder.get_object("column_view");
const column_text = workbench.builder.get_object("column_text");
const column_id = workbench.builder.get_object("column_id");

const ItemClass = GObject.registerClass(
{
Properties: {
id: GObject.ParamSpec.int(
"id",
"ID",
"An ID",
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
0,
GLib.MAXINT32,
0,
),
text: GObject.ParamSpec.string(
"text",
"Text",
"Some Text",
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
"",
),
},
},
class ItemClass extends Gom.Resource {},
);

const data_model = new Gio.ListStore({ item_type: ItemClass });
let adapter;
let repository;

async function initDatabase() {
adapter = new Gom.Adapter();
await adapter.open_async(workbench.resolve("db.sqlite"));
repository = new Gom.Repository({ adapter });

// Set up table and primary key
ItemClass.set_table("items");
ItemClass.set_primary_key("id");

// Perform automatic migration
await repository.automatic_migrate_async(1, [ItemClass]);
}

async function onInsert() {
const text = text_entry.text;
const item = new ItemClass({ repository, text });
const success = await item.save_async();
if (!success) {
console.error("Failed to insert");
return;
}

await load();
}

async function load() {
const text = search_entry.text || "";

data_model.remove_all();
// Create a filter for Text matching
const filter = Gom.Filter.new_glob(ItemClass, "text", `*${text}*`);
const resource_group = await repository.find_async(ItemClass, filter);

await resource_group.fetch_async(0, resource_group.count);
for (let i = 0; i < resource_group.count; i++) {
const item = resource_group.get_index(i);
if (item) data_model.append(item);
}
}

column_text.factory.connect("setup", (_self, list_item) => {
const label = new Gtk.Label({
margin_start: 12,
margin_end: 12,
});
list_item.set_child(label);
});

column_text.factory.connect("bind", (_self, list_item) => {
const label_widget = list_item.get_child();
const model_item = list_item.get_item();
label_widget.label = model_item.text;
});

column_id.factory.connect("setup", (_self, list_item) => {
const label = new Gtk.Label({
margin_start: 12,
margin_end: 12,
});
list_item.set_child(label);
});

column_id.factory.connect("bind", (_self, list_item) => {
const label_widget = list_item.get_child();
const model_item = list_item.get_item();
label_widget.label = model_item.id.toString();
});

column_view.model = new Gtk.SingleSelection({
model: data_model,
});

try {
await initDatabase();

search_entry.connect("search-changed", () => {
load().catch(console.error);
});

insert_button.connect("clicked", () => {
onInsert().catch(console.error);
});

await load();
} catch (err) {
console.error(err);
}
6 changes: 6 additions & 0 deletions src/Database/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "platform",
"description": "Search, load and store data using SQLite",
"panels": ["code", "preview"],
"autorun": true
}
Loading