|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/MiracleOS-Team/libxdg-go/notificationDaemon" |
| 10 | + "github.com/dlasky/gotk3-layershell/layershell" |
| 11 | + "github.com/gotk3/gotk3/gdk" |
| 12 | + "github.com/gotk3/gotk3/glib" |
| 13 | + "github.com/gotk3/gotk3/gtk" |
| 14 | +) |
| 15 | + |
| 16 | +func createNotification(notification *notificationDaemon.Notification, nDaemon *notificationDaemon.Daemon) *gtk.Box { |
| 17 | + notificationBox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 15) |
| 18 | + sc, _ := notificationBox.GetStyleContext() |
| 19 | + sc.AddClass("ntf_main_div") |
| 20 | + |
| 21 | + ntfTopBar, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 15) |
| 22 | + |
| 23 | + sc, _ = ntfTopBar.GetStyleContext() |
| 24 | + sc.AddClass("ntf_top_bar") |
| 25 | + |
| 26 | + ntfTopBarText, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 15) |
| 27 | + |
| 28 | + sc, _ = ntfTopBarText.GetStyleContext() |
| 29 | + sc.AddClass("nf_topbar_text") |
| 30 | + |
| 31 | + ntfTopBarImage, _ := gtk.ImageNewFromIconName(notification.AppIcon, gtk.ICON_SIZE_BUTTON) |
| 32 | + |
| 33 | + ntfTopBarTextLabel, _ := gtk.LabelNew(notification.AppName) |
| 34 | + |
| 35 | + ntfTopBarDeleteButtonBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 15) |
| 36 | + |
| 37 | + ntfTopBarDeleteButton, _ := gtk.ButtonNew() |
| 38 | + |
| 39 | + sc, _ = ntfTopBarDeleteButton.GetStyleContext() |
| 40 | + sc.AddClass("button") |
| 41 | + |
| 42 | + ntfTopBarDeleteButtonLabel, _ := gtk.LabelNew("Close") |
| 43 | + |
| 44 | + ntfTopBarDeleteButton.Connect("clicked", func() { |
| 45 | + nDaemon.CloseNotificationAsUser(notification.ID) |
| 46 | + }) |
| 47 | + |
| 48 | + ntfTopBarDeleteButton.Add(ntfTopBarDeleteButtonLabel) |
| 49 | + ntfTopBarDeleteButtonBox.PackEnd(ntfTopBarDeleteButton, false, false, 0) |
| 50 | + ntfTopBar.PackEnd(ntfTopBarDeleteButtonBox, false, false, 0) |
| 51 | + ntfTopBarText.PackStart(ntfTopBarImage, false, false, 0) |
| 52 | + ntfTopBarText.PackStart(ntfTopBarTextLabel, false, false, 0) |
| 53 | + ntfTopBar.PackStart(ntfTopBarText, false, false, 0) |
| 54 | + notificationBox.PackStart(ntfTopBar, false, false, 0) |
| 55 | + |
| 56 | + notificationBody, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 15) |
| 57 | + |
| 58 | + //TODO: Handle notification Image |
| 59 | + |
| 60 | + notificationTextBody, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 15) |
| 61 | + |
| 62 | + sc, _ = notificationTextBody.GetStyleContext() |
| 63 | + sc.AddClass("ntf_text_contents") |
| 64 | + |
| 65 | + if notification.Summary != "" { |
| 66 | + notificationSummary, _ := gtk.LabelNew(notification.Summary) |
| 67 | + notificationSummary.SetXAlign(0) |
| 68 | + sc, _ = notificationSummary.GetStyleContext() |
| 69 | + sc.AddClass("h2") |
| 70 | + notificationTextBody.PackStart(notificationSummary, false, false, 0) |
| 71 | + } |
| 72 | + |
| 73 | + if notification.Body != "" { |
| 74 | + notificationBody, _ := gtk.LabelNew(notification.Body) |
| 75 | + notificationBody.SetXAlign(0) |
| 76 | + notificationTextBody.PackStart(notificationBody, false, false, 0) |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + hours, minutes, _ := notification.Timestamp.Clock() |
| 81 | + |
| 82 | + timeLabel, _ := gtk.LabelNew(fmt.Sprintf("%d:%02d", hours, minutes)) |
| 83 | + timeLabel.SetXAlign(0) |
| 84 | + sc, _ = timeLabel.GetStyleContext() |
| 85 | + sc.AddClass("h4") |
| 86 | + |
| 87 | + notificationTextBody.PackEnd(timeLabel, false, false, 0) |
| 88 | + |
| 89 | + notificationBody.PackStart(notificationTextBody, false, false, 0) |
| 90 | + notificationBox.PackStart(notificationBody, false, false, 0) |
| 91 | + |
| 92 | + return notificationBox |
| 93 | +} |
| 94 | + |
| 95 | +func createNotificationBarTitle(nDaemon *notificationDaemon.Daemon) *gtk.Box { |
| 96 | + tBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 10) |
| 97 | + |
| 98 | + title, _ := gtk.LabelNew(strings.Join([]string{strconv.Itoa(len(nDaemon.Notifications)), " Notifications"}, "")) |
| 99 | + sc, _ := title.GetStyleContext() |
| 100 | + sc.AddClass("h1") |
| 101 | + |
| 102 | + glib.TimeoutAdd(uint(100), func() bool { |
| 103 | + // Get new date/time info. |
| 104 | + title.SetText(strings.Join([]string{strconv.Itoa(len(nDaemon.Notifications)), " Notifications"}, "")) |
| 105 | + |
| 106 | + // Return true to keep the timeout active. |
| 107 | + return true |
| 108 | + }) |
| 109 | + |
| 110 | + closeAllButtonText, _ := gtk.LabelNew("Clear all") |
| 111 | + sc, _ = closeAllButtonText.GetStyleContext() |
| 112 | + sc.AddClass("button") |
| 113 | + |
| 114 | + closeAllButton, _ := gtk.ButtonNew() |
| 115 | + closeAllButton.Add(closeAllButtonText) |
| 116 | + |
| 117 | + closeAllButton.Connect("clicked", func() { |
| 118 | + for _, elem := range nDaemon.Notifications { |
| 119 | + nDaemon.CloseNotificationAsUser(elem.ID) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + tBox.PackStart(title, false, false, 0) |
| 124 | + tBox.PackEnd(closeAllButton, false, false, 0) |
| 125 | + return tBox |
| 126 | +} |
| 127 | + |
| 128 | +func createNotificationBar(nDaemon *notificationDaemon.Daemon) *gtk.Window { |
| 129 | + win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) |
| 130 | + win.SetTitle("Notification Bar") |
| 131 | + win.SetDecorated(false) |
| 132 | + win.SetResizable(false) |
| 133 | + |
| 134 | + layershell.InitForWindow(win) |
| 135 | + layershell.SetNamespace(win, "miracleos") |
| 136 | + |
| 137 | + layershell.SetAnchor(win, layershell.LAYER_SHELL_EDGE_BOTTOM, true) |
| 138 | + layershell.SetAnchor(win, layershell.LAYER_SHELL_EDGE_RIGHT, true) |
| 139 | + |
| 140 | + layershell.SetLayer(win, layershell.LAYER_SHELL_LAYER_TOP) |
| 141 | + layershell.SetMargin(win, layershell.LAYER_SHELL_EDGE_TOP, 0) |
| 142 | + layershell.SetMargin(win, layershell.LAYER_SHELL_EDGE_LEFT, 0) |
| 143 | + layershell.SetMargin(win, layershell.LAYER_SHELL_EDGE_RIGHT, 0) |
| 144 | + |
| 145 | + layershell.SetKeyboardMode(win, layershell.LAYER_SHELL_KEYBOARD_MODE_NONE) |
| 146 | + disp, _ := gdk.DisplayGetDefault() |
| 147 | + mon, _ := disp.GetMonitor(0) |
| 148 | + layershell.SetMonitor(win, mon) |
| 149 | + |
| 150 | + mBox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 10) |
| 151 | + |
| 152 | + for _, nt := range nDaemon.Notifications { |
| 153 | + mBox.PackEnd(createNotification(&nt, nDaemon), false, false, 0) |
| 154 | + } |
| 155 | + |
| 156 | + mBox.PackStart(createNotificationBarTitle(nDaemon), false, false, 0) |
| 157 | + |
| 158 | + win.Add(mBox) |
| 159 | + |
| 160 | + return win |
| 161 | +} |
| 162 | + |
| 163 | +func listenNotifications() *notificationDaemon.Daemon { |
| 164 | + daemon := notificationDaemon.NewDaemon(notificationDaemon.Config{ |
| 165 | + Capabilities: []string{"body", "actions", "actions-ions", "icon-static"}, |
| 166 | + }) |
| 167 | + if err := daemon.Start(); err != nil { |
| 168 | + log.Fatalf("Failed to start notification daemon: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + return daemon |
| 172 | +} |
0 commit comments