Implemented time in notifications

This commit is contained in:
Erik Reider
2021-07-28 13:21:52 +02:00
parent f1054317ac
commit 7c977531d1
6 changed files with 94 additions and 22 deletions

View File

@@ -1,14 +1,11 @@
project('sway-notificaton-center', ['c', 'vala'], project('sway-notificaton-center', ['c', 'vala'],
version: '0.1.0', version: '0.1.0',
meson_version: '>= 0.50.0', meson_version: '>= 0.50.0',
default_options: [ 'warning_level=2', default_options: [ 'warning_level=2' ],
],
) )
i18n = import('i18n') i18n = import('i18n')
subdir('data') subdir('data')
subdir('src') subdir('src')
subdir('po') subdir('po')

View File

@@ -51,9 +51,15 @@ namespace SwayNotificatonCenter {
} }
public bool toggle_visibility () { public bool toggle_visibility () {
var vis = !this.visible; var cc_visibility = !this.visible;
this.set_visible (vis); this.set_visible (cc_visibility);
return vis; if(cc_visibility) {
foreach (var w in box.get_children ()) {
var noti = (Notification) w;
noti.set_time ();
}
}
return cc_visibility;
} }
public void close_notification (uint32 id) { public void close_notification (uint32 id) {
@@ -67,6 +73,7 @@ namespace SwayNotificatonCenter {
public void add_notification (NotifyParams param, NotiDaemon notiDaemon) { public void add_notification (NotifyParams param, NotiDaemon notiDaemon) {
var noti = new Notification (param, notiDaemon, true); var noti = new Notification (param, notiDaemon, true);
noti.set_time ();
box.pack_end (noti, false, true, 0); box.pack_end (noti, false, true, 0);
} }
} }

View File

@@ -10,6 +10,7 @@ namespace SwayNotificatonCenter {
public string[] actions { get; set; } public string[] actions { get; set; }
public HashTable<string, Variant> hints { get; set; } public HashTable<string, Variant> hints { get; set; }
public int expire_timeout { get; set; } public int expire_timeout { get; set; }
public int64 time { get; set; } // Epoch in seconds
public NotifyParams (uint32 applied_id, public NotifyParams (uint32 applied_id,
string app_name, string app_name,
@@ -29,6 +30,7 @@ namespace SwayNotificatonCenter {
this.actions = actions; this.actions = actions;
this.hints = hints; this.hints = hints;
this.expire_timeout = expire_timeout; this.expire_timeout = expire_timeout;
this.time = (int64) (GLib.get_real_time () * 0.000001);
} }
public void printParams () { public void printParams () {
@@ -59,7 +61,7 @@ namespace SwayNotificatonCenter {
[DBus (name = "org.freedesktop.Notifications")] [DBus (name = "org.freedesktop.Notifications")]
public class NotiDaemon : Object { public class NotiDaemon : Object {
private uint32 noti_id = 1; private uint32 noti_id = 0;
public NotiWindow notiWin; public NotiWindow notiWin;
private DBusInit dbusInit; private DBusInit dbusInit;

View File

@@ -10,7 +10,13 @@ sway_notificaton_center_deps = [
dependency('gio-2.0', version: '>= 2.50'), dependency('gio-2.0', version: '>= 2.50'),
dependency('gtk+-3.0', version: '>= 3.22'), dependency('gtk+-3.0', version: '>= 3.22'),
dependency('libhandy-1', version: '>= 1.0.0'), dependency('libhandy-1', version: '>= 1.0.0'),
meson.get_compiler('c').find_library('gtk-layer-shell') meson.get_compiler('c').find_library('gtk-layer-shell'),
meson.get_compiler('c').find_library('m', required : false),
]
args = [
'--target-glib=2.50',
'--pkg=GtkLayerShell-0.1',
] ]
gnome = import('gnome') gnome = import('gnome')
@@ -22,14 +28,14 @@ sway_notificaton_center_sources += gnome.compile_resources('sway_notificaton_cen
executable('sway-nc', executable('sway-nc',
sway_notificaton_center_sources, sway_notificaton_center_sources,
vala_args: [ '--target-glib=2.50', '--pkg=GtkLayerShell-0.1' ], vala_args: args,
dependencies: sway_notificaton_center_deps, dependencies: sway_notificaton_center_deps,
install: true, install: true,
) )
executable('sway-nc-client', executable('sway-nc-client',
['client.vala', 'constants.vala'], ['client.vala', 'constants.vala'],
vala_args: [ '--target-glib=2.50', '--pkg=GtkLayerShell-0.1' ], vala_args: args,
dependencies: sway_notificaton_center_deps, dependencies: sway_notificaton_center_deps,
install: true, install: true,
) )

View File

@@ -46,6 +46,10 @@
<property name="can-focus">False</property> <property name="can-focus">False</property>
<property name="margin-end">14</property> <property name="margin-end">14</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child> <child>
<object class="GtkLabel" id="summary"> <object class="GtkLabel" id="summary">
<property name="visible">True</property> <property name="visible">True</property>
@@ -56,6 +60,25 @@
<attribute name="size" value="12288"/> <attribute name="size" value="12288"/>
</attributes> </attributes>
</object> </object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="time">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">True</property> <property name="fill">True</property>
@@ -68,10 +91,12 @@
<property name="can-focus">False</property> <property name="can-focus">False</property>
<property name="wrap">True</property> <property name="wrap">True</property>
<property name="xalign">0</property> <property name="xalign">0</property>
<property name="yalign">0</property>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">True</property>
<property name="fill">True</property> <property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property> <property name="position">1</property>
</packing> </packing>
</child> </child>

View File

@@ -11,12 +11,14 @@ namespace SwayNotificatonCenter {
[GtkChild] [GtkChild]
unowned Gtk.Label summary; unowned Gtk.Label summary;
[GtkChild] [GtkChild]
unowned Gtk.Label time;
[GtkChild]
unowned Gtk.Label body; unowned Gtk.Label body;
[GtkChild] [GtkChild]
unowned Gtk.Image img; unowned Gtk.Image img;
private int open_timeout = 0; private int open_timeout = 0;
private const int millis = 5000; private const int millis = 10000;
public NotifyParams param; public NotifyParams param;
@@ -47,6 +49,39 @@ namespace SwayNotificatonCenter {
if (show) this.show_all (); if (show) this.show_all ();
} }
public void set_time () {
this.time.set_text (get_readable_time ());
}
private string get_readable_time () {
string value = "Now";
double diff = (GLib.get_real_time () * 0.000001) - param.time;
double secs = diff / 60;
double hours = secs / 60;
double days = hours / 24;
if (secs >= 1 && hours < 1) {
// 1m - 1h
var val = Math.floor (secs);
value = val.to_string () + " min";
if (val > 1) value += "s";
value += " ago";
} else if (hours >= 1 && hours < 24) {
// 1h - 24h
var val = Math.floor (hours);
value = val.to_string () + " hour";
if (val > 1) value += "s";
value += " ago";
} else {
// Days
var val = Math.floor (days);
value = val.to_string () + " day";
if (val > 1) value += "s";
value += " ago";
}
return value;
}
private void set_icon () { private void set_icon () {
if (param.app_icon == "") { if (param.app_icon == "") {
// Get the app icon // Get the app icon
@@ -73,7 +108,7 @@ namespace SwayNotificatonCenter {
int ms = param.expire_timeout > 0 ? param.expire_timeout : millis; int ms = param.expire_timeout > 0 ? param.expire_timeout : millis;
if (param.expire_timeout != 0) { if (param.expire_timeout != 0) {
open_timeout = 1; open_timeout = 1;
Timeout.add ((int) (ms), () => { Timeout.add (ms, () => {
open_timeout--; open_timeout--;
if (open_timeout == 0) callback (this); if (open_timeout == 0) callback (this);
return false; return false;