summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/NotificationDisplay.qml
blob: 034066870af83fc94a0941f7b3a9dcd8258082d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import QtQml
import QtQml.Models
import QtQuick
import Quickshell
import Quickshell.Widgets
import Quickshell.Wayland
import qs.Services
import QtQuick.Layouts
import Quickshell.Services.Notifications

Scope {
  readonly property ShellScreen activeScreen: Array.from(Quickshell.screens).find(screen => screen.name === Array.from(NiriService.workspaces).find(ws => ws.is_focused)?.output) ?? null

  Instantiator {
    id: notifsRepeater

    model: ScriptModel {
      values: NotificationManager.active ? [] : NotificationManager.groups
    }

    delegate: PanelWindow {
      id: notifWindow

      required property var modelData
      required property var index

      property int activeIx: modelData.length - 1
      onModelDataChanged: {
        notifWindow.activeIx = modelData.length - 1;
      }

      property color textColor: {
        if (notifWindow.modelData?.[notifWindow.activeIx]?.urgency == NotificationUrgency.Low)
	  return "#ff999999";
	return "white";
      }
      property color backgroundColor: {
        if (notifWindow.modelData?.[notifWindow.activeIx]?.urgency == NotificationUrgency.Critical)
	  return "#dd900000";
	return "black";
      }

      anchors {
        right: true
	top: true
      }

      readonly property real spaceAbove: {
        var res = 0;
        for (let i = 0; i < notifWindow.index; i++) {
	  (_ => {})(notifsRepeater.objectAt(i).modelData);
	  res += notifsRepeater.objectAt(i).height + 8;
	}
	return res;
      }

      margins {
        right: 26 + 8
	top: 8 + spaceAbove
      }

      color: "transparent"

      implicitHeight: Math.max(notifCount.visible ? notifCount.contentHeight : 0, notifSummary.contentHeight) + (notifBody.visible ? 8 + notifBody.contentHeight : 0) + (notifActions.visible ? 8 + notifActions.height : 0) + (notifTime.visible ? 8 + notifTime.contentHeight : 0) + 16
      implicitWidth: 400

      WrapperMouseArea {
	enabled: true

        anchors.fill: parent

	cursorShape: Qt.PointingHandCursor

	onClicked: {
	  for (const notif of notifWindow.modelData)
	    notif.dismiss();
	}

	property real angleRem: 0
    	property real sensitivity: 1 / 120
	onWheel: event => {
	  angleRem += event.angleDelta.y;
	  const d = Math.round(angleRem * sensitivity);
	  angleRem -= d / sensitivity;
	  notifWindow.activeIx = ((notifWindow.modelData?.length ?? 1) + notifWindow.activeIx - d) % (notifWindow.modelData?.length ?? 1);
	}

	Rectangle {
	  color: notifWindow.backgroundColor
	  anchors.fill: parent
	  border {
	    color: Qt.hsla(195/360, 1, 0.45, 1)
	    width: 2
	  }

	  GridLayout {
	    id: notifLayout

	    width: 400 - 16
	    anchors.fill: parent
	    anchors.margins: 8
	    columnSpacing: 8
	    rowSpacing: 8

	    columns: notifImage.visible ? 3 : 2
	    rows: {
	      var res = 1;
	      if (notifBody.visible)
		res += 1;
	      if (notifActions.visible)
		res += 1;
	      if (notifTime.visible)
	        res += 1;
	      return res;
	    }

	    Text {
	      id: notifCount

	      visible: notifWindow.modelData?.length > 1 ?? false
	      text: `${notifWindow.activeIx + 1}/${notifWindow.modelData?.length ?? ""}`

	      font.pointSize: 10
	      font.family: "Fira Sans"
	      font.bold: true
	      font.features: { "tnum": 1 }
	      color: notifWindow.textColor
	      maximumLineCount: 1

	      Layout.fillWidth: false
	      Layout.row: 0
	      Layout.column: notifImage.visible ? 1 : 0
	    }

	    Text {
	      id: notifSummary

	      text: notifWindow.modelData?.[notifWindow.activeIx]?.summary ?? ""

	      font.pointSize: 10
	      font.family: "Fira Sans"
	      font.italic: true
	      color: notifWindow.textColor
	      maximumLineCount: 1
	      elide: Text.ElideRight

	      Layout.fillWidth: true
	      Layout.row: 0
	      Layout.column: (notifCount.visible ? 1 : 0) + (notifImage.visible ? 1 : 0)
	      Layout.columnSpan: notifCount.visible ? 1 : 2
	    }

	    Image {
	      id: notifImage

	      visible: (notifWindow.modelData?.[notifWindow.activeIx]?.image || notifWindow.modelData?.[notifWindow.activeIx]?.appIcon) ?? false

	      onStatusChanged: {
	        if (notifImage.status == Image.Error)
		  notifImage.visible = false;
	      }

	      source: (notifWindow.modelData?.[notifWindow.activeIx]?.image || notifWindow.modelData?.[notifWindow.activeIx]?.appIcon) ?? ""
	      fillMode: Image.PreserveAspectFit
	      asynchronous: true
	      smooth: true
	      mipmap: true

	      Layout.maximumWidth: 50
	      Layout.column: 0
	      Layout.row: 0
	      Layout.fillHeight: true
	      Layout.rowSpan: 1 + (notifBody.visible ? 1 : 0) + (notifTime.visible ? 1 : 0)
	    }

	    Text {
	      id: notifBody

	      visible: notifWindow.modelData?.[notifWindow.activeIx]?.body ?? false
	      text: notifWindow.modelData?.[notifWindow.activeIx]?.body ?? ""
	      textFormat: Text.RichText
	      wrapMode: Text.Wrap

	      font.pointSize: 10
	      font.family: "Fira Sans"
	      color: notifWindow.textColor

	      Layout.fillWidth: true
	      Layout.row: 1
	      Layout.column: notifImage.visible ? 1 : 0
	      Layout.columnSpan: notifCount.visible ? 2 : 1
	    }

	    Text {
	      id: notifTime

	      Connections {
		target: NotificationManager.clock
		function onDateChanged() {
		  notifTime.text = NotificationManager.formatTime(notifWindow.modelData?.[notifWindow.activeIx]?.receivedTime);
		}
	      }

	      visible: notifTime.text && notifTime.text !== "now"
	      text: NotificationManager.formatTime(notifWindow.modelData?.[notifWindow.activeIx]?.receivedTime)

	      font.pointSize: 8
	      font.family: "Fira Sans"
	      font.italic: true
	      color: "#555"
	      maximumLineCount: 1
	      horizontalAlignment: Text.AlignRight

	      Layout.fillWidth: true
	      Layout.row: notifBody.visible ? 2 : 1
	      Layout.column: notifImage.visible ? 1 : 0
	      Layout.columnSpan: 2
	    }

	    RowLayout {
	      id: notifActions

	      visible: notifWindow.modelData?.[notifWindow.activeIx]?.actions.length > 0 ?? false

	      spacing: 8
	      uniformCellSizes: true

	      width: 400 - 16
	      Layout.row: 1 + (notifBody.visible ? 1 : 0) + (notifTime.visible ? 1 : 0)
	      Layout.column: 0
	      Layout.columnSpan: 2 + (notifImage.visible ? 1 : 0)

	      Repeater {
		model: ScriptModel {
		  values: notifWindow.modelData?.[notifWindow.activeIx]?.actions
		}

		delegate: WrapperMouseArea {
		  id: actionMouseArea

		  required property var modelData

		  height: actionLabelWrapper.implicitHeight
		  Layout.fillWidth: true
		  Layout.horizontalStretchFactor: 1

		  hoverEnabled: true
		  cursorShape: Qt.PointingHandCursor

		  onClicked: actionMouseArea.modelData?.invoke()

		  Rectangle {
		    anchors.fill: parent

		    color: actionMouseArea.containsMouse ? "#20ffffff" : "transparent"

		    border {
		      width: 2
		      color: "#20ffffff"
		    }

		    WrapperItem {
		      id: actionLabelWrapper

		      margin: 8
		      anchors.centerIn: parent

		      RowLayout {
			id: actionLabelLayout

			spacing: 8

			IconImage {
			  id: actionIcon

			  visible: notifWindow.modelData?.[notifWindow.activeIx]?.hasActionIcons

			  onStatusChanged: {
			    if (actionIcon.status == Image.Error)
			      actionIcon.visible = false;
			  }

			  implicitSize: 16
			  source: {
			    if (!actionIcon.visible)
			      return "";

			    let icon = actionMouseArea.modelData?.identifier ?? ""
			    if (icon.includes("?path=")) {
			      const split = icon.split("?path=")
			      if (split.length !== 2)
				return icon
			      const name = split[0]
			      const path = split[1]
			      const fileName = name.substring(
					       name.lastIndexOf("/") + 1)
			      return `file://${path}/${fileName}`
			    }
			    return icon
			  }
			  asynchronous: true
			  smooth: true
			  mipmap: true

			  Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
			}

			Text {
			  id: actionLabel

			  visible: actionMouseArea.modelData?.text ?? false

			  text: actionMouseArea.modelData?.text ?? ""

			  font.pointSize: 10
			  font.family: "Fira Sans"
			  color: notifWindow.textColor
			  maximumLineCount: 1
			  elide: Text.ElideRight
			}
		      }
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
    }
  }
}