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'],
version: '0.1.0',
meson_version: '>= 0.50.0',
default_options: [ 'warning_level=2',
],
version: '0.1.0',
meson_version: '>= 0.50.0',
default_options: [ 'warning_level=2' ],
)
i18n = import('i18n')
subdir('data')
subdir('src')
subdir('po')

View File

@@ -51,9 +51,15 @@ namespace SwayNotificatonCenter {
}
public bool toggle_visibility () {
var vis = !this.visible;
this.set_visible (vis);
return vis;
var cc_visibility = !this.visible;
this.set_visible (cc_visibility);
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) {
@@ -67,6 +73,7 @@ namespace SwayNotificatonCenter {
public void add_notification (NotifyParams param, NotiDaemon notiDaemon) {
var noti = new Notification (param, notiDaemon, true);
noti.set_time ();
box.pack_end (noti, false, true, 0);
}
}

View File

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

View File

@@ -10,7 +10,13 @@ sway_notificaton_center_deps = [
dependency('gio-2.0', version: '>= 2.50'),
dependency('gtk+-3.0', version: '>= 3.22'),
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')
@@ -22,14 +28,14 @@ sway_notificaton_center_sources += gnome.compile_resources('sway_notificaton_cen
executable('sway-nc',
sway_notificaton_center_sources,
vala_args: [ '--target-glib=2.50', '--pkg=GtkLayerShell-0.1' ],
vala_args: args,
dependencies: sway_notificaton_center_deps,
install: true,
)
executable('sway-nc-client',
['client.vala', 'constants.vala'],
vala_args: [ '--target-glib=2.50', '--pkg=GtkLayerShell-0.1' ],
vala_args: args,
dependencies: sway_notificaton_center_deps,
install: true,
)

View File

@@ -47,14 +47,37 @@
<property name="margin-end">14</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="summary">
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="ellipsize">end</property>
<property name="xalign">0</property>
<attributes>
<attribute name="size" value="12288"/>
</attributes>
<child>
<object class="GtkLabel" id="summary">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="ellipsize">end</property>
<property name="xalign">0</property>
<attributes>
<attribute name="size" value="12288"/>
</attributes>
</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>
<property name="expand">False</property>
@@ -68,10 +91,12 @@
<property name="can-focus">False</property>
<property name="wrap">True</property>
<property name="xalign">0</property>
<property name="yalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>

View File

@@ -11,12 +11,14 @@ namespace SwayNotificatonCenter {
[GtkChild]
unowned Gtk.Label summary;
[GtkChild]
unowned Gtk.Label time;
[GtkChild]
unowned Gtk.Label body;
[GtkChild]
unowned Gtk.Image img;
private int open_timeout = 0;
private const int millis = 5000;
private const int millis = 10000;
public NotifyParams param;
@@ -47,6 +49,39 @@ namespace SwayNotificatonCenter {
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 () {
if (param.app_icon == "") {
// Get the app icon
@@ -73,7 +108,7 @@ namespace SwayNotificatonCenter {
int ms = param.expire_timeout > 0 ? param.expire_timeout : millis;
if (param.expire_timeout != 0) {
open_timeout = 1;
Timeout.add ((int) (ms), () => {
Timeout.add (ms, () => {
open_timeout--;
if (open_timeout == 0) callback (this);
return false;