add settings

This commit is contained in:
sonichy
2025-05-12 09:33:19 +08:00
parent 2516cc72b2
commit 51ddc825e9
7 changed files with 85 additions and 25 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 127 KiB

View File

@@ -3,13 +3,23 @@ Display date and time on GNOME taskbar.
![alt](preview.png) ![alt](preview.png)
## Changelog ## Changelog
### V1.1 (2025-05-12)
Support set memo text in tooltip.
### V1.0 (2025-04-22) ### V1.0 (2025-04-22)
Display date and time on GNOME taskbar. Display date and time on GNOME taskbar.
## Reference ## Reference
[Guide](https://gjs.guide/extensions/development/creating.html) [Guide](https://gjs.guide/extensions/development/creating.html)
[Cinnamon_Applet](https://github.com/sonichy/Cinnamon_Applet) [Cinnamon_Applet](https://github.com/sonichy/Cinnamon_Applet)
[Calendar](https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/dateMenu.js#L901) [Calendar](https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/dateMenu.js#L901)
[Tooltip](https://gitlab.com/arcmenu/ArcMenu)
[Dialog](https://gjs.guide/extensions/topics/dialogs.html)
[Settings](https://gjs.guide/extensions/development/preferences.html)
[Settings](https://gjs.guide/guides/gtk/3/16-settings.html#title:-application-settings)
## GSettings schema must be compiled
`glib-compile-schemas schemas/`
## Debug ## Debug
dbus-run-session -- gnome-shell --nested --wayland dbus-run-session -- gnome-shell --nested --wayland
dconf-editor

View File

@@ -1,5 +1,8 @@
import GLib from "gi://GLib"; import GLib from "gi://GLib";
import St from 'gi://St'; import St from 'gi://St';
import Clutter from "gi://Clutter";
import Gio from 'gi://Gio';
import * as Dialog from 'resource:///org/gnome/shell/ui/dialog.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js'; import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; import * as Main from 'resource:///org/gnome/shell/ui/main.js';
@@ -12,27 +15,61 @@ export default class DatetimeExtension extends Extension {
enable() { enable() {
this._indicator = new PanelMenu.Button(0.0, this.metadata.name, false); this._indicator = new PanelMenu.Button(0.0, this.metadata.name, false);
var label = new St.Label({ text: '00:00\n1/1 一' }); var label = new St.Label({ text: '00:00\n1/1 一', x_align: Clutter.ActorAlign.CENTER, y_align: Clutter.ActorAlign.CENTER });
label.set_style('text-align:center');
this._indicator.add_child(label); this._indicator.add_child(label);
Main.panel.addToStatusArea(this.uuid, this._indicator); Main.panel.addToStatusArea(this.uuid, this._indicator);
const menuItem = new PopupMenu.PopupMenuItem(''); const menu_calendar = new PopupMenu.PopupMenuItem('');
//https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/dateMenu.js#L901 //https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/dateMenu.js#L901
let calendar = new Calendar.Calendar(); let calendar = new Calendar.Calendar();
menuItem.add_child(calendar);
menuItem.height = 300;
let now = new Date(); let now = new Date();
calendar.setDate(now); calendar.setDate(now);
this._indicator.menu.addMenuItem(menuItem); menu_calendar.add_child(calendar);
menu_calendar.height = 300;
this._indicator.menu.addMenuItem(menu_calendar);
//const menuItem1 = new PopupMenu.PopupMenuItem(''); //https://gjs.guide/extensions/development/preferences.html
//menuItem1.label.set_style('text-align:center'); //无效 const schema = 'org.gnome.shell.extensions.datetime';
//this._indicator.menu.addMenuItem(menuItem1); this._settings = this.getSettings(schema);
this.label_tooltip = new St.Label({ text: '' }); const menu_set = new PopupMenu.PopupMenuItem('Set');
this.label_tooltip.set_style('background:#222;padding:10px;border:1px solid #aaa;border-radius:10px;'); menu_set.connect('activate', () => {
//https://gjs.guide/extensions/topics/dialogs.html
const dialog = new Dialog.Dialog(global.stage, 'my-dialog');
const box = new St.BoxLayout();
const label = new St.Label({ text: 'Memo', y_align: Clutter.ActorAlign.CENTER });
label.style = 'margin:0 10px;';
box.add_child(label);
var s = this._settings.get_string('memo');
const entry = new St.Entry({ text: s });
entry.width = 200;
box.add_child(entry);
dialog.contentLayout.add_child(box);
dialog.addButton({
label: 'Save',
action: () => {
this._settings.set_string('memo', entry.text);
dialog.destroy();
},
});
dialog.addButton({
label: 'Close',
isDefault: true,
action: () => {
dialog.destroy();
},
});
dialog.set_position(parseInt(global.stage.width/2 - dialog.width/2), parseInt(global.stage.height/2 - dialog.height/2));
});
this._indicator.menu.addMenuItem(menu_set);
this.label_tooltip = new St.Label();
this.label_tooltip.set_style('background:#222; padding:10px; border:1px solid #aaa; border-radius:10px;');
global.stage.add_child(this.label_tooltip); global.stage.add_child(this.label_tooltip);
this.label_tooltip.hide(); this.label_tooltip.hide();
this._indicator.connect('notify::hover', () => { this._indicator.connect('notify::hover', () => {
@@ -41,12 +78,13 @@ export default class DatetimeExtension extends Extension {
if (x == 0 && y != 0) //LEFT if (x == 0 && y != 0) //LEFT
this.label_tooltip.set_position(x + this._indicator.width + 1, y); this.label_tooltip.set_position(x + this._indicator.width + 1, y);
else if (x != 0 && y == 0) //TOP else if (x != 0 && y == 0) //TOP
this.label_tooltip.set_position(x, y + this._indicator.height + 1); this.label_tooltip.set_position(x - this.label_tooltip.width / 2 + this._indicator.width / 2, y + this._indicator.height + 1);
else else
if (this._indicator.height == Main.panel.height) //BOTTOM if (this._indicator.height == Main.panel.height) //BOTTOM
this.label_tooltip.set_position(x, y - this.label_tooltip.height - 1); this.label_tooltip.set_position(parseInt(x - this.label_tooltip.width / 2 + this._indicator.width / 2), y - this.label_tooltip.height - 1);
else //RIGHT else //RIGHT
this.label_tooltip.set_position(x - this.label_tooltip.width - 1, y); this.label_tooltip.set_position(x - this.label_tooltip.width - 1, y);
//console.log(x - this.label_tooltip.width / 2 + this._indicator.width / 2);
if (this._indicator.hover) if (this._indicator.hover)
this.label_tooltip.show(); this.label_tooltip.show();
else else
@@ -64,10 +102,10 @@ export default class DatetimeExtension extends Extension {
var day = date.getDay(); var day = date.getDay();
var weekday = ["日", "一", "二", "三", "四", "五", "六"]; var weekday = ["日", "一", "二", "三", "四", "五", "六"];
var weekday1 = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; var weekday1 = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
var text = h + ' : ' + m + '\n' + (date.getMonth() + 1) + '/' + date.getDate() + ' ' + weekday[day]; var text = h + ':' + m + '\n' + (date.getMonth() + 1) + '/' + date.getDate() + ' ' + weekday[day];
label.set_text(text); label.set_text(text);
//menuItem1.label.text = date.toLocaleString() + ' ' + weekday1[day]; var s = this._settings.get_string('memo');
this.label_tooltip.text = date.toLocaleString() + ' ' + weekday1[day]; this.label_tooltip.text = date.toLocaleString() + ' ' + weekday1[day] + '\n' + s;
// Run as loop, not once. // Run as loop, not once.
return GLib.SOURCE_CONTINUE; return GLib.SOURCE_CONTINUE;
}); });
@@ -80,9 +118,10 @@ export default class DatetimeExtension extends Extension {
this._timeout = null; this._timeout = null;
} }
this.label_tooltip?.destroy(); this.label_tooltip?.destroy();
this.label_tooltip = null; this.label_tooltip = null;
this._indicator?.destroy(); this._indicator?.destroy();
this._indicator = null; this._indicator = null;
this._settings = null;
} }
} }

View File

@@ -1,9 +1,10 @@
{ {
"uuid": "datetime@sonichy", "uuid": "datetime@sonichy",
"name": "datetime", "name": "datetime",
"settings-schema": "org.gnome.shell.extensions.datetime",
"description": "Display date and time on GNOME taskbar", "description": "Display date and time on GNOME taskbar",
"author": "sonichy", "author": "sonichy",
"version": "1.0", "version": "1.0",
"shell-version": [ "45", "46", "47", "48" ], "shell-version": [ "45", "46", "47", "48" ],
"url": "https://github.com/sonichy/GNOME_extension" "url": "https://github.com/sonichy/GNOME_extension"
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema id="org.gnome.shell.extensions.datetime" path="/org/gnome/shell/extensions/datetime/">
<key name="memo" type="s">
<default>""</default>
<summary>Set memo</summary>
<description>Show this in tooltip</description>
</key>
</schema>
</schemalist>