-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fae40ed
commit c4ee997
Showing
5 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ src/*/*.gresource | |
src/*/*.gresource.xml | ||
src/*/jsconfig.json | ||
src/*/rustc-ice-*.txt | ||
src/*/*.sqlite* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |