-
Notifications
You must be signed in to change notification settings - Fork 18
/
egList.js
124 lines (93 loc) · 3.6 KB
/
egList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/gjs
/*
GJS example showing how to build Gtk javascript applications
using Gtk TreeView and ListStore
Run it with:
gjs egList.js
*/
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObj = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
// Get application folder and add it into the imports path
function getAppFileInfo() {
let stack = (new Error()).stack,
stackLine = stack.split('\n')[1],
coincidence, path, file;
if (!stackLine) throw new Error('Could not find current file (1)');
coincidence = new RegExp('@(.+):\\d+').exec(stackLine);
if (!coincidence) throw new Error('Could not find current file (2)');
path = coincidence[1];
file = Gio.File.new_for_path(path);
return [file.get_path(), file.get_parent().get_path(), file.get_basename()];
}
const path = getAppFileInfo()[1];
imports.searchPath.push(path);
const App = function () {
this.title = 'Example List';
GLib.set_prgname(this.title);
};
App.prototype.run = function (ARGV) {
this.application = new Gtk.Application();
this.application.connect('activate', () => { this.onActivate(); });
this.application.connect('startup', () => { this.onStartup(); });
this.application.run([]);
};
App.prototype.onActivate = function () {
this.window.show_all();
};
App.prototype.onStartup = function() {
this.buildUI();
};
App.prototype.buildUI = function() {
this.window = new Gtk.ApplicationWindow({ application: this.application,
default_height: 300,
default_width: 720,
window_position: Gtk.WindowPosition.CENTER });
try {
this.window.set_icon_from_file(path + '/assets/appIcon.png');
} catch (err) {
this.window.set_icon_name('application-x-executable');
}
this.window.set_titlebar(this.getHeader());
this.window.add(this.getBody());
};
App.prototype.getHeader = function () {
this.headerBar = new Gtk.HeaderBar();
this.headerBar.set_show_close_button(true);
return this.headerBar;
};
App.prototype.getBody = function () {
let scroll, store, tree, col;
scroll = new Gtk.ScrolledWindow({ vexpand: true });
store = new Gtk.ListStore();
store.set_column_types([GObj.TYPE_INT, GObj.TYPE_STRING, GObj.TYPE_STRING, GObj.TYPE_BOOLEAN]);
store.set(store.append(), [0, 1, 2, 3], [0, '0A', 'Name 0', false]);
store.set(store.append(), [0, 1, 2, 3], [1, '1B', 'Name 1', false]);
store.set(store.append(), [0, 1, 2, 3], [2, '2C', 'Name 2', false]);
store.set(store.append(), [0, 1, 2, 3], [3, '3D', 'Name 3', false]);
tree = new Gtk.TreeView({ headers_visible: false, vexpand: true, hexpand: true });
tree.set_model(store);
scroll.add(tree);
col = new Gtk.TreeViewColumn();
tree.append_column(col);
let text1 = new Gtk.CellRendererText();
col.pack_start(text1, true);
col.set_cell_data_func(text1, (col, cell, model, iter) => { this.cellFuncText1(col, cell, model, iter); });
let text2 = new Gtk.CellRendererText();
col.pack_start(text2, true);
col.set_cell_data_func(text2, (col, cell, model, iter) => { this.cellFuncText2(col, cell, model, iter); });
return scroll;
};
App.prototype.cellFuncText1 = function (col, cell, model, iter) {
cell.editable = false;
cell.text = model.get_value(iter, 1);
};
App.prototype.cellFuncText2 = function (col, cell, model, iter) {
cell.editable = false;
cell.text = model.get_value(iter, 2);
};
//Run the application
let app = new App();
app.run(ARGV);