38 lines
2.8 KiB
JavaScript
38 lines
2.8 KiB
JavaScript
imports.gi.versions.Gtk = '4.0';
|
|
const {Gtk, Gio, GLib, Gdk} = imports.gi;
|
|
const output = ARGV[0];
|
|
const application = new Gtk.Application({application_id: ARGV[1] || 'dev.ferese.RimeValidation'});
|
|
application.connect('activate', () => {
|
|
const css = new Gtk.CssProvider(); css.load_from_string('window { font-family: "MiSans"; }');
|
|
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
const window = new Gtk.ApplicationWindow({application, title: 'Ferese 原生 Rime 验证', default_width: 880, default_height: 560});
|
|
const box = new Gtk.Box({orientation: Gtk.Orientation.VERTICAL, spacing: 18,
|
|
margin_start: 28, margin_end: 28, margin_top: 28, margin_bottom: 28});
|
|
box.append(new Gtk.Label({label: '独立 Rime · 原生 Wayland 候选窗', xalign: 0}));
|
|
box.append(new Gtk.Label({label: '输入 nihao;空格或数字选字,− / + 翻页,Shift 切换中英文。', wrap: true, xalign: 0}));
|
|
const normal = new Gtk.Entry({placeholder_text: '普通输入框', hexpand: true});
|
|
const password = new Gtk.Entry({placeholder_text: '敏感输入测试框(只使用测试字符)', visibility: false,
|
|
input_purpose: Gtk.InputPurpose.PASSWORD, hexpand: true});
|
|
const status = new Gtk.Label({label: '等待输入', xalign: 0});
|
|
const save = () => {
|
|
const focused = window.get_focus();
|
|
const state = {normal: normal.get_text(), password_length: password.get_text().length,
|
|
focus: focused && (focused === password || focused.is_ancestor(password)) ? 'password' : 'normal'};
|
|
// This is a disposable validation fixture, containing only test input.
|
|
GLib.file_set_contents(output, JSON.stringify(state));
|
|
status.set_label('普通输入:' + normal.get_text().length + ' 字符;敏感输入:' + password.get_text().length + ' 字符');
|
|
};
|
|
for (const widget of [normal, password]) widget.connect('changed', save);
|
|
box.append(normal); box.append(password); box.append(status);
|
|
box.append(new Gtk.Box({vexpand: true}));
|
|
for (const [name, action] of [['normal', () => normal.grab_focus()], ['password', () => password.grab_focus()],
|
|
['clear', () => {normal.set_text(''); password.set_text(''); normal.grab_focus(); save();}],
|
|
['top', () => {box.remove(normal); box.insert_child_after(normal, box.get_first_child().get_next_sibling()); normal.grab_focus();}],
|
|
['bottom', () => {box.remove(normal); box.append(normal); normal.grab_focus();}]]) {
|
|
const entry = new Gio.SimpleAction({name}); entry.connect('activate', action); application.add_action(entry);
|
|
}
|
|
window.set_child(box); window.present();
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {normal.grab_focus(); save(); return GLib.SOURCE_REMOVE;});
|
|
});
|
|
application.run([]);
|