-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.js
More file actions
234 lines (209 loc) · 8.15 KB
/
item.js
File metadata and controls
234 lines (209 loc) · 8.15 KB
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
import GObject from 'gi://GObject';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import St from 'gi://St';
import {gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
import {DeleteConfirmationDialog} from './delete.js';
import {EditTitleDialog} from './editTitle.js';
export const ArticleMenuItem = GObject.registerClass(
class ArticleMenuItem extends PopupMenu.PopupBaseMenuItem {
_init(article, api, notifications, refreshCallback, showArchiveButton, showStarButton, showCopyButton, showDeleteButton, showEditTitleButton) {
super._init({
reactive: true,
can_focus: true,
style_class: 'popup-menu-item',
});
this._article = article;
this._api = api;
this._notifications = notifications;
this._refreshCallback = refreshCallback;
// main horizontal container
const mainBox = new St.BoxLayout({
vertical: false,
x_expand: true,
style_class: 'popup-menu-item-box',
});
this.add_child(mainBox);
// content area for article info
const contentBox = new St.BoxLayout({
vertical: true,
x_expand: true,
style: 'spacing: 4px;',
});
mainBox.add_child(contentBox);
// article title
const mainLabel = new St.Label({
text: this._getTruncatedTitle(50),
style_class: 'popup-menu-item-title',
x_expand: true,
x_align: Clutter.ActorAlign.START,
});
contentBox.add_child(mainLabel);
// article domain
const domainLabel = new St.Label({
text: this._getDomain(),
style_class: 'popup-menu-item-subtitle',
style: 'font-size: 0.8em; color: #888;',
});
contentBox.add_child(domainLabel);
// actions container
const actionsBox = new St.BoxLayout({
vertical: false,
x_align: Clutter.ActorAlign.END,
style: 'spacing: 4px;',
});
mainBox.add_child(actionsBox);
// Conditionally create buttons based on settings
this._createButtons(actionsBox, showArchiveButton, showStarButton, showCopyButton, showDeleteButton, showEditTitleButton);
// connect main click handler for opening article
this.connect('activate', () => this._openArticle());
}
_createButtons(actionsBox, showArchiveButton, showStarButton, showCopyButton, showDeleteButton, showEditTitleButton) {
// archive/unarchive button
if (showArchiveButton) {
const archiveButton = new St.Button({
style_class: 'popup-menu-icon-button',
can_focus: true,
reactive: true,
});
const archiveIcon = new St.Icon({
icon_name: this._article.is_archived ? 'checkmark-symbolic' : 'bookmark-new-symbolic',
style_class: 'popup-menu-icon',
});
archiveButton.set_child(archiveIcon);
archiveButton.connect('clicked', () => this._toggleArchive());
actionsBox.add_child(archiveButton);
}
// star/unstar button
if (showStarButton) {
const starButton = new St.Button({
style_class: 'popup-menu-icon-button',
can_focus: true,
reactive: true,
});
const starIcon = new St.Icon({
icon_name: this._article.is_starred ? 'starred-symbolic' : 'non-starred-symbolic',
style_class: 'popup-menu-icon',
});
starButton.set_child(starIcon);
starButton.connect('clicked', () => this._toggleStar());
actionsBox.add_child(starButton);
}
// copy button
if (showCopyButton) {
const copyButton = new St.Button({
style_class: 'popup-menu-icon-button',
can_focus: true,
reactive: true,
});
const copyIcon = new St.Icon({
icon_name: 'edit-copy-symbolic',
style_class: 'popup-menu-icon',
});
copyButton.set_child(copyIcon);
copyButton.connect('clicked', () => {
this._copyArticleUrl();
this._parent._getTopMenu().close();
});
actionsBox.add_child(copyButton);
}
// edit title button
if (showEditTitleButton) {
const editTitleButton = new St.Button({
style_class: 'popup-menu-icon-button',
can_focus: true,
reactive: true,
});
const editTitleIcon = new St.Icon({
icon_name: 'document-edit-symbolic',
style_class: 'popup-menu-icon',
});
editTitleButton.set_child(editTitleIcon);
editTitleButton.connect('clicked', () => this._editTitle());
actionsBox.add_child(editTitleButton);
}
// delete button
if (showDeleteButton) {
const deleteButton = new St.Button({
style_class: 'popup-menu-icon-button',
can_focus: true,
reactive: true,
});
const deleteIcon = new St.Icon({
icon_name: 'edit-delete-symbolic',
style_class: 'popup-menu-icon',
});
deleteButton.set_child(deleteIcon);
deleteButton.connect('clicked', () => this._deleteArticle());
actionsBox.add_child(deleteButton);
}
}
_getTruncatedTitle(maxLength) {
if (this._article.title.length > maxLength)
return `${this._article.title.substr(0, maxLength - 3)}...`;
return this._article.title;
}
_getDomain() {
try {
return GLib.Uri.parse(this._article.url, GLib.UriFlags.NONE).get_host().replace('www.', '');
} catch (e) {
console.error(`Failed to parse URL ${this._article.url}:`, e);
return '<Invalid URL>';
}
}
_openArticle() {
try {
Gio.AppInfo.launch_default_for_uri(this._article.url, null);
} catch (e) {
console.error('Failed to open article:', e);
this._notifications.showError(_('Failed to open article'));
}
}
async _toggleArchive() {
try {
if (this._article.is_archived) {
await this._api.markAsUnread(this._article.id);
this._notifications.showInfo(_('Article marked as unread'));
} else {
await this._api.markAsRead(this._article.id);
this._notifications.showInfo(_('Article marked as read'));
}
this._refreshCallback();
} catch (e) {
console.error('Failed to toggle marked status:', e);
this._notifications.showError(_('Failed to toggle marked status'));
}
}
async _toggleStar() {
try {
if (this._article.is_starred) {
await this._api.unstar(this._article.id);
this._notifications.showInfo(_('Article removed from favorites'));
} else {
await this._api.star(this._article.id);
this._notifications.showInfo(_('Article added to favorites'));
}
this._refreshCallback();
} catch (e) {
console.error('Failed to toggle favorite status:', e);
this._notifications.showError(_('Failed to toggle favorite status'));
}
}
_copyArticleUrl() {
const clipboard = St.Clipboard.get_default();
clipboard.set_text(St.ClipboardType.CLIPBOARD, this._article.url);
}
_editTitle() {
const dialog = new EditTitleDialog(this._article, this._api, this._notifications, this._refreshCallback);
dialog.open();
}
_deleteArticle() {
const dialog = new DeleteConfirmationDialog(this._article, this._api, this._notifications, this._refreshCallback);
dialog.open();
}
destroy() {
super.destroy();
}
});